Skip to content

Commit

Permalink
test: add e2e test suite for management.cattle.io controller
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Feb 1, 2024
1 parent e9ee1aa commit b5ad235
Show file tree
Hide file tree
Showing 16 changed files with 1,022 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ issues:
linters:
- testpackage
- gochecknoglobals
- path: internal/controllers/import_controller(_v3)?\.go
linters:
- dupl
- text: var-naming
linters:
- revive
Expand Down
6 changes: 4 additions & 2 deletions internal/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ import (
)

const (
importLabelName = "cluster-api.cattle.io/rancher-auto-import"
ownedLabelName = "cluster-api.cattle.io/owned"
importLabelName = "cluster-api.cattle.io/rancher-auto-import"
ownedLabelName = "cluster-api.cattle.io/owned"
capiClusterOwner = "cluster-api.cattle.io/capi-cluster-owner"
capiClusterOwnerNamespace = "cluster-api.cattle.io/capi-cluster-owner-ns"

defaultRequeueDuration = 1 * time.Minute
)
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/import_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {

capiCluster = &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Name: fmt.Sprintf("test-cluster-%s", testObjectsSuffix),
Namespace: testNamespace,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (

managementv3 "github.com/rancher-sandbox/rancher-turtles/internal/rancher/management/v3"
"github.com/rancher-sandbox/rancher-turtles/util"
turtlesnaming "github.com/rancher-sandbox/rancher-turtles/util/naming"
turtlespredicates "github.com/rancher-sandbox/rancher-turtles/util/predicates"
)

Expand Down Expand Up @@ -85,7 +84,6 @@ func (r *CAPIImportManagementV3Reconciler) SetupWithManager(ctx context.Context,
}

// Watch Rancher managementv3 clusters
// NOTE: we will import the types from rancher in the future
err = c.Watch(
source.Kind(mgr.GetCache(), &managementv3.Cluster{}),
handler.EnqueueRequestsFromMapFunc(rancherClusterToCapiCluster(ctx, capiPredicates, r.RancherClient)),
Expand Down Expand Up @@ -175,18 +173,48 @@ func (r *CAPIImportManagementV3Reconciler) Reconcile(ctx context.Context, req ct
func (r *CAPIImportManagementV3Reconciler) reconcile(ctx context.Context, capiCluster *clusterv1.Cluster) (ctrl.Result, error) {
log := log.FromContext(ctx)

// fetch the rancher cluster
rancherCluster := &managementv3.Cluster{ObjectMeta: metav1.ObjectMeta{
Namespace: capiCluster.Namespace,
Name: turtlesnaming.Name(capiCluster.Name).ToRancherName(),
}}
// placeholder name/namespace must be set to be able to test this but it won't be used.
rancherCluster := &managementv3.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "c-",
Namespace: capiCluster.Namespace,
Labels: map[string]string{
capiClusterOwner: capiCluster.Name,
capiClusterOwnerNamespace: capiCluster.Namespace,
},
},
}

rancherClusterList := &managementv3.ClusterList{}
selectors := []client.ListOption{
client.MatchingLabels{
capiClusterOwner: capiCluster.Name,
capiClusterOwnerNamespace: capiCluster.Namespace,
ownedLabelName: "",
},
}
err := r.RancherClient.List(ctx, rancherClusterList, selectors...)

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 len(rancherClusterList.Items) != 0 {
if len(rancherClusterList.Items) > 1 {
log.Info("More than one rancher cluster found. Will default to using the first one.")
}

rancherCluster = &rancherClusterList.Items[0]
}

if !capiCluster.ObjectMeta.DeletionTimestamp.IsZero() {
err := r.deleteDependentRancherCluster(ctx, capiCluster)
if err != nil {
return ctrl.Result{Requeue: true}, fmt.Errorf("error deleting associated managementv3.Cluster resources: %w", err)
}
}

if !rancherCluster.ObjectMeta.DeletionTimestamp.IsZero() {
return reconcileDelete(ctx, capiCluster)
}
Expand All @@ -213,21 +241,17 @@ func (r *CAPIImportManagementV3Reconciler) reconcileNormal(ctx context.Context,

if err := r.RancherClient.Create(ctx, &managementv3.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,
}},
Namespace: capiCluster.Namespace,
GenerateName: "c-",
Labels: map[string]string{
ownedLabelName: "",
capiClusterOwner: capiCluster.Name,
capiClusterOwnerNamespace: capiCluster.Namespace,
ownedLabelName: "",
},
},
Spec: managementv3.ClusterSpec{
DisplayName: fmt.Sprintf("c-displayname-%s", capiCluster.Name),
Description: "c-description",
DisplayName: capiCluster.Name,
Description: "CAPI cluster automatically imported to Rancher",
},
}); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating rancher cluster: %w", err)
Expand All @@ -242,20 +266,13 @@ func (r *CAPIImportManagementV3Reconciler) reconcileNormal(ctx context.Context,
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 {
if managementv3.ClusterConditionAgentDeployed.IsTrue(rancherCluster) {
log.Info("agent already deployed, no action needed")
return ctrl.Result{}, nil
}

// get the registration manifest
manifest, err := getClusterRegistrationManifest(ctx, rancherCluster.Status.ClusterName, capiCluster.Namespace, r.RancherClient, r.InsecureSkipVerify)
manifest, err := getClusterRegistrationManifest(ctx, rancherCluster.Name, rancherCluster.Name, r.RancherClient, r.InsecureSkipVerify)
if err != nil {
return ctrl.Result{}, err
}
Expand All @@ -280,3 +297,30 @@ func (r *CAPIImportManagementV3Reconciler) reconcileNormal(ctx context.Context,

return ctrl.Result{}, nil
}

func (r *CAPIImportManagementV3Reconciler) deleteDependentRancherCluster(ctx context.Context, capiCluster *clusterv1.Cluster) error {
log := log.FromContext(ctx)

rancherClusters := &managementv3.ClusterList{}
selectors := []client.ListOption{
client.MatchingLabels{
capiClusterOwner: capiCluster.Name,
capiClusterOwnerNamespace: capiCluster.Namespace,
},
}

err := r.RancherClient.List(ctx, rancherClusters, selectors...)
if err != nil {
log.Error(err, fmt.Sprintf("Unable to fetch rancher cluster owned by capi cluster %s/%s for deletion", capiCluster.Namespace, capiCluster.Name))
}

for i := range rancherClusters.Items {
err := r.RancherClient.Delete(ctx, &rancherClusters.Items[i])
if err != nil {
log.Error(err, fmt.Sprintf("Unable to delete dependent managementv3.Cluster resource %s", client.ObjectKeyFromObject(&rancherClusters.Items[i])))
return err
}
}

return nil
}
Loading

0 comments on commit b5ad235

Please sign in to comment.