Merge pull request #8271 from mcluseau/main

fix(pkg/repository/maintenance): handle when there's no container status
pull/8353/head
Xun Jiang/Bruce Jiang 2024-10-28 13:50:25 +08:00 committed by GitHub
commit 8058a38058
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 16 deletions

View File

@ -0,0 +1 @@
fix(pkg/repository/maintenance): don't panic when there's no container statuses

View File

@ -117,7 +117,20 @@ func GetMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, e
} }
// we only have one maintenance pod for the job // 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) { func GetLatestMaintenanceJob(cli client.Client, ns string) (*batchv1.Job, error) {

View File

@ -188,33 +188,54 @@ func TestGetMaintenanceResultFromJob(t *testing.T) {
}, },
} }
// Set up test pod // Set up test pod with no status
pod := &v1.Pod{ pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test-pod", Name: "test-pod",
Namespace: "default", Namespace: "default",
Labels: map[string]string{"job-name": job.Name}, Labels: map[string]string{"job-name": job.Name},
}, },
Status: v1.PodStatus{ }
ContainerStatuses: []v1.ContainerStatus{
{ // Create a fake Kubernetes client
State: v1.ContainerState{ cli := fake.NewClientBuilder().WithObjects(job, pod).Build()
Terminated: &v1.ContainerStateTerminated{
Message: "test message", // 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",
}, },
}, },
}, },
}, },
} }
// Create a fake Kubernetes client // This call should return the termination message with no error
cli := fake.NewClientBuilder().WithObjects(job, pod).Build() cli = fake.NewClientBuilder().WithObjects(job, pod).Build()
result, err = GetMaintenanceResultFromJob(cli, job)
// Call the function
result, err := GetMaintenanceResultFromJob(cli, job)
// Check if the result and error match the expectation
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "test message", result) assert.Equal(t, "test message", result)
} }