Skip to content

Commit a969a17

Browse files
committed
Controller: Set CNI args on the delegate request
The CNI args has to be set in the delegate request so the cni-args in the network attachment annotation are considered. Signed-off-by: Lionel Jouin <[email protected]>
1 parent 8e06025 commit a969a17

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

.github/workflows/check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Linters
3232
uses: golangci/golangci-lint-action@v3
3333
with:
34-
version: v1.48.0
34+
version: v1.54.2
3535
args: --timeout 3m --verbose cmd/... pkg/...
3636

3737
- name: Test

pkg/controller/pod.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (pnc *PodNetworksController) processNextWorkItem() bool {
243243
PodNetNS: netnsPath,
244244
})
245245
if err != nil {
246-
klog.Errorf("error removing attachments: %v")
246+
klog.Errorf("error removing attachments: %v", err)
247247
return true
248248
}
249249
}
@@ -440,11 +440,20 @@ func podContainerID(pod *corev1.Pod) string {
440440
}
441441

442442
func addIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement) string {
443+
attributes := ""
444+
if len(network.IPRequest) > 0 || network.MacRequest != "" || network.CNIArgs != nil {
445+
attributes = fmt.Sprintf(" (ips: %v, mac: %s, cni-args: %v)",
446+
network.IPRequest,
447+
network.MacRequest,
448+
network.CNIArgs,
449+
)
450+
}
443451
return fmt.Sprintf(
444-
"pod [%s]: added interface %s to network: %s",
452+
"pod [%s]: added interface %s to network: %s%s",
445453
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
446454
network.InterfaceRequest,
447455
network.Name,
456+
attributes,
448457
)
449458
}
450459

@@ -465,10 +474,11 @@ func rejectInterfaceAddEventFormat(pod *corev1.Pod) string {
465474
}
466475

467476
func interfaceAttributes(networkData nadv1.NetworkSelectionElement) *multusapi.DelegateInterfaceAttributes {
468-
if len(networkData.IPRequest) > 0 || networkData.MacRequest != "" {
477+
if len(networkData.IPRequest) > 0 || networkData.MacRequest != "" || networkData.CNIArgs != nil {
469478
return &multusapi.DelegateInterfaceAttributes{
470479
IPRequest: networkData.IPRequest,
471480
MacRequest: networkData.MacRequest,
481+
CNIArgs: networkData.CNIArgs,
472482
}
473483
}
474484
return nil

pkg/controller/pod_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
6666
Context("with an existing running pod", func() {
6767
const (
6868
cniVersion = "0.3.0"
69+
ipAddr = "172.16.0.1"
6970
macAddr = "02:03:04:05:06:07"
7071
namespace = "default"
7172
networkName = "tiny-net"
@@ -244,6 +245,62 @@ var _ = Describe("Dynamic Attachment controller", func() {
244245
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
245246
})
246247
})
248+
249+
When("an attachment is added with attributes (IPs, MAC, cni-args)", func() {
250+
JustBeforeEach(func() {
251+
pod = updatePodSpec(pod)
252+
netSelectionElements := append(generateNetworkSelectionElements(namespace, networkName),
253+
nad.NetworkSelectionElement{
254+
Name: networkToAdd,
255+
Namespace: namespace,
256+
InterfaceRequest: "net1",
257+
IPRequest: []string{ipAddr},
258+
MacRequest: macAddr,
259+
CNIArgs: &map[string]interface{}{
260+
"foo": "bar",
261+
},
262+
},
263+
)
264+
serelizedNetSelectionElements, _ := json.Marshal(netSelectionElements)
265+
pod.Annotations[nad.NetworkAttachmentAnnot] = string(serelizedNetSelectionElements)
266+
_, err := k8sClient.CoreV1().Pods(namespace).UpdateStatus(
267+
context.TODO(),
268+
pod,
269+
metav1.UpdateOptions{})
270+
Expect(err).NotTo(HaveOccurred())
271+
})
272+
273+
It("an `AddedInterface` event is seen in the event recorded", func() {
274+
cniArgs := &map[string]string{"foo": "bar"}
275+
expectedEventPayload := fmt.Sprintf(
276+
"Normal AddedInterface pod [%s]: added interface %s to network: %s (ips: [%s], mac: %s, cni-args: %v)",
277+
annotations.NamespacedName(namespace, podName),
278+
"net1",
279+
networkToAdd,
280+
ipAddr,
281+
macAddr,
282+
cniArgs,
283+
)
284+
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
285+
})
286+
287+
It("the pod network-status is updated with the new network attachment", func() {
288+
Eventually(func() ([]nad.NetworkStatus, error) {
289+
updatedPod, err := k8sClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
290+
if err != nil {
291+
return nil, err
292+
}
293+
status, err := annotations.PodDynamicNetworkStatus(updatedPod)
294+
if err != nil {
295+
return nil, err
296+
}
297+
return status, nil
298+
}).Should(ConsistOf(
299+
ifaceStatus(namespace, networkName, "net0", ""),
300+
ifaceStatus(namespace, networkToAdd, "net1", macAddr)))
301+
})
302+
})
303+
247304
})
248305
})
249306
})
@@ -425,7 +482,7 @@ func podNetworkConfig(networkNames ...string) map[string]string {
425482
}
426483
}
427484

428-
func generateNetworkSelectionAnnotation(namespace string, networkNames ...string) string {
485+
func generateNetworkSelectionElements(namespace string, networkNames ...string) []nad.NetworkSelectionElement {
429486
var netSelectionElements []nad.NetworkSelectionElement
430487
for i, networkName := range networkNames {
431488
netSelectionElements = append(
@@ -439,6 +496,11 @@ func generateNetworkSelectionAnnotation(namespace string, networkNames ...string
439496
if netSelectionElements == nil {
440497
netSelectionElements = make([]nad.NetworkSelectionElement, 0)
441498
}
499+
return netSelectionElements
500+
}
501+
502+
func generateNetworkSelectionAnnotation(namespace string, networkNames ...string) string {
503+
netSelectionElements := generateNetworkSelectionElements(namespace, networkNames...)
442504
serelizedNetSelectionElements, err := json.Marshal(netSelectionElements)
443505
if err != nil {
444506
return ""

0 commit comments

Comments
 (0)