Merge pull request #200 from jjhuff/prefix_container

Add a 'k8s' prefix to docker containers that we manage
This commit is contained in:
Daniel Smith 2014-06-21 13:56:14 -07:00
commit dc35a98ebe
2 changed files with 22 additions and 17 deletions

View File

@ -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] {

View File

@ -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 <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",
},
}
@ -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",
},
{