Skip to content

Commit

Permalink
network/kubevirt-ipam-controller: Add new net-attach-def resource
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
RamLavi committed Aug 5, 2024
1 parent 00ee34c commit 1960a98
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion hack/components/bump-kubevirt-ipam-controller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ echo 'Adjust kubevirt-ipam-controller to CNAO'

echo 'Copy manifests'
shopt -s extglob
rm -rf data/kubevirt-ipam-controller/!(002-rbac.yaml)
rm -rf data/kubevirt-ipam-controller/!(002-rbac.yaml|004-primary-udn-networkattachdef.yaml)

# CRD
crd_manifest="https://raw.githubusercontent.com/k8snetworkplumbingwg/ipamclaims/${IPAMCLAIMS_CRD_VERSION}/artifacts/k8s.cni.cncf.io_ipamclaims.yaml"
Expand Down
1 change: 1 addition & 0 deletions pkg/network/kubevirt_ipam_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
63 changes: 63 additions & 0 deletions pkg/network/kubevirt_ipam_controller_test.go
Original file line number Diff line number Diff line change
@@ -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)),
),
))
})
})
})
10 changes: 10 additions & 0 deletions test/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/onsi/gomega"

monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
k8snetworkplumbingwgv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
testenv "github.com/kubevirt/cluster-network-addons-operator/test/env"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
Expand Down Expand Up @@ -389,6 +390,10 @@ func checkForComponentRemoval(component *Component) error {
errsAppend(checkForPrometheusRuleRemoval(component.PrometheusRule))
}

if component.networkAttachmentDefinition != "" {
errsAppend(checkForNetworkAttachmentDefinitionRemoval(component.PrometheusRule))
}

return errsToErr(errs)
}

Expand Down Expand Up @@ -708,6 +713,11 @@ func checkForPrometheusRuleRemoval(name string) error {
return isNotFound("PrometheusRule", name, err)
}

func checkForNetworkAttachmentDefinitionRemoval(name string) error {
err := testenv.Client.Get(context.Background(), types.NamespacedName{Name: name, Namespace: corev1.NamespaceDefault}, &k8snetworkplumbingwgv1.NetworkAttachmentDefinition{})
return isNotFound("NetworkAttachmentDefinition", name, err)
}

func getMonitoringEndpoint() (*corev1.Endpoints, error) {
By("Finding CNAO prometheus endpoint")
endpoint := &corev1.Endpoints{}
Expand Down
12 changes: 7 additions & 5 deletions test/check/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Component struct {
Service string
ServiceMonitor string
PrometheusRule string
networkAttachmentDefinition string
}

var (
Expand Down Expand Up @@ -87,11 +88,12 @@ var (
Deployments: []string{"secondary-dns"},
}
KubevirtIpamController = Component{
ComponentName: "KubevirtIpamController",
ClusterRole: "kubevirt-ipam-controller-manager-role",
ClusterRoleBinding: "kubevirt-ipam-controller-manager-rolebinding",
Deployments: []string{"kubevirt-ipam-controller-manager"},
DaemonSets: []string{"passt-binding-cni"},
ComponentName: "KubevirtIpamController",
ClusterRole: "kubevirt-ipam-controller-manager-role",
ClusterRoleBinding: "kubevirt-ipam-controller-manager-rolebinding",
Deployments: []string{"kubevirt-ipam-controller-manager"},
DaemonSets: []string{"passt-binding-cni"},
networkAttachmentDefinition: "primary-user-defined-network",
}
AllComponents = []Component{
KubeMacPoolComponent,
Expand Down

0 comments on commit 1960a98

Please sign in to comment.