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"
@@ -174,7 +176,7 @@ func (pnc *PodNetworksController) handleResult(err error, dynamicAttachmentReque
174176
175177 currentRetries := pnc .workqueue .NumRequeues (dynamicAttachmentRequest )
176178 if currentRetries <= maxRetries {
177- klog .Errorf ("re-queued request for: %v" , dynamicAttachmentRequest )
179+ klog .Errorf ("re-queued request for: %v. Error: %v " , dynamicAttachmentRequest , err )
178180 pnc .workqueue .AddRateLimited (dynamicAttachmentRequest )
179181 return
180182 }
@@ -196,7 +198,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
196198 }
197199 podNamespace := oldPod .GetNamespace ()
198200 podName := oldPod .GetName ()
199- klog .V (logging .Debug ).Infof ("pod [%s] updated" , namespacedName (podNamespace , podName ))
201+ klog .V (logging .Debug ).Infof ("pod [%s] updated" , annotations . NamespacedName (podNamespace , podName ))
200202
201203 oldNetworkSelectionElements , err := networkSelectionElements (oldPod .Annotations , podNamespace )
202204 if err != nil {
@@ -211,7 +213,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
211213 }
212214
213215 toAdd := exclusiveNetworks (newNetworkSelectionElements , oldNetworkSelectionElements )
214- klog .Infof ("%d attachments to add to pod %s" , len (toAdd ), namespacedName (podNamespace , podName ))
216+ klog .Infof ("%d attachments to add to pod %s" , len (toAdd ), annotations . NamespacedName (podNamespace , podName ))
215217
216218 netnsPath , err := pnc .netnsPath (newPod )
217219 if err != nil {
@@ -230,7 +232,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
230232 }
231233
232234 toRemove := exclusiveNetworks (oldNetworkSelectionElements , newNetworkSelectionElements )
233- klog .Infof ("%d attachments to remove from pod %s" , len (toRemove ), namespacedName (podNamespace , podName ))
235+ klog .Infof ("%d attachments to remove from pod %s" , len (toRemove ), annotations . NamespacedName (podNamespace , podName ))
234236 if len (toRemove ) > 0 {
235237 pnc .workqueue .Add (
236238 & DynamicAttachmentRequest {
@@ -243,10 +245,6 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
243245 }
244246}
245247
246- func namespacedName (podNamespace string , podName string ) string {
247- return fmt .Sprintf ("%s/%s" , podNamespace , podName )
248- }
249-
250248func (pnc * PodNetworksController ) addNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
251249 for i := range dynamicAttachmentRequest .AttachmentNames {
252250 netToAdd := dynamicAttachmentRequest .AttachmentNames [i ]
@@ -274,6 +272,15 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
274272 }
275273 klog .Infof ("response: %v" , * response .Result )
276274
275+ newIfaceStatus , err := annotations .AddDynamicIfaceToStatus (pod , netToAdd , response )
276+ if err != nil {
277+ return fmt .Errorf ("failed to compute the updated network status: %v" , err )
278+ }
279+
280+ if err := pnc .updatePodNetworkStatus (pod , newIfaceStatus ); err != nil {
281+ return err
282+ }
283+
277284 pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , addIfaceEventFormat (pod , netToAdd ))
278285 }
279286
@@ -307,12 +314,34 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
307314 }
308315 klog .Infof ("response: %v" , * response )
309316
317+ newIfaceStatus , err := annotations .DeleteDynamicIfaceFromStatus (pod , netToRemove )
318+ if err != nil {
319+ return fmt .Errorf (
320+ "failed to compute the dynamic network attachments after deleting network: %s, iface: %s: %v" ,
321+ netToRemove .Name ,
322+ netToRemove .InterfaceRequest ,
323+ err ,
324+ )
325+ }
326+ if err := pnc .updatePodNetworkStatus (pod , newIfaceStatus ); err != nil {
327+ return err
328+ }
329+
310330 pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , removeIfaceEventFormat (pod , netToRemove ))
311331 }
312332
313333 return nil
314334}
315335
336+ func (pnc * PodNetworksController ) updatePodNetworkStatus (pod * corev1.Pod , newIfaceStatus string ) error {
337+ pod .Annotations [nadv1 .NetworkStatusAnnot ] = newIfaceStatus
338+
339+ if _ , err := pnc .k8sClientSet .CoreV1 ().Pods (pod .GetNamespace ()).Update (context .Background (), pod , metav1.UpdateOptions {}); err != nil {
340+ return fmt .Errorf ("failed to update pod's network-status annotations for %s: %v" , pod .GetName (), err )
341+ }
342+ return nil
343+ }
344+
316345func networkSelectionElements (podAnnotations map [string ]string , podNamespace string ) ([]* nadv1.NetworkSelectionElement , error ) {
317346 podNetworks , ok := podAnnotations [nadv1 .NetworkAttachmentAnnot ]
318347 if ! ok {
@@ -408,7 +437,7 @@ func podContainerID(pod *corev1.Pod) string {
408437func addIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
409438 return fmt .Sprintf (
410439 "pod [%s]: added interface %s to network: %s" ,
411- namespacedName (pod .GetNamespace (), pod .GetName ()),
440+ annotations . NamespacedName (pod .GetNamespace (), pod .GetName ()),
412441 network .InterfaceRequest ,
413442 network .Name ,
414443 )
@@ -417,7 +446,7 @@ func addIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement
417446func removeIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
418447 return fmt .Sprintf (
419448 "pod [%s]: removed interface %s from network: %s" ,
420- namespacedName (pod .GetNamespace (), pod .GetName ()),
449+ annotations . NamespacedName (pod .GetNamespace (), pod .GetName ()),
421450 network .InterfaceRequest ,
422451 network .Name ,
423452 )
0 commit comments