From 0e20f7d73659c8dcec771f8d72f525360c26fcc3 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 18 Feb 2015 15:05:16 -0800 Subject: [PATCH] pkg/kubelet: recreate infra pod if the pod is changed --- pkg/kubelet/kubelet.go | 30 ++++++++++++++++++------------ pkg/kubelet/kubelet_test.go | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 1507115c72c..e00027f4486 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1047,6 +1047,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke glog.Errorf("Unable to get pod with name %q and uid %q info with error(%v)", podFullName, uid, err) } + podChanged := false for _, container := range pod.Spec.Containers { expectedHash := dockertools.HashContainer(&container) dockerContainerName := dockertools.BuildDockerName(uid, podFullName, &container) @@ -1055,8 +1056,8 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke glog.V(3).Infof("pod %q container %q exists as %v", podFullName, container.Name, containerID) // look for changes in the container. - podChanged := hash != 0 && hash != expectedHash - if !podChanged { + containerChanged := hash != 0 && hash != expectedHash + if !containerChanged { // TODO: This should probably be separated out into a separate goroutine. // If the container's liveness probe is unsuccessful, set readiness to false. If liveness is succesful, do a readiness check and set // readiness accordingly. If the initalDelay since container creation on liveness probe has not passed the probe will return Success. @@ -1088,6 +1089,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke } glog.Infof("pod %q container %q is unhealthy. Container will be killed and re-created.", podFullName, container.Name, live) } else { + podChanged = true glog.Infof("pod %q container %q hash changed (%d vs %d). Container will be killed and re-created.", podFullName, container.Name, hash, expectedHash) } if err := kl.killContainer(dockerContainer); err != nil { @@ -1095,16 +1097,6 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke continue } killedContainers[containerID] = empty{} - - if podChanged { - // Also kill associated pod infra container if the pod changed. - if podInfraContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uid, dockertools.PodInfraContainerName); found { - if err := kl.killContainer(podInfraContainer); err != nil { - glog.V(1).Infof("Failed to kill pod infra container %q: %v", podInfraContainer.ID, err) - continue - } - } - } } // Check RestartPolicy for container @@ -1167,6 +1159,20 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke containersToKeep[containerID] = empty{} } + if podChanged { + // Also kill associated pod infra container if the pod changed. + if podInfraContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uid, dockertools.PodInfraContainerName); found { + if err := kl.killContainer(podInfraContainer); err != nil { + glog.V(1).Infof("Failed to kill pod infra container %q: %v", podInfraContainer.ID, err) + } + } + podInfraContainerID, err = kl.createPodInfraContainer(pod) + if err != nil { + glog.Errorf("Failed to recreate pod infra container: %v for pod %q", err, podFullName) + } + containersToKeep[podInfraContainerID] = empty{} + } + // Kill any containers in this pod which were not identified above (guards against duplicates). for id, container := range dockerContainers { curPodFullName, curUUID, _, _ := dockertools.ParseDockerName(container.Names[0]) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 7c0a2c92cb5..75b5a7d29a3 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -931,7 +931,7 @@ func TestSyncPodBadHash(t *testing.T) { t.Errorf("unexpected error: %v", err) } - verifyCalls(t, fakeDocker, []string{"list", "stop", "stop", "list", "create", "start"}) + verifyCalls(t, fakeDocker, []string{"list", "stop", "list", "create", "start", "stop", "create", "start"}) // A map interation is used to delete containers, so must not depend on // order here.