Skip to content

Commit

Permalink
Add a k8s prefix to docker containers that we manage
Browse files Browse the repository at this point in the history
  • Loading branch information
jjhuff committed Jun 21, 2014
1 parent 25997dc commit 460821e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
23 changes: 14 additions & 9 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -595,15 +600,15 @@ 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()
if err != nil {
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
}
}
Expand Down Expand Up @@ -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] {
Expand Down
16 changes: 8 additions & 8 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -494,13 +494,13 @@ func TestSyncManifestsDoesNothing(t *testing.T) {
}
fakeDocker.containerList = []docker.APIContainers{
{
// format is <container-id>--<manifest-id>
Names: []string{"bar--foo"},
// format is k8s--<container-id>--<manifest-id>
Names: []string{"/k8s--bar--foo"},
ID: "1234",
},
{
// network container
Names: []string{"k8snet--foo--"},
Names: []string{"/k8s--net--foo--"},
ID: "9876",
},
}
Expand Down Expand Up @@ -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",
},
{
Expand Down

0 comments on commit 460821e

Please sign in to comment.