From 8aa8eb8cfe1de30bc455671df6e21ca5b47d6f27 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Wed, 14 Feb 2024 17:00:24 +0530 Subject: [PATCH] Changing the logic for adding clusterrolelabel Signed-off-by: Feny Mehta --- .../toolchaincluster_controller.go | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/controllers/toolchaincluster/toolchaincluster_controller.go b/controllers/toolchaincluster/toolchaincluster_controller.go index 4be49b6f..3f7d3a97 100644 --- a/controllers/toolchaincluster/toolchaincluster_controller.go +++ b/controllers/toolchaincluster/toolchaincluster_controller.go @@ -16,13 +16,14 @@ import ( ) // NewReconciler returns a new Reconciler -func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration) *Reconciler { +func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration, useClusterRL bool) *Reconciler { cacheLog := log.Log.WithName("toolchaincluster_cache") clusterCacheService := cluster.NewToolchainClusterService(mgr.GetClient(), cacheLog, namespace, timeout) return &Reconciler{ client: mgr.GetClient(), scheme: mgr.GetScheme(), clusterCacheService: clusterCacheService, + useClusterRL: useClusterRL, } } @@ -38,6 +39,7 @@ type Reconciler struct { client client.Client scheme *runtime.Scheme clusterCacheService cluster.ToolchainClusterService + useClusterRL bool } // Reconcile reads that state of the cluster for a ToolchainCluster object and makes changes based on the state read @@ -61,5 +63,29 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. return reconcile.Result{}, err } + // add toolchaincluster role label if not present + reqLogger.Info("adding cluster role label based on type") + if err := r.addToolchainClusterRoleLabelFromType(ctx, toolchainCluster); err != nil { + return reconcile.Result{}, err + } return reconcile.Result{}, r.clusterCacheService.AddOrUpdateToolchainCluster(toolchainCluster) } + +func (r *Reconciler) addToolchainClusterRoleLabelFromType(ctx context.Context, toolchainCluster *toolchainv1alpha1.ToolchainCluster) error { + logger := log.FromContext(ctx) + + if !r.useClusterRL { + logger.Info("Skiping the cluster role label setting as not running in host cluster") + return nil + } + clusterRoleLabel := cluster.RoleLabel(cluster.Tenant) + if _, exists := toolchainCluster.Labels[clusterRoleLabel]; !exists { + logger.Info("setting cluster role label for toolchaincluster", clusterRoleLabel, toolchainCluster.Name) + // We use only the label key, the value can remain empty. + toolchainCluster.Labels[clusterRoleLabel] = "" + if err := r.client.Update(ctx, toolchainCluster); err != nil { + return err + } + } + return nil +}