Skip to content

Commit

Permalink
Avoid using KubeVirt client
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <jvilaca@redhat.com>
Signed-off-by: Ram Lavi <ralavi@redhat.com>
  • Loading branch information
machadovilaca authored and RamLavi committed Oct 16, 2023
1 parent 5011c06 commit c151e01
Show file tree
Hide file tree
Showing 45 changed files with 852 additions and 2,594 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ require (
github.com/openshift/origin v4.1.0+incompatible
github.com/operator-framework/operator-sdk v1.12.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16
github.com/prometheus/common v0.44.0
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.37.0
github.com/spf13/pflag v1.0.5
github.com/thanhpk/randstr v1.0.4
golang.org/x/oauth2 v0.8.0
Expand Down
12 changes: 5 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,8 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
Expand Down Expand Up @@ -1371,18 +1371,17 @@ github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
Expand All @@ -1394,9 +1393,8 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.37.0 h1:ccBbHCgIiT9uSoFY0vX8H3zsNR5eLt17/RQLUvn8pXE=
github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA=
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
Expand Down
77 changes: 53 additions & 24 deletions test/e2e/monitoring/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import (
"context"
"fmt"
"math/rand"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/testsuite"

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"
)
Expand Down Expand Up @@ -149,42 +156,37 @@ 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,
Labels: map[string]string{
"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() {
Expand All @@ -198,40 +200,67 @@ var _ = Context("Prometheus Alerts", func() {
})
})

func createVirtualMachineWithPrimaryInterfaceMacAddress(virtClient kubecli.KubevirtClient, macAddress string) error {
vmi := kvtests.NewRandomVMI()
func newRandomVMI() *v1.VirtualMachineInstance {
vmi := libvmi.New(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
libvmi.WithNetwork(v1.DefaultPodNetwork()),
)
vmi.ObjectMeta.Namespace = components.Namespace
vmi.Spec.Domain.Resources.Requests = k8sv1.ResourceList{}

if checks.IsARM64(testsuite.Arch) {
// Cirros image need 256M to boot on ARM64,
vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("256Mi")
} else {
vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("128Mi")
}

return vmi
}

func createVirtualMachineWithPrimaryInterfaceMacAddress(macAddress string) error {
vmi := newRandomVMI()
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 {
Expand Down

0 comments on commit c151e01

Please sign in to comment.