Skip to content

Commit 847a162

Browse files
committed
fix NodePodProbe nil labels panic
Signed-off-by: Jayant <212013719+Jayant-kernel@users.noreply.github.com>
1 parent c9ada9a commit 847a162

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

pkg/controller/nodepodprobe/node_pod_probe_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
337337
util.SetPodConditionIfMsgChanged(podClone, condition)
338338
}
339339
oldMetadata := podClone.ObjectMeta.DeepCopy()
340+
if podClone.Labels == nil {
341+
podClone.Labels = map[string]string{}
342+
}
340343
if podClone.Annotations == nil {
341344
podClone.Annotations = map[string]string{}
342345
}

pkg/controller/nodepodprobe/node_pod_probe_controller_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,62 @@ func TestSyncPodFromNodePodProbe(t *testing.T) {
797797
}
798798
}
799799

800+
func TestUpdatePodProbeStatusInitializesNilLabels(t *testing.T) {
801+
pod := &corev1.Pod{
802+
ObjectMeta: metav1.ObjectMeta{
803+
Name: "pod-1",
804+
Namespace: "default",
805+
},
806+
}
807+
ppm := &appsv1alpha1.PodProbeMarker{
808+
ObjectMeta: metav1.ObjectMeta{
809+
Name: "ppm-1",
810+
Namespace: "default",
811+
},
812+
Spec: appsv1alpha1.PodProbeMarkerSpec{
813+
Probes: []appsv1alpha1.PodContainerProbe{
814+
{
815+
Name: "healthy",
816+
MarkerPolicy: []appsv1alpha1.ProbeMarkerPolicy{
817+
{
818+
State: appsv1alpha1.ProbeSucceeded,
819+
Labels: map[string]string{
820+
"server-healthy": "true",
821+
},
822+
},
823+
},
824+
},
825+
},
826+
},
827+
}
828+
status := appsv1alpha1.PodProbeStatus{
829+
ProbeStates: []appsv1alpha1.ContainerProbeState{
830+
{
831+
Name: "ppm-1#healthy",
832+
State: appsv1alpha1.ProbeSucceeded,
833+
},
834+
},
835+
}
836+
837+
fakeClient := fake.NewClientBuilder().
838+
WithScheme(scheme).
839+
WithObjects(pod, ppm).
840+
Build()
841+
recon := ReconcileNodePodProbe{Client: fakeClient}
842+
843+
if err := recon.updatePodProbeStatus(pod, status); err != nil {
844+
t.Fatalf("updatePodProbeStatus failed: %s", err.Error())
845+
}
846+
847+
updatedPod := &corev1.Pod{}
848+
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}, updatedPod); err != nil {
849+
t.Fatalf("get Pod failed: %s", err.Error())
850+
}
851+
if updatedPod.Labels["server-healthy"] != "true" {
852+
t.Fatalf("expected label server-healthy=true, got %v", updatedPod.Labels)
853+
}
854+
}
855+
800856
func checkNodePodProbeEqual(c client.WithWatch, t *testing.T, expect []*appsv1alpha1.NodePodProbe) bool {
801857
for i := range expect {
802858
obj := expect[i]

0 commit comments

Comments
 (0)