@@ -22,6 +22,7 @@ import (
22
22
nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
23
23
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
24
24
nadlisterv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/listers/k8s.cni.cncf.io/v1"
25
+ multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
25
26
26
27
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/annotations"
27
28
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/cri"
@@ -151,13 +152,13 @@ func (pnc *PodNetworksController) handleDynamicInterfaceRequest(dynamicAttachmen
151
152
if err != nil {
152
153
return err
153
154
}
154
- return pnc .addNetworks (dynamicAttachmentRequest . AttachmentNames , pod )
155
+ return pnc .addNetworks (dynamicAttachmentRequest , pod )
155
156
} else if dynamicAttachmentRequest .Type == "remove" {
156
157
pod , err := pnc .podsLister .Pods (dynamicAttachmentRequest .PodNamespace ).Get (dynamicAttachmentRequest .PodName )
157
158
if err != nil {
158
159
return err
159
160
}
160
- return pnc .removeNetworks (dynamicAttachmentRequest . AttachmentNames , pod )
161
+ return pnc .removeNetworks (dynamicAttachmentRequest , pod )
161
162
} else {
162
163
klog .Infof ("very weird attachment request: %+v" , dynamicAttachmentRequest )
163
164
}
@@ -246,19 +247,67 @@ func namespacedName(podNamespace string, podName string) string {
246
247
return fmt .Sprintf ("%s/%s" , podNamespace , podName )
247
248
}
248
249
249
- func (pnc * PodNetworksController ) addNetworks (netsToAdd []* nadv1.NetworkSelectionElement , pod * corev1.Pod ) error {
250
- for i := range netsToAdd {
251
- klog .Infof ("network to add: %v" , netsToAdd [i ])
252
- pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , "add network: %s" , netsToAdd [i ].Name )
250
+ func (pnc * PodNetworksController ) addNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
251
+ for i := range dynamicAttachmentRequest .AttachmentNames {
252
+ netToAdd := dynamicAttachmentRequest .AttachmentNames [i ]
253
+ klog .Infof ("network to add: %v" , netToAdd )
254
+
255
+ netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netToAdd .Namespace ).Get (netToAdd .Name )
256
+ if err != nil {
257
+ klog .Errorf ("failed to access the networkattachmentdefinition %s/%s: %v" , netToAdd .Namespace , netToAdd .Name , err )
258
+ return err
259
+ }
260
+ response , err := pnc .multusClient .InvokeDelegate (
261
+ multusapi .CreateDelegateRequest (
262
+ multuscni .CmdAdd ,
263
+ podContainerID (pod ),
264
+ dynamicAttachmentRequest .PodNetNS ,
265
+ netToAdd .InterfaceRequest ,
266
+ pod .GetNamespace (),
267
+ pod .GetName (),
268
+ string (pod .UID ),
269
+ []byte (netAttachDef .Spec .Config ),
270
+ ))
271
+
272
+ if err != nil {
273
+ return fmt .Errorf ("failed to ADD delegate: %v" , err )
274
+ }
275
+ klog .Infof ("response: %v" , * response .Result )
276
+
277
+ pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , addIfaceEventFormat (pod , netToAdd ))
253
278
}
254
279
255
280
return nil
256
281
}
257
282
258
- func (pnc * PodNetworksController ) removeNetworks (netsToRemove []* nadv1.NetworkSelectionElement , pod * corev1.Pod ) error {
259
- for i := range netsToRemove {
260
- klog .Infof ("network to remove: %v" , netsToRemove [i ])
261
- pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , "removed network: %s" , netsToRemove [i ].Name )
283
+ func (pnc * PodNetworksController ) removeNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
284
+ for i := range dynamicAttachmentRequest .AttachmentNames {
285
+ netToRemove := dynamicAttachmentRequest .AttachmentNames [i ]
286
+ klog .Infof ("network to remove: %v" , dynamicAttachmentRequest .AttachmentNames [i ])
287
+
288
+ netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netToRemove .Namespace ).Get (netToRemove .Name )
289
+ if err != nil {
290
+ klog .Errorf ("failed to access the network-attachment-definition %s/%s: %v" , netToRemove .Namespace , netToRemove .Name , err )
291
+ return err
292
+ }
293
+
294
+ response , err := pnc .multusClient .InvokeDelegate (
295
+ multusapi .CreateDelegateRequest (
296
+ multuscni .CmdDel ,
297
+ podContainerID (pod ),
298
+ dynamicAttachmentRequest .PodNetNS ,
299
+ netToRemove .InterfaceRequest ,
300
+ pod .GetNamespace (),
301
+ pod .GetName (),
302
+ string (pod .UID ),
303
+ []byte (netAttachDef .Spec .Config ),
304
+ ))
305
+ if err != nil {
306
+ return fmt .Errorf ("failed to remove delegate: %v" , err )
307
+ }
308
+ klog .Infof ("response: %v" , * response )
309
+
310
+ pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , removeIfaceEventFormat (pod , netToRemove ))
262
311
}
263
312
264
313
return nil
@@ -355,3 +404,21 @@ func podContainerID(pod *corev1.Pod) string {
355
404
}
356
405
return cidURI
357
406
}
407
+
408
+ func addIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
409
+ return fmt .Sprintf (
410
+ "pod [%s]: added interface %s to network: %s" ,
411
+ namespacedName (pod .GetNamespace (), pod .GetName ()),
412
+ network .InterfaceRequest ,
413
+ network .Name ,
414
+ )
415
+ }
416
+
417
+ func removeIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
418
+ return fmt .Sprintf (
419
+ "pod [%s]: removed interface %s from network: %s" ,
420
+ namespacedName (pod .GetNamespace (), pod .GetName ()),
421
+ network .InterfaceRequest ,
422
+ network .Name ,
423
+ )
424
+ }
0 commit comments