Skip to content

Commit b6dc13c

Browse files
authored
summarize the runtime permission (#158)
* summarize the runtime permission * summarize the runtime permission * format constant.go * fix lint err * list role within ns * format constant.go
1 parent 0078e68 commit b6dc13c

File tree

2 files changed

+138
-8
lines changed

2 files changed

+138
-8
lines changed

controllers/constant/constants.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package constant
1818

1919
const (
20-
NamespaceScopeManagedPrefix = "nss-managed-role-from-"
21-
NamespaceScopeConfigmapName = "namespace-scope"
22-
NamespaceScopeFinalizer = "finalizer.nss.operator.ibm.com"
23-
NamespaceScopeLabel = "managedby-namespace-scope"
24-
DefaultRestartLabelsKey = "intent"
25-
DefaultRestartLabelsValue = "projected"
26-
NamespaceScopeServiceAccount = "ibm-namespace-scope-operator"
27-
InjectorMark = "nss.operator.ibm.com/managed-operators"
20+
NamespaceScopeManagedPrefix = "nss-managed-role-from-"
21+
NamespaceScopeConfigmapName = "namespace-scope"
22+
NamespaceScopeFinalizer = "finalizer.nss.operator.ibm.com"
23+
NamespaceScopeLabel = "managedby-namespace-scope"
24+
DefaultRestartLabelsKey = "intent"
25+
DefaultRestartLabelsValue = "projected"
26+
NamespaceScopeServiceAccount = "ibm-namespace-scope-operator"
27+
InjectorMark = "nss.operator.ibm.com/managed-operators"
28+
NamespaceScopeConfigmapLabelKey = "namespace-scope-configmap"
29+
NamespaceScopeRuntimePrefix = "nss-runtime-managed-role-from-"
2830
)

controllers/namespacescope_controller.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ func (r *NamespaceScopeReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
128128
return ctrl.Result{}, err
129129
}
130130

131+
for _, namespaceMember := range instance.Spec.NamespaceMembers {
132+
if rolesList, _ := r.GetRolesFromNamespace(namespaceMember); len(rolesList) != 0 {
133+
var summarizedRules []rbacv1.PolicyRule
134+
for _, role := range rolesList {
135+
if role.Name != constant.NamespaceScopeManagedPrefix+instance.Namespace &&
136+
role.Name != constant.NamespaceScopeRuntimePrefix+instance.Namespace {
137+
summarizedRules = append(summarizedRules, role.Rules...)
138+
}
139+
}
140+
141+
if err := r.CreateRuntimeRoleToNamespace(instance, namespaceMember, summarizedRules); err != nil {
142+
klog.Infof("Failed to create runtime role: %v", err)
143+
return ctrl.Result{}, nil
144+
}
145+
}
146+
}
147+
131148
klog.Infof("Finished reconciling NamespaceScope: %s", req.NamespacedName)
132149
return ctrl.Result{RequeueAfter: 60 * time.Second}, nil
133150
}
@@ -255,6 +272,24 @@ func (r *NamespaceScopeReconciler) PushRbacToNamespace(instance *operatorv1.Name
255272
return nil
256273
}
257274

275+
func (r *NamespaceScopeReconciler) CreateRuntimeRoleToNamespace(instance *operatorv1.NamespaceScope, toNs string, summarizedRules []rbacv1.PolicyRule) error {
276+
fromNs := instance.Namespace
277+
278+
operatorNs, err := util.GetOperatorNamespace()
279+
if err != nil {
280+
klog.Error("get operator namespace failed: ", err)
281+
return err
282+
}
283+
if toNs == operatorNs {
284+
return nil
285+
}
286+
if err := r.generateRuntimeRoleForNSS(instance, summarizedRules, fromNs, toNs); err != nil {
287+
return err
288+
}
289+
290+
return nil
291+
}
292+
258293
func (r *NamespaceScopeReconciler) DeleteRbacFromUnmanagedNamespace(instance *operatorv1.NamespaceScope) error {
259294
cm := &corev1.ConfigMap{}
260295
cmKey := types.NamespacedName{Name: instance.Spec.ConfigmapName, Namespace: instance.Namespace}
@@ -365,6 +400,74 @@ func (r *NamespaceScopeReconciler) generateRBACForNSS(instance *operatorv1.Names
365400
}
366401
return err
367402
}
403+
404+
return nil
405+
}
406+
407+
func (r *NamespaceScopeReconciler) generateRuntimeRoleForNSS(instance *operatorv1.NamespaceScope, summarizedRules []rbacv1.PolicyRule, fromNs, toNs string) error {
408+
labels := map[string]string{
409+
"namespace-scope-configmap": instance.Namespace + "-" + instance.Spec.ConfigmapName,
410+
}
411+
412+
if err := r.createRuntimeRoleForNSS(labels, summarizedRules, fromNs, toNs); err != nil {
413+
if errors.IsAlreadyExists(err) {
414+
if err := r.updateRuntimeRoleForNSS(labels, summarizedRules, fromNs, toNs); err != nil {
415+
return err
416+
}
417+
return nil
418+
}
419+
if errors.IsForbidden(err) {
420+
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+
}
422+
return err
423+
}
424+
425+
return nil
426+
}
427+
428+
func (r *NamespaceScopeReconciler) createRuntimeRoleForNSS(labels map[string]string, summarizedRules []rbacv1.PolicyRule, fromNs, toNs string) error {
429+
name := constant.NamespaceScopeRuntimePrefix + fromNs
430+
namespace := toNs
431+
role := &rbacv1.Role{
432+
ObjectMeta: metav1.ObjectMeta{
433+
Name: name,
434+
Namespace: namespace,
435+
Labels: labels,
436+
},
437+
Rules: summarizedRules,
438+
}
439+
if err := r.Create(ctx, role); err != nil {
440+
if errors.IsAlreadyExists(err) {
441+
return err
442+
}
443+
klog.Errorf("Failed to create role %s/%s: %v", namespace, name, err)
444+
return err
445+
}
446+
447+
klog.Infof("Created role %s/%s", namespace, name)
448+
return nil
449+
}
450+
451+
func (r *NamespaceScopeReconciler) updateRuntimeRoleForNSS(labels map[string]string, summarizedRules []rbacv1.PolicyRule, fromNs, toNs string) error {
452+
name := constant.NamespaceScopeRuntimePrefix + fromNs
453+
namespace := toNs
454+
455+
role := &rbacv1.Role{
456+
ObjectMeta: metav1.ObjectMeta{
457+
Name: name,
458+
Namespace: namespace,
459+
Labels: labels,
460+
},
461+
Rules: summarizedRules,
462+
}
463+
464+
if err := r.Update(ctx, role); err != nil {
465+
klog.Errorf("Failed to create role %s/%s: %v", namespace, name, err)
466+
return err
467+
}
468+
469+
klog.Infof("Updated role %s/%s", namespace, name)
470+
368471
return nil
369472
}
370473

@@ -465,6 +568,31 @@ func (r *NamespaceScopeReconciler) generateRBACToNamespace(instance *operatorv1.
465568
return nil
466569
}
467570

571+
func (r *NamespaceScopeReconciler) GetRolesFromNamespace(namespace string) ([]rbacv1.Role, error) {
572+
rolesList := &rbacv1.RoleList{}
573+
574+
opts := []client.ListOption{
575+
client.InNamespace(namespace),
576+
}
577+
if err := r.Reader.List(ctx, rolesList, opts...); err != nil {
578+
if errors.IsNotFound(err) {
579+
klog.Infof("Roles not found in namespace %s: %v", namespace, err)
580+
return nil, nil
581+
}
582+
klog.Errorf("Cannot list roles in namespace %s: %v", namespace, err)
583+
return nil, err
584+
}
585+
586+
roles := []rbacv1.Role{}
587+
for _, role := range rolesList.Items {
588+
if _, ok := role.Labels[constant.NamespaceScopeConfigmapLabelKey]; ok {
589+
roles = append(roles, role)
590+
}
591+
}
592+
593+
return roles, nil
594+
}
595+
468596
func (r *NamespaceScopeReconciler) GetServiceAccountFromNamespace(instance *operatorv1.NamespaceScope, namespace string) ([]string, error) {
469597
labels := instance.Spec.RestartLabels
470598
pods := &corev1.PodList{}

0 commit comments

Comments
 (0)