Skip to content

Commit 0fb0e8a

Browse files
committed
controller, multus: invoke the delegates via multus server
Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent 7cf661e commit 0fb0e8a

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

pkg/controller/pod.go

+77-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
2323
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
2424
nadlisterv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/listers/k8s.cni.cncf.io/v1"
25+
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
2526

2627
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/annotations"
2728
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/cri"
@@ -151,13 +152,13 @@ func (pnc *PodNetworksController) handleDynamicInterfaceRequest(dynamicAttachmen
151152
if err != nil {
152153
return err
153154
}
154-
return pnc.addNetworks(dynamicAttachmentRequest.AttachmentNames, pod)
155+
return pnc.addNetworks(dynamicAttachmentRequest, pod)
155156
} else if dynamicAttachmentRequest.Type == "remove" {
156157
pod, err := pnc.podsLister.Pods(dynamicAttachmentRequest.PodNamespace).Get(dynamicAttachmentRequest.PodName)
157158
if err != nil {
158159
return err
159160
}
160-
return pnc.removeNetworks(dynamicAttachmentRequest.AttachmentNames, pod)
161+
return pnc.removeNetworks(dynamicAttachmentRequest, pod)
161162
} else {
162163
klog.Infof("very weird attachment request: %+v", dynamicAttachmentRequest)
163164
}
@@ -246,19 +247,67 @@ func namespacedName(podNamespace string, podName string) string {
246247
return fmt.Sprintf("%s/%s", podNamespace, podName)
247248
}
248249

249-
func (pnc *PodNetworksController) addNetworks(netsToAdd []*nadv1.NetworkSelectionElement, pod *corev1.Pod) error {
250-
for i := range netsToAdd {
251-
klog.Infof("network to add: %v", netsToAdd[i])
252-
pnc.Eventf(pod, corev1.EventTypeNormal, "AddedInterface", "add network: %s", netsToAdd[i].Name)
250+
func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicAttachmentRequest, pod *corev1.Pod) error {
251+
for i := range dynamicAttachmentRequest.AttachmentNames {
252+
netToAdd := dynamicAttachmentRequest.AttachmentNames[i]
253+
klog.Infof("network to add: %v", netToAdd)
254+
255+
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToAdd.Namespace).Get(netToAdd.Name)
256+
if err != nil {
257+
klog.Errorf("failed to access the networkattachmentdefinition %s/%s: %v", netToAdd.Namespace, netToAdd.Name, err)
258+
return err
259+
}
260+
response, err := pnc.multusClient.InvokeDelegate(
261+
multusapi.CreateDelegateRequest(
262+
multuscni.CmdAdd,
263+
podContainerID(pod),
264+
dynamicAttachmentRequest.PodNetNS,
265+
netToAdd.InterfaceRequest,
266+
pod.GetNamespace(),
267+
pod.GetName(),
268+
string(pod.UID),
269+
[]byte(netAttachDef.Spec.Config),
270+
))
271+
272+
if err != nil {
273+
return fmt.Errorf("failed to ADD delegate: %v", err)
274+
}
275+
klog.Infof("response: %v", *response.Result)
276+
277+
pnc.Eventf(pod, corev1.EventTypeNormal, "AddedInterface", addIfaceEventFormat(pod, netToAdd))
253278
}
254279

255280
return nil
256281
}
257282

258-
func (pnc *PodNetworksController) removeNetworks(netsToRemove []*nadv1.NetworkSelectionElement, pod *corev1.Pod) error {
259-
for i := range netsToRemove {
260-
klog.Infof("network to remove: %v", netsToRemove[i])
261-
pnc.Eventf(pod, corev1.EventTypeNormal, "RemovedInterface", "removed network: %s", netsToRemove[i].Name)
283+
func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *DynamicAttachmentRequest, pod *corev1.Pod) error {
284+
for i := range dynamicAttachmentRequest.AttachmentNames {
285+
netToRemove := dynamicAttachmentRequest.AttachmentNames[i]
286+
klog.Infof("network to remove: %v", dynamicAttachmentRequest.AttachmentNames[i])
287+
288+
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToRemove.Namespace).Get(netToRemove.Name)
289+
if err != nil {
290+
klog.Errorf("failed to access the network-attachment-definition %s/%s: %v", netToRemove.Namespace, netToRemove.Name, err)
291+
return err
292+
}
293+
294+
response, err := pnc.multusClient.InvokeDelegate(
295+
multusapi.CreateDelegateRequest(
296+
multuscni.CmdDel,
297+
podContainerID(pod),
298+
dynamicAttachmentRequest.PodNetNS,
299+
netToRemove.InterfaceRequest,
300+
pod.GetNamespace(),
301+
pod.GetName(),
302+
string(pod.UID),
303+
[]byte(netAttachDef.Spec.Config),
304+
))
305+
if err != nil {
306+
return fmt.Errorf("failed to remove delegate: %v", err)
307+
}
308+
klog.Infof("response: %v", *response)
309+
310+
pnc.Eventf(pod, corev1.EventTypeNormal, "RemovedInterface", removeIfaceEventFormat(pod, netToRemove))
262311
}
263312

264313
return nil
@@ -355,3 +404,21 @@ func podContainerID(pod *corev1.Pod) string {
355404
}
356405
return cidURI
357406
}
407+
408+
func addIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement) string {
409+
return fmt.Sprintf(
410+
"pod [%s]: added interface %s to network: %s",
411+
namespacedName(pod.GetNamespace(), pod.GetName()),
412+
network.InterfaceRequest,
413+
network.Name,
414+
)
415+
}
416+
417+
func removeIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement) string {
418+
return fmt.Sprintf(
419+
"pod [%s]: removed interface %s from network: %s",
420+
namespacedName(pod.GetNamespace(), pod.GetName()),
421+
network.InterfaceRequest,
422+
network.Name,
423+
)
424+
}

pkg/controller/pod_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ var _ = Describe("Dynamic Attachment controller", func() {
141141
})
142142

143143
It("an `AddedInterface` event is seen in the event recorded ", func() {
144-
expectedEventPayload := fmt.Sprintf("Normal AddedInterface add network: %s", networkToAdd)
144+
expectedEventPayload := fmt.Sprintf(
145+
"Normal AddedInterface pod [%s]: added interface %s to network: %s",
146+
namespacedName(namespace, podName),
147+
"net1",
148+
networkToAdd,
149+
)
145150
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
146151
})
147152
})
@@ -157,7 +162,12 @@ var _ = Describe("Dynamic Attachment controller", func() {
157162
})
158163

159164
It("an `RemovedInterface` event is seen in the event recorded ", func() {
160-
expectedEventPayload := fmt.Sprintf("Normal RemovedInterface removed network: %s", networkName)
165+
expectedEventPayload := fmt.Sprintf(
166+
"Normal RemovedInterface pod [%s]: removed interface %s from network: %s",
167+
namespacedName(namespace, podName),
168+
"net0",
169+
networkName,
170+
)
161171
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
162172
})
163173
})

0 commit comments

Comments
 (0)