From 006919082bdf5723463c5c61f7e4816eed494755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Mon, 16 Oct 2023 10:24:53 +0100 Subject: [PATCH] Avoid using KubeVirt client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vilaça --- test/e2e/monitoring/alerts_test.go | 64 +++++++++++++++++++----------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/test/e2e/monitoring/alerts_test.go b/test/e2e/monitoring/alerts_test.go index 9d4070466c..6dc5d416f1 100644 --- a/test/e2e/monitoring/alerts_test.go +++ b/test/e2e/monitoring/alerts_test.go @@ -4,21 +4,26 @@ import ( "context" "fmt" "math/rand" - "os" "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" k8sv1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "kubevirt.io/client-go/kubecli" + k8slabels "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/types" + v1 "kubevirt.io/api/core/v1" kvtests "kubevirt.io/kubevirt/tests" + "kubevirt.io/kubevirt/tests/libvmi" kvtutil "kubevirt.io/kubevirt/tests/util" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" cnao "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/shared" "github.com/kubevirt/cluster-network-addons-operator/pkg/components" . "github.com/kubevirt/cluster-network-addons-operator/test/check" + testenv "github.com/kubevirt/cluster-network-addons-operator/test/env" "github.com/kubevirt/cluster-network-addons-operator/test/kubectl" . "github.com/kubevirt/cluster-network-addons-operator/test/operations" ) @@ -149,20 +154,15 @@ var _ = Context("Prometheus Alerts", func() { }) Context("and there are duplicate MACs", func() { - var virtClient kubecli.KubevirtClient var err error AfterEach(func() { By("deleting test namespace") - err = virtClient.CoreV1().Namespaces().Delete(context.Background(), kvtutil.NamespaceTestDefault, metav1.DeleteOptions{}) + err = testenv.Client.Delete(context.Background(), &k8sv1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: kvtutil.NamespaceTestDefault}}) Expect(err).ToNot(HaveOccurred()) }) BeforeEach(func() { - By("starting virtClient") - virtClient, err = kubecli.GetKubevirtClientFromFlags("", os.Getenv("KUBECONFIG")) - Expect(err).ToNot(HaveOccurred()) - By("creating test namespace that is not managed by kubemacpool (opted-out)") namespace := &k8sv1.Namespace{ObjectMeta: metav1.ObjectMeta{ Name: kvtutil.NamespaceTestDefault, @@ -170,21 +170,21 @@ var _ = Context("Prometheus Alerts", func() { "mutatevirtualmachines.kubemacpool.io": "ignore", }, }} - _, err = virtClient.CoreV1().Namespaces().Create(context.Background(), namespace, metav1.CreateOptions{}) + err := testenv.Client.Create(context.Background(), namespace) Expect(err).ToNot(HaveOccurred()) By("creating 2 VMs with a duplicate MAC") - err = createVirtualMachineWithPrimaryInterfaceMacAddress(virtClient, "00-B0-D0-63-C2-26") + err = createVirtualMachineWithPrimaryInterfaceMacAddress("00-B0-D0-63-C2-26") Expect(err).ToNot(HaveOccurred()) - err = createVirtualMachineWithPrimaryInterfaceMacAddress(virtClient, "00-B0-D0-63-C2-26") + err = createVirtualMachineWithPrimaryInterfaceMacAddress("00-B0-D0-63-C2-26") Expect(err).ToNot(HaveOccurred()) By("cleaning namespace labels, returning the namespace to managed by kubemacpool") - err = cleanNamespaceLabels(virtClient, kvtutil.NamespaceTestDefault) + err = cleanNamespaceLabels(kvtutil.NamespaceTestDefault) Expect(err).ToNot(HaveOccurred()) By("restaring kubemacpool pods") - restartKubemacpoolPods(virtClient) + restartKubemacpoolPods() }) It("should issue KubeMacPoolDuplicateMacsFound alert", func() { @@ -198,40 +198,56 @@ var _ = Context("Prometheus Alerts", func() { }) }) -func createVirtualMachineWithPrimaryInterfaceMacAddress(virtClient kubecli.KubevirtClient, macAddress string) error { - vmi := kvtests.NewRandomVMI() +func createVirtualMachineWithPrimaryInterfaceMacAddress(macAddress string) error { + vmi := libvmi.New( + libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()), + libvmi.WithNetwork(v1.DefaultPodNetwork()), + ) + vmi.ObjectMeta.Namespace = components.Namespace + vmi.Spec.Domain.Resources.Requests = k8sv1.ResourceList{} + vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("128Mi") + vm := kvtests.NewRandomVirtualMachine(vmi, true) vm.Spec.Template.Spec.Domain.Devices.Interfaces[0].MacAddress = macAddress - _, err := virtClient.VirtualMachine(kvtutil.NamespaceTestDefault).Create(context.TODO(), vm) + err := testenv.Client.Create(context.Background(), vm) return err } -func cleanNamespaceLabels(virtClient kubecli.KubevirtClient, namespace string) error { - nsObject, err := virtClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{}) +func cleanNamespaceLabels(namespace string) error { + nsObject := &k8sv1.Namespace{} + err := testenv.Client.Get(context.Background(), types.NamespacedName{Name: namespace}, nsObject) if err != nil { return err } nsObject.Labels = make(map[string]string) - _, err = virtClient.CoreV1().Namespaces().Update(context.TODO(), nsObject, metav1.UpdateOptions{}) - return err + return testenv.Client.Update(context.Background(), nsObject) } -func restartKubemacpoolPods(virtClient kubecli.KubevirtClient) { - pods, err := virtClient.CoreV1().Pods(components.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=kubemacpool"}) +func restartKubemacpoolPods() { + labelSelector, err := k8slabels.Parse("app=kubemacpool") + Expect(err).ToNot(HaveOccurred()) + + listOptions := k8sclient.ListOptions{ + LabelSelector: labelSelector, + Namespace: components.Namespace, + } + + pods := &k8sv1.PodList{} + err = testenv.Client.List(context.Background(), pods, &listOptions) Expect(err).ToNot(HaveOccurred()) nPods := len(pods.Items) for _, pod := range pods.Items { - err = virtClient.CoreV1().Pods(components.Namespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{}) + err = testenv.Client.Delete(context.Background(), &pod) Expect(err).ToNot(HaveOccurred()) } Eventually(func() error { - pods, err = virtClient.CoreV1().Pods(components.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=kubemacpool"}) + err = testenv.Client.List(context.Background(), pods, &listOptions) Expect(err).ToNot(HaveOccurred()) if len(pods.Items) != nPods {