Skip to content

Commit

Permalink
Merge pull request #5 from linkernetworks/johnlin/docker-cli
Browse files Browse the repository at this point in the history
docker client & pause container find
  • Loading branch information
John-Lin authored Jun 2, 2018
2 parents 4e7b84b + a29bdff commit 217d6b7
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 75 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: go
sudo: true
services:
- docker

#refer to https://github.com/pseudomuto/protoc-gen-doc/blob/master/.travis.yml
env:
Expand All @@ -16,13 +18,13 @@ before_install:
- sudo apt-get install -y git build-essential openvswitch-switch
- go get -u github.com/kardianos/govendor
- go get -u github.com/pierrre/gotestcover
- govendor sync
- govendor sync -v
- if [ ! -d "${PROTOC_TARGET}" ]; then curl -fsSL "$PROTOC_RELEASE" > "${PROTOC_TARGET}.zip"; fi
- if [ -f "${PROTOC_TARGET}.zip" ]; then unzip "${PROTOC_TARGET}.zip" -d "${PROTOC_TARGET}"; fi
- go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

script:
- sudo -E env PATH=$PATH TEST_OVS=1 gotestcover -coverprofile=coverage.txt -covermode=atomic ./ovs
- sudo -E env PATH=$PATH TEST_OVS=1 gotestcover -coverprofile=coverage.txt -covermode=atomic ./ovs ./docker
after_success:
- bash <(curl -s https://codecov.io/bash)

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ clean:
-rm -f server/${BINARY}-*

test:
sudo -E env PATH=$$PATH TEST_OVS=1 go test -v ./ovs
sudo -E env PATH=$$PATH TEST_OVS=1 go test -v ./ovs ./docker

.PHONY: server client clean
.PHONY: server client test clean
30 changes: 30 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package docker

import (
client "docker.io/go-docker"
"docker.io/go-docker/api/types"
"golang.org/x/net/context"
)

// docker ps -a <Container ID>
func ListContainer() ([]types.Container, error) {
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
}
return cli.ContainerList(context.Background(), types.ContainerListOptions{})
}

// docker inspect <Container ID>
func InspectContainer(containerID string) (types.ContainerJSON, error) {
cli, err := client.NewEnvClient()
if err != nil {
return types.ContainerJSON{}, err
}
return cli.ContainerInspect(context.Background(), containerID)
}

// docker inspect <Container ID> | grep -E 'SandboxKey|Id'
func GetSandboxKey(containerInfo types.ContainerJSON) string {
return containerInfo.NetworkSettings.SandboxKey
}
31 changes: 31 additions & 0 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package docker

import (
"testing"

"docker.io/go-docker/api/types"

"github.com/stretchr/testify/assert"
)

func TestGetSandboxKey(t *testing.T) {
containerInfo := types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: "43add2074a1bd05e6ba1e661de5b9ed109a058eabdb83032e4801389c78b035a",
},
NetworkSettings: &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Bridge: "",
SandboxID: "66d0d6831275fd6102f22105cde6c7442bbde4974343c74adedd5b1650e1443d",
HairpinMode: false,
LinkLocalIPv6Address: "",
LinkLocalIPv6PrefixLen: 0,
SandboxKey: "/var/run/docker/netns/66d0d6831275",
SecondaryIPAddresses: nil,
SecondaryIPv6Addresses: nil,
},
},
}
netnsPath := GetSandboxKey(containerInfo)
assert.Equal(t, "/var/run/docker/netns/66d0d6831275", netnsPath)
}
25 changes: 25 additions & 0 deletions docker/pause-container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package docker

import (
"fmt"
"regexp"

"docker.io/go-docker/api/types"
)

func FindK8SPauseContainerID(containers []types.Container, PodName, Namespace, PodUUID string) (string, error) {
// ex: k8s_POD_myinit_default_05ab36d8-65aa-11e8-b35e-42010af00248_0
pattern := fmt.Sprintf("k8s_POD_%s_%s_%s_\\d+", PodName, Namespace, PodUUID)
r, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
for _, container := range containers {
// the first container name would be fine
// don't know why container will have more than one name
if r.MatchString(container.Names[0]) {
return container.ID, nil
}
}
return "", nil
}
24 changes: 24 additions & 0 deletions docker/pause-container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package docker

import (
"testing"

"docker.io/go-docker/api/types"
"github.com/stretchr/testify/assert"
)

func TestFindK8SPauseContainerID(t *testing.T) {
containers := []types.Container{
types.Container{
ID: "18720aa37e71",
Names: []string{"k8s_POD_mongo-0_default_553be55e-532c-11e8-bea4-42010af0009f_0"},
},
types.Container{
ID: "12332aa37f99",
Names: []string{"k8s_POD_redis_default_667be33e-178c-99e2-bea6-68090bd0001d_20"},
},
}
cid, err := FindK8SPauseContainerID(containers, "mongo-0", "default", "553be55e-532c-11e8-bea4-42010af0009f")
assert.NoError(t, err)
assert.Equal(t, "18720aa37e71", cid, "they should be equal")
}
4 changes: 4 additions & 0 deletions ovs/ovs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
"testing"

"github.com/digitalocean/go-openvswitch/ovs"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -164,6 +165,9 @@ func TestAddDelPort(t *testing.T) {
}

func TestAddDelPortFail(t *testing.T) {
if _, ok := os.LookupEnv("TEST_OVS"); !ok {
t.SkipNow()
}
mode := changeVSCtl(t)
defer resetVSCtl(mode)

Expand Down
Loading

0 comments on commit 217d6b7

Please sign in to comment.