|
| 1 | +package annotations |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + cni100 "github.com/containernetworking/cni/pkg/types/100" |
| 11 | + |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" |
| 16 | + "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("NetworkStatusFromResponse", func() { |
| 20 | + const ( |
| 21 | + ifaceName = "ens32" |
| 22 | + namespace = "ns1" |
| 23 | + networkName = "tenantnetwork" |
| 24 | + podName = "tpod" |
| 25 | + ) |
| 26 | + |
| 27 | + DescribeTable("add dynamic interface to network status", func(initialNetStatus []nadv1.NetworkStatus, resultIPs []string, expectedNetworkStatus string) { |
| 28 | + Expect( |
| 29 | + AddDynamicIfaceToStatus( |
| 30 | + newPod(podName, namespace, initialNetStatus...), |
| 31 | + newNetworkSelectionElementWithIface(networkName, ifaceName, namespace), |
| 32 | + newResponse(resultIPs...), |
| 33 | + ), |
| 34 | + ).To(Equal(expectedNetworkStatus)) |
| 35 | + }, |
| 36 | + Entry("initial empty pod", []nadv1.NetworkStatus{}, nil, `[{"name":"ns1/tenantnetwork","interface":"pepenet","mac":"02:03:04:05:06:07","dns":{}}]`), |
| 37 | + Entry("pod with a network present in the network status", []nadv1.NetworkStatus{ |
| 38 | + { |
| 39 | + Name: "net1", |
| 40 | + Interface: "iface1", |
| 41 | + Mac: "00:00:00:20:10:00", |
| 42 | + }}, |
| 43 | + nil, |
| 44 | + `[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}},{"name":"ns1/tenantnetwork","interface":"pepenet","mac":"02:03:04:05:06:07","dns":{}}]`), |
| 45 | + Entry("result with IPs", []nadv1.NetworkStatus{ |
| 46 | + { |
| 47 | + Name: "net1", |
| 48 | + Interface: "iface1", |
| 49 | + Mac: "00:00:00:20:10:00", |
| 50 | + }}, |
| 51 | + []string{"10.10.10.10/24"}, |
| 52 | + `[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}},{"name":"ns1/tenantnetwork","interface":"pepenet","ips":["10.10.10.10/24"],"mac":"02:03:04:05:06:07","dns":{}}]`)) |
| 53 | + |
| 54 | + DescribeTable("remove an interface to the current network status", func(initialNetStatus []nadv1.NetworkStatus, networkName, ifaceName, expectedNetworkStatus string) { |
| 55 | + Expect( |
| 56 | + DeleteDynamicIfaceFromStatus( |
| 57 | + newPod(podName, namespace, initialNetStatus...), |
| 58 | + networkName, |
| 59 | + ifaceName, |
| 60 | + ), |
| 61 | + ).To(Equal(expectedNetworkStatus)) |
| 62 | + }, |
| 63 | + Entry("when there aren't any existing interfaces", nil, "net1", "iface1", "[]"), |
| 64 | + Entry("when we remove all the currently existing interfaces", []nadv1.NetworkStatus{ |
| 65 | + { |
| 66 | + Name: "net1", |
| 67 | + Interface: "iface1", |
| 68 | + Mac: "00:00:00:20:10:00", |
| 69 | + }}, "net1", "iface1", "[]"), |
| 70 | + Entry("when there is *not* a matching interface to remove", []nadv1.NetworkStatus{ |
| 71 | + { |
| 72 | + Name: "net1", |
| 73 | + Interface: "iface1", |
| 74 | + Mac: "00:00:00:20:10:00", |
| 75 | + }}, "net2", "iface1", `[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}}]`), |
| 76 | + Entry("when we remove one of the existing interfaces", []nadv1.NetworkStatus{ |
| 77 | + { |
| 78 | + Name: "net1", |
| 79 | + Interface: "iface1", |
| 80 | + Mac: "00:00:00:20:10:00", |
| 81 | + }, |
| 82 | + { |
| 83 | + Name: "net2", |
| 84 | + Interface: "iface2", |
| 85 | + Mac: "aa:bb:cc:20:10:00", |
| 86 | + }, |
| 87 | + }, "net2", "iface2", `[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}}]`)) |
| 88 | +}) |
| 89 | + |
| 90 | +func newPod(podName string, namespace string, netStatus ...nadv1.NetworkStatus) *corev1.Pod { |
| 91 | + status, err := json.Marshal(netStatus) |
| 92 | + if err != nil { |
| 93 | + return nil |
| 94 | + } |
| 95 | + return &corev1.Pod{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: podName, |
| 98 | + Namespace: namespace, |
| 99 | + Annotations: map[string]string{ |
| 100 | + nadv1.NetworkStatusAnnot: string(status), |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func newResponse(ips ...string) *api.Response { |
| 107 | + var ipConfs []*cni100.IPConfig |
| 108 | + for i := range ips { |
| 109 | + ip, ipNet, err := net.ParseCIDR(ips[i]) |
| 110 | + if err != nil { |
| 111 | + |
| 112 | + } |
| 113 | + ipNet.IP = ip |
| 114 | + ipConfs = append(ipConfs, &cni100.IPConfig{Address: *ipNet}) |
| 115 | + } |
| 116 | + |
| 117 | + ifaces := []*cni100.Interface{{ |
| 118 | + Name: "pepenet", |
| 119 | + Mac: "02:03:04:05:06:07", |
| 120 | + Sandbox: "pepe", |
| 121 | + }} |
| 122 | + return &api.Response{ |
| 123 | + Result: &cni100.Result{ |
| 124 | + CNIVersion: "1.0.0", |
| 125 | + Interfaces: ifaces, |
| 126 | + IPs: ipConfs, |
| 127 | + }} |
| 128 | +} |
0 commit comments