From 24ab8d81e920225aa36bd23c9c74264c922355d9 Mon Sep 17 00:00:00 2001 From: Alexandr Demicev Date: Tue, 21 Jan 2025 16:39:04 +0100 Subject: [PATCH] Remove all cluster v1 code Signed-off-by: Alexandr Demicev --- internal/controllers/cleanup_controller.go | 74 ++++ internal/controllers/import_controller.go | 410 ------------------ .../controllers/import_controller_test.go | 398 ----------------- .../controllers/import_controller_v3_test.go | 5 + .../embedded_capi_disabled_test.go | 54 --- .../embedded-capi-disabled/suite_test.go | 155 ------- .../import-gitops/import_gitops_test.go | 139 ------ test/e2e/suites/import-gitops/suite_test.go | 193 --------- 8 files changed, 79 insertions(+), 1349 deletions(-) create mode 100644 internal/controllers/cleanup_controller.go delete mode 100644 internal/controllers/import_controller.go delete mode 100644 internal/controllers/import_controller_test.go delete mode 100644 test/e2e/suites/embedded-capi-disabled/embedded_capi_disabled_test.go delete mode 100644 test/e2e/suites/embedded-capi-disabled/suite_test.go delete mode 100644 test/e2e/suites/import-gitops/import_gitops_test.go delete mode 100644 test/e2e/suites/import-gitops/suite_test.go diff --git a/internal/controllers/cleanup_controller.go b/internal/controllers/cleanup_controller.go new file mode 100644 index 00000000..71f6dc0b --- /dev/null +++ b/internal/controllers/cleanup_controller.go @@ -0,0 +1,74 @@ +/* +Copyright © 2023 - 2024 SUSE LLC + +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 controllers + +import ( + "context" + "fmt" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + managementv3 "github.com/rancher/turtles/api/rancher/management/v3" +) + +// CAPICleanupReconciler is a reconciler for cleanup of managementv3 clusters. +type CAPICleanupReconciler struct { + RancherClient client.Client + Scheme *runtime.Scheme +} + +// SetupWithManager sets up reconciler with manager. +func (r *CAPICleanupReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, options controller.Options) error { + if err := ctrl.NewControllerManagedBy(mgr). + Named("cleanup"). + For(&managementv3.Cluster{}). + WithOptions(options). + WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool { + _, exist := object.GetLabels()[ownedLabelName] + return exist + })). + Complete(reconcile.AsReconciler(r.RancherClient, r)); err != nil { + return fmt.Errorf("creating new downgrade controller: %w", err) + } + + return nil +} + +// Reconcile performs check for clusters and removes finalizer on the clusters in deleteion +// still containing the turtles finalizer. +func (r *CAPICleanupReconciler) Reconcile(ctx context.Context, cluster *managementv3.Cluster) (res ctrl.Result, err error) { + log := log.FromContext(ctx) + + patchBase := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{}) + + if cluster.DeletionTimestamp.IsZero() || !controllerutil.RemoveFinalizer(cluster, managementv3.CapiClusterFinalizer) { + return + } + + if err = r.RancherClient.Patch(ctx, cluster, patchBase); err != nil { + log.Error(err, "Unable to remove turtles finalizer from cluster"+cluster.GetName()) + } + + return +} diff --git a/internal/controllers/import_controller.go b/internal/controllers/import_controller.go deleted file mode 100644 index ccef2d55..00000000 --- a/internal/controllers/import_controller.go +++ /dev/null @@ -1,410 +0,0 @@ -/* -Copyright © 2023 - 2024 SUSE LLC - -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 controllers - -import ( - "context" - "fmt" - "strings" - - corev1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - errorutils "k8s.io/apimachinery/pkg/util/errors" - "k8s.io/client-go/tools/record" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/event" - "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/predicate" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" - - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - "sigs.k8s.io/cluster-api/controllers/external" - "sigs.k8s.io/cluster-api/controllers/remote" - "sigs.k8s.io/cluster-api/util/conditions" - "sigs.k8s.io/cluster-api/util/predicates" - - managementv3 "github.com/rancher/turtles/api/rancher/management/v3" - provisioningv1 "github.com/rancher/turtles/api/rancher/provisioning/v1" - "github.com/rancher/turtles/feature" - "github.com/rancher/turtles/util" - turtlesannotations "github.com/rancher/turtles/util/annotations" - turtlesnaming "github.com/rancher/turtles/util/naming" - turtlespredicates "github.com/rancher/turtles/util/predicates" -) - -// CAPIImportReconciler represents a reconciler for importing CAPI clusters in Rancher. -type CAPIImportReconciler struct { - Client client.Client - RancherClient client.Client - recorder record.EventRecorder - WatchFilterValue string - Scheme *runtime.Scheme - InsecureSkipVerify bool - - controller controller.Controller - externalTracker external.ObjectTracker - remoteClientGetter remote.ClusterClientGetter -} - -// SetupWithManager sets up reconciler with manager. -func (r *CAPIImportReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { - log := log.FromContext(ctx) - - if r.remoteClientGetter == nil { - r.remoteClientGetter = remote.NewClusterClient - } - - capiPredicates := predicates.All(r.Scheme, log, - predicates.ResourceHasFilterLabel(r.Scheme, log, r.WatchFilterValue), - turtlespredicates.ClusterWithoutImportedAnnotation(log), - turtlespredicates.ClusterWithReadyControlPlane(log), - turtlespredicates.ClusterOrNamespaceWithImportLabel(ctx, log, r.Client, importLabelName), - ) - - c, err := ctrl.NewControllerManagedBy(mgr). - For(&clusterv1.Cluster{}). - WithOptions(options). - WithEventFilter(capiPredicates). - Build(r) - if err != nil { - return fmt.Errorf("creating new controller: %w", err) - } - - // Watch Rancher provisioningv1 clusters - // NOTE: we will import the types from rancher in the future - err = c.Watch( - source.Kind[client.Object](mgr.GetCache(), &provisioningv1.Cluster{}, - handler.EnqueueRequestsFromMapFunc(r.rancherClusterToCapiCluster(ctx, capiPredicates)), - )) - if err != nil { - return fmt.Errorf("adding watch for Rancher cluster: %w", err) - } - - ns := &corev1.Namespace{} - - err = c.Watch( - source.Kind[client.Object](mgr.GetCache(), ns, - handler.EnqueueRequestsFromMapFunc(namespaceToCapiClusters(ctx, capiPredicates, r.Client)), - )) - if err != nil { - return fmt.Errorf("adding watch for namespaces: %w", err) - } - - r.recorder = mgr.GetEventRecorderFor("rancher-turtles") - r.controller = c - r.externalTracker = external.ObjectTracker{ - Controller: c, - } - - return nil -} - -// +kubebuilder:rbac:groups="",resources=secrets;events;configmaps,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;create;update -// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch;update;patch -// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=*,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups=provisioning.cattle.io,resources=clusters;clusters/status,verbs=get;list;watch;create;update;delete;patch -// +kubebuilder:rbac:groups=management.cattle.io,resources=clusterregistrationtokens;clusterregistrationtokens/status,verbs=get;list;watch - -// Reconcile reconciles a CAPI cluster, creating a Rancher cluster if needed and applying the import manifests. -func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, reterr error) { - log := log.FromContext(ctx) - log.Info("Reconciling CAPI cluster") - - capiCluster := &clusterv1.Cluster{} - if err := r.Client.Get(ctx, req.NamespacedName, capiCluster); err != nil { - if apierrors.IsNotFound(err) { - return ctrl.Result{Requeue: true}, nil - } - - return ctrl.Result{Requeue: true}, err - } - - patchBase := client.MergeFromWithOptions(capiCluster.DeepCopy(), client.MergeFromWithOptimisticLock{}) - - log = log.WithValues("cluster", capiCluster.Name) - - // Collect errors as an aggregate to return together after all patches have been performed. - var errs []error - - if !capiCluster.ObjectMeta.DeletionTimestamp.IsZero() && controllerutil.RemoveFinalizer(capiCluster, managementv3.CapiClusterFinalizer) { - if err := r.Client.Patch(ctx, capiCluster, patchBase); err != nil { - log.Error(err, "failed to remove CAPI cluster finalizer "+managementv3.CapiClusterFinalizer) - errs = append(errs, err) - } - } - - // Wait for controlplane to be ready. This should never be false as the predicates - // do the filtering. - if !capiCluster.Status.ControlPlaneReady && !conditions.IsTrue(capiCluster, clusterv1.ControlPlaneReadyCondition) { - log.Info("clusters control plane is not ready, requeue") - return ctrl.Result{RequeueAfter: defaultRequeueDuration}, nil - } - - result, err := r.reconcile(ctx, capiCluster) - if err != nil { - errs = append(errs, fmt.Errorf("error reconciling cluster: %w", err)) - } - - if err := r.Client.Patch(ctx, capiCluster, patchBase); err != nil { - errs = append(errs, fmt.Errorf("failed to patch cluster: %w", err)) - } - - if len(errs) > 0 { - return ctrl.Result{}, errorutils.NewAggregate(errs) - } - - return result, nil -} - -func (r *CAPIImportReconciler) reconcile(ctx context.Context, capiCluster *clusterv1.Cluster) (ctrl.Result, error) { - log := log.FromContext(ctx) - - // fetch the rancher cluster - rancherCluster := &provisioningv1.Cluster{ObjectMeta: metav1.ObjectMeta{ - Namespace: capiCluster.Namespace, - Name: turtlesnaming.Name(capiCluster.Name).ToRancherName(), - }} - - err := r.RancherClient.Get(ctx, client.ObjectKeyFromObject(rancherCluster), rancherCluster) - if client.IgnoreNotFound(err) != nil { - log.Error(err, fmt.Sprintf("Unable to fetch rancher cluster %s", client.ObjectKeyFromObject(rancherCluster))) - return ctrl.Result{Requeue: true}, err - } - - if !rancherCluster.ObjectMeta.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, capiCluster) - } - - return r.reconcileNormal(ctx, capiCluster, rancherCluster) -} - -func (r *CAPIImportReconciler) reconcileNormal(ctx context.Context, capiCluster *clusterv1.Cluster, - rancherCluster *provisioningv1.Cluster, -) (ctrl.Result, error) { - log := log.FromContext(ctx) - - err := r.RancherClient.Get(ctx, client.ObjectKeyFromObject(rancherCluster), rancherCluster) - if apierrors.IsNotFound(err) { - shouldImport, err := util.ShouldAutoImport(ctx, log, r.Client, capiCluster, importLabelName) - if err != nil { - return ctrl.Result{}, err - } - - if !shouldImport { - log.Info("not auto importing cluster as namespace or cluster isn't marked auto import") - return ctrl.Result{}, nil - } - - newCluster := &provisioningv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: turtlesnaming.Name(capiCluster.Name).ToRancherName(), - Namespace: capiCluster.Namespace, - OwnerReferences: []metav1.OwnerReference{{ - APIVersion: clusterv1.GroupVersion.String(), - Kind: clusterv1.ClusterKind, - Name: capiCluster.Name, - UID: capiCluster.UID, - }}, - Labels: map[string]string{ - ownedLabelName: "", - }, - }, - } - - if feature.Gates.Enabled(feature.PropagateLabels) { - for labelKey, labelVal := range capiCluster.Labels { - newCluster.Labels[labelKey] = labelVal - } - } - - if err := r.RancherClient.Create(ctx, newCluster); err != nil { - return ctrl.Result{}, fmt.Errorf("error creating rancher cluster: %w", err) - } - - return ctrl.Result{Requeue: true}, nil - } - - if err != nil { - log.Error(err, fmt.Sprintf("Unable to fetch rancher cluster %s", client.ObjectKeyFromObject(rancherCluster))) - - return ctrl.Result{}, err - } - - if rancherCluster.Status.ClusterName == "" { - log.Info("cluster name not set yet, requeue") - return ctrl.Result{Requeue: true}, nil - } - - log.Info("found cluster name", "name", rancherCluster.Status.ClusterName) - - if rancherCluster.Status.AgentDeployed { - log.Info("agent is deployed, no action needed") - return ctrl.Result{}, nil - } - - // Get custom CAcert if agentTLSMode feature is enabled - caCert, err := getTrustedCAcert(ctx, r.Client, feature.Gates.Enabled(feature.AgentTLSMode)) - if err != nil { - return ctrl.Result{}, fmt.Errorf("error getting CA cert: %w", err) - } - - // get the registration manifest - manifest, err := getClusterRegistrationManifest(ctx, rancherCluster.Status.ClusterName, capiCluster.Namespace, r.RancherClient, - caCert, r.InsecureSkipVerify) - if err != nil { - return ctrl.Result{}, err - } - - if manifest == "" { - log.Info("Import manifest URL not set yet, requeue") - return ctrl.Result{Requeue: true}, nil - } - - log.Info("Creating import manifest") - - remoteClient, err := r.remoteClientGetter(ctx, capiCluster.Name, r.Client, client.ObjectKeyFromObject(capiCluster)) - if err != nil { - return ctrl.Result{}, fmt.Errorf("getting remote cluster client: %w", err) - } - - if requeue, err := validateImportReadiness(ctx, remoteClient, strings.NewReader(manifest)); err != nil { - return ctrl.Result{}, fmt.Errorf("verifying import manifest: %w", err) - } else if requeue { - log.Info("Import manifests are being deleted, not ready to be applied yet, requeue") - return ctrl.Result{RequeueAfter: defaultRequeueDuration}, nil - } - - if err := createImportManifest(ctx, remoteClient, strings.NewReader(manifest)); err != nil { - return ctrl.Result{}, fmt.Errorf("creating import manifest: %w", err) - } - - log.Info("Successfully applied import manifest") - - if feature.Gates.Enabled(feature.PropagateLabels) { - patchBase := client.MergeFromWithOptions(rancherCluster.DeepCopy(), client.MergeFromWithOptimisticLock{}) - - if rancherCluster.Labels == nil { - rancherCluster.Labels = map[string]string{} - } - - for labelKey, labelVal := range capiCluster.Labels { - rancherCluster.Labels[labelKey] = labelVal - } - - if err := r.Client.Patch(ctx, rancherCluster, patchBase); err != nil { - return ctrl.Result{}, fmt.Errorf("failed to patch Rancher cluster: %w", err) - } - - log.Info("Successfully propagated labels to Rancher cluster") - } - - return ctrl.Result{}, nil -} - -func (r *CAPIImportReconciler) rancherClusterToCapiCluster(ctx context.Context, clusterPredicate predicate.Funcs) handler.MapFunc { - log := log.FromContext(ctx) - - return func(_ context.Context, o client.Object) []ctrl.Request { - capiCluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{ - Name: turtlesnaming.Name(o.GetName()).ToCapiName(), - Namespace: o.GetNamespace(), - }} - if err := r.Client.Get(ctx, client.ObjectKeyFromObject(capiCluster), capiCluster); err != nil { - if !apierrors.IsNotFound(err) { - log.Error(err, "getting capi cluster") - } - - return nil - } - - if !clusterPredicate.Generic(event.GenericEvent{Object: capiCluster}) { - return nil - } - - return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: capiCluster.Namespace, Name: capiCluster.Name}}} - } -} - -func (r *CAPIImportReconciler) reconcileDelete(ctx context.Context, capiCluster *clusterv1.Cluster) (ctrl.Result, error) { - log := log.FromContext(ctx) - log.Info("Reconciling rancher cluster deletion") - - // If the Rancher Cluster was already imported, then annotate the CAPI cluster so that we don't auto-import again. - log.Info(fmt.Sprintf("Rancher cluster is being removed, annotating CAPI cluster %s with %s", - capiCluster.Name, - turtlesannotations.ClusterImportedAnnotation)) - - annotations := capiCluster.GetAnnotations() - if annotations == nil { - annotations = map[string]string{} - } - - annotations[turtlesannotations.ClusterImportedAnnotation] = trueAnnotationValue - capiCluster.SetAnnotations(annotations) - - return ctrl.Result{}, nil -} - -// CAPICleanupReconciler is a reconciler for cleanup of managementv3 clusters. -type CAPICleanupReconciler struct { - RancherClient client.Client - Scheme *runtime.Scheme -} - -// SetupWithManager sets up reconciler with manager. -func (r *CAPICleanupReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, options controller.Options) error { - if err := ctrl.NewControllerManagedBy(mgr). - Named("cleanup"). - For(&managementv3.Cluster{}). - WithOptions(options). - WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool { - _, exist := object.GetLabels()[ownedLabelName] - return exist - })). - Complete(reconcile.AsReconciler(r.RancherClient, r)); err != nil { - return fmt.Errorf("creating new downgrade controller: %w", err) - } - - return nil -} - -// Reconcile performs check for clusters and removes finalizer on the clusters in deleteion -// still containing the turtles finalizer. -func (r *CAPICleanupReconciler) Reconcile(ctx context.Context, cluster *managementv3.Cluster) (res ctrl.Result, err error) { - log := log.FromContext(ctx) - - patchBase := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{}) - - if cluster.DeletionTimestamp.IsZero() || !controllerutil.RemoveFinalizer(cluster, managementv3.CapiClusterFinalizer) { - return - } - - if err = r.RancherClient.Patch(ctx, cluster, patchBase); err != nil { - log.Error(err, "Unable to remove turtles finalizer from cluster"+cluster.GetName()) - } - - return -} diff --git a/internal/controllers/import_controller_test.go b/internal/controllers/import_controller_test.go deleted file mode 100644 index 16feb7d8..00000000 --- a/internal/controllers/import_controller_test.go +++ /dev/null @@ -1,398 +0,0 @@ -/* -Copyright © 2023 - 2024 SUSE LLC - -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 controllers - -import ( - "fmt" - "net/http" - "net/http/httptest" - "strings" - "time" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - managementv3 "github.com/rancher/turtles/api/rancher/management/v3" - provisioningv1 "github.com/rancher/turtles/api/rancher/provisioning/v1" - "github.com/rancher/turtles/internal/controllers/testdata" - "github.com/rancher/turtles/internal/test" - turtlesnaming "github.com/rancher/turtles/util/naming" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - "sigs.k8s.io/cluster-api/controllers/remote" - "sigs.k8s.io/cluster-api/util/secret" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -var ( - testLabelName = "test-label" - testLabelVal = "true" -) - -var _ = Describe("reconcile CAPI Cluster", func() { - var ( - r *CAPIImportReconciler - ns *corev1.Namespace - capiCluster *clusterv1.Cluster - rancherCluster *provisioningv1.Cluster - clusterRegistrationToken *managementv3.ClusterRegistrationToken - capiKubeconfigSecret *corev1.Secret - clusterName = "generated-rancher-cluster" - sampleTemplate string - ) - - BeforeEach(func() { - var err error - - ns, err = testEnv.CreateNamespace(ctx, "commonns") - Expect(err).ToNot(HaveOccurred()) - ns.Labels = map[string]string{ - importLabelName: "true", - } - Expect(cl.Update(ctx, ns)).To(Succeed()) - - sampleTemplate = setTemplateParams( - testdata.ImportManifest, - map[string]string{"${TEST_CASE_NAME}": "provisioningv1"}, - ) - - r = &CAPIImportReconciler{ - Client: testEnv, - RancherClient: testEnv, // rancher and rancher-turtles deployed in the same cluster - remoteClientGetter: remote.NewClusterClient, - Scheme: testEnv.GetScheme(), - } - - capiCluster = &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-cluster", - Namespace: ns.Name, - }, - } - - rancherCluster = &provisioningv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: turtlesnaming.Name(capiCluster.Name).ToRancherName(), - Namespace: ns.Name, - }, - } - - clusterRegistrationToken = &managementv3.ClusterRegistrationToken{ - ObjectMeta: metav1.ObjectMeta{ - Name: clusterName, - Namespace: ns.Name, - }, - } - - capiKubeconfigSecret = &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("%s-kubeconfig", capiCluster.Name), - Namespace: ns.Name, - }, - Data: map[string][]byte{ - secret.KubeconfigDataName: kubeConfigBytes, - }, - } - }) - - AfterEach(func() { - objs, err := manifestToObjects(strings.NewReader(sampleTemplate)) - clientObjs := []client.Object{ - capiCluster, - rancherCluster, - clusterRegistrationToken, - capiKubeconfigSecret, - } - for _, obj := range objs { - clientObj, ok := obj.(client.Object) - Expect(ok).To(BeTrue()) - clientObjs = append(clientObjs, clientObj) - } - Expect(err).ToNot(HaveOccurred()) - Expect(test.CleanupAndWait(ctx, cl, clientObjs...)).To(Succeed()) - Expect(testEnv.Cleanup(ctx, ns)).To(Succeed()) - }) - - It("should reconcile a CAPI cluster when control plane not ready", func() { - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - - Eventually(func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.RequeueAfter).To(Equal(defaultRequeueDuration)) - }).Should(Succeed()) - }) - - It("should remove a CAPI cluster with turtles finalizer", func() { - capiCluster.Finalizers = []string{managementv3.CapiClusterFinalizer} - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - Expect(cl.Delete(ctx, capiCluster)).To(Succeed()) - - Eventually(func(g Gomega) { - _, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(cl.Get(ctx, client.ObjectKeyFromObject(capiCluster), capiCluster)).To(HaveOccurred()) - }).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster doesn't exist", func() { - capiCluster.Labels = map[string]string{ - importLabelName: "true", - testLabelName: testLabelVal, - } - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - }).Should(Succeed()) - - Eventually(ctx, func(g Gomega) { - g.Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), rancherCluster)).To(Succeed()) - g.Expect(rancherCluster.Labels).To(HaveKeyWithValue(testLabelName, testLabelVal)) - }).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster doesn't exist and annotation is set on the namespace", func() { - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - }).Should(Succeed()) - - Eventually(testEnv.GetAs(rancherCluster, &provisioningv1.Cluster{})).ShouldNot(BeNil()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists", func() { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte(sampleTemplate)) - })) - defer server.Close() - - capiCluster.Labels = map[string]string{ - testLabelName: testLabelVal, - } - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Expect(cl.Create(ctx, capiKubeconfigSecret)).To(Succeed()) - - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - cluster := rancherCluster.DeepCopy() - cluster.Status.ClusterName = clusterName - Expect(cl.Status().Update(ctx, cluster)).To(Succeed()) - - Expect(cl.Create(ctx, clusterRegistrationToken)).To(Succeed()) - token := clusterRegistrationToken.DeepCopy() - token.Status.ManifestURL = server.URL - Expect(cl.Status().Update(ctx, token)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - _, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - - objs, err := manifestToObjects(strings.NewReader(sampleTemplate)) - g.Expect(err).ToNot(HaveOccurred()) - - for _, obj := range objs { - u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) - g.Expect(err).ToNot(HaveOccurred()) - - unstructuredObj := &unstructured.Unstructured{} - unstructuredObj.SetUnstructuredContent(u) - unstructuredObj.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) - - g.Expect(cl.Get(ctx, client.ObjectKey{ - Namespace: unstructuredObj.GetNamespace(), - Name: unstructuredObj.GetName(), - }, unstructuredObj)).To(Succeed()) - } - - g.Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), rancherCluster)).To(Succeed()) - g.Expect(rancherCluster.Labels).To(HaveKeyWithValue(testLabelName, testLabelVal)) - }, 30*time.Second).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists but cluster name not set", func() { - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - }).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists and agent is deployed", func() { - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - cluster := rancherCluster.DeepCopy() - cluster.Status.AgentDeployed = true - Expect(cl.Status().Update(ctx, cluster)).To(Succeed()) - - _, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - Expect(err).ToNot(HaveOccurred()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists and registration manifests not exist", func() { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte("")) - })) - defer server.Close() - - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Expect(cl.Create(ctx, capiKubeconfigSecret)).To(Succeed()) - - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - cluster := rancherCluster.DeepCopy() - cluster.Status.ClusterName = clusterName - Expect(cl.Status().Update(ctx, cluster)).To(Succeed()) - - Expect(cl.Create(ctx, clusterRegistrationToken)).To(Succeed()) - token := clusterRegistrationToken.DeepCopy() - token.Status.ManifestURL = server.URL - Expect(cl.Status().Update(ctx, token)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - }).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists and a cluster registration token does not exist", func() { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte("")) - })) - defer server.Close() - - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Expect(cl.Create(ctx, capiKubeconfigSecret)).To(Succeed()) - - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - cluster := rancherCluster.DeepCopy() - cluster.Status.ClusterName = clusterName - Expect(cl.Status().Update(ctx, cluster)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - g.Expect(cl.Get(ctx, client.ObjectKeyFromObject(clusterRegistrationToken), clusterRegistrationToken)).ToNot(HaveOccurred()) - }).Should(Succeed()) - }) - - It("should reconcile a CAPI cluster when rancher cluster exists and registration manifests url is empty", func() { - Expect(cl.Create(ctx, capiCluster)).To(Succeed()) - capiCluster.Status.ControlPlaneReady = true - Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed()) - - Expect(cl.Create(ctx, capiKubeconfigSecret)).To(Succeed()) - - Expect(cl.Create(ctx, rancherCluster)).To(Succeed()) - cluster := rancherCluster.DeepCopy() - cluster.Status.ClusterName = clusterName - Expect(cl.Status().Update(ctx, cluster)).To(Succeed()) - - Expect(testEnv.Create(ctx, clusterRegistrationToken)).To(Succeed()) - - Eventually(ctx, func(g Gomega) { - res, err := r.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: capiCluster.Namespace, - Name: capiCluster.Name, - }, - }) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(res.Requeue).To(BeTrue()) - }).Should(Succeed()) - }) -}) diff --git a/internal/controllers/import_controller_v3_test.go b/internal/controllers/import_controller_v3_test.go index c9616ad0..a38b54f0 100644 --- a/internal/controllers/import_controller_v3_test.go +++ b/internal/controllers/import_controller_v3_test.go @@ -45,6 +45,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" ) +var ( + testLabelName = "test-label" + testLabelVal = "true" +) + var _ = Describe("reconcile CAPI Cluster", func() { var ( r *CAPIImportManagementV3Reconciler diff --git a/test/e2e/suites/embedded-capi-disabled/embedded_capi_disabled_test.go b/test/e2e/suites/embedded-capi-disabled/embedded_capi_disabled_test.go deleted file mode 100644 index f5514151..00000000 --- a/test/e2e/suites/embedded-capi-disabled/embedded_capi_disabled_test.go +++ /dev/null @@ -1,54 +0,0 @@ -//go:build e2e -// +build e2e - -/* -Copyright © 2023 - 2024 SUSE LLC - -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 embedded_capi_disabled - -import ( - . "github.com/onsi/ginkgo/v2" - . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" - - "k8s.io/utils/ptr" - - "github.com/rancher/turtles/test/e2e" - "github.com/rancher/turtles/test/e2e/specs" -) - -var _ = Describe("[AWS] [EKS] Create and delete CAPI cluster functionality should work with namespace auto-import (embedded capi disable from start)", Label(e2e.FullTestLabel), func() { - BeforeEach(func() { - SetClient(bootstrapClusterProxy.GetClient()) - SetContext(ctx) - }) - - specs.CreateUsingGitOpsSpec(ctx, func() specs.CreateUsingGitOpsSpecInput { - return specs.CreateUsingGitOpsSpecInput{ - E2EConfig: e2e.LoadE2EConfig(), - BootstrapClusterProxy: bootstrapClusterProxy, - ClusterTemplate: e2e.CAPIAwsEKSMMP, - ClusterName: "highlander-e2e-cluster1", - ControlPlaneMachineCount: ptr.To[int](1), - WorkerMachineCount: ptr.To[int](1), - GitAddr: gitAddress, - SkipDeletionTest: false, - LabelNamespace: true, - RancherServerURL: hostName, - CAPIClusterCreateWaitName: "wait-capa-create-cluster", - DeleteClusterWaitName: "wait-eks-delete", - } - }) -}) diff --git a/test/e2e/suites/embedded-capi-disabled/suite_test.go b/test/e2e/suites/embedded-capi-disabled/suite_test.go deleted file mode 100644 index c7a6cced..00000000 --- a/test/e2e/suites/embedded-capi-disabled/suite_test.go +++ /dev/null @@ -1,155 +0,0 @@ -//go:build e2e -// +build e2e - -/* -Copyright © 2023 - 2024 SUSE LLC - -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 embedded_capi_disabled - -import ( - "context" - "encoding/json" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/rancher/turtles/test/e2e" - "github.com/rancher/turtles/test/testenv" - capiframework "sigs.k8s.io/cluster-api/test/framework" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -// Test suite global vars. -var ( - // hostName is the host name for the Rancher Manager server. - hostName string - - ctx = context.Background() - - setupClusterResult *testenv.SetupTestClusterResult - bootstrapClusterProxy capiframework.ClusterProxy - gitAddress string -) - -func TestE2E(t *testing.T) { - RegisterFailHandler(Fail) - - ctrl.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - - RunSpecs(t, "rancher-turtles-e2e-embedded-capi-disabled") -} - -var _ = SynchronizedBeforeSuite( - func() []byte { - setupClusterResult = testenv.SetupTestCluster(ctx, testenv.SetupTestClusterInput{ - E2EConfig: e2e.LoadE2EConfig(), - Scheme: e2e.InitScheme(), - }) - - testenv.RancherDeployIngress(ctx, testenv.RancherDeployIngressInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CustomIngress: e2e.NginxIngress, - DefaultIngressClassPatch: e2e.IngressClassPatch, - }) - - // NOTE: deploy Rancher first with the embedded-cluster-api feature disabled. - // and the deploy Rancher Turtles. - rancherHookResult := testenv.DeployRancher(ctx, testenv.DeployRancherInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - RancherFeatures: "embedded-cluster-api=false", - RancherPatches: [][]byte{e2e.RancherSettingPatch}, - }) - - rtInput := testenv.DeployRancherTurtlesInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersYAML: e2e.CapiProviders, - AdditionalValues: map[string]string{ - "rancherTurtles.features.embedded-capi.disabled": "false", - "rancherTurtles.features.managementv3-cluster.enabled": "false", - }, - WaitForDeployments: testenv.DefaultDeployments, - } - - testenv.DeployRancherTurtles(ctx, rtInput) - - // NOTE: there are no short or local tests in this suite - By("Deploying additional infrastructure providers") - - testenv.CAPIOperatorDeployProvider(ctx, testenv.CAPIOperatorDeployProviderInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersSecretsYAML: [][]byte{ - e2e.AWSProviderSecret, - e2e.AzureIdentitySecret, - e2e.GCPProviderSecret, - }, - CAPIProvidersYAML: e2e.FullProviders, - WaitForDeployments: []testenv.NamespaceName{ - { - Name: "capa-controller-manager", - Namespace: "capa-system", - }, - { - Name: "capz-controller-manager", - Namespace: "capz-system", - }, - { - Name: "capg-controller-manager", - Namespace: "capg-system", - }, - }, - }) - - giteaResult := testenv.DeployGitea(ctx, testenv.DeployGiteaInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - ValuesFile: e2e.GiteaValues, - CustomIngressConfig: e2e.GiteaIngress, - }) - - data, err := json.Marshal(e2e.Setup{ - ClusterName: setupClusterResult.ClusterName, - KubeconfigPath: setupClusterResult.KubeconfigPath, - GitAddress: giteaResult.GitAddress, - RancherHostname: rancherHookResult.Hostname, - }) - Expect(err).ToNot(HaveOccurred()) - return data - }, - func(sharedData []byte) { - setup := e2e.Setup{} - Expect(json.Unmarshal(sharedData, &setup)).To(Succeed()) - - gitAddress = setup.GitAddress - hostName = setup.RancherHostname - - bootstrapClusterProxy = capiframework.NewClusterProxy(setup.ClusterName, setup.KubeconfigPath, e2e.InitScheme(), capiframework.WithMachineLogCollector(capiframework.DockerLogCollector{})) - Expect(bootstrapClusterProxy).ToNot(BeNil(), "cluster proxy should not be nil") - }, -) - -var _ = SynchronizedAfterSuite( - func() { - }, - func() { - testenv.UninstallGitea(ctx, testenv.UninstallGiteaInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - }) - - testenv.CleanupTestCluster(ctx, testenv.CleanupTestClusterInput{ - SetupTestClusterResult: *setupClusterResult, - }) - }, -) diff --git a/test/e2e/suites/import-gitops/import_gitops_test.go b/test/e2e/suites/import-gitops/import_gitops_test.go deleted file mode 100644 index 95489c1c..00000000 --- a/test/e2e/suites/import-gitops/import_gitops_test.go +++ /dev/null @@ -1,139 +0,0 @@ -//go:build e2e -// +build e2e - -/* -Copyright © 2023 - 2024 SUSE LLC - -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 import_gitops - -import ( - _ "embed" - - . "github.com/onsi/ginkgo/v2" - . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" - - "k8s.io/utils/ptr" - "sigs.k8s.io/controller-runtime/pkg/envtest/komega" - - "github.com/rancher/turtles/test/e2e" - "github.com/rancher/turtles/test/e2e/specs" -) - -var _ = Describe("[Docker] [Kubeadm] Create and delete CAPI cluster functionality should work with namespace auto-import", func() { - BeforeEach(func() { - SetClient(bootstrapClusterProxy.GetClient()) - SetContext(ctx) - }) - - specs.CreateUsingGitOpsSpec(ctx, func() specs.CreateUsingGitOpsSpecInput { - return specs.CreateUsingGitOpsSpecInput{ - E2EConfig: e2e.LoadE2EConfig(), - BootstrapClusterProxy: bootstrapClusterProxy, - ClusterTemplate: e2e.CAPIDockerKubeadm, - AdditionalTemplates: [][]byte{e2e.CAPIKindnet}, - ClusterName: "clusterv1-docker-kubeadm", - ControlPlaneMachineCount: ptr.To[int](1), - WorkerMachineCount: ptr.To[int](1), - GitAddr: gitAddress, - SkipDeletionTest: false, - LabelNamespace: true, - TestClusterReimport: true, - RancherServerURL: hostName, - CAPIClusterCreateWaitName: "wait-rancher", - DeleteClusterWaitName: "wait-controllers", - } - }) -}) - -var _ = Describe("[AWS] [EKS] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.FullTestLabel), func() { - BeforeEach(func() { - komega.SetClient(bootstrapClusterProxy.GetClient()) - komega.SetContext(ctx) - }) - - specs.CreateUsingGitOpsSpec(ctx, func() specs.CreateUsingGitOpsSpecInput { - return specs.CreateUsingGitOpsSpecInput{ - E2EConfig: e2e.LoadE2EConfig(), - BootstrapClusterProxy: bootstrapClusterProxy, - ClusterTemplate: e2e.CAPIAwsEKSMMP, - ClusterName: "clusterv1-eks", - ControlPlaneMachineCount: ptr.To[int](1), - WorkerMachineCount: ptr.To[int](1), - GitAddr: gitAddress, - SkipDeletionTest: false, - LabelNamespace: true, - RancherServerURL: hostName, - CAPIClusterCreateWaitName: "wait-capa-create-cluster", - DeleteClusterWaitName: "wait-eks-delete", - } - }) -}) - -var _ = Describe("[vSphere] [Kubeadm] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.LocalTestLabel), func() { - BeforeEach(func() { - SetClient(bootstrapClusterProxy.GetClient()) - SetContext(ctx) - }) - - specs.CreateUsingGitOpsSpec(ctx, func() specs.CreateUsingGitOpsSpecInput { - return specs.CreateUsingGitOpsSpecInput{ - E2EConfig: e2e.LoadE2EConfig(), - BootstrapClusterProxy: bootstrapClusterProxy, - ClusterTemplate: e2e.CAPIvSphereKubeadm, - ClusterName: "cluster-vsphere-kubeadm", - ControlPlaneMachineCount: ptr.To[int](1), - WorkerMachineCount: ptr.To[int](1), - GitAddr: gitAddress, - SkipDeletionTest: false, - LabelNamespace: true, - RancherServerURL: hostName, - CAPIClusterCreateWaitName: "wait-capv-create-cluster", - DeleteClusterWaitName: "wait-vsphere-delete", - AdditionalTemplateVariables: map[string]string{ - "NAMESPACE": "default", - "VIP_NETWORK_INTERFACE": "", - }, - } - }) -}) - -var _ = Describe("[vSphere] [RKE2] Create and delete CAPI cluster functionality should work with namespace auto-import", Label(e2e.LocalTestLabel), func() { - BeforeEach(func() { - SetClient(bootstrapClusterProxy.GetClient()) - SetContext(ctx) - }) - - specs.CreateUsingGitOpsSpec(ctx, func() specs.CreateUsingGitOpsSpecInput { - return specs.CreateUsingGitOpsSpecInput{ - E2EConfig: e2e.LoadE2EConfig(), - BootstrapClusterProxy: bootstrapClusterProxy, - ClusterTemplate: e2e.CAPIvSphereRKE2, - ClusterName: "cluster-vsphere-rke2", - ControlPlaneMachineCount: ptr.To[int](1), - WorkerMachineCount: ptr.To[int](1), - GitAddr: gitAddress, - SkipDeletionTest: false, - LabelNamespace: true, - RancherServerURL: hostName, - CAPIClusterCreateWaitName: "wait-capv-create-cluster", - DeleteClusterWaitName: "wait-vsphere-delete", - AdditionalTemplateVariables: map[string]string{ - "NAMESPACE": "default", - "VIP_NETWORK_INTERFACE": "", - }, - } - }) -}) diff --git a/test/e2e/suites/import-gitops/suite_test.go b/test/e2e/suites/import-gitops/suite_test.go deleted file mode 100644 index ed40b246..00000000 --- a/test/e2e/suites/import-gitops/suite_test.go +++ /dev/null @@ -1,193 +0,0 @@ -//go:build e2e -// +build e2e - -/* -Copyright © 2023 - 2024 SUSE LLC - -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 import_gitops - -import ( - "context" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/rancher/turtles/test/e2e" - "github.com/rancher/turtles/test/testenv" - "k8s.io/apimachinery/pkg/util/json" - capiframework "sigs.k8s.io/cluster-api/test/framework" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -// Test suite global vars. -var ( - // hostName is the host name for the Rancher Manager server. - hostName string - - ctx = context.Background() - - setupClusterResult *testenv.SetupTestClusterResult - bootstrapClusterProxy capiframework.ClusterProxy - gitAddress string -) - -func TestE2E(t *testing.T) { - RegisterFailHandler(Fail) - - ctrl.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - - RunSpecs(t, "rancher-turtles-e2e-import-gitops") -} - -var _ = SynchronizedBeforeSuite( - func() []byte { - setupClusterResult = testenv.SetupTestCluster(ctx, testenv.SetupTestClusterInput{ - E2EConfig: e2e.LoadE2EConfig(), - Scheme: e2e.InitScheme(), - }) - - testenv.RancherDeployIngress(ctx, testenv.RancherDeployIngressInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CustomIngress: e2e.NginxIngress, - DefaultIngressClassPatch: e2e.IngressClassPatch, - }) - - rancherHookResult := testenv.DeployRancher(ctx, testenv.DeployRancherInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - RancherPatches: [][]byte{e2e.RancherSettingPatch}, - }) - - if shortTestOnly() { - testenv.DeployChartMuseum(ctx, testenv.DeployChartMuseumInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - }) - - testenv.DeployRancherTurtles(ctx, testenv.DeployRancherTurtlesInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersYAML: e2e.CapiProviders, - WaitForDeployments: append( - testenv.DefaultDeployments, - testenv.NamespaceName{ - Name: "caapf-controller-manager", - Namespace: e2e.RancherTurtlesNamespace, - }, - ), - }) - } else { - testenv.DeployRancherTurtles(ctx, testenv.DeployRancherTurtlesInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersYAML: e2e.CapiProviders, - WaitForDeployments: testenv.DefaultDeployments, - }) - } - - if !shortTestOnly() && !localTestOnly() { - By("Running full tests, deploying additional infrastructure providers") - - testenv.CAPIOperatorDeployProvider(ctx, testenv.CAPIOperatorDeployProviderInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersSecretsYAML: [][]byte{ - e2e.AWSProviderSecret, - e2e.AzureIdentitySecret, - e2e.GCPProviderSecret, - }, - CAPIProvidersYAML: e2e.FullProviders, - WaitForDeployments: []testenv.NamespaceName{ - { - Name: "capa-controller-manager", - Namespace: "capa-system", - }, - { - Name: "capz-controller-manager", - Namespace: "capz-system", - }, - { - Name: "capg-controller-manager", - Namespace: "capg-system", - }, - }, - }) - } else if Label(e2e.LocalTestLabel).MatchesLabelFilter(GinkgoLabelFilter()) { - By("Running local vSphere tests, deploying vSphere infrastructure provider") - - testenv.CAPIOperatorDeployProvider(ctx, testenv.CAPIOperatorDeployProviderInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - CAPIProvidersSecretsYAML: [][]byte{ - e2e.VSphereProviderSecret, - }, - CAPIProvidersYAML: e2e.CapvProvider, - WaitForDeployments: []testenv.NamespaceName{ - { - Name: "capv-controller-manager", - Namespace: "capv-system", - }, - }, - }) - } - - giteaResult := testenv.DeployGitea(ctx, testenv.DeployGiteaInput{ - BootstrapClusterProxy: setupClusterResult.BootstrapClusterProxy, - ValuesFile: e2e.GiteaValues, - CustomIngressConfig: e2e.GiteaIngress, - }) - - data, err := json.Marshal(e2e.Setup{ - ClusterName: setupClusterResult.ClusterName, - KubeconfigPath: setupClusterResult.KubeconfigPath, - GitAddress: giteaResult.GitAddress, - RancherHostname: rancherHookResult.Hostname, - }) - Expect(err).ToNot(HaveOccurred()) - return data - }, - func(sharedData []byte) { - setup := e2e.Setup{} - Expect(json.Unmarshal(sharedData, &setup)).To(Succeed()) - - gitAddress = setup.GitAddress - hostName = setup.RancherHostname - - bootstrapClusterProxy = capiframework.NewClusterProxy(setup.ClusterName, setup.KubeconfigPath, e2e.InitScheme(), capiframework.WithMachineLogCollector(capiframework.DockerLogCollector{})) - Expect(bootstrapClusterProxy).ToNot(BeNil(), "cluster proxy should not be nil") - }, -) - -var _ = SynchronizedAfterSuite( - func() { - }, - func() { - testenv.UninstallGitea(ctx, testenv.UninstallGiteaInput{ - BootstrapClusterProxy: bootstrapClusterProxy, - }) - - testenv.UninstallRancherTurtles(ctx, testenv.UninstallRancherTurtlesInput{ - BootstrapClusterProxy: bootstrapClusterProxy, - }) - - testenv.CleanupTestCluster(ctx, testenv.CleanupTestClusterInput{ - SetupTestClusterResult: *setupClusterResult, - }) - }, -) - -func shortTestOnly() bool { - return GinkgoLabelFilter() == e2e.ShortTestLabel -} - -func localTestOnly() bool { - return GinkgoLabelFilter() == e2e.LocalTestLabel -}