Fix a bug where the network container could be torn down before other pods.

This can break PreStop that hits localhost in the pod.

(cherry picked from commit aac696d44c)
This commit is contained in:
Brendan Burns 2015-06-12 20:49:32 -07:00
parent f84cee17d1
commit 6eda9c4976

View File

@ -1036,29 +1036,38 @@ func (dm *DockerManager) KillPod(pod kubecontainer.Pod) error {
// can be Len errors + the networkPlugin teardown error. // can be Len errors + the networkPlugin teardown error.
errs := make(chan error, len(pod.Containers)+1) errs := make(chan error, len(pod.Containers)+1)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
var networkID types.UID
for _, container := range pod.Containers { for _, container := range pod.Containers {
wg.Add(1) wg.Add(1)
go func(container *kubecontainer.Container) { go func(container *kubecontainer.Container) {
defer util.HandleCrash() defer util.HandleCrash()
defer wg.Done()
// TODO: Handle this without signaling the pod infra container to // TODO: Handle this without signaling the pod infra container to
// adapt to the generic container runtime. // adapt to the generic container runtime.
if container.Name == PodInfraContainerName { if container.Name == PodInfraContainerName {
err := dm.networkPlugin.TearDownPod(pod.Namespace, pod.Name, kubeletTypes.DockerID(container.ID)) // Store the container runtime for later deletion.
if err != nil { // We do this so that PreStop handlers can run in the network namespace.
glog.Errorf("Failed tearing down the infra container: %v", err) networkID = container.ID
errs <- err return
} }
} if err := dm.killContainer(container.ID); err != nil {
err := dm.killContainer(container.ID)
if err != nil {
glog.Errorf("Failed to delete container: %v; Skipping pod %q", err, pod.ID) glog.Errorf("Failed to delete container: %v; Skipping pod %q", err, pod.ID)
errs <- err errs <- err
} }
wg.Done()
}(container) }(container)
} }
wg.Wait() wg.Wait()
if len(networkID) > 0 {
if err := dm.networkPlugin.TearDownPod(pod.Namespace, pod.Name, kubeletTypes.DockerID(networkID)); err != nil {
glog.Errorf("Failed tearing down the infra container: %v", err)
errs <- err
}
if err := dm.killContainer(networkID); err != nil {
glog.Errorf("Failed to delete container: %v; Skipping pod %q", err, pod.ID)
errs <- err
}
}
close(errs) close(errs)
if len(errs) > 0 { if len(errs) > 0 {
errList := []error{} errList := []error{}