From a1fc8f48b391d563784e9e27a3ba36ee21134a8c Mon Sep 17 00:00:00 2001 From: Miguel Duarte Barroso Date: Tue, 17 May 2022 12:10:30 +0200 Subject: [PATCH] reconciler: account for pods that do not have net-status annotations Signed-off-by: Miguel Duarte Barroso --- pkg/reconciler/wrappedPod.go | 30 +++++++++++++++++++++++------- pkg/reconciler/wrappedPod_test.go | 9 +++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/reconciler/wrappedPod.go b/pkg/reconciler/wrappedPod.go index 3229eb5c1..9ed265ba3 100644 --- a/pkg/reconciler/wrappedPod.go +++ b/pkg/reconciler/wrappedPod.go @@ -25,8 +25,12 @@ type podWrapper struct { type void struct{} func wrapPod(pod v1.Pod) *podWrapper { + podIPSet, err := getFlatIPSet(pod) + if err != nil { + podIPSet = map[string]void{} + } return &podWrapper{ - ips: getFlatIPSet(pod), + ips: podIPSet, phase: pod.Status.Phase, } } @@ -57,14 +61,18 @@ func indexPods(livePodList []v1.Pod, whereaboutsPodNames map[string]void) map[st return podMap } -func getFlatIPSet(pod v1.Pod) map[string]void { +func getFlatIPSet(pod v1.Pod) (map[string]void, error) { var empty void ipSet := map[string]void{} - networkStatusAnnotationValue := []byte(pod.Annotations[MultusNetworkStatusAnnotation]) var networkStatusList []k8snetworkplumbingwgv1.NetworkStatus - if err := json.Unmarshal(networkStatusAnnotationValue, &networkStatusList); err != nil { - _ = logging.Errorf("could not parse network annotation %s for pod: %s; error: %v", networkStatusAnnotationValue, composePodRef(pod), err) - return ipSet + + networkStatusAnnotationValue := networkStatusFromPod(pod) + if err := json.Unmarshal([]byte(networkStatusAnnotationValue), &networkStatusList); err != nil { + return ipSet, logging.Errorf( + "could not parse network annotation %s for pod: %s; error: %v", + networkStatusAnnotationValue, + composePodRef(pod), + err) } for _, network := range networkStatusList { @@ -78,5 +86,13 @@ func getFlatIPSet(pod v1.Pod) map[string]void { logging.Debugf("Added IP %s for pod %s", ip, composePodRef(pod)) } } - return ipSet + return ipSet, nil +} + +func networkStatusFromPod(pod v1.Pod) string { + networkStatusAnnotationValue, isStatusAnnotationPresent := pod.Annotations[MultusNetworkStatusAnnotation] + if !isStatusAnnotationPresent || len(networkStatusAnnotationValue) == 0 { + return "[]" + } + return networkStatusAnnotationValue } diff --git a/pkg/reconciler/wrappedPod_test.go b/pkg/reconciler/wrappedPod_test.go index 79d9afd93..faae9ff19 100644 --- a/pkg/reconciler/wrappedPod_test.go +++ b/pkg/reconciler/wrappedPod_test.go @@ -122,6 +122,15 @@ var _ = Describe("Pod Wrapper operations", func() { } Expect(wrapPod(pod).ips).To(BeEmpty()) }) + + It("returns an empty list when a pod does not feature network status annotations", func() { + pod := v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{}, + }, + } + Expect(wrapPod(pod).ips).To(BeEmpty()) + }) }) Context("the index pods operation", func() {