Merge pull request #124677 from HirazawaUi/add-const-ContainerStatusUnknown

kubelet: Use constant replace same value variables of the ContainerStateTerminated Reason field
This commit is contained in:
Kubernetes Prow Robot 2024-06-06 17:05:23 -07:00 committed by GitHub
commit 009a291573
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 12 additions and 9 deletions

View File

@ -274,6 +274,9 @@ const (
ContainerStateUnknown State = "unknown" ContainerStateUnknown State = "unknown"
) )
// ContainerReasonStatusUnknown indicates a container the status of the container cannot be determined.
const ContainerReasonStatusUnknown string = "ContainerStatusUnknown"
// Container provides the runtime information for a container, such as ID, hash, // Container provides the runtime information for a container, such as ID, hash,
// state of the container. // state of the container.
type Container struct { type Container struct {

View File

@ -2028,7 +2028,7 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
// twice. "container never ran" is different than "container ran and failed". This is handled differently in the kubelet // twice. "container never ran" is different than "container ran and failed". This is handled differently in the kubelet
// and it is handled differently in higher order logic like crashloop detection and handling // and it is handled differently in higher order logic like crashloop detection and handling
status.State.Terminated = &v1.ContainerStateTerminated{ status.State.Terminated = &v1.ContainerStateTerminated{
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
Message: "The container could not be located when the pod was terminated", Message: "The container could not be located when the pod was terminated",
ExitCode: 137, // this code indicates an error ExitCode: 137, // this code indicates an error
} }
@ -2255,7 +2255,7 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
// https://github.com/kubernetes/kubernetes/blob/90c9f7b3e198e82a756a68ffeac978a00d606e55/pkg/kubelet/kubelet_pods.go#L1440-L1445 // https://github.com/kubernetes/kubernetes/blob/90c9f7b3e198e82a756a68ffeac978a00d606e55/pkg/kubelet/kubelet_pods.go#L1440-L1445
// This prevents the pod from becoming pending // This prevents the pod from becoming pending
status.LastTerminationState.Terminated = &v1.ContainerStateTerminated{ status.LastTerminationState.Terminated = &v1.ContainerStateTerminated{
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
Message: "The container could not be located when the pod was deleted. The container used to be Running", Message: "The container could not be located when the pod was deleted. The container used to be Running",
ExitCode: 137, ExitCode: 137,
} }

View File

@ -2154,7 +2154,7 @@ func waitingWithLastTerminationUnknown(cName string, restartCount int32) v1.Cont
}, },
LastTerminationState: v1.ContainerState{ LastTerminationState: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{ Terminated: &v1.ContainerStateTerminated{
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
Message: "The container could not be located when the pod was deleted. The container used to be Running", Message: "The container could not be located when the pod was deleted. The container used to be Running",
ExitCode: 137, ExitCode: 137,
}, },

View File

@ -2135,7 +2135,7 @@ func TestGenerateAPIPodStatusWithReasonCache(t *testing.T) {
"unknown": {Terminated: &v1.ContainerStateTerminated{ "unknown": {Terminated: &v1.ContainerStateTerminated{
ExitCode: 137, ExitCode: 137,
Message: "The container could not be located when the pod was terminated", Message: "The container could not be located when the pod was terminated",
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
}}, }},
}, },
expectedLastTerminationState: map[string]v1.ContainerState{ expectedLastTerminationState: map[string]v1.ContainerState{

View File

@ -446,7 +446,7 @@ func (m *manager) TerminatePod(pod *v1.Pod) {
} }
status.ContainerStatuses[i].State = v1.ContainerState{ status.ContainerStatuses[i].State = v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{ Terminated: &v1.ContainerStateTerminated{
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
Message: "The container could not be located when the pod was terminated", Message: "The container could not be located when the pod was terminated",
ExitCode: 137, ExitCode: 137,
}, },
@ -462,7 +462,7 @@ func (m *manager) TerminatePod(pod *v1.Pod) {
} }
status.InitContainerStatuses[i].State = v1.ContainerState{ status.InitContainerStatuses[i].State = v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{ Terminated: &v1.ContainerStateTerminated{
Reason: "ContainerStatusUnknown", Reason: kubecontainer.ContainerReasonStatusUnknown,
Message: "The container could not be located when the pod was terminated", Message: "The container could not be located when the pod was terminated",
ExitCode: 137, ExitCode: 137,
}, },

View File

@ -638,7 +638,7 @@ func TestTerminatePod(t *testing.T) {
assert.False(t, newStatus.InitContainerStatuses[i].State.Terminated == nil, "expected init containers to be terminated") assert.False(t, newStatus.InitContainerStatuses[i].State.Terminated == nil, "expected init containers to be terminated")
} }
expectUnknownState := v1.ContainerState{Terminated: &v1.ContainerStateTerminated{Reason: "ContainerStatusUnknown", Message: "The container could not be located when the pod was terminated", ExitCode: 137}} expectUnknownState := v1.ContainerState{Terminated: &v1.ContainerStateTerminated{Reason: kubecontainer.ContainerReasonStatusUnknown, Message: "The container could not be located when the pod was terminated", ExitCode: 137}}
if !reflect.DeepEqual(newStatus.InitContainerStatuses[0].State, expectUnknownState) { if !reflect.DeepEqual(newStatus.InitContainerStatuses[0].State, expectUnknownState) {
t.Errorf("terminated container state not defaulted: %s", cmp.Diff(newStatus.InitContainerStatuses[0].State, expectUnknownState)) t.Errorf("terminated container state not defaulted: %s", cmp.Diff(newStatus.InitContainerStatuses[0].State, expectUnknownState))
} }
@ -723,7 +723,7 @@ func TestTerminatePodWaiting(t *testing.T) {
assert.False(t, container.State.Waiting == nil, "expected init containers to be waiting") assert.False(t, container.State.Waiting == nil, "expected init containers to be waiting")
} }
expectUnknownState := v1.ContainerState{Terminated: &v1.ContainerStateTerminated{Reason: "ContainerStatusUnknown", Message: "The container could not be located when the pod was terminated", ExitCode: 137}} expectUnknownState := v1.ContainerState{Terminated: &v1.ContainerStateTerminated{Reason: kubecontainer.ContainerReasonStatusUnknown, Message: "The container could not be located when the pod was terminated", ExitCode: 137}}
if !reflect.DeepEqual(newStatus.InitContainerStatuses[0].State, expectUnknownState) { if !reflect.DeepEqual(newStatus.InitContainerStatuses[0].State, expectUnknownState) {
t.Errorf("terminated container state not defaulted: %s", cmp.Diff(newStatus.InitContainerStatuses[0].State, expectUnknownState)) t.Errorf("terminated container state not defaulted: %s", cmp.Diff(newStatus.InitContainerStatuses[0].State, expectUnknownState))
} }
@ -772,7 +772,7 @@ func TestTerminatePod_DefaultUnknownStatus(t *testing.T) {
if state.Terminated == nil || state.Running != nil || state.Waiting != nil { if state.Terminated == nil || state.Running != nil || state.Waiting != nil {
t.Fatalf("unexpected state: %#v", state) t.Fatalf("unexpected state: %#v", state)
} }
if state.Terminated.ExitCode != 137 || state.Terminated.Reason != "ContainerStatusUnknown" || len(state.Terminated.Message) == 0 { if state.Terminated.ExitCode != 137 || state.Terminated.Reason != kubecontainer.ContainerReasonStatusUnknown || len(state.Terminated.Message) == 0 {
t.Fatalf("unexpected terminated state: %#v", state.Terminated) t.Fatalf("unexpected terminated state: %#v", state.Terminated)
} }
} }