Skip to content

Commit 90b2f03

Browse files
committed
controller: update network status once per event
Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent ec83d47 commit 90b2f03

File tree

1 file changed

+65
-61
lines changed

1 file changed

+65
-61
lines changed

pkg/controller/pod.go

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ import (
3131
)
3232

3333
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"
3638
)
3739

3840
type DynamicAttachmentRequestType string
3941

4042
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
4647
}
4748

4849
func (dar *DynamicAttachmentRequest) String() string {
@@ -147,18 +148,10 @@ func (pnc *PodNetworksController) processNextWorkItem() bool {
147148

148149
func (pnc *PodNetworksController) handleDynamicInterfaceRequest(dynamicAttachmentRequest *DynamicAttachmentRequest) error {
149150
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)
162155
} else {
163156
klog.Infof("very weird attachment request: %+v", dynamicAttachmentRequest)
164157
}
@@ -221,11 +214,10 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
221214
if len(toAdd) > 0 {
222215
pnc.workqueue.Add(
223216
&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,
229221
})
230222
}
231223

@@ -234,18 +226,20 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
234226
if len(toRemove) > 0 {
235227
pnc.workqueue.Add(
236228
&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,
242233
})
243234
}
244235
}
245236

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]
249243
klog.Infof("network to add: %v", netToAdd)
250244

251245
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToAdd.Namespace).Get(netToAdd.Name)
@@ -275,28 +269,34 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
275269
}
276270
klog.Infof("response: %v", *response.Result)
277271

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()),
281278
)
282-
if err != nil {
283-
return fmt.Errorf("failed to compute the updated network status: %v", err)
284-
}
279+
}
285280

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+
}
289285

290-
pnc.Eventf(pod, corev1.EventTypeNormal, "AddedInterface", addIfaceEventFormat(pod, netToAdd))
286+
if err := nadutils.SetNetworkStatus(pnc.k8sClientSet, pod, newIfaceStatus); err != nil {
287+
return err
291288
}
292289

293290
return nil
294291
}
295292

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])
300300

301301
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToRemove.Namespace).Get(netToRemove.Name)
302302
if err != nil {
@@ -308,7 +308,7 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
308308
if err != nil {
309309
return err
310310
}
311-
response, err := pnc.multusClient.InvokeDelegate(
311+
_, err = pnc.multusClient.InvokeDelegate(
312312
multusapi.CreateDelegateRequest(
313313
multuscni.CmdDel,
314314
podContainerID(pod),
@@ -323,22 +323,26 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
323323
if err != nil {
324324
return fmt.Errorf("failed to remove delegate: %v", err)
325325
}
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-
}
340326

327+
removedNets = append(removedNets, netToRemove)
341328
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
342346
}
343347

344348
return nil

0 commit comments

Comments
 (0)