Add a k8s prefix to docker containers that we manage

This commit is contained in:
Justin Huff 2014-06-21 13:16:20 -07:00
parent 25997dcfe2
commit 460821e370
2 changed files with 22 additions and 17 deletions

View File

@ -229,10 +229,12 @@ func unescapeDash(in string) (out string) {
return return
} }
const containerNamePrefix = "k8s"
// Creates a name which can be reversed to identify both manifest id and container name. // Creates a name which can be reversed to identify both manifest id and container name.
func manifestAndContainerToDockerName(manifest *api.ContainerManifest, container *api.Container) string { func manifestAndContainerToDockerName(manifest *api.ContainerManifest, container *api.Container) string {
// Note, manifest.Id could be blank. // 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 // 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:] name = name[1:]
} }
parts := strings.Split(name, "--") parts := strings.Split(name, "--")
if len(parts) > 0 { if len(parts) == 0 || parts[0] != containerNamePrefix {
containerName = unescapeDash(parts[0]) return
} }
if len(parts) > 1 { if len(parts) > 1 {
manifestId = unescapeDash(parts[1]) containerName = unescapeDash(parts[1])
}
if len(parts) > 2 {
manifestId = unescapeDash(parts[2])
} }
return 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) { func (kl *Kubelet) networkContainerExists(manifest *api.ContainerManifest) (string, bool, error) {
pods, err := kl.ListContainers() pods, err := kl.ListContainers()
@ -603,7 +608,7 @@ func (kl *Kubelet) networkContainerExists(manifest *api.ContainerManifest) (stri
return "", false, err return "", false, err
} }
for _, name := range pods { for _, name := range pods {
if strings.Contains(name, networkContainerName+"--"+manifest.Id+"--") { if strings.Contains(name, containerNamePrefix+"--"+networkContainerName+"--"+manifest.Id+"--") {
return name, true, nil return name, true, nil
} }
} }
@ -683,9 +688,9 @@ func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
existingContainers, _ := kl.ListContainers() existingContainers, _ := kl.ListContainers()
log.Printf("Existing:\n%#v Desired: %#v", existingContainers, desired) log.Printf("Existing:\n%#v Desired: %#v", existingContainers, desired)
for _, container := range existingContainers { for _, container := range existingContainers {
// This is slightly hacky, but we ignore containers that lack '--' in their name // Skip containers that we didn't create to allow users to manually
// to allow users to manually spin up their own containers if they want. // spin up their own containers if they want.
if !strings.Contains(container, "--") { if !strings.HasPrefix(container, "/"+containerNamePrefix+"--") {
continue continue
} }
if !desired[container] { if !desired[container] {

View File

@ -183,10 +183,10 @@ func TestContainerExists(t *testing.T) {
} }
fakeDocker.containerList = []docker.APIContainers{ 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{ fakeDocker.container = &docker.Container{
@ -494,13 +494,13 @@ func TestSyncManifestsDoesNothing(t *testing.T) {
} }
fakeDocker.containerList = []docker.APIContainers{ fakeDocker.containerList = []docker.APIContainers{
{ {
// format is <container-id>--<manifest-id> // format is k8s--<container-id>--<manifest-id>
Names: []string{"bar--foo"}, Names: []string{"/k8s--bar--foo"},
ID: "1234", ID: "1234",
}, },
{ {
// network container // network container
Names: []string{"k8snet--foo--"}, Names: []string{"/k8s--net--foo--"},
ID: "9876", ID: "9876",
}, },
} }
@ -535,13 +535,13 @@ func TestSyncManifestsDeletes(t *testing.T) {
} }
fakeDocker.containerList = []docker.APIContainers{ fakeDocker.containerList = []docker.APIContainers{
{ {
// the '--' is required for the kubelet to manage the container // the k8s prefix is required for the kubelet to manage the container
Names: []string{"foo--bar"}, Names: []string{"/k8s--foo--bar"},
ID: "1234", ID: "1234",
}, },
{ {
// network container // network container
Names: []string{"k8snet--foo--"}, Names: []string{"/k8s--net--foo--"},
ID: "9876", ID: "9876",
}, },
{ {