Skip to content

Commit 4f7277b

Browse files
authored
Do not include failing pods in kube environment report #4448 (#625)
* Do not include failing pods in kube environment report #4448 * Added ref to github issue
1 parent f4133fb commit 4f7277b

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

internal/kube/kube.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ func processPods(list *corev1.PodList, logger *logger.Logger) ([]*PodData, error
196196
cancel() // send cancel signal to goroutines
197197
return
198198
}
199-
mutex.Lock()
200-
podsData = append(podsData, data)
201-
mutex.Unlock()
199+
// Only append if data is not nil (NewPodData returns nil for skipped pods)
200+
if data != nil {
201+
mutex.Lock()
202+
podsData = append(podsData, data)
203+
mutex.Unlock()
204+
}
202205
}
203206
}(pod)
204207
}

internal/kube/kube_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,101 @@ func TestKubeTestSuite(t *testing.T) {
293293

294294
suite.Run(t, new(KubeTestSuite))
295295
}
296+
297+
// TestProcessPodsWithFailedPodsWithoutImageIDs tests that failed pods without image IDs
298+
// are skipped and do not result in nil entries in the returned slice
299+
// This relates to issue https://github.com/kosli-dev/server/issues/4448
300+
func TestProcessPodsWithFailedPodsWithoutImageIDs(t *testing.T) {
301+
testLogger := logger.NewStandardLogger()
302+
303+
// Create a mix of pods: running, failed with image IDs, and failed without image IDs
304+
pods := &corev1.PodList{
305+
Items: []corev1.Pod{
306+
{
307+
ObjectMeta: metav1.ObjectMeta{
308+
Name: "running-pod",
309+
Namespace: "test-ns",
310+
},
311+
Status: corev1.PodStatus{
312+
Phase: corev1.PodRunning,
313+
ContainerStatuses: []corev1.ContainerStatus{
314+
{
315+
Name: "container1",
316+
Image: "nginx:1.21.3",
317+
ImageID: "docker-pullable://nginx@sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36",
318+
},
319+
},
320+
},
321+
},
322+
{
323+
ObjectMeta: metav1.ObjectMeta{
324+
Name: "failed-pod-without-imageid",
325+
Namespace: "test-ns",
326+
},
327+
Status: corev1.PodStatus{
328+
Phase: corev1.PodFailed,
329+
ContainerStatuses: []corev1.ContainerStatus{
330+
{
331+
Name: "container1",
332+
Image: "nginx:1.21.3",
333+
ImageID: "", // Empty ImageID - should be skipped
334+
},
335+
},
336+
},
337+
},
338+
{
339+
ObjectMeta: metav1.ObjectMeta{
340+
Name: "another-running-pod",
341+
Namespace: "test-ns",
342+
},
343+
Status: corev1.PodStatus{
344+
Phase: corev1.PodRunning,
345+
ContainerStatuses: []corev1.ContainerStatus{
346+
{
347+
Name: "container1",
348+
Image: "nginx:1.21.0",
349+
ImageID: "docker-pullable://nginx@sha256:123a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36",
350+
},
351+
},
352+
},
353+
},
354+
{
355+
ObjectMeta: metav1.ObjectMeta{
356+
Name: "another-failed-pod-without-imageid",
357+
Namespace: "test-ns",
358+
},
359+
Status: corev1.PodStatus{
360+
Phase: corev1.PodFailed,
361+
ContainerStatuses: []corev1.ContainerStatus{
362+
{
363+
Name: "container1",
364+
Image: "busybox:latest",
365+
ImageID: "", // Empty ImageID - should be skipped
366+
},
367+
},
368+
},
369+
},
370+
},
371+
}
372+
373+
result, err := processPods(pods, testLogger)
374+
require.NoError(t, err, "processPods should not return an error")
375+
376+
// We should only get 2 pods (the two running ones), not 4
377+
require.Equal(t, 2, len(result), "Expected only running pods to be included")
378+
379+
// Verify no nil entries
380+
for i, podData := range result {
381+
require.NotNil(t, podData, "Pod data at index %d should not be nil", i)
382+
}
383+
384+
// Verify the correct pods are included
385+
podNames := make([]string, len(result))
386+
for i, podData := range result {
387+
podNames[i] = podData.PodName
388+
}
389+
require.Contains(t, podNames, "running-pod")
390+
require.Contains(t, podNames, "another-running-pod")
391+
require.NotContains(t, podNames, "failed-pod-without-imageid")
392+
require.NotContains(t, podNames, "another-failed-pod-without-imageid")
393+
}

0 commit comments

Comments
 (0)