From 00d32acfd64b44a87b34bb3b153099ed0ca0b026 Mon Sep 17 00:00:00 2001 From: Ram Lavi Date: Sun, 4 Aug 2024 11:50:50 +0300 Subject: [PATCH] network/kubevirt-ipam-controller: Add new net-attach-def resource This resource does not belong to kubevirt-ipam-controller, but is currently piggy-backing this component in order to deploy the primary user-defined-network net-attach-def [0]. This net-attach-def is deployed on default namespace, as this way it will be available to all VMs that need to consume it. [0] https://kubevirt.io/user-guide/network/network_binding_plugins/#deployment Signed-off-by: Ram Lavi --- .../004-primary-udn-networkattachdef.yaml | 18 ++++++ .../bump-kubevirt-ipam-controller.sh | 28 +++++++++ pkg/network/kubevirt_ipam_controller.go | 1 + pkg/network/kubevirt_ipam_controller_test.go | 63 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 data/kubevirt-ipam-controller/004-primary-udn-networkattachdef.yaml create mode 100644 pkg/network/kubevirt_ipam_controller_test.go diff --git a/data/kubevirt-ipam-controller/004-primary-udn-networkattachdef.yaml b/data/kubevirt-ipam-controller/004-primary-udn-networkattachdef.yaml new file mode 100644 index 0000000000..2916e9eec0 --- /dev/null +++ b/data/kubevirt-ipam-controller/004-primary-udn-networkattachdef.yaml @@ -0,0 +1,18 @@ +{{ if .EnableNetworkAttachmentDefinition }} +--- +apiVersion: "k8s.cni.cncf.io/v1" +kind: NetworkAttachmentDefinition +metadata: + name: primary-user-defined-network + namespace: default +spec: + config: '{ + "cniVersion": "1.0.0", + "name": "primary-user-defined-network", + "plugins": [ + { + "type": "cni-passt-binding-plugin" + } + ] +}' +{{ end }} diff --git a/hack/components/bump-kubevirt-ipam-controller.sh b/hack/components/bump-kubevirt-ipam-controller.sh index ebfac4b77f..ff44b01099 100755 --- a/hack/components/bump-kubevirt-ipam-controller.sh +++ b/hack/components/bump-kubevirt-ipam-controller.sh @@ -144,6 +144,31 @@ echo 'Adjust kubevirt-ipam-controller to CNAO' sed -i '/containers:/i\{{ if .EnableSCC }}\ serviceAccountName: passt-binding-cni\ {{ end }}' 003-passtbindingcni.yaml + + cat < NetworkAttachmentDefinition_primary-user-defined-network.yaml +{{ if .EnableNetworkAttachmentDefinition }} +--- +apiVersion: "k8s.cni.cncf.io/v1" +kind: NetworkAttachmentDefinition +metadata: + name: primary-user-defined-network + namespace: default +spec: + config: '{ + "cniVersion": "1.0.0", + "name": "primary-user-defined-network", + "plugins": [ + { + "type": "cni-passt-binding-plugin" + } + ] +}' +{{ end }} +EOF + + YAML_FILE=004-primary-udn-networkattachdef.yaml + touch ${YAML_FILE} + cat NetworkAttachmentDefinition_primary-user-defined-network.yaml >> ${YAML_FILE} ) echo 'Copy manifests' @@ -163,6 +188,9 @@ sed -i '/app\.kubernetes\.io\//d' data/kubevirt-ipam-controller/001-kubevirtipam # Passt binding CNI cp ${KUBEVIRT_IPAM_CONTROLLER_PATH}/config/cnao/003-passtbindingcni.yaml data/kubevirt-ipam-controller +# primary user-dufined-network (UDN) NetworkAttachmentDefinition +cp ${KUBEVIRT_IPAM_CONTROLLER_PATH}/config/cnao/004-primary-udn-networkattachdef.yaml data/kubevirt-ipam-controller + echo 'Get kubevirt-ipam-controller image name and update it under CNAO' KUBEVIRT_IPAM_CONTROLLER_TAG=$(git-utils::get_component_tag ${KUBEVIRT_IPAM_CONTROLLER_PATH}) KUBEVIRT_IPAM_CONTROLLER_IMAGE=ghcr.io/kubevirt/ipam-controller diff --git a/pkg/network/kubevirt_ipam_controller.go b/pkg/network/kubevirt_ipam_controller.go index ee6cf90a0e..b1ddac5109 100644 --- a/pkg/network/kubevirt_ipam_controller.go +++ b/pkg/network/kubevirt_ipam_controller.go @@ -49,6 +49,7 @@ func renderKubevirtIPAMController(conf *cnao.NetworkAddonsConfigSpec, manifestDi } data.Data["IsOpenshift"] = clusterInfo.OpenShift4 data.Data["EnableSCC"] = clusterInfo.SCCAvailable + data.Data["EnableNetworkAttachmentDefinition"] = clusterInfo.NetAttachDefAvailable objs, err := render.RenderDir(filepath.Join(manifestDir, "kubevirt-ipam-controller"), &data) if err != nil { diff --git a/pkg/network/kubevirt_ipam_controller_test.go b/pkg/network/kubevirt_ipam_controller_test.go new file mode 100644 index 0000000000..13939dbb41 --- /dev/null +++ b/pkg/network/kubevirt_ipam_controller_test.go @@ -0,0 +1,63 @@ +package network + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + osv1 "github.com/openshift/api/operator/v1" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" + + cnao "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/shared" +) + +var _ = Describe("Testing kubevirt ipam controller", func() { + Context("Render KubevirtIpamController", func() { + conf := &cnao.NetworkAddonsConfigSpec{ImagePullPolicy: v1.PullAlways, Multus: &cnao.Multus{}, KubevirtIpamController: &cnao.KubevirtIpamController{}, PlacementConfiguration: &cnao.PlacementConfiguration{Workloads: &cnao.Placement{}}} + manifestDir := "../../data" + openshiftNetworkConf := &osv1.Network{} + clusterInfo := &ClusterInfo{SCCAvailable: true, OpenShift4: false} + expectedGroupVersionKind := schema.GroupVersionKind{ + Group: "k8s.cni.cncf.io", + Kind: "NetworkAttachmentDefinition", + Version: "v1", + } + const expectedName = "primary-user-defined-network" + + It("and NetAttachDefAvailable resource is available, should add the primary-udn network-attach-def obj", func() { + clusterInfo.NetAttachDefAvailable = true + objs, err := Render(conf, manifestDir, openshiftNetworkConf, clusterInfo) + Expect(err).NotTo(HaveOccurred()) + Expect(objs).NotTo(BeEmpty()) + + Expect(objs).To(ContainElement( + SatisfyAll( + WithTransform(func(obj *unstructured.Unstructured) string { + return obj.GetName() + }, Equal(expectedName)), + WithTransform(func(obj *unstructured.Unstructured) schema.GroupVersionKind { + return obj.GetObjectKind().GroupVersionKind() + }, Equal(expectedGroupVersionKind)), + ), + )) + }) + It("and NetAttachDefAvailable resource is not available, should not add the primary-udn network-attach-def obj", func() { + clusterInfo.NetAttachDefAvailable = false + objs, err := Render(conf, manifestDir, openshiftNetworkConf, clusterInfo) + Expect(err).NotTo(HaveOccurred()) + Expect(objs).NotTo(BeEmpty()) + + Expect(objs).ToNot(ContainElement( + SatisfyAll( + WithTransform(func(obj *unstructured.Unstructured) string { + return obj.GetName() + }, Equal(expectedName)), + WithTransform(func(obj *unstructured.Unstructured) schema.GroupVersionKind { + return obj.GetObjectKind().GroupVersionKind() + }, Equal(expectedGroupVersionKind)), + ), + )) + }) + }) +})