Skip to content

Commit

Permalink
Implement Cluster and CRT creation in a designated namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Nov 16, 2023
1 parent b14a296 commit 0146b57
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions charts/rancher-turtles/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
- args:
- --leader-elect
- --feature-gates=rancher-kube-secret-patch={{ index .Values "rancherTurtles" "features" "rancher-kubeconfigs" "label"}}
- --namespace={{ .Values.rancherTurtles.namespace }}
{{- range .Values.rancherTurtles.managerArguments }}
- {{ . }}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ rules:
- get
- list
- watch
- create
- apiGroups:
- provisioning.cattle.io
resources:
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ rules:
- get
- list
- watch
- create
- apiGroups:
- provisioning.cattle.io
resources:
Expand Down
23 changes: 15 additions & 8 deletions internal/controllers/import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -318,17 +317,25 @@ 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 {
return "", fmt.Errorf("error getting registration token for cluster %s: %w", clusterName, err)
} else if err != nil {
if err := r.RancherClient.Create(ctx, token); err != nil {
return "", fmt.Errorf("failed to create cluster registration token for cluster %s: %w", clusterName, err)
}
}

if token.Status.ManifestURL == "" {
Expand Down
7 changes: 7 additions & 0 deletions internal/rancher/management/v3/clusterregistrationtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

// ClusterRegistrationTokenStatus is the struct representing the status 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"`
Expand Down

0 comments on commit 0146b57

Please sign in to comment.