From 3a14fb8942e19e6576417faa03647bc2eeb2dddc Mon Sep 17 00:00:00 2001 From: Danil Grigorev Date: Thu, 16 Nov 2023 10:57:36 +0100 Subject: [PATCH] Implement Cluster and CRT creation in a designated namespace Signed-off-by: Danil Grigorev --- .../templates/rancher-turtles-components.yaml | 1 + config/rbac/role.yaml | 1 + internal/controllers/import_controller.go | 20 +++++++++++-------- .../management/v3/clusterregistrationtoken.go | 7 +++++++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/charts/rancher-turtles/templates/rancher-turtles-components.yaml b/charts/rancher-turtles/templates/rancher-turtles-components.yaml index e8ee40a19..93645521f 100644 --- a/charts/rancher-turtles/templates/rancher-turtles-components.yaml +++ b/charts/rancher-turtles/templates/rancher-turtles-components.yaml @@ -226,6 +226,7 @@ rules: - get - list - watch + - create - apiGroups: - provisioning.cattle.io resources: diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 367aa310a..0792eed1e 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -157,6 +157,7 @@ rules: - get - list - watch + - create - apiGroups: - provisioning.cattle.io resources: diff --git a/internal/controllers/import_controller.go b/internal/controllers/import_controller.go index 3adcc7987..7a533e156 100644 --- a/internal/controllers/import_controller.go +++ b/internal/controllers/import_controller.go @@ -62,8 +62,7 @@ const ( importLabelName = "cluster-api.cattle.io/rancher-auto-import" ownedLabelName = "cluster-api.cattle.io/owned" - defaultRequeueDuration = 1 * time.Minute - clusterRegistrationTokenName = "default-token" + defaultRequeueDuration = 1 * time.Minute ) // CAPIImportReconciler represents a reconciler for importing CAPI clusters in Rancher. @@ -272,7 +271,7 @@ func (r *CAPIImportReconciler) reconcileNormal(ctx context.Context, capiCluster } // get the registration manifest - manifest, err := r.getClusterRegistrationManifest(ctx, rancherCluster.Status.ClusterName) + manifest, err := r.getClusterRegistrationManifest(ctx, rancherCluster.Status.ClusterName, capiCluster.Namespace) if err != nil { return ctrl.Result{}, err } @@ -318,13 +317,18 @@ func (r *CAPIImportReconciler) reconcileDelete(ctx context.Context, capiCluster return ctrl.Result{}, nil } -func (r *CAPIImportReconciler) getClusterRegistrationManifest(ctx context.Context, clusterName string) (string, error) { +func (r *CAPIImportReconciler) getClusterRegistrationManifest(ctx context.Context, clusterName, namespace string) (string, error) { log := log.FromContext(ctx) - token := &managementv3.ClusterRegistrationToken{ObjectMeta: metav1.ObjectMeta{ - Name: clusterRegistrationTokenName, - Namespace: clusterName, - }} + token := &managementv3.ClusterRegistrationToken{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterName, + Namespace: namespace, + }, + Spec: managementv3.ClusterRegistrationTokenSpec{ + ClusterName: clusterName, + }, + } err := r.RancherClient.Get(ctx, client.ObjectKeyFromObject(token), token) if client.IgnoreNotFound(err) != nil { diff --git a/internal/rancher/management/v3/clusterregistrationtoken.go b/internal/rancher/management/v3/clusterregistrationtoken.go index ce5177d02..9db80257d 100644 --- a/internal/rancher/management/v3/clusterregistrationtoken.go +++ b/internal/rancher/management/v3/clusterregistrationtoken.go @@ -11,9 +11,16 @@ type ClusterRegistrationToken struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` + Spec ClusterRegistrationTokenSpec `json:"spec"` + Status ClusterRegistrationTokenStatus `json:"status,omitempty"` } +// ClusterRegistrationTokenSpec is the struct representing the spec of a Rancher ClusterRegistrationToken. +type ClusterRegistrationTokenSpec struct { + ClusterName string `json:"clusterName"` +} + // ClusterRegistrationTokenStatus is the struct representing the status of a Rancher ClusterRegistrationToken. type ClusterRegistrationTokenStatus struct { ManifestURL string `json:"manifestUrl"`