mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-06-24 14:33:03 +00:00
fix: pod analyzer catches errors when containers are in Terminated state (#1438)
Signed-off-by: Guoxun Wei <guwe@microsoft.com> Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
parent
e7783482ce
commit
dceda9a6a1
@ -123,6 +123,20 @@ func analyzeContainerStatusFailures(a common.Analyzer, statuses []v1.ContainerSt
|
|||||||
Sensitive: []common.Sensitive{},
|
Sensitive: []common.Sensitive{},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else if containerStatus.State.Terminated != nil {
|
||||||
|
if containerStatus.State.Terminated.ExitCode != 0 {
|
||||||
|
// This represents a container that is terminated abnormally
|
||||||
|
// https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-state-terminated
|
||||||
|
exitCode := containerStatus.State.Terminated.ExitCode
|
||||||
|
reason := containerStatus.State.Terminated.Reason
|
||||||
|
if reason == "" {
|
||||||
|
reason = "Unknown"
|
||||||
|
}
|
||||||
|
failures = append(failures, common.Failure{
|
||||||
|
Text: fmt.Sprintf("the termination reason is %s exitCode=%d container=%s pod=%s", reason, exitCode, containerStatus.Name, name),
|
||||||
|
Sensitive: []common.Sensitive{},
|
||||||
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// when pod is Running but its ReadinessProbe fails
|
// when pod is Running but its ReadinessProbe fails
|
||||||
if !containerStatus.Ready && statusPhase == "Running" {
|
if !containerStatus.Ready && statusPhase == "Running" {
|
||||||
|
@ -343,6 +343,57 @@ func TestPodAnalyzer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Terminated container with non-zero exit code",
|
||||||
|
config: common.Analyzer{
|
||||||
|
Client: &kubernetes.Client{
|
||||||
|
Client: fake.NewSimpleClientset(
|
||||||
|
&v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "Pod1",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Status: v1.PodStatus{
|
||||||
|
Phase: v1.PodFailed,
|
||||||
|
ContainerStatuses: []v1.ContainerStatus{
|
||||||
|
{
|
||||||
|
Name: "Container1",
|
||||||
|
Ready: false,
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{
|
||||||
|
ExitCode: 1,
|
||||||
|
Reason: "Error",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Container2",
|
||||||
|
Ready: false,
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{
|
||||||
|
ExitCode: 2,
|
||||||
|
Reason: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
Context: context.Background(),
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
expectations: []struct {
|
||||||
|
name string
|
||||||
|
failuresCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default/Pod1",
|
||||||
|
failuresCount: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
podAnalyzer := PodAnalyzer{}
|
podAnalyzer := PodAnalyzer{}
|
||||||
|
Loading…
Reference in New Issue
Block a user