Skip to content

Commit 9041116

Browse files
committed
WIP: inject fake multusclient
Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent 3374798 commit 9041116

File tree

150 files changed

+19887
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+19887
-484
lines changed

.github/workflows/e2e-container.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
- name: Test - provisioning the examples
3232
run: e2e/test-provisioning-examples.sh
3333

34+
- name: Test - execute golang based e2e tests
35+
run: make e2e/test
36+
3437
- name: Cleanup cluster
3538
run: |
3639
kind delete cluster # gracefully remove the cluster

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ manifests:
2424
IMAGE_REGISTRY=${IMAGE_REGISTRY} CRI_SOCKET_PATH=${CRI_SOCKET_PATH} hack/generate_manifests.sh
2525

2626
test:
27-
$(GO) test -v ./...
27+
$(GO) test -v ./pkg/...
28+
29+
e2e/test:
30+
$(GO) test -v -count=1 ./e2e/...

cmd/dynamic-networks-controller/networks-controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/cri"
2626
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/cri/containerd"
2727
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/logging"
28+
"github.com/maiqueb/multus-dynamic-networks-controller/pkg/multuscni"
2829
)
2930

3031
const (
@@ -96,10 +97,10 @@ func newController(stopChannel chan struct{}, configuration *config.Multus) (*co
9697
nadInformerFactory,
9798
eventBroadcaster,
9899
newEventRecorder(eventBroadcaster),
99-
configuration.MultusSocketPath,
100100
k8sClient,
101101
nadClientSet,
102102
containerRuntime,
103+
multuscni.NewClient(configuration.MultusSocketPath),
103104
)
104105
if err != nil {
105106
return nil, fmt.Errorf("failed to create the pod networks controller: %v", err)

e2e/e2e_test.go

+72-30
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,80 @@ var _ = Describe("Multus dynamic networks controller", func() {
8989

9090
Expect(clients.addNetworkToPod(pod, networkName, namespace, ifaceToAdd)).To(Succeed())
9191

92-
Eventually(func() []nettypes.NetworkStatus {
93-
pods, err := clients.ListPods(namespace, fmt.Sprintf("app=%s", podName))
94-
if err != nil {
95-
return nil
96-
}
97-
if len(pods.Items) != 1 {
98-
return nil
99-
}
100-
return podsDynamicNetworks(&pods.Items[0])
101-
}).Should(
102-
ConsistOf(
103-
nettypes.NetworkStatus{
104-
Name: networkName,
105-
Interface: ifaceToAdd,
106-
Mac: "",
107-
}))
92+
Eventually(
93+
func() []nettypes.NetworkStatus {
94+
return filterPods(clients, namespace, podName, func(networkStatus nettypes.NetworkStatus) bool {
95+
return !networkStatus.Default
96+
})
97+
},
98+
).Should(
99+
WithTransform(
100+
cleanMACAddressesFromStatus(),
101+
ConsistOf(
102+
nettypes.NetworkStatus{
103+
Name: fmt.Sprintf("%s/%s", namespace, networkName),
104+
Interface: ifaceToAdd,
105+
},
106+
nettypes.NetworkStatus{
107+
Name: fmt.Sprintf("%s/%s", namespace, networkName),
108+
Interface: initialPodIfaceName,
109+
})))
108110
})
109111

110112
It("manages to remove an interface from a running pod", func() {
111113
const ifaceToRemove = initialPodIfaceName
112-
114+
Expect(
115+
filterPods(clients, namespace, podName, func(networkStatus nettypes.NetworkStatus) bool {
116+
return !networkStatus.Default
117+
})).Should(
118+
WithTransform(
119+
cleanMACAddressesFromStatus(),
120+
ConsistOf(nettypes.NetworkStatus{
121+
Name: fmt.Sprintf("%s/%s", namespace, networkName),
122+
Interface: initialPodIfaceName,
123+
}),
124+
))
113125
Expect(clients.removeNetworkFromPod(pod, networkName, namespace, ifaceToRemove)).To(Succeed())
114126

115-
Eventually(func() []nettypes.NetworkStatus {
116-
pods, err := clients.ListPods(namespace, fmt.Sprintf("app=%s", podName))
117-
if err != nil {
118-
return nil
119-
}
120-
return podsDynamicNetworks(&pods.Items[0])
121-
}).Should(BeEmpty())
127+
Eventually(
128+
func() []nettypes.NetworkStatus {
129+
return filterPods(clients, namespace, podName, func(networkStatus nettypes.NetworkStatus) bool {
130+
return !networkStatus.Default
131+
})
132+
},
133+
).Should(BeEmpty())
122134
})
123135
})
124136
})
125137
})
126138

139+
func cleanMACAddressesFromStatus() func(status []nettypes.NetworkStatus) []nettypes.NetworkStatus {
140+
return func(status []nettypes.NetworkStatus) []nettypes.NetworkStatus {
141+
for i := range status {
142+
status[i].Mac = ""
143+
}
144+
return status
145+
}
146+
}
147+
148+
type NetworkStatusPredicate func(networkStatus nettypes.NetworkStatus) bool
149+
150+
func filterPods(clients *Clients, namespace, podName string, p NetworkStatusPredicate) []nettypes.NetworkStatus {
151+
pods, err := clients.ListPods(namespace, fmt.Sprintf("app=%s", podName))
152+
if err != nil {
153+
return nil
154+
}
155+
var podNetworkStatus []nettypes.NetworkStatus
156+
for _, netStatus := range podsDynamicNetworks(&pods.Items[0]) {
157+
if p(netStatus) {
158+
podNetworkStatus = append(podNetworkStatus, netStatus)
159+
}
160+
}
161+
return podNetworkStatus
162+
}
163+
127164
func podsDynamicNetworks(pod *corev1.Pod) []nettypes.NetworkStatus {
128-
newNetsAnnotations, wasFound := pod.Annotations[annotations.DynamicNetworksAnnotation]
165+
newNetsAnnotations, wasFound := pod.Annotations[nettypes.NetworkStatusAnnot]
129166
if !wasFound {
130167
return nil
131168
}
@@ -473,12 +510,17 @@ func removeFromDynamicNetworksAnnotation(pod *corev1.Pod, networkName string, ne
473510
updatedNetworkSelectionElements = append(updatedNetworkSelectionElements, *currentNetworkSelectionElements[i])
474511
}
475512

476-
newSelectionElements, err := json.Marshal(updatedNetworkSelectionElements)
477-
if err != nil {
478-
return ""
513+
var newSelectionElements string
514+
if len(updatedNetworkSelectionElements) > 0 {
515+
newSelectionElementsBytes, err := json.Marshal(updatedNetworkSelectionElements)
516+
if err != nil {
517+
return ""
518+
}
519+
newSelectionElements = string(newSelectionElementsBytes)
520+
} else {
521+
newSelectionElements = "[]"
479522
}
480-
481-
return string(newSelectionElements)
523+
return newSelectionElements
482524
}
483525

484526
type dynamicNetworkInfo struct {

go.mod

+42-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ go 1.18
44

55
require (
66
github.com/containerd/containerd v1.6.8
7-
github.com/containernetworking/cni v1.1.1
7+
github.com/containernetworking/cni v1.1.2
88
github.com/gogo/protobuf v1.3.2
99
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.3.0
1010
github.com/onsi/ginkgo v1.16.5
11+
github.com/onsi/ginkgo/v2 v2.1.3
1112
github.com/onsi/gomega v1.17.0
1213
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
14+
gopkg.in/k8snetworkplumbingwg/multus-cni.v3 v3.9.1
1315
k8s.io/api v0.24.4
1416
k8s.io/apimachinery v0.24.4
1517
k8s.io/client-go v0.24.4
@@ -31,7 +33,7 @@ require (
3133
github.com/emicklei/go-restful v2.10.0+incompatible // indirect
3234
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
3335
github.com/fsnotify/fsnotify v1.5.1 // indirect
34-
github.com/go-logr/logr v1.2.2 // indirect
36+
github.com/go-logr/logr v1.2.3 // indirect
3537
github.com/go-openapi/jsonpointer v0.19.5 // indirect
3638
github.com/go-openapi/jsonreference v0.19.5 // indirect
3739
github.com/go-openapi/swag v0.19.14 // indirect
@@ -62,24 +64,56 @@ require (
6264
github.com/sirupsen/logrus v1.8.1 // indirect
6365
github.com/spf13/pflag v1.0.5 // indirect
6466
go.opencensus.io v0.23.0 // indirect
65-
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
67+
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
6668
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
6769
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
68-
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
70+
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
6971
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
7072
golang.org/x/text v0.3.7 // indirect
7173
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
7274
google.golang.org/appengine v1.6.7 // indirect
73-
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
75+
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect
7476
google.golang.org/grpc v1.43.0 // indirect
75-
google.golang.org/protobuf v1.27.1 // indirect
77+
google.golang.org/protobuf v1.28.0 // indirect
7678
gopkg.in/inf.v0 v0.9.1 // indirect
79+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
7780
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
7881
gopkg.in/yaml.v2 v2.4.0 // indirect
7982
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
80-
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
83+
k8s.io/kube-openapi v0.0.0-20220413171646-5e7f5fdc6da6 // indirect
8184
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
8285
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
8386
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
84-
sigs.k8s.io/yaml v1.2.0 // indirect
87+
sigs.k8s.io/yaml v1.3.0 // indirect
8588
)
89+
90+
replace (
91+
k8s.io/api => k8s.io/api v0.24.4
92+
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.4
93+
k8s.io/apimachinery => k8s.io/apimachinery v0.24.4
94+
k8s.io/apiserver => k8s.io/apiserver v0.24.4
95+
k8s.io/cli-runtime => k8s.io/cli-runtime v0.24.4
96+
k8s.io/client-go => k8s.io/client-go v0.24.4
97+
k8s.io/cloud-provider => k8s.io/cloud-provider v0.24.4
98+
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.24.4
99+
k8s.io/code-generator => k8s.io/code-generator v0.24.4
100+
k8s.io/component-base => k8s.io/component-base v0.24.4
101+
k8s.io/component-helpers => k8s.io/component-helpers v0.24.4
102+
k8s.io/controller-manager => k8s.io/controller-manager v0.24.4
103+
k8s.io/cri-api => k8s.io/cri-api v0.24.4
104+
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.24.4
105+
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.24.4
106+
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.24.4
107+
k8s.io/kube-proxy => k8s.io/kube-proxy v0.24.4
108+
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.24.4
109+
k8s.io/kubectl => k8s.io/kubectl v0.24.4
110+
k8s.io/kubelet => k8s.io/kubelet v0.24.4
111+
k8s.io/kubernetes => k8s.io/kubernetes v1.22.8
112+
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.24.4
113+
k8s.io/metrics => k8s.io/metrics v0.24.4
114+
k8s.io/mount-utils => k8s.io/mount-utils v0.24.4
115+
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.24.4
116+
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.24.4
117+
)
118+
119+
replace gopkg.in/k8snetworkplumbingwg/multus-cni.v3 => github.com/k8snetworkplumbingwg/multus-cni v0.0.0-20220926142625-1aac2431b86a

0 commit comments

Comments
 (0)