-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from linkernetworks/johnlin/docker-cli
docker client & pause container find
- Loading branch information
Showing
8 changed files
with
389 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.