Skip to content

Commit ec83d47

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

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

pkg/annotations/dynamic-network-status.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,49 @@ import (
88

99
nettypes "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
1010
nadutils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
11-
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
1211
)
1312

14-
func AddDynamicIfaceToStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement, response *multusapi.Response) ([]nettypes.NetworkStatus, error) {
13+
func AddDynamicIfaceToStatus(currentPod *corev1.Pod, attachmentResults ...AttachmentResult) ([]nettypes.NetworkStatus, error) {
1514
currentIfaceStatus, err := podDynamicNetworkStatus(currentPod)
1615
if err != nil {
1716
return nil, err
1817
}
1918

20-
if response != nil && response.Result != nil {
21-
newIfaceStatus, err := nadutils.CreateNetworkStatus(
22-
response.Result,
23-
NamespacedName(networkSelectionElement.Namespace, networkSelectionElement.Name),
24-
false,
25-
nil,
26-
)
27-
if err != nil {
28-
return nil, fmt.Errorf("failed to create NetworkStatus from the response: %v", err)
19+
for _, attachmentResult := range attachmentResults {
20+
response := attachmentResult.result
21+
networkSelectionElement := attachmentResult.attachment
22+
if response != nil && response.Result != nil {
23+
newIfaceStatus, err := nadutils.CreateNetworkStatus(
24+
response.Result,
25+
NamespacedName(networkSelectionElement.Namespace, networkSelectionElement.Name),
26+
false,
27+
nil,
28+
)
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to create NetworkStatus from the response: %v", err)
31+
}
32+
currentIfaceStatus = append(currentIfaceStatus, *newIfaceStatus)
2933
}
30-
31-
return append(currentIfaceStatus, *newIfaceStatus), nil
3234
}
33-
return nil, fmt.Errorf("got an empty response from multus: %+v", response)
35+
return currentIfaceStatus, nil
3436
}
3537

36-
func DeleteDynamicIfaceFromStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement) ([]nettypes.NetworkStatus, error) {
38+
func DeleteDynamicIfaceFromStatus(currentPod *corev1.Pod, networkSelectionElements ...*nettypes.NetworkSelectionElement) ([]nettypes.NetworkStatus, error) {
3739
currentIfaceStatus, err := podDynamicNetworkStatus(currentPod)
3840
if err != nil {
3941
return nil, err
4042
}
4143

42-
netName := NamespacedName(networkSelectionElement.Namespace, networkSelectionElement.Name)
4344
var newIfaceStatus []nettypes.NetworkStatus
44-
newIfaceStatus = make([]nettypes.NetworkStatus, 0)
45-
for i := range currentIfaceStatus {
46-
if currentIfaceStatus[i].Name == netName && currentIfaceStatus[i].Interface == networkSelectionElement.InterfaceRequest {
47-
continue
45+
for _, networkSelectionElement := range networkSelectionElements {
46+
netName := NamespacedName(networkSelectionElement.Namespace, networkSelectionElement.Name)
47+
newIfaceStatus = make([]nettypes.NetworkStatus, 0)
48+
for i := range currentIfaceStatus {
49+
if currentIfaceStatus[i].Name == netName && currentIfaceStatus[i].Interface == networkSelectionElement.InterfaceRequest {
50+
continue
51+
}
52+
newIfaceStatus = append(newIfaceStatus, currentIfaceStatus[i])
4853
}
49-
newIfaceStatus = append(newIfaceStatus, currentIfaceStatus[i])
5054
}
5155
return newIfaceStatus, nil
5256
}

pkg/annotations/dynamic-network-status_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ var _ = Describe("NetworkStatusFromResponse", func() {
3030
Expect(
3131
AddDynamicIfaceToStatus(
3232
newPod(podName, namespace, initialNetStatus...),
33-
newNetworkSelectionElementWithIface(networkName, ifaceName, namespace),
34-
newResponse(ifaceToAdd, macAddr, resultIPs...),
33+
*NewAttachmentResult(
34+
newNetworkSelectionElementWithIface(networkName, ifaceName, namespace),
35+
newResponse(ifaceToAdd, macAddr, resultIPs...),
36+
),
3537
),
3638
).To(Equal(expectedNetworkStatus))
3739
},

pkg/annotations/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package annotations
2+
3+
import (
4+
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
5+
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
6+
)
7+
8+
type AttachmentResult struct {
9+
attachment *nadv1.NetworkSelectionElement
10+
result *multusapi.Response
11+
}
12+
13+
func NewAttachmentResult(attachment *nadv1.NetworkSelectionElement, result *multusapi.Response) *AttachmentResult {
14+
return &AttachmentResult{
15+
attachment: attachment,
16+
result: result,
17+
}
18+
}

pkg/controller/pod.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
275275
}
276276
klog.Infof("response: %v", *response.Result)
277277

278-
newIfaceStatus, err := annotations.AddDynamicIfaceToStatus(pod, netToAdd, response)
278+
newIfaceStatus, err := annotations.AddDynamicIfaceToStatus(
279+
pod,
280+
*annotations.NewAttachmentResult(netToAdd, response),
281+
)
279282
if err != nil {
280283
return fmt.Errorf("failed to compute the updated network status: %v", err)
281284
}

0 commit comments

Comments
 (0)