Skip to content

Commit

Permalink
Reconcile clusters on tenant change
Browse files Browse the repository at this point in the history
Fixes changes to the cluster template not being picked up.
  • Loading branch information
bastjan committed Jul 31, 2024
1 parent dfd7675 commit d052a09
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -82,10 +83,41 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, request ctrl.Request)
func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&synv1alpha1.Cluster{}).
Watches(&synv1alpha1.Tenant{}, handler.EnqueueRequestsFromMapFunc(enqueueClustersForTenantMapFunc(mgr.GetClient()))).
Owns(&synv1alpha1.GitRepo{}).
Owns(&corev1.ServiceAccount{}).
Owns(&corev1.Secret{}).
Owns(&rbacv1.Role{}).
Owns(&rbacv1.RoleBinding{}).
Complete(r)
}

// enqueueClustersForTenantMapFunc returns a function that lists all clusters for a tenant and returns a list of reconcile requests for them.
// It does select clusters by the synv1alpha1.LabelNameTenant label.
func enqueueClustersForTenantMapFunc(cli client.Client) func(ctx context.Context, o client.Object) []reconcile.Request {
return func(ctx context.Context, o client.Object) []reconcile.Request {
l := log.FromContext(ctx).WithName("enqueueClustersForTenantMapFunc")

var clusters synv1alpha1.ClusterList
err := cli.List(ctx, &clusters,
client.InNamespace(o.GetNamespace()),
client.MatchingLabels{
synv1alpha1.LabelNameTenant: o.GetName(),
})
if err != nil {
l.Error(err, "Failed to list clusters")
return []reconcile.Request{}
}

requests := make([]reconcile.Request, len(clusters.Items))
for i, tenant := range clusters.Items {
requests[i] = reconcile.Request{
NamespacedName: client.ObjectKey{
Namespace: tenant.Namespace,
Name: tenant.Name,
},
}
}
return requests
}
}

0 comments on commit d052a09

Please sign in to comment.