11package controller
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "reflect"
78 "strings"
89 "time"
910
1011 corev1 "k8s.io/api/core/v1"
12+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113 "k8s.io/apimachinery/pkg/runtime"
1214 "k8s.io/apimachinery/pkg/util/wait"
1315 v1coreinformerfactory "k8s.io/client-go/informers"
@@ -26,6 +28,7 @@ import (
2628 "github.com/maiqueb/multus-dynamic-networks-controller/pkg/annotations"
2729 "github.com/maiqueb/multus-dynamic-networks-controller/pkg/cri"
2830 "github.com/maiqueb/multus-dynamic-networks-controller/pkg/logging"
31+ "github.com/maiqueb/multus-dynamic-networks-controller/pkg/multuscni"
2932)
3033
3134const (
@@ -150,13 +153,13 @@ func (pnc *PodNetworksController) handleDynamicInterfaceRequest(dynamicAttachmen
150153 if err != nil {
151154 return err
152155 }
153- return pnc .addNetworks (dynamicAttachmentRequest .AttachmentNames , pod )
156+ return pnc .addNetworks (dynamicAttachmentRequest .AttachmentNames , pod , dynamicAttachmentRequest . PodNetNS )
154157 } else if dynamicAttachmentRequest .Type == "remove" {
155158 pod , err := pnc .podsLister .Pods (dynamicAttachmentRequest .PodNamespace ).Get (dynamicAttachmentRequest .PodName )
156159 if err != nil {
157160 return err
158161 }
159- return pnc .removeNetworks (dynamicAttachmentRequest .AttachmentNames , pod )
162+ return pnc .removeNetworks (dynamicAttachmentRequest .AttachmentNames , pod , dynamicAttachmentRequest . PodNetNS )
160163 } else {
161164 klog .Infof ("very weird attachment request: %+v" , dynamicAttachmentRequest )
162165 }
@@ -169,7 +172,7 @@ func (pnc *PodNetworksController) handleResult(err error, dynamicAttachmentReque
169172 pnc .workqueue .Forget (dynamicAttachmentRequest )
170173 return
171174 }
172-
175+ klog . Warningf ( "attachment failed for %v: %v" , * dynamicAttachmentRequest , err )
173176 currentRetries := pnc .workqueue .NumRequeues (dynamicAttachmentRequest )
174177 if currentRetries <= maxRetries {
175178 klog .Errorf ("re-queued request for: %v" , dynamicAttachmentRequest )
@@ -245,19 +248,73 @@ func namespacedName(podNamespace string, podName string) string {
245248 return fmt .Sprintf ("%s/%s" , podNamespace , podName )
246249}
247250
248- func (pnc * PodNetworksController ) addNetworks (netsToAdd []* nadv1.NetworkSelectionElement , pod * corev1.Pod ) error {
251+ func (pnc * PodNetworksController ) addNetworks (netsToAdd []* nadv1.NetworkSelectionElement , pod * corev1.Pod , netnsPath string ) error {
249252 for i := range netsToAdd {
250253 klog .Infof ("network to add: %v" , netsToAdd [i ])
254+
255+ netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netsToAdd [i ].Namespace ).Get (netsToAdd [i ].Name )
256+ if err != nil {
257+ klog .Errorf ("failed to access the network-attachment-definition %s/%s: %v" , netsToAdd [i ].Namespace , netsToAdd [i ].Name , err )
258+ return err
259+ }
260+ response , err := multuscni .DoCNI (
261+ multuscni .MultusDelegateURL (),
262+ delegateRequest ("ADD" , netsToAdd [i ], netAttachDef , pod , netnsPath ),
263+ pnc .multusSocketPath ,
264+ )
265+ if err != nil {
266+ return fmt .Errorf ("failed to ADD delegate: %v" , err )
267+ }
268+ klog .Infof ("response: %s" , string (response ))
269+
270+ newIfaceStatus , err := annotations .AddDynamicIfaceStatus (pod , netsToAdd [0 ].Name , netsToAdd [0 ].InterfaceRequest )
271+ if err != nil {
272+ return fmt .Errorf ("failed to create the new dynamic network status annotation: %v" , err )
273+ }
274+ pod .Annotations [annotations .DynamicNetworksAnnotation ] = newIfaceStatus
275+
276+ _ , err = pnc .k8sClientSet .CoreV1 ().Pods (pod .GetNamespace ()).Update (context .Background (), pod , metav1.UpdateOptions {})
277+ if err != nil {
278+ return fmt .Errorf ("failed to add pod's dynamic annotations for %s: %v" , pod .GetName (), err )
279+ }
280+
251281 pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , "add network: %s" , netsToAdd [i ].Name )
252282 }
253283
254284 return nil
255285}
256286
257- func (pnc * PodNetworksController ) removeNetworks (netsToRemove []* nadv1.NetworkSelectionElement , pod * corev1.Pod ) error {
287+ func (pnc * PodNetworksController ) removeNetworks (netsToRemove []* nadv1.NetworkSelectionElement , pod * corev1.Pod , netnsPath string ) error {
258288 for i := range netsToRemove {
259289 klog .Infof ("network to remove: %v" , netsToRemove [i ])
290+
291+ netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netsToRemove [i ].Namespace ).Get (netsToRemove [i ].Name )
292+ if err != nil {
293+ klog .Errorf ("failed to access the network-attachment-definition %s/%s: %v" , netsToRemove [i ].Namespace , netsToRemove [i ].Name , err )
294+ return err
295+ }
296+
297+ response , err := multuscni .DoCNI (
298+ multuscni .MultusDelegateURL (),
299+ delegateRequest ("DEL" , netsToRemove [i ], netAttachDef , pod , netnsPath ),
300+ pnc .multusSocketPath ,
301+ )
302+ if err != nil {
303+ return fmt .Errorf ("failed to remove delegate: %v" , err )
304+ }
305+
306+ newPodIfaceStatus , err := annotations .DeleteDynamicIfaceStatus (pod , netsToRemove [0 ].Name , netsToRemove [0 ].InterfaceRequest )
307+ if err != nil {
308+ return fmt .Errorf ("failed to compute the dynamic network attachments after deleting network: %s, iface: %s: %v" , netsToRemove [0 ].Name , netsToRemove [0 ].InterfaceRequest , err )
309+ }
310+ pod .Annotations [annotations .DynamicNetworksAnnotation ] = newPodIfaceStatus
311+
312+ _ , err = pnc .k8sClientSet .CoreV1 ().Pods (pod .GetNamespace ()).Update (context .Background (), pod , metav1.UpdateOptions {})
313+ if err != nil {
314+ return fmt .Errorf ("failed to add pod's dynamic annotations for %s: %v" , pod .GetName (), err )
315+ }
260316 pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , "removed network: %s" , netsToRemove [i ].Name )
317+ klog .Infof ("response: %s" , string (response ))
261318 }
262319
263320 return nil
@@ -354,3 +411,19 @@ func podContainerID(pod *corev1.Pod) string {
354411 }
355412 return cidURI
356413}
414+
415+ func delegateRequest (command string , network * nadv1.NetworkSelectionElement , netAttachDef * nadv1.NetworkAttachmentDefinition , pod * corev1.Pod , netns string ) interface {} {
416+ klog .V (logging .Debug ).Infof ("the net-attach-def config: %v" , netAttachDef .Spec .Config )
417+ addRequest := & multuscni.Request {
418+ Env : map [string ]string {
419+ "CNI_COMMAND" : command ,
420+ "CNI_CONTAINERID" : podContainerID (pod ),
421+ "CNI_NETNS" : netns ,
422+ "CNI_IFNAME" : network .InterfaceRequest ,
423+ "CNI_ARGS" : fmt .Sprintf ("K8S_POD_NAMESPACE=%s;K8S_POD_NAME=%s;K8S_POD_UID=%s" , pod .GetNamespace (), pod .GetName (), string (pod .UID )),
424+ },
425+ Config : []byte (netAttachDef .Spec .Config ),
426+ }
427+
428+ return addRequest
429+ }
0 commit comments