1
1
package controller
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"reflect"
7
8
"strings"
8
9
"time"
9
10
10
11
corev1 "k8s.io/api/core/v1"
12
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
13
"k8s.io/apimachinery/pkg/runtime"
12
14
"k8s.io/apimachinery/pkg/util/wait"
13
15
v1coreinformerfactory "k8s.io/client-go/informers"
@@ -174,7 +176,7 @@ func (pnc *PodNetworksController) handleResult(err error, dynamicAttachmentReque
174
176
175
177
currentRetries := pnc .workqueue .NumRequeues (dynamicAttachmentRequest )
176
178
if currentRetries <= maxRetries {
177
- klog .Errorf ("re-queued request for: %v" , dynamicAttachmentRequest )
179
+ klog .Errorf ("re-queued request for: %v. Error: %v " , dynamicAttachmentRequest , err )
178
180
pnc .workqueue .AddRateLimited (dynamicAttachmentRequest )
179
181
return
180
182
}
@@ -196,7 +198,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
196
198
}
197
199
podNamespace := oldPod .GetNamespace ()
198
200
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 ))
200
202
201
203
oldNetworkSelectionElements , err := networkSelectionElements (oldPod .Annotations , podNamespace )
202
204
if err != nil {
@@ -211,7 +213,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
211
213
}
212
214
213
215
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 ))
215
217
216
218
netnsPath , err := pnc .netnsPath (newPod )
217
219
if err != nil {
@@ -230,7 +232,7 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
230
232
}
231
233
232
234
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 ))
234
236
if len (toRemove ) > 0 {
235
237
pnc .workqueue .Add (
236
238
& DynamicAttachmentRequest {
@@ -243,10 +245,6 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
243
245
}
244
246
}
245
247
246
- func namespacedName (podNamespace string , podName string ) string {
247
- return fmt .Sprintf ("%s/%s" , podNamespace , podName )
248
- }
249
-
250
248
func (pnc * PodNetworksController ) addNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
251
249
for i := range dynamicAttachmentRequest .AttachmentNames {
252
250
netToAdd := dynamicAttachmentRequest .AttachmentNames [i ]
@@ -274,6 +272,15 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
274
272
}
275
273
klog .Infof ("response: %v" , * response .Result )
276
274
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
+
277
284
pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , addIfaceEventFormat (pod , netToAdd ))
278
285
}
279
286
@@ -307,12 +314,34 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
307
314
}
308
315
klog .Infof ("response: %v" , * response )
309
316
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
+
310
330
pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , removeIfaceEventFormat (pod , netToRemove ))
311
331
}
312
332
313
333
return nil
314
334
}
315
335
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
+
316
345
func networkSelectionElements (podAnnotations map [string ]string , podNamespace string ) ([]* nadv1.NetworkSelectionElement , error ) {
317
346
podNetworks , ok := podAnnotations [nadv1 .NetworkAttachmentAnnot ]
318
347
if ! ok {
@@ -408,7 +437,7 @@ func podContainerID(pod *corev1.Pod) string {
408
437
func addIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
409
438
return fmt .Sprintf (
410
439
"pod [%s]: added interface %s to network: %s" ,
411
- namespacedName (pod .GetNamespace (), pod .GetName ()),
440
+ annotations . NamespacedName (pod .GetNamespace (), pod .GetName ()),
412
441
network .InterfaceRequest ,
413
442
network .Name ,
414
443
)
@@ -417,7 +446,7 @@ func addIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement
417
446
func removeIfaceEventFormat (pod * corev1.Pod , network * nadv1.NetworkSelectionElement ) string {
418
447
return fmt .Sprintf (
419
448
"pod [%s]: removed interface %s from network: %s" ,
420
- namespacedName (pod .GetNamespace (), pod .GetName ()),
449
+ annotations . NamespacedName (pod .GetNamespace (), pod .GetName ()),
421
450
network .InterfaceRequest ,
422
451
network .Name ,
423
452
)
0 commit comments