From 29fe72a142a5e8abc47a8f4225a510721a952e75 Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Sat, 20 Jan 2024 19:25:02 +0100 Subject: [PATCH] Ignore default network status The default network status must be ignored and be kept. A unit test has been added to cover this case. --- pkg/controller/pod.go | 31 ++++++++++++++++++++++++------- pkg/controller/pod_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/pkg/controller/pod.go b/pkg/controller/pod.go index 45ddc94d..e16957c9 100644 --- a/pkg/controller/pod.go +++ b/pkg/controller/pod.go @@ -200,14 +200,9 @@ func (pnc *PodNetworksController) processNextWorkItem() bool { return true } - networkSelectionElements, err := annotations.PodNetworkSelectionElements(pod) // ignore error as other functions below? + networkSelectionElements, networkStatus, err := getPodNetworks(pod) if err != nil { - klog.Errorf("failed to get NetworkSelectionElements: %v", err) - return true - } - networkStatus, err := annotations.PodDynamicNetworkStatus(pod) - if err != nil { - klog.Errorf("failed to get NetworkStatus: %v", err) + klog.Errorf("failed to get pod networks: %v", err) return true } indexedNetworkSelectionElements := annotations.IndexNetworkSelectionElements(networkSelectionElements) @@ -588,3 +583,25 @@ func separateNamespaceAndName(namespacedName string) (namespace string, name str } return splitNamespacedName[0], splitNamespacedName[1], nil } + +func getPodNetworks(pod *corev1.Pod) ([]nadv1.NetworkSelectionElement, []nadv1.NetworkStatus, error) { + networkSelectionElements, err := annotations.PodNetworkSelectionElements(pod) + if err != nil { + return nil, nil, err + } + networkStatus, err := annotations.PodDynamicNetworkStatus(pod) + if err != nil { + return nil, nil, err + } + + networkStatusWithoutDefault := []nadv1.NetworkStatus{} + + for i := range networkStatus { + if networkStatus[i].Default { + continue + } + networkStatusWithoutDefault = append(networkStatusWithoutDefault, networkStatus[i]) + } + + return networkSelectionElements, networkStatusWithoutDefault, err +} diff --git a/pkg/controller/pod_test.go b/pkg/controller/pod_test.go index 0409e8ae..86a26859 100644 --- a/pkg/controller/pod_test.go +++ b/pkg/controller/pod_test.go @@ -587,6 +587,35 @@ var _ = Describe("Dynamic Attachment controller", func() { }) }) + When("an attachment status for the default network is added", func() { + JustBeforeEach(func() { + pod = updatePodSpec(pod) + pod.Annotations[nad.NetworkStatusAnnot] = `[{"name":"default/tiny-net","default": true,"interface":"net0","dns":{}}]` + _, err := k8sClient.CoreV1().Pods(namespace).UpdateStatus( + context.TODO(), + updatePodSpec(pod), + metav1.UpdateOptions{}) + Expect(err).NotTo(HaveOccurred()) + }) + + It("the pod network-status has not changed (default network status must be ignored)", func() { + defaultNet := ifaceStatusForDefaultNamespace(networkName, "net0", "") + defaultNet.Default = true + + Eventually(func() ([]nad.NetworkStatus, error) { + updatedPod, err := k8sClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) + if err != nil { + return nil, err + } + status, err := annotations.PodDynamicNetworkStatus(updatedPod) + if err != nil { + return nil, err + } + return status, nil + }).Should(ConsistOf(defaultNet)) + }) + }) + }) }) })