mirror of https://github.com/k3s-io/k3s
kubelet: Clean up computePodContainerChanges.
More `continue`, less `else` to make it less indent and more readable.pull/6/head
parent
ce9c100cb3
commit
3b28c6262b
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue