Add KillContainerInPod in DockerManager

This changes adds one of the functions that DockerManager needs to implement
the Runtime interface.
This commit is contained in:
Yu-Ju Hong
2015-04-30 16:04:32 -07:00
parent 441a4e6f30
commit b37f23f1eb
3 changed files with 147 additions and 104 deletions

View File

@@ -355,107 +355,6 @@ func apiContainerToContainer(c docker.APIContainers) container.Container {
}
}
func dockerContainersToPod(containers dockertools.DockerContainers) container.Pod {
var pod container.Pod
for _, c := range containers {
dockerName, hash, err := dockertools.ParseDockerName(c.Names[0])
if err != nil {
continue
}
pod.Containers = append(pod.Containers, &container.Container{
ID: types.UID(c.ID),
Name: dockerName.ContainerName,
Hash: hash,
Image: c.Image,
})
// TODO(yifan): Only one evaluation is enough.
pod.ID = dockerName.PodUID
name, namespace, _ := kubecontainer.ParsePodFullName(dockerName.PodFullName)
pod.Name = name
pod.Namespace = namespace
}
return pod
}
func TestKillContainerWithError(t *testing.T) {
containers := []docker.APIContainers{
{
ID: "1234",
Names: []string{"/k8s_foo_qux_new_1234_42"},
},
{
ID: "5678",
Names: []string{"/k8s_bar_qux_new_5678_42"},
},
}
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
fakeDocker := testKubelet.fakeDocker
fakeDocker.ContainerList = containers
for _, c := range fakeDocker.ContainerList {
kubelet.readinessManager.SetReadiness(c.ID, true)
}
kubelet.dockerClient = fakeDocker
c := apiContainerToContainer(fakeDocker.ContainerList[0])
fakeDocker.Errors["stop"] = fmt.Errorf("sample error")
err := kubelet.containerManager.KillContainer(c.ID)
if err == nil {
t.Errorf("expected error, found nil")
}
verifyCalls(t, fakeDocker, []string{"stop"})
killedContainer := containers[0]
liveContainer := containers[1]
ready := kubelet.readinessManager.GetReadiness(killedContainer.ID)
if ready {
t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, ready)
}
ready = kubelet.readinessManager.GetReadiness(liveContainer.ID)
if !ready {
t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, ready)
}
}
func TestKillContainer(t *testing.T) {
containers := []docker.APIContainers{
{
ID: "1234",
Names: []string{"/k8s_foo_qux_new_1234_42"},
},
{
ID: "5678",
Names: []string{"/k8s_bar_qux_new_5678_42"},
},
}
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
fakeDocker := testKubelet.fakeDocker
fakeDocker.ContainerList = append([]docker.APIContainers{}, containers...)
fakeDocker.Container = &docker.Container{
Name: "foobar",
}
for _, c := range fakeDocker.ContainerList {
kubelet.readinessManager.SetReadiness(c.ID, true)
}
c := apiContainerToContainer(fakeDocker.ContainerList[0])
err := kubelet.containerManager.KillContainer(c.ID)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
verifyCalls(t, fakeDocker, []string{"stop"})
killedContainer := containers[0]
liveContainer := containers[1]
ready := kubelet.readinessManager.GetReadiness(killedContainer.ID)
if ready {
t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, ready)
}
ready = kubelet.readinessManager.GetReadiness(liveContainer.ID)
if !ready {
t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, ready)
}
}
var emptyPodUIDs map[types.UID]metrics.SyncPodType
func generatePodInfraContainerHash(pod *api.Pod) uint64 {