Skip to content

Commit

Permalink
Merge pull request #222 from maiqueb/reconciler-no-network-status
Browse files Browse the repository at this point in the history
reconciler: account for pods that do not have net-status annotations
  • Loading branch information
maiqueb committed May 19, 2022
2 parents 1c2cd40 + a1fc8f4 commit 2789b2e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
30 changes: 23 additions & 7 deletions pkg/reconciler/wrappedPod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions pkg/reconciler/wrappedPod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 2789b2e

Please sign in to comment.