@@ -256,14 +256,6 @@ func NewComponents(input ComponentsInput) (Components, error) {
256
256
return nil , errors .Wrap (err , "failed to set the TargetNamespace on the components" )
257
257
}
258
258
259
- // ensures all the ClusterRole and ClusterRoleBinding have the name prefixed with the namespace name and that
260
- // all the clusterRole/clusterRoleBinding namespaced subjects refers to targetNamespace
261
- // Nb. Making all the RBAC rules "namespaced" is required for supporting multi-tenancy
262
- objs , err = fixRBAC (objs , input .Options .TargetNamespace )
263
- if err != nil {
264
- return nil , errors .Wrap (err , "failed to fix ClusterRoleBinding names" )
265
- }
266
-
267
259
// Add common labels.
268
260
objs = addCommonLabels (objs , input .Provider )
269
261
@@ -336,21 +328,59 @@ func fixTargetNamespace(objs []unstructured.Unstructured, targetNamespace string
336
328
o .SetNamespace (targetNamespace )
337
329
}
338
330
339
- if o .GetKind () == mutatingWebhookConfigurationKind || o .GetKind () == validatingWebhookConfigurationKind || o .GetKind () == customResourceDefinitionKind {
331
+ switch o .GetKind () {
332
+ case clusterRoleBindingKind :
333
+ // Convert Unstructured into a typed object
334
+ binding := & rbacv1.ClusterRoleBinding {}
335
+ if err := scheme .Scheme .Convert (& o , binding , nil ); err != nil {
336
+ return nil , err
337
+ }
338
+
339
+ // ensure that namespaced subjects refers to targetNamespace
340
+ for s := range binding .Subjects {
341
+ if binding .Subjects [s ].Namespace != "" {
342
+ binding .Subjects [s ].Namespace = targetNamespace
343
+ }
344
+ }
345
+
346
+ // Convert ClusterRoleBinding back to Unstructured
347
+ if err := scheme .Scheme .Convert (binding , & o , nil ); err != nil {
348
+ return nil , err
349
+ }
350
+
351
+ case roleBindingKind :
352
+ binding := & rbacv1.RoleBinding {}
353
+ if err := scheme .Scheme .Convert (& o , binding , nil ); err != nil {
354
+ return nil , err
355
+ }
356
+
357
+ // ensure that namespaced subjects refers to targetNamespace
358
+ for k := range binding .Subjects {
359
+ if binding .Subjects [k ].Namespace != "" {
360
+ binding .Subjects [k ].Namespace = targetNamespace
361
+ }
362
+ }
363
+
364
+ // Convert RoleBinding back to Unstructured
365
+ if err := scheme .Scheme .Convert (binding , & o , nil ); err != nil {
366
+ return nil , err
367
+ }
368
+
369
+ case mutatingWebhookConfigurationKind , validatingWebhookConfigurationKind , customResourceDefinitionKind :
340
370
var err error
341
371
o , err = fixWebhookNamespaceReferences (o , targetNamespace )
342
372
if err != nil {
343
373
return nil , err
344
374
}
345
- }
346
375
347
- if o . GetKind () == certificateKind {
376
+ case certificateKind :
348
377
var err error
349
378
o , err = fixCertificate (o , originalNamespace , targetNamespace )
350
379
if err != nil {
351
380
return nil , err
352
381
}
353
382
}
383
+
354
384
objs [i ] = o
355
385
}
356
386
return objs , nil
@@ -493,78 +523,6 @@ func fixCertificate(o unstructured.Unstructured, originalNamespace, targetNamesp
493
523
return o , nil
494
524
}
495
525
496
- // fixRBAC ensures all the ClusterRole and ClusterRoleBinding have the name prefixed with the namespace name and that
497
- // all the clusterRole/clusterRoleBinding namespaced subjects refers to targetNamespace.
498
- func fixRBAC (objs []unstructured.Unstructured , targetNamespace string ) ([]unstructured.Unstructured , error ) {
499
- renamedClusterRoles := map [string ]string {}
500
- for _ , o := range objs {
501
- // if the object has Kind ClusterRole
502
- if o .GetKind () == clusterRoleKind {
503
- // assign a namespaced name
504
- currentName := o .GetName ()
505
- newName := fmt .Sprintf ("%s-%s" , targetNamespace , currentName )
506
- o .SetName (newName )
507
-
508
- renamedClusterRoles [currentName ] = newName
509
- }
510
- }
511
-
512
- for i := range objs {
513
- o := objs [i ]
514
- switch o .GetKind () {
515
- case clusterRoleBindingKind : // if the object has Kind ClusterRoleBinding
516
- // Convert Unstructured into a typed object
517
- b := & rbacv1.ClusterRoleBinding {}
518
- if err := scheme .Scheme .Convert (& o , b , nil ); err != nil {
519
- return nil , err
520
- }
521
-
522
- // assign a namespaced name
523
- b .Name = fmt .Sprintf ("%s-%s" , targetNamespace , b .Name )
524
-
525
- // ensure that namespaced subjects refers to targetNamespace
526
- for k := range b .Subjects {
527
- if b .Subjects [k ].Namespace != "" {
528
- b .Subjects [k ].Namespace = targetNamespace
529
- }
530
- }
531
-
532
- // if the referenced ClusterRole was renamed, change the RoleRef
533
- if newName , ok := renamedClusterRoles [b .RoleRef .Name ]; ok {
534
- b .RoleRef .Name = newName
535
- }
536
-
537
- // Convert ClusterRoleBinding back to Unstructured
538
- if err := scheme .Scheme .Convert (b , & o , nil ); err != nil {
539
- return nil , err
540
- }
541
- objs [i ] = o
542
-
543
- case roleBindingKind : // if the object has Kind RoleBinding
544
- // Convert Unstructured into a typed object
545
- b := & rbacv1.RoleBinding {}
546
- if err := scheme .Scheme .Convert (& o , b , nil ); err != nil {
547
- return nil , err
548
- }
549
-
550
- // ensure that namespaced subjects refers to targetNamespace
551
- for k := range b .Subjects {
552
- if b .Subjects [k ].Namespace != "" {
553
- b .Subjects [k ].Namespace = targetNamespace
554
- }
555
- }
556
-
557
- // Convert RoleBinding back to Unstructured
558
- if err := scheme .Scheme .Convert (b , & o , nil ); err != nil {
559
- return nil , err
560
- }
561
- objs [i ] = o
562
- }
563
- }
564
-
565
- return objs , nil
566
- }
567
-
568
526
// addCommonLabels ensures all the provider components have a consistent set of labels.
569
527
func addCommonLabels (objs []unstructured.Unstructured , provider config.Provider ) []unstructured.Unstructured {
570
528
for _ , o := range objs {
0 commit comments