@@ -10,11 +10,11 @@ import (
10
10
"time"
11
11
12
12
openshiftcrypto "github.com/openshift/library-go/pkg/crypto"
13
+ "github.com/openshift/library-go/pkg/operator/resource/resourceapply"
13
14
"github.com/pkg/errors"
14
15
certificatesv1 "k8s.io/api/certificates/v1"
15
16
corev1 "k8s.io/api/core/v1"
16
17
rbacv1 "k8s.io/api/rbac/v1"
17
- apierrors "k8s.io/apimachinery/pkg/api/errors"
18
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
19
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
20
20
"k8s.io/client-go/kubernetes"
@@ -357,7 +357,7 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
357
357
continue
358
358
}
359
359
360
- err := a .createKubeClientPermissions (template . Name , kcrc , cluster , addon )
360
+ err := a .createKubeClientPermissions (kcrc , cluster , addon )
361
361
if err != nil {
362
362
return err
363
363
}
@@ -376,7 +376,6 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
376
376
}
377
377
378
378
func (a * CRDTemplateAgentAddon ) createKubeClientPermissions (
379
- templateName string ,
380
379
kcrc * addonapiv1alpha1.KubeClientRegistrationConfig ,
381
380
cluster * clusterv1.ManagedCluster ,
382
381
addon * addonapiv1alpha1.ManagedClusterAddOn ,
@@ -409,8 +408,7 @@ func (a *CRDTemplateAgentAddon) createKubeClientPermissions(
409
408
APIGroup : rbacv1 .GroupName ,
410
409
Name : pc .CurrentCluster .ClusterRoleName ,
411
410
}
412
- err := a .createPermissionBinding (templateName ,
413
- cluster .Name , addon .Name , cluster .Name , roleRef , & owner )
411
+ err := a .createPermissionBinding (cluster .Name , addon .Name , cluster .Name , roleRef , & owner )
414
412
if err != nil {
415
413
return err
416
414
}
@@ -421,8 +419,8 @@ func (a *CRDTemplateAgentAddon) createKubeClientPermissions(
421
419
422
420
// set owner reference nil since the rolebinding has different namespace with the ManagedClusterAddon
423
421
// TODO: cleanup the rolebinding when the addon is deleted
424
- err := a .createPermissionBinding (templateName ,
425
- cluster . Name , addon . Name , pc .SingleNamespace .Namespace , pc .SingleNamespace .RoleRef , nil )
422
+ err := a .createPermissionBinding (cluster . Name , addon . Name ,
423
+ pc .SingleNamespace .Namespace , pc .SingleNamespace .RoleRef , nil )
426
424
if err != nil {
427
425
return err
428
426
}
@@ -431,16 +429,9 @@ func (a *CRDTemplateAgentAddon) createKubeClientPermissions(
431
429
return nil
432
430
}
433
431
434
- func (a * CRDTemplateAgentAddon ) createPermissionBinding (templateName , clusterName , addonName , namespace string ,
432
+ func (a * CRDTemplateAgentAddon ) createPermissionBinding (clusterName , addonName , namespace string ,
435
433
roleRef rbacv1.RoleRef , owner * metav1.OwnerReference ) error {
436
- // TODO: confirm the group
437
- groups := agent .DefaultGroups (clusterName , addonName )
438
- subject := []rbacv1.Subject {}
439
- for _ , group := range groups {
440
- subject = append (subject , rbacv1.Subject {
441
- Kind : rbacv1 .GroupKind , APIGroup : rbacv1 .GroupName , Name : group ,
442
- })
443
- }
434
+
444
435
binding := & rbacv1.RoleBinding {
445
436
ObjectMeta : metav1.ObjectMeta {
446
437
Name : fmt .Sprintf ("open-cluster-management:%s:%s:agent" ,
@@ -451,27 +442,29 @@ func (a *CRDTemplateAgentAddon) createPermissionBinding(templateName, clusterNam
451
442
AddonTemplateLabelKey : "" ,
452
443
},
453
444
},
454
- RoleRef : roleRef ,
455
- Subjects : subject ,
445
+ RoleRef : roleRef ,
446
+ Subjects : []rbacv1.Subject {
447
+ {
448
+ Kind : rbacv1 .GroupKind ,
449
+ APIGroup : rbacv1 .GroupName ,
450
+ Name : clusterAddonGroup (clusterName , addonName ),
451
+ },
452
+ },
456
453
}
457
454
if owner != nil {
458
455
binding .OwnerReferences = []metav1.OwnerReference {* owner }
459
456
}
460
- _ , err := a .rolebindingLister .RoleBindings (namespace ).Get (binding .Name )
461
- switch {
462
- case err == nil :
463
- // TODO: update the rolebinding if it is not the same
464
- a .logger .Info ("Rolebinding already exists" , "rolebindingName" , binding .Name )
465
- return nil
466
- case apierrors .IsNotFound (err ):
467
- _ , createErr := a .hubKubeClient .RbacV1 ().RoleBindings (namespace ).Create (
468
- context .TODO (), binding , metav1.CreateOptions {})
469
- if createErr != nil && ! apierrors .IsAlreadyExists (createErr ) {
470
- return createErr
471
- }
472
- case err != nil :
473
- return err
457
+
458
+ _ , modified , err := resourceapply .ApplyRoleBinding (context .TODO (),
459
+ a .hubKubeClient .RbacV1 (), a .eventRecorder , binding )
460
+ if err == nil && modified {
461
+ a .logger .Info ("Rolebinding for addon updated" , "namespace" , binding .Namespace , "name" , binding .Name ,
462
+ "clusterName" , clusterName , "addonName" , addonName )
474
463
}
464
+ return err
465
+ }
475
466
476
- return nil
467
+ // clusterAddonGroup returns the group that represents the addon for the cluster
468
+ func clusterAddonGroup (clusterName , addonName string ) string {
469
+ return fmt .Sprintf ("system:open-cluster-management:cluster:%s:addon:%s" , clusterName , addonName )
477
470
}
0 commit comments