Skip to content

Commit 0c9ee89

Browse files
LionelJouinmaiqueb
authored andcommitted
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 0c9ee89

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
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

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ linters:
5858
enable:
5959
- bodyclose
6060
- deadcode
61-
- depguard
6261
- dogsled
6362
- dupl
6463
- errcheck

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: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ 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"
7273
podName = "tiny-winy-pod"
7374
)
75+
cniArgs := &map[string]string{"foo": "bar"}
7476
var (
7577
eventRecorder *record.FakeRecorder
7678
k8sClient k8sclient.Interface
@@ -168,8 +170,8 @@ var _ = Describe("Dynamic Attachment controller", func() {
168170
}
169171
return status, nil
170172
}).Should(ConsistOf(
171-
ifaceStatus(namespace, networkName, "net0", ""),
172-
ifaceStatus(namespace, networkToAdd, "net1", macAddr)))
173+
ifaceStatusForDefaultNamespace(networkName, "net0", ""),
174+
ifaceStatusForDefaultNamespace(networkToAdd, "net1", macAddr)))
173175
})
174176
})
175177

@@ -233,7 +235,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
233235
}
234236
return status, nil
235237
}).Should(ConsistOf(
236-
ifaceStatus(namespace, networkName, "net0", "")))
238+
ifaceStatusForDefaultNamespace(networkName, "net0", "")))
237239
})
238240

239241
It("throws an event indicating the interface add operation is rejected", func() {
@@ -244,6 +246,61 @@ var _ = Describe("Dynamic Attachment controller", func() {
244246
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
245247
})
246248
})
249+
250+
When("an attachment is added with attributes (IPs, MAC, cni-args)", func() {
251+
JustBeforeEach(func() {
252+
pod = updatePodSpec(pod)
253+
netSelectionElements := append(generateNetworkSelectionElements(namespace, networkName),
254+
nad.NetworkSelectionElement{
255+
Name: networkToAdd,
256+
Namespace: namespace,
257+
InterfaceRequest: "net1",
258+
IPRequest: []string{ipAddr},
259+
MacRequest: macAddr,
260+
CNIArgs: &map[string]interface{}{
261+
"foo": "bar",
262+
},
263+
},
264+
)
265+
serelizedNetSelectionElements, _ := json.Marshal(netSelectionElements)
266+
pod.Annotations[nad.NetworkAttachmentAnnot] = string(serelizedNetSelectionElements)
267+
_, err := k8sClient.CoreV1().Pods(namespace).UpdateStatus(
268+
context.TODO(),
269+
pod,
270+
metav1.UpdateOptions{})
271+
Expect(err).NotTo(HaveOccurred())
272+
})
273+
274+
It("an `AddedInterface` event is seen in the event recorded", func() {
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+
ifaceStatusForDefaultNamespace(networkName, "net0", ""),
300+
ifaceStatusForDefaultNamespace(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 ""
@@ -479,7 +541,8 @@ func dummyMultusConfig() string {
479541
}`
480542
}
481543

482-
func ifaceStatus(namespace, networkName, ifaceName, macAddress string) nad.NetworkStatus {
544+
func ifaceStatusForDefaultNamespace(networkName, ifaceName, macAddress string) nad.NetworkStatus {
545+
const namespace = "default"
483546
return nad.NetworkStatus{
484547
Name: fmt.Sprintf("%s/%s", namespace, networkName),
485548
Interface: ifaceName,

0 commit comments

Comments
 (0)