diff --git a/pkg/kubelet/dockertools/manager.go b/pkg/kubelet/dockertools/manager.go index b1740f2074..d94428cea6 100644 --- a/pkg/kubelet/dockertools/manager.go +++ b/pkg/kubelet/dockertools/manager.go @@ -892,3 +892,22 @@ func (dm *DockerManager) PortForward(pod *kubecontainer.Pod, port uint16, stream command.Stdout = stream return command.Run() } + +// KillContainer kills a container identified by containerID. +// Internally, it invokes docker's StopContainer API with a timeout of 10s. +// TODO(yifan): Use new ContainerID type. +func (dm *DockerManager) KillContainer(containerID types.UID) error { + ID := string(containerID) + glog.V(2).Infof("Killing container with id %q", ID) + dm.readinessManager.RemoveReadiness(ID) + err := dm.client.StopContainer(ID, 10) + + ref, ok := dm.containerRefManager.GetRef(ID) + if !ok { + glog.Warningf("No ref for pod '%v'", ID) + } else { + // TODO: pass reason down here, and state, or move this call up the stack. + dm.recorder.Eventf(ref, "killing", "Killing %v", ID) + } + return err +} diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 269b8a581d..b51439585f 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -753,7 +753,7 @@ func (kl *Kubelet) runContainer(pod *api.Pod, container *api.Container, netMode, if container.Lifecycle != nil && container.Lifecycle.PostStart != nil { handlerErr := kl.handlerRunner.Run(id, pod, container, container.Lifecycle.PostStart) if handlerErr != nil { - kl.killContainerByID(id) + kl.containerManager.KillContainer(types.UID(id)) return dockertools.DockerID(""), fmt.Errorf("failed to call event handler: %v", handlerErr) } } @@ -909,26 +909,6 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string, return nameservers, searches, nil } -// Kill a docker container -func (kl *Kubelet) killContainer(c *kubecontainer.Container) error { - return kl.killContainerByID(string(c.ID)) -} - -func (kl *Kubelet) killContainerByID(ID string) error { - glog.V(2).Infof("Killing container with id %q", ID) - kl.readinessManager.RemoveReadiness(ID) - err := kl.dockerClient.StopContainer(ID, 10) - - ref, ok := kl.containerRefManager.GetRef(ID) - if !ok { - glog.Warningf("No ref for pod '%v'", ID) - } else { - // TODO: pass reason down here, and state, or move this call up the stack. - kl.recorder.Eventf(ref, "killing", "Killing %v", ID) - } - return err -} - // createPodInfraContainer starts the pod infra container for a pod. Returns the docker container ID of the newly created container. func (kl *Kubelet) createPodInfraContainer(pod *api.Pod) (dockertools.DockerID, error) { @@ -1029,7 +1009,7 @@ func (kl *Kubelet) killPod(pod kubecontainer.Pod) error { errs <- err } } - err := kl.killContainer(container) + err := kl.containerManager.KillContainer(container.ID) if err != nil { glog.Errorf("Failed to delete container: %v; Skipping pod %q", err, pod.ID) errs <- err @@ -1340,7 +1320,7 @@ func (kl *Kubelet) syncPod(pod *api.Pod, mirrorPod *api.Pod, runningPod kubecont _, keep := containerChanges.containersToKeep[dockertools.DockerID(container.ID)] if !keep { glog.V(3).Infof("Killing unwanted container %+v", container) - err = kl.killContainer(container) + err = kl.containerManager.KillContainer(container.ID) if err != nil { glog.Errorf("Error killing container: %v", err) } diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 0bdd7a6785..4867ce9d04 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -368,19 +368,18 @@ func TestKillContainerWithError(t *testing.T) { Names: []string{"/k8s_bar_qux_new_5678_42"}, }, } - fakeDocker := &dockertools.FakeDockerClient{ - Errors: make(map[string]error), - ContainerList: append([]docker.APIContainers{}, containers...), - } 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.killContainer(&c) + err := kubelet.containerManager.KillContainer(c.ID) if err == nil { t.Errorf("expected error, found nil") } @@ -420,7 +419,7 @@ func TestKillContainer(t *testing.T) { } c := apiContainerToContainer(fakeDocker.ContainerList[0]) - err := kubelet.killContainer(&c) + err := kubelet.containerManager.KillContainer(c.ID) if err != nil { t.Errorf("unexpected error: %v", err) }