From 9cb9a5271c5b9015bb1fc43cf4a37effb48c44cb Mon Sep 17 00:00:00 2001 From: Talor Itzhak Date: Tue, 19 Nov 2024 20:21:50 +0200 Subject: [PATCH] e2e:hypershift: implement deployer interface Implement the deployer interface for HyperShift platform. Signed-off-by: Talor Itzhak --- test/utils/deploy/hypershift.go | 92 +++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/test/utils/deploy/hypershift.go b/test/utils/deploy/hypershift.go index 8fb10157f..1b7646f9f 100644 --- a/test/utils/deploy/hypershift.go +++ b/test/utils/deploy/hypershift.go @@ -1,13 +1,30 @@ package deploy import ( + "bytes" "context" + "fmt" + "github.com/openshift-kni/numaresources-operator/internal/wait" + "os" "time" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + serializer "k8s.io/apimachinery/pkg/runtime/serializer/json" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/kubernetes/scheme" + + machineconfigv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" - . "github.com/onsi/ginkgo/v2" nropv1 "github.com/openshift-kni/numaresources-operator/api/numaresourcesoperator/v1" + "github.com/openshift-kni/numaresources-operator/internal/nodepools" + e2eclient "github.com/openshift-kni/numaresources-operator/test/utils/clients" + "github.com/openshift-kni/numaresources-operator/test/utils/hypershift" + "github.com/openshift-kni/numaresources-operator/test/utils/objects" ) var _ Deployer = &HyperShiftNRO{} @@ -17,13 +34,78 @@ type HyperShiftNRO struct { KcConfigMapObj *corev1.ConfigMap } -func (h HyperShiftNRO) Deploy(ctx context.Context) *nropv1.NUMAResourcesOperator { +const ( + HostedClustersNamespaceName = "clusters" + ConfigDataKey = "config" +) + +func (h *HyperShiftNRO) Deploy(ctx context.Context) *nropv1.NUMAResourcesOperator { GinkgoHelper() - By("deploying NRO for HyperShift platform not supported just yet") + utilruntime.Must(machineconfigv1.AddToScheme(scheme.Scheme)) + + nroObj := objects.TestNRO(objects.EmptyMatchLabels()) + + if _, ok := os.LookupEnv("E2E_NROP_INSTALL_SKIP_KC"); ok { + By("using cluster kubeletconfig configmap (if any)") + } else { + By("creating KubeletConfig ConfigMap on the management cluster") + kcObj, err := objects.TestKC(objects.EmptyMatchLabels()) + Expect(err).To(Not(HaveOccurred())) + cm := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: kcObj.GetName(), Namespace: HostedClustersNamespaceName}} + b, err := encodeManifest(kcObj, scheme.Scheme) + Expect(err).To(Not(HaveOccurred())) + cm.Data = map[string]string{ConfigDataKey: string(b)} + Expect(e2eclient.MNGClient.Create(ctx, cm)).To(Succeed()) + h.KcConfigMapObj = cm + + hostedClusterName, err := hypershift.GetHostedClusterName() + Expect(err).To(Not(HaveOccurred())) + np, err := nodepools.GetByClusterName(ctx, e2eclient.MNGClient, hostedClusterName) + Expect(err).To(Not(HaveOccurred())) + + By(fmt.Sprintf("attaching KubeletConfig ConfigMap to nodepool %s", np.Name)) + Expect(nodepools.AttachConfigObject(ctx, e2eclient.MNGClient, cm)).To(Succeed()) + + By(fmt.Sprintf("waiting for nodepool %s transition to updating config", np.Name)) + Expect(wait.ForUpdatingConfig(ctx, e2eclient.MNGClient, np.Name, np.Namespace)).To(Succeed()) + By(fmt.Sprintf("waiting for nodepool %s transition to config ready", np.Name)) + Expect(wait.ForConfigToBeReady(ctx, e2eclient.MNGClient, np.Name, np.Namespace)).To(Succeed()) + } + + By(fmt.Sprintf("creating the NRO object: %s", nroObj.Name)) + err := e2eclient.Client.Create(ctx, nroObj) + Expect(err).NotTo(HaveOccurred()) + h.NroObj = nroObj return h.NroObj } -func (h HyperShiftNRO) Teardown(ctx context.Context, timeout time.Duration) { +func (h *HyperShiftNRO) Teardown(ctx context.Context, timeout time.Duration) { GinkgoHelper() - By("Teardown NRO for HyperShift not supported just yet") + if h.KcConfigMapObj != nil { + hostedClusterName, err := hypershift.GetHostedClusterName() + Expect(err).To(Not(HaveOccurred())) + np, err := nodepools.GetByClusterName(ctx, e2eclient.MNGClient, hostedClusterName) + Expect(err).To(Not(HaveOccurred())) + + By(fmt.Sprintf("deataching KubeletConfig ConfigMap from nodepool %s", np.Name)) + Expect(nodepools.DeAttachConfigObject(ctx, e2eclient.MNGClient, h.KcConfigMapObj)).To(Succeed()) + + By(fmt.Sprintf("waiting for nodepool %s transition to updating config", np.Name)) + Expect(wait.ForUpdatingConfig(ctx, e2eclient.MNGClient, np.Name, np.Namespace)).To(Succeed()) + By(fmt.Sprintf("waiting for nodepool %s transition to config ready", np.Name)) + Expect(wait.ForConfigToBeReady(ctx, e2eclient.MNGClient, np.Name, np.Namespace)).To(Succeed()) + + Expect(e2eclient.MNGClient.Delete(ctx, h.KcConfigMapObj)).To(Succeed()) + } + Expect(e2eclient.Client.Delete(ctx, h.NroObj)).To(Succeed()) +} + +func encodeManifest(obj runtime.Object, scheme *runtime.Scheme) ([]byte, error) { + yamlSerializer := serializer.NewSerializerWithOptions( + serializer.DefaultMetaFactory, scheme, scheme, + serializer.SerializerOptions{Yaml: true, Pretty: true, Strict: true}, + ) + buff := bytes.Buffer{} + err := yamlSerializer.Encode(obj, &buff) + return buff.Bytes(), err }