mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #21349 from Random-Liu/cleanup-should-container-be-restarted
Auto commit by PR queue bot
This commit is contained in:
commit
6b8e0d45f0
@ -46,28 +46,28 @@ type RuntimeHelper interface {
|
|||||||
// ShouldContainerBeRestarted checks whether a container needs to be restarted.
|
// ShouldContainerBeRestarted checks whether a container needs to be restarted.
|
||||||
// TODO(yifan): Think about how to refactor this.
|
// TODO(yifan): Think about how to refactor this.
|
||||||
func ShouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatus *PodStatus) bool {
|
func ShouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatus *PodStatus) bool {
|
||||||
// Get all dead container status.
|
// Get latest container status.
|
||||||
var resultStatus []*ContainerStatus
|
status := podStatus.FindContainerStatusByName(container.Name)
|
||||||
for _, containerStatus := range podStatus.ContainerStatuses {
|
// If the container was never started before, we should start it.
|
||||||
if containerStatus.Name == container.Name && containerStatus.State == ContainerStateExited {
|
// NOTE(random-liu): If all historical containers were GC'd, we'll also return true here.
|
||||||
resultStatus = append(resultStatus, containerStatus)
|
if status == nil {
|
||||||
}
|
return true
|
||||||
}
|
}
|
||||||
|
// Check whether container is running
|
||||||
// Check RestartPolicy for dead container.
|
if status.State == ContainerStateRunning {
|
||||||
if len(resultStatus) > 0 {
|
return false
|
||||||
if pod.Spec.RestartPolicy == api.RestartPolicyNever {
|
}
|
||||||
glog.V(4).Infof("Already ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
// Check RestartPolicy for dead container
|
||||||
|
if pod.Spec.RestartPolicy == api.RestartPolicyNever {
|
||||||
|
glog.V(4).Infof("Already ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if pod.Spec.RestartPolicy == api.RestartPolicyOnFailure {
|
||||||
|
// Check the exit code.
|
||||||
|
if status.ExitCode == 0 {
|
||||||
|
glog.V(4).Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if pod.Spec.RestartPolicy == api.RestartPolicyOnFailure {
|
|
||||||
// Check the exit code of last run. Note: This assumes the result is sorted
|
|
||||||
// by the created time in reverse order.
|
|
||||||
if resultStatus[0].ExitCode == 0 {
|
|
||||||
glog.V(4).Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -134,3 +134,74 @@ func TestExpandCommandAndArgs(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldContainerBeRestarted(t *testing.T) {
|
||||||
|
pod := &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
UID: "12345678",
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: "new",
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "no-history"},
|
||||||
|
{Name: "alive"},
|
||||||
|
{Name: "succeed"},
|
||||||
|
{Name: "failed"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
podStatus := &PodStatus{
|
||||||
|
ID: pod.UID,
|
||||||
|
Name: pod.Name,
|
||||||
|
Namespace: pod.Namespace,
|
||||||
|
ContainerStatuses: []*ContainerStatus{
|
||||||
|
{
|
||||||
|
Name: "alive",
|
||||||
|
State: ContainerStateRunning,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "succeed",
|
||||||
|
State: ContainerStateExited,
|
||||||
|
ExitCode: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "failed",
|
||||||
|
State: ContainerStateExited,
|
||||||
|
ExitCode: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "alive",
|
||||||
|
State: ContainerStateExited,
|
||||||
|
ExitCode: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "failed",
|
||||||
|
State: ContainerStateExited,
|
||||||
|
ExitCode: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
policies := []api.RestartPolicy{
|
||||||
|
api.RestartPolicyNever,
|
||||||
|
api.RestartPolicyOnFailure,
|
||||||
|
api.RestartPolicyAlways,
|
||||||
|
}
|
||||||
|
expected := map[string][]bool{
|
||||||
|
"no-history": {true, true, true},
|
||||||
|
"alive": {false, false, false},
|
||||||
|
"succeed": {false, false, true},
|
||||||
|
"failed": {false, true, true},
|
||||||
|
}
|
||||||
|
for _, c := range pod.Spec.Containers {
|
||||||
|
for i, policy := range policies {
|
||||||
|
pod.Spec.RestartPolicy = policy
|
||||||
|
e := expected[c.Name][i]
|
||||||
|
r := ShouldContainerBeRestarted(&c, pod, podStatus)
|
||||||
|
if r != e {
|
||||||
|
t.Errorf("Restart for container %q with restart policy %q expected %t, got %t",
|
||||||
|
c.Name, policy, e, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user