From 1f98cb5b815121a3fde9e4b883d66fa300b514ed Mon Sep 17 00:00:00 2001 From: Ronny Baturov Date: Mon, 16 Dec 2024 17:19:12 +0200 Subject: [PATCH] Added a version check to the upgrade suite The upgrade suite is only supported on operator versions 4.18 or newer * Added a util func to find the operator pod. * Utilized the buildinfo.json inside the container to inspect the operator version. Signed-off-by: Ronny Baturov --- test/e2e/upgrade/upgrade_test.go | 30 +++++++++++++++- test/utils/deploy/find.go | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/utils/deploy/find.go diff --git a/test/e2e/upgrade/upgrade_test.go b/test/e2e/upgrade/upgrade_test.go index 10e70f3a5..33fc60c6f 100644 --- a/test/e2e/upgrade/upgrade_test.go +++ b/test/e2e/upgrade/upgrade_test.go @@ -18,29 +18,57 @@ package upgrade import ( "context" + "encoding/json" + "path/filepath" + "strings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/util/version" "sigs.k8s.io/controller-runtime/pkg/client" nropv1 "github.com/openshift-kni/numaresources-operator/api/numaresourcesoperator/v1" "github.com/openshift-kni/numaresources-operator/internal/api/annotations" + "github.com/openshift-kni/numaresources-operator/internal/api/buildinfo" nropmcp "github.com/openshift-kni/numaresources-operator/internal/machineconfigpools" + "github.com/openshift-kni/numaresources-operator/internal/remoteexec" "github.com/openshift-kni/numaresources-operator/pkg/objectnames" e2eclient "github.com/openshift-kni/numaresources-operator/test/utils/clients" + "github.com/openshift-kni/numaresources-operator/test/utils/deploy" "github.com/openshift-kni/numaresources-operator/test/utils/objects" + machineconfigv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" ) var _ = Describe("Upgrade", Label("upgrade"), func() { var err error var initialized bool - BeforeEach(func() { if !initialized { Expect(e2eclient.ClientsEnabled).To(BeTrue(), "failed to create runtime-controller client") } + + nropObj := objects.TestNRO() + nname := client.ObjectKeyFromObject(nropObj) + + err = e2eclient.Client.Get(context.TODO(), nname, nropObj) + Expect(err).NotTo(HaveOccurred(), "failed to get the NRO resource: %v", err) + + pod, err := deploy.FindNUMAResourcesOperatorPod(context.TODO(), e2eclient.Client, nropObj) + Expect(err).ToNot(HaveOccurred()) + stdout, _, err := remoteexec.CommandOnPod(context.TODO(), e2eclient.K8sClient, pod, "/bin/cat", filepath.Join("/usr/local/share/", "buildinfo.json")) + Expect(err).ToNot(HaveOccurred()) + + bi := &buildinfo.BuildInfo{} + err = json.Unmarshal(stdout, &bi) + Expect(err).ToNot(HaveOccurred()) + Expect(bi.Branch).ToNot(BeEmpty()) + operatorVersion := version.MustParse(strings.TrimPrefix(bi.Branch, "release-")) + minVersion := version.MustParse("4.18") + if operatorVersion.LessThan(minVersion) { + Skip("Upgrade suite is only supported on operator versions 4.18 or newer") + } initialized = true }) diff --git a/test/utils/deploy/find.go b/test/utils/deploy/find.go new file mode 100644 index 000000000..eccc66eaa --- /dev/null +++ b/test/utils/deploy/find.go @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package deploy + +import ( + "context" + "errors" + "fmt" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/klog/v2" + + "sigs.k8s.io/controller-runtime/pkg/client" + + nropv1 "github.com/openshift-kni/numaresources-operator/api/numaresourcesoperator/v1" +) + +func FindNUMAResourcesOperatorPod(ctx context.Context, cli client.Client, nrop *nropv1.NUMAResourcesOperator) (*corev1.Pod, error) { + if len(nrop.Status.NodeGroups) < 1 { + return nil, errors.New("node groups not reported, nothing to do") + } + // nrop places all daemonsets in the same namespace on which it resides, so any group is fine + namespace := nrop.Status.NodeGroups[0].DaemonSet.Namespace // shortcut + klog.InfoS("NROP pod", "namespace", namespace) + + sel, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "numaresources-operator", + }, + }) + if err != nil { + return nil, err + } + klog.InfoS("NROP pod", "selector", sel.String()) + + podList := corev1.PodList{} + err = cli.List(ctx, &podList, &client.ListOptions{Namespace: namespace, LabelSelector: sel}) + if err != nil { + return nil, err + } + + if len(podList.Items) != 1 { + return nil, fmt.Errorf("unexpected number of pods found: %d", len(podList.Items)) + } + + return &podList.Items[0], nil +}