From 460821e370f271f7d8744ced28f099d0f018f64c Mon Sep 17 00:00:00 2001 From: Justin Huff Date: Sat, 21 Jun 2014 13:16:20 -0700 Subject: [PATCH] Add a k8s prefix to docker containers that we manage --- pkg/kubelet/kubelet.go | 23 ++++++++++++++--------- pkg/kubelet/kubelet_test.go | 16 ++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 25cd9fa3e59..05b8cafcd44 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -229,10 +229,12 @@ func unescapeDash(in string) (out string) { return } +const containerNamePrefix = "k8s" + // Creates a name which can be reversed to identify both manifest id and container name. func manifestAndContainerToDockerName(manifest *api.ContainerManifest, container *api.Container) string { // Note, manifest.Id could be blank. - return fmt.Sprintf("%s--%s--%x", escapeDash(container.Name), escapeDash(manifest.Id), rand.Uint32()) + return fmt.Sprintf("%s--%s--%s--%x", containerNamePrefix, escapeDash(container.Name), escapeDash(manifest.Id), rand.Uint32()) } // Upacks a container name, returning the manifest id and container name we would have used to @@ -244,11 +246,14 @@ func dockerNameToManifestAndContainer(name string) (manifestId, containerName st name = name[1:] } parts := strings.Split(name, "--") - if len(parts) > 0 { - containerName = unescapeDash(parts[0]) + if len(parts) == 0 || parts[0] != containerNamePrefix { + return } if len(parts) > 1 { - manifestId = unescapeDash(parts[1]) + containerName = unescapeDash(parts[1]) + } + if len(parts) > 2 { + manifestId = unescapeDash(parts[2]) } return } @@ -595,7 +600,7 @@ func (kl *Kubelet) WatchEtcd(watchChannel <-chan *etcd.Response, changeChannel c } } -const networkContainerName = "k8snet" +const networkContainerName = "net" func (kl *Kubelet) networkContainerExists(manifest *api.ContainerManifest) (string, bool, error) { pods, err := kl.ListContainers() @@ -603,7 +608,7 @@ func (kl *Kubelet) networkContainerExists(manifest *api.ContainerManifest) (stri return "", false, err } for _, name := range pods { - if strings.Contains(name, networkContainerName+"--"+manifest.Id+"--") { + if strings.Contains(name, containerNamePrefix+"--"+networkContainerName+"--"+manifest.Id+"--") { return name, true, nil } } @@ -683,9 +688,9 @@ func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error { existingContainers, _ := kl.ListContainers() log.Printf("Existing:\n%#v Desired: %#v", existingContainers, desired) for _, container := range existingContainers { - // This is slightly hacky, but we ignore containers that lack '--' in their name - // to allow users to manually spin up their own containers if they want. - if !strings.Contains(container, "--") { + // Skip containers that we didn't create to allow users to manually + // spin up their own containers if they want. + if !strings.HasPrefix(container, "/"+containerNamePrefix+"--") { continue } if !desired[container] { diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 24a3f567380..fd395640652 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -183,10 +183,10 @@ func TestContainerExists(t *testing.T) { } fakeDocker.containerList = []docker.APIContainers{ { - Names: []string{"foo--qux--1234"}, + Names: []string{"/k8s--foo--qux--1234"}, }, { - Names: []string{"bar--qux--1234"}, + Names: []string{"/k8s--bar--qux--1234"}, }, } fakeDocker.container = &docker.Container{ @@ -494,13 +494,13 @@ func TestSyncManifestsDoesNothing(t *testing.T) { } fakeDocker.containerList = []docker.APIContainers{ { - // format is -- - Names: []string{"bar--foo"}, + // format is k8s---- + Names: []string{"/k8s--bar--foo"}, ID: "1234", }, { // network container - Names: []string{"k8snet--foo--"}, + Names: []string{"/k8s--net--foo--"}, ID: "9876", }, } @@ -535,13 +535,13 @@ func TestSyncManifestsDeletes(t *testing.T) { } fakeDocker.containerList = []docker.APIContainers{ { - // the '--' is required for the kubelet to manage the container - Names: []string{"foo--bar"}, + // the k8s prefix is required for the kubelet to manage the container + Names: []string{"/k8s--foo--bar"}, ID: "1234", }, { // network container - Names: []string{"k8snet--foo--"}, + Names: []string{"/k8s--net--foo--"}, ID: "9876", }, {