Skip to content

Commit

Permalink
fix(pkg/repository/maintenance): don't panic when there's no containe…
Browse files Browse the repository at this point in the history
…r statuses

Signed-off-by: Mikaël Cluseau <[email protected]>
  • Loading branch information
mcluseau committed Oct 22, 2024
1 parent c53ab20 commit e770f0c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8271-mcluseau
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix(pkg/repository/maintenance): don't panic when there's no container statuses
15 changes: 14 additions & 1 deletion pkg/repository/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,20 @@ func GetMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, e
}

// we only have one maintenance pod for the job
return podList.Items[0].Status.ContainerStatuses[0].State.Terminated.Message, nil
pod := podList.Items[0]

statuses := pod.Status.ContainerStatuses
if len(statuses) == 0 {
return "", fmt.Errorf("no container statuses found for job %s", job.Name)
}

// we only have one maintenance container
terminated := statuses[0].State.Terminated
if terminated == nil {
return "", fmt.Errorf("container for job %s is not terminated", job.Name)
}

return terminated.Message, nil
}

func GetLatestMaintenanceJob(cli client.Client, ns string) (*batchv1.Job, error) {
Expand Down
49 changes: 35 additions & 14 deletions pkg/repository/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,54 @@ func TestGetMaintenanceResultFromJob(t *testing.T) {
},
}

// Set up test pod
// Set up test pod with no status
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "default",
Labels: map[string]string{"job-name": job.Name},
},
Status: v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: "test message",
},
},
},
},
},
}

// Create a fake Kubernetes client
cli := fake.NewClientBuilder().WithObjects(job, pod).Build()

// Call the function
// test an error should be returned
result, err := GetMaintenanceResultFromJob(cli, job)
assert.Error(t, err)
assert.Equal(t, "", result)

// Set a non-terminated container status to the pod
pod.Status = v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{},
},
},
}

// Test an error should be returned
cli = fake.NewClientBuilder().WithObjects(job, pod).Build()
result, err = GetMaintenanceResultFromJob(cli, job)
assert.Error(t, err)
assert.Equal(t, "", result)

// Set a terminated container status to the pod
pod.Status = v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: "test message",
},
},
},
},
}

// Check if the result and error match the expectation
// This call should return the termination message with no error
cli = fake.NewClientBuilder().WithObjects(job, pod).Build()
result, err = GetMaintenanceResultFromJob(cli, job)
assert.NoError(t, err)
assert.Equal(t, "test message", result)
}
Expand Down

0 comments on commit e770f0c

Please sign in to comment.