Skip to content

Commit

Permalink
Changing the logic for adding clusterrolelabel
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Feb 14, 2024
1 parent ef55b59 commit 8aa8eb8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion controllers/toolchaincluster/toolchaincluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 19 in controllers/toolchaincluster/toolchaincluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchaincluster/toolchaincluster_controller.go#L19

Added line #L19 was not covered by tests
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,

Check warning on line 26 in controllers/toolchaincluster/toolchaincluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchaincluster/toolchaincluster_controller.go#L26

Added line #L26 was not covered by tests
}
}

Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 8aa8eb8

Please sign in to comment.