@@ -18,6 +18,8 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "crypto/sha256"
22
+ "encoding/hex"
21
23
"strings"
22
24
"time"
23
25
@@ -415,7 +417,7 @@ func (r *NamespaceScopeReconciler) generateRBACToNamespace(instance *operatorv1.
415
417
return err
416
418
}
417
419
418
- if err := r .CreateRole (roleList , labels , fromNs , toNs ); err != nil {
420
+ if err := r .CreateRole (roleList , labels , sa , fromNs , toNs ); err != nil {
419
421
if errors .IsForbidden (err ) {
420
422
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 )
421
423
}
@@ -491,7 +493,7 @@ func (r *NamespaceScopeReconciler) GetRolesFromServiceAccount(sa string, namespa
491
493
return util .ToStringSlice (util .MakeSet (roleNameList )), nil
492
494
}
493
495
494
- func (r * NamespaceScopeReconciler ) CreateRole (roleNames []string , labels map [string ]string , fromNs string , toNs string ) error {
496
+ func (r * NamespaceScopeReconciler ) CreateRole (roleNames []string , labels map [string ]string , saName , fromNs , toNs string ) error {
495
497
for _ , roleName := range roleNames {
496
498
originalRole := & rbacv1.Role {}
497
499
if err := r .Get (ctx , types.NamespacedName {Name : roleName , Namespace : fromNs }, originalRole ); err != nil {
@@ -502,7 +504,8 @@ func (r *NamespaceScopeReconciler) CreateRole(roleNames []string, labels map[str
502
504
klog .Errorf ("Failed to get role %s in namespace %s: %v" , roleName , fromNs , err )
503
505
return err
504
506
}
505
- name := strings .Split (roleName , "." )[0 ] + "-" + labels ["namespace-scope-configmap" ]
507
+ hashedServiceAccount := sha256 .Sum256 ([]byte (saName + fromNs ))
508
+ name := strings .Split (roleName , "." )[0 ] + "-" + hex .EncodeToString (hashedServiceAccount [:7 ])
506
509
namespace := toNs
507
510
role := & rbacv1.Role {
508
511
ObjectMeta : metav1.ObjectMeta {
@@ -543,7 +546,8 @@ func (r *NamespaceScopeReconciler) DeleteRole(labels map[string]string, toNs str
543
546
544
547
func (r * NamespaceScopeReconciler ) CreateRoleBinding (roleNames []string , labels map [string ]string , saName , fromNs , toNs string ) error {
545
548
for _ , roleName := range roleNames {
546
- name := strings .Split (roleName , "." )[0 ] + "-" + labels ["namespace-scope-configmap" ]
549
+ hashedServiceAccount := sha256 .Sum256 ([]byte (saName + fromNs ))
550
+ name := strings .Split (roleName , "." )[0 ] + "-" + hex .EncodeToString (hashedServiceAccount [:7 ])
547
551
namespace := toNs
548
552
subjects := []rbacv1.Subject {}
549
553
subject := rbacv1.Subject {
0 commit comments