mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #6844 from yifan-gu/cleankube
kubelet: Clean up computePodContainerChanges.
This commit is contained in:
commit
abab9ea31f
@ -1123,16 +1123,42 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, runningPod kubeconta
|
|||||||
expectedHash := dockertools.HashContainer(&container)
|
expectedHash := dockertools.HashContainer(&container)
|
||||||
|
|
||||||
c := runningPod.FindContainerByName(container.Name)
|
c := runningPod.FindContainerByName(container.Name)
|
||||||
if c != nil {
|
if c == nil {
|
||||||
|
if kl.shouldContainerBeRestarted(&container, pod, &podStatus) {
|
||||||
|
// 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)
|
containerID := dockertools.DockerID(c.ID)
|
||||||
hash := c.Hash
|
hash := c.Hash
|
||||||
glog.V(3).Infof("pod %q container %q exists as %v", podFullName, container.Name, containerID)
|
glog.V(3).Infof("pod %q container %q exists as %v", podFullName, container.Name, containerID)
|
||||||
|
|
||||||
if !createPodInfraContainer {
|
if createPodInfraContainer {
|
||||||
// look for changes in the container.
|
// 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
|
containerChanged := hash != 0 && hash != expectedHash
|
||||||
if !containerChanged {
|
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)
|
result, err := kl.probeContainer(pod, podStatus, container, string(c.ID), c.Created)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO(vmarmol): examine this logic.
|
// TODO(vmarmol): examine this logic.
|
||||||
@ -1146,29 +1172,7 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, runningPod kubeconta
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
glog.Infof("pod %q container %q is unhealthy (probe result: %v), it will be killed and re-created.", podFullName, container.Name, result)
|
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{}
|
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 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
|
|
||||||
// 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{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// After the loop one of the following should be true:
|
// After the loop one of the following should be true:
|
||||||
|
Loading…
Reference in New Issue
Block a user