Skip to content

Commit

Permalink
Fix crash when pod container has restarts but is running (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Williams <[email protected]>
  • Loading branch information
max-rocket-internet and max-rocket-internet authored Sep 11, 2023
1 parent c70ba66 commit 73ef276
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/checkup/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func CheckPods(resources *v1.PodList) (results symptoms.SymptomList) {
}
}

if scs.RestartCount != 0 {
if scs.RestartCount > 0 && scs.LastTerminationState.Terminated != nil {
if time.Since(scs.LastTerminationState.Terminated.FinishedAt.Time).Hours() > 1 {
results.Add(symptoms.Symptom{
Message: fmt.Sprintf("container '%s' has been restarted %d times", scs.Name, scs.RestartCount),
Expand Down
39 changes: 39 additions & 0 deletions pkg/checkup/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,45 @@ func TestCheckPodsWithRestarts(t *testing.T) {
assert.Equal(t, "critical", result.Symptoms[0].Severity)
}

func TestCheckPodsWithRestartsAndRunning(t *testing.T) {
dummyResources := v1.PodList{
Items: []v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Deployment",
},
},
},
Status: v1.PodStatus{
StartTime: createNewTimeStampPtr(time.Now()),
Phase: "Running",
Conditions: []v1.PodCondition{},
ContainerStatuses: []v1.ContainerStatus{
{
Ready: true,
Name: "c1",
RestartCount: 1,
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
ExitCode: 0,
Reason: "Completed",
},
},
},
},
},
},
},
}

result := CheckPods(&dummyResources)

assert.Len(t, result.Symptoms, 0)
}

func TestCheckPodsNoOwner(t *testing.T) {
dummyResources := v1.PodList{
Items: []v1.Pod{
Expand Down

0 comments on commit 73ef276

Please sign in to comment.