Skip to content

Commit

Permalink
e2e:hypershift: implement deployer interface
Browse files Browse the repository at this point in the history
Implement the deployer interface for HyperShift platform.

Signed-off-by: Talor Itzhak <[email protected]>
  • Loading branch information
Tal-or committed Nov 19, 2024
1 parent 388d19e commit 9cb9a52
Showing 1 changed file with 87 additions and 5 deletions.
92 changes: 87 additions & 5 deletions test/utils/deploy/hypershift.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand All @@ -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
}

0 comments on commit 9cb9a52

Please sign in to comment.