@@ -31,18 +31,19 @@ import (
31
31
)
32
32
33
33
const (
34
- AdvertisedName = "pod-networks-updates"
35
- maxRetries = 2
34
+ AdvertisedName = "pod-networks-updates"
35
+ maxRetries = 2
36
+ add DynamicAttachmentRequestType = "add"
37
+ remove DynamicAttachmentRequestType = "remove"
36
38
)
37
39
38
40
type DynamicAttachmentRequestType string
39
41
40
42
type DynamicAttachmentRequest struct {
41
- PodName string
42
- PodNamespace string
43
- AttachmentNames []* nadv1.NetworkSelectionElement
44
- Type DynamicAttachmentRequestType
45
- PodNetNS string
43
+ Pod * corev1.Pod
44
+ Attachments []* nadv1.NetworkSelectionElement
45
+ Type DynamicAttachmentRequestType
46
+ PodNetNS string
46
47
}
47
48
48
49
func (dar * DynamicAttachmentRequest ) String () string {
@@ -147,18 +148,10 @@ func (pnc *PodNetworksController) processNextWorkItem() bool {
147
148
148
149
func (pnc * PodNetworksController ) handleDynamicInterfaceRequest (dynamicAttachmentRequest * DynamicAttachmentRequest ) error {
149
150
klog .Infof ("handleDynamicInterfaceRequest: read from queue: %v" , dynamicAttachmentRequest )
150
- if dynamicAttachmentRequest .Type == "add" {
151
- pod , err := pnc .podsLister .Pods (dynamicAttachmentRequest .PodNamespace ).Get (dynamicAttachmentRequest .PodName )
152
- if err != nil {
153
- return err
154
- }
155
- return pnc .addNetworks (dynamicAttachmentRequest , pod )
156
- } else if dynamicAttachmentRequest .Type == "remove" {
157
- pod , err := pnc .podsLister .Pods (dynamicAttachmentRequest .PodNamespace ).Get (dynamicAttachmentRequest .PodName )
158
- if err != nil {
159
- return err
160
- }
161
- return pnc .removeNetworks (dynamicAttachmentRequest , pod )
151
+ if dynamicAttachmentRequest .Type == add {
152
+ return pnc .addNetworks (dynamicAttachmentRequest )
153
+ } else if dynamicAttachmentRequest .Type == remove {
154
+ return pnc .removeNetworks (dynamicAttachmentRequest )
162
155
} else {
163
156
klog .Infof ("very weird attachment request: %+v" , dynamicAttachmentRequest )
164
157
}
@@ -221,11 +214,10 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
221
214
if len (toAdd ) > 0 {
222
215
pnc .workqueue .Add (
223
216
& DynamicAttachmentRequest {
224
- PodName : podName ,
225
- PodNamespace : podNamespace ,
226
- AttachmentNames : toAdd ,
227
- Type : add ,
228
- PodNetNS : netnsPath ,
217
+ Pod : newPod ,
218
+ Attachments : toAdd ,
219
+ Type : add ,
220
+ PodNetNS : netnsPath ,
229
221
})
230
222
}
231
223
@@ -234,18 +226,20 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
234
226
if len (toRemove ) > 0 {
235
227
pnc .workqueue .Add (
236
228
& DynamicAttachmentRequest {
237
- PodName : podName ,
238
- PodNamespace : podNamespace ,
239
- AttachmentNames : toRemove ,
240
- Type : remove ,
241
- PodNetNS : netnsPath ,
229
+ Pod : newPod ,
230
+ Attachments : toRemove ,
231
+ Type : remove ,
232
+ PodNetNS : netnsPath ,
242
233
})
243
234
}
244
235
}
245
236
246
- func (pnc * PodNetworksController ) addNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
247
- for i := range dynamicAttachmentRequest .AttachmentNames {
248
- netToAdd := dynamicAttachmentRequest .AttachmentNames [i ]
237
+ func (pnc * PodNetworksController ) addNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest ) error {
238
+ pod := dynamicAttachmentRequest .Pod
239
+
240
+ var attachmentResults []annotations.AttachmentResult
241
+ for i := range dynamicAttachmentRequest .Attachments {
242
+ netToAdd := dynamicAttachmentRequest .Attachments [i ]
249
243
klog .Infof ("network to add: %v" , netToAdd )
250
244
251
245
netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netToAdd .Namespace ).Get (netToAdd .Name )
@@ -275,28 +269,34 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
275
269
}
276
270
klog .Infof ("response: %v" , * response .Result )
277
271
278
- newIfaceStatus , err := annotations .AddDynamicIfaceToStatus (
279
- pod ,
280
- * annotations .NewAttachmentResult (netToAdd , response ),
272
+ attachmentResults = append (attachmentResults , * annotations .NewAttachmentResult (netToAdd , response ))
273
+ pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , addIfaceEventFormat (pod , netToAdd ))
274
+ klog .Infof (
275
+ "added interface %s to pod %s" ,
276
+ annotations .NamespacedName (netToAdd .Namespace , netToAdd .Name ),
277
+ annotations .NamespacedName (pod .GetNamespace (), pod .GetName ()),
281
278
)
282
- if err != nil {
283
- return fmt .Errorf ("failed to compute the updated network status: %v" , err )
284
- }
279
+ }
285
280
286
- if err := nadutils .SetNetworkStatus (pnc .k8sClientSet , pod , newIfaceStatus ); err != nil {
287
- return err
288
- }
281
+ newIfaceStatus , err := annotations .AddDynamicIfaceToStatus (pod , attachmentResults ... )
282
+ if err != nil {
283
+ return fmt .Errorf ("failed to compute the updated network status: %v" , err )
284
+ }
289
285
290
- pnc .Eventf (pod , corev1 .EventTypeNormal , "AddedInterface" , addIfaceEventFormat (pod , netToAdd ))
286
+ if err := nadutils .SetNetworkStatus (pnc .k8sClientSet , pod , newIfaceStatus ); err != nil {
287
+ return err
291
288
}
292
289
293
290
return nil
294
291
}
295
292
296
- func (pnc * PodNetworksController ) removeNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest , pod * corev1.Pod ) error {
297
- for i := range dynamicAttachmentRequest .AttachmentNames {
298
- netToRemove := dynamicAttachmentRequest .AttachmentNames [i ]
299
- klog .Infof ("network to remove: %v" , dynamicAttachmentRequest .AttachmentNames [i ])
293
+ func (pnc * PodNetworksController ) removeNetworks (dynamicAttachmentRequest * DynamicAttachmentRequest ) error {
294
+ pod := dynamicAttachmentRequest .Pod
295
+
296
+ var removedNets []* nadv1.NetworkSelectionElement
297
+ for i := range dynamicAttachmentRequest .Attachments {
298
+ netToRemove := dynamicAttachmentRequest .Attachments [i ]
299
+ klog .Infof ("network to remove: %v" , dynamicAttachmentRequest .Attachments [i ])
300
300
301
301
netAttachDef , err := pnc .netAttachDefLister .NetworkAttachmentDefinitions (netToRemove .Namespace ).Get (netToRemove .Name )
302
302
if err != nil {
@@ -308,7 +308,7 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
308
308
if err != nil {
309
309
return err
310
310
}
311
- response , err : = pnc .multusClient .InvokeDelegate (
311
+ _ , err = pnc .multusClient .InvokeDelegate (
312
312
multusapi .CreateDelegateRequest (
313
313
multuscni .CmdDel ,
314
314
podContainerID (pod ),
@@ -323,22 +323,26 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
323
323
if err != nil {
324
324
return fmt .Errorf ("failed to remove delegate: %v" , err )
325
325
}
326
- klog .Infof ("response: %v" , * response )
327
-
328
- newIfaceStatus , err := annotations .DeleteDynamicIfaceFromStatus (pod , netToRemove )
329
- if err != nil {
330
- return fmt .Errorf (
331
- "failed to compute the dynamic network attachments after deleting network: %s, iface: %s: %v" ,
332
- netToRemove .Name ,
333
- netToRemove .InterfaceRequest ,
334
- err ,
335
- )
336
- }
337
- if err := nadutils .SetNetworkStatus (pnc .k8sClientSet , pod , newIfaceStatus ); err != nil {
338
- return err
339
- }
340
326
327
+ removedNets = append (removedNets , netToRemove )
341
328
pnc .Eventf (pod , corev1 .EventTypeNormal , "RemovedInterface" , removeIfaceEventFormat (pod , netToRemove ))
329
+ klog .Infof (
330
+ "removed interface %s from pod %s" ,
331
+ annotations .NamespacedName (netToRemove .Namespace , netToRemove .Name ),
332
+ annotations .NamespacedName (pod .GetNamespace (), pod .GetName ()),
333
+ )
334
+ }
335
+
336
+ newIfaceStatus , err := annotations .DeleteDynamicIfaceFromStatus (pod , removedNets ... )
337
+ if err != nil {
338
+ return fmt .Errorf (
339
+ "failed to compute the dynamic network attachments after unplugging interface for pod %s: %v" ,
340
+ annotations .NamespacedName (pod .GetNamespace (), pod .GetName ()),
341
+ err ,
342
+ )
343
+ }
344
+ if err := nadutils .SetNetworkStatus (pnc .k8sClientSet , pod , newIfaceStatus ); err != nil {
345
+ return err
342
346
}
343
347
344
348
return nil
0 commit comments