Skip to content

Commit

Permalink
Ignore default network status
Browse files Browse the repository at this point in the history
The default network status must be ignored and be kept.
A unit test has been added to cover this case.
  • Loading branch information
LionelJouin authored and maiqueb committed Jan 22, 2024
1 parent 093e15d commit 29fe72a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions pkg/controller/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

})
})
})
Expand Down

0 comments on commit 29fe72a

Please sign in to comment.