Merge pull request #5213 from yifan-gu/clean_syncPod

Refactor pkg/kubelet/kubelet.go: shouldContainerBeRestarted().
This commit is contained in:
Dawn Chen 2015-03-10 09:44:32 -07:00
commit 106e38ebb3

View File

@ -1074,11 +1074,12 @@ func (kl *Kubelet) makePodDataDirs(pod *api.BoundPod) error {
return nil return nil
} }
func (kl *Kubelet) shouldContainerBeRestarted(pod *api.BoundPod, containerName, dockerContainerName string, uid types.UID) bool { func (kl *Kubelet) shouldContainerBeRestarted(container *api.Container, pod *api.BoundPod) bool {
podFullName := GetPodFullName(pod)
// Check RestartPolicy for dead container // Check RestartPolicy for dead container
recentContainers, err := dockertools.GetRecentDockerContainersWithNameAndUUID(kl.dockerClient, GetPodFullName(pod), uid, containerName) recentContainers, err := dockertools.GetRecentDockerContainersWithNameAndUUID(kl.dockerClient, podFullName, pod.UID, container.Name)
if err != nil { if err != nil {
glog.Errorf("Error listing recent containers:%s", dockerContainerName) glog.Errorf("Error listing recent containers for pod %q: %v", podFullName, err)
// TODO(dawnchen): error handling here? // TODO(dawnchen): error handling here?
} }
// set dead containers to unready state // set dead containers to unready state
@ -1088,16 +1089,14 @@ func (kl *Kubelet) shouldContainerBeRestarted(pod *api.BoundPod, containerName,
if len(recentContainers) > 0 { if len(recentContainers) > 0 {
if pod.Spec.RestartPolicy.Never != nil { if pod.Spec.RestartPolicy.Never != nil {
glog.Infof("Already ran container with name %s, do nothing", glog.Infof("Already ran container %q of pod %q, do nothing", container.Name, podFullName)
dockerContainerName)
return false return false
} }
if pod.Spec.RestartPolicy.OnFailure != nil { if pod.Spec.RestartPolicy.OnFailure != nil {
// Check the exit code of last run // Check the exit code of last run
if recentContainers[0].State.ExitCode == 0 { if recentContainers[0].State.ExitCode == 0 {
glog.Infof("Already successfully ran container with name %s, do nothing", glog.Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, podFullName)
dockerContainerName)
return false return false
} }
} }
@ -1247,7 +1246,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, containersInPod dockertools.Docker
} }
} }
if !kl.shouldContainerBeRestarted(pod, container.Name, dockerContainerName, uid) { if !kl.shouldContainerBeRestarted(&container, pod) {
continue continue
} }