From 3b28c6262b59c1ad406a8a94b78e7313c4524ccc Mon Sep 17 00:00:00 2001 From: Yifan Gu Date: Tue, 14 Apr 2015 15:59:24 -0700 Subject: [PATCH] kubelet: Clean up computePodContainerChanges. More `continue`, less `else` to make it less indent and more readable. --- pkg/kubelet/kubelet.go | 82 ++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 9a0d6a7e1ca..a38f899d345 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1107,52 +1107,56 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, runningPod kubeconta expectedHash := dockertools.HashContainer(&container) c := runningPod.FindContainerByName(container.Name) - if c != nil { - containerID := dockertools.DockerID(c.ID) - hash := c.Hash - glog.V(3).Infof("pod %q container %q exists as %v", podFullName, container.Name, containerID) - - if !createPodInfraContainer { - // look for changes in the container. - - containerChanged := hash != 0 && hash != expectedHash - if !containerChanged { - result, err := kl.probeContainer(pod, podStatus, container, string(c.ID), c.Created) - if err != nil { - // TODO(vmarmol): examine this logic. - glog.V(2).Infof("probe no-error: %q", container.Name) - containersToKeep[containerID] = index - continue - } - if result == probe.Success { - glog.V(4).Infof("probe success: %q", container.Name) - containersToKeep[containerID] = index - continue - } - glog.Infof("pod %q container %q is unhealthy (probe result: %v), it will be killed and re-created.", podFullName, container.Name, result) - } else { - glog.Infof("pod %q container %q hash changed (%d vs %d), it will be killed and re-created.", podFullName, container.Name, hash, expectedHash) - } - containersToStart[index] = empty{} - } else { // createPodInfraContainer == true and Container exists - // If we're creating infra containere everything will be killed anyway - // If RestartPolicy is Always or OnFailure we restart containers that were running before we - // killed them when restarting Infra Container. - if pod.Spec.RestartPolicy != api.RestartPolicyNever { - glog.V(1).Infof("Infra Container is being recreated. %q will be restarted.", container.Name) - containersToStart[index] = empty{} - } - continue - } - } else { + if c == nil { if kl.shouldContainerBeRestarted(&container, pod, &podStatus) { - // If we are here it means that the container is dead and sould be restarted, or never existed and should + // If we are here it means that the container is dead and should be restarted, or never existed and should // be created. We may be inserting this ID again if the container has changed and it has // RestartPolicy::Always, but it's not a big deal. glog.V(3).Infof("Container %+v is dead, but RestartPolicy says that we should restart it.", container) containersToStart[index] = empty{} } + continue } + + containerID := dockertools.DockerID(c.ID) + hash := c.Hash + glog.V(3).Infof("pod %q container %q exists as %v", podFullName, container.Name, containerID) + + if createPodInfraContainer { + // createPodInfraContainer == true and Container exists + // If we're creating infra containere everything will be killed anyway + // If RestartPolicy is Always or OnFailure we restart containers that were running before we + // killed them when restarting Infra Container. + if pod.Spec.RestartPolicy != api.RestartPolicyNever { + glog.V(1).Infof("Infra Container is being recreated. %q will be restarted.", container.Name) + containersToStart[index] = empty{} + } + continue + } + + // At this point, the container is running and pod infra container is good. + // We will look for changes and check healthiness for the container. + containerChanged := hash != 0 && hash != expectedHash + if containerChanged { + glog.Infof("pod %q container %q hash changed (%d vs %d), it will be killed and re-created.", podFullName, container.Name, hash, expectedHash) + containersToStart[index] = empty{} + continue + } + + result, err := kl.probeContainer(pod, podStatus, container, string(c.ID), c.Created) + if err != nil { + // TODO(vmarmol): examine this logic. + glog.V(2).Infof("probe no-error: %q", container.Name) + containersToKeep[containerID] = index + continue + } + if result == probe.Success { + glog.V(4).Infof("probe success: %q", container.Name) + containersToKeep[containerID] = index + continue + } + glog.Infof("pod %q container %q is unhealthy (probe result: %v), it will be killed and re-created.", podFullName, container.Name, result) + containersToStart[index] = empty{} } // After the loop one of the following should be true: