Skip to content

Commit 51cbfa7

Browse files
authored
Perform the RBAC propagation in parallel across namespaces (#343)
* perform the RBAC propagation in parallel across namespaces Signed-off-by: YuChen <[email protected]> * perform roles copied for SA in parallel in single namespace Signed-off-by: YuChen <[email protected]> --------- Signed-off-by: YuChen <[email protected]>
1 parent e0a26cf commit 51cbfa7

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

controllers/namespacescope_controller.go

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"reflect"
2525
"regexp"
2626
"strings"
27+
"sync"
2728
"time"
2829

2930
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -283,14 +284,32 @@ func (r *NamespaceScopeReconciler) PushRbacToNamespace(ctx context.Context, inst
283284
return err
284285
}
285286

287+
var wg sync.WaitGroup
288+
errorChannel := make(chan error, len(instance.Status.ValidatedMembers))
289+
286290
for _, toNs := range instance.Status.ValidatedMembers {
287291
if toNs == operatorNs {
288292
continue
289293
}
290-
if err := r.generateRBACToNamespace(ctx, instance, saNames, fromNs, toNs); err != nil {
291-
return err
292-
}
294+
295+
wg.Add(1)
296+
go func(toNs string) {
297+
defer wg.Done()
298+
if err := r.generateRBACToNamespace(ctx, instance, saNames, fromNs, toNs); err != nil {
299+
errorChannel <- err
300+
}
301+
}(toNs)
302+
}
303+
304+
// Wait for all RBAC generation to finish
305+
wg.Wait()
306+
close(errorChannel)
307+
308+
// Return the first error encountered, if any
309+
if len(errorChannel) > 0 {
310+
return <-errorChannel
293311
}
312+
294313
return nil
295314
}
296315

@@ -470,28 +489,46 @@ func (r *NamespaceScopeReconciler) generateRBACToNamespace(ctx context.Context,
470489
"app.kubernetes.io/managed-by": "ibm-namespace-scope-operator",
471490
"app.kubernetes.io/name": instance.Spec.ConfigmapName,
472491
}
492+
493+
var wg sync.WaitGroup
494+
errorChannel := make(chan error, len(saNames))
495+
473496
for _, sa := range saNames {
474-
roleList, err := r.GetRolesFromServiceAccount(ctx, sa, fromNs)
497+
wg.Add(1)
475498

476-
klog.V(2).Infof("Roles waiting to be copied: %v", roleList)
499+
go func(sa string) {
500+
defer wg.Done()
477501

478-
if err != nil {
479-
return err
480-
}
502+
roleList, err := r.GetRolesFromServiceAccount(ctx, sa, fromNs)
503+
if err != nil {
504+
errorChannel <- err
505+
}
481506

482-
if err := r.CreateRole(ctx, roleList, labels, sa, fromNs, toNs); err != nil {
483-
if errors.IsForbidden(err) {
484-
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot create resource roles in API group rbac.authorization.k8s.io in the namespace %s. Please authorize service account ibm-namespace-scope-operator namespace admin permission of %s namespace", toNs, toNs)
507+
klog.V(2).Infof("Roles waiting to be copied for SA %s: %v", sa, roleList)
508+
509+
if err := r.CreateRole(ctx, roleList, labels, sa, fromNs, toNs); err != nil {
510+
if errors.IsForbidden(err) {
511+
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot create resource roles in API group rbac.authorization.k8s.io in the namespace %s. Please authorize service account ibm-namespace-scope-operator namespace admin permission of %s namespace", toNs, toNs)
512+
}
513+
errorChannel <- err
485514
}
486-
return err
487-
}
488-
if err := r.CreateRoleBinding(ctx, roleList, labels, sa, fromNs, toNs); err != nil {
489-
if errors.IsForbidden(err) {
490-
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot create resource rolebindings in API group rbac.authorization.k8s.io in the namespace %s. Please authorize service account ibm-namespace-scope-operator namespace admin permission of %s namespace", toNs, toNs)
515+
516+
if err := r.CreateRoleBinding(ctx, roleList, labels, sa, fromNs, toNs); err != nil {
517+
if errors.IsForbidden(err) {
518+
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot create resource rolebindings in API group rbac.authorization.k8s.io in the namespace %s. Please authorize service account ibm-namespace-scope-operator namespace admin permission of %s namespace", toNs, toNs)
519+
}
520+
errorChannel <- err
491521
}
492-
return err
493-
}
522+
}(sa)
494523
}
524+
525+
wg.Wait()
526+
close(errorChannel)
527+
528+
if len(errorChannel) > 0 {
529+
return <-errorChannel
530+
}
531+
495532
return nil
496533
}
497534

0 commit comments

Comments
 (0)