Skip to content

Commit

Permalink
feat(controllers/tenant): ensure per-tenant owners roles
Browse files Browse the repository at this point in the history
add gitops ready cluster roles per tenant owners.

Signed-off-by: Massimiliano Giovagnoli <[email protected]>
  • Loading branch information
maxgio92 committed Aug 13, 2022
1 parent 852ab16 commit ff53cc2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controllers/tenant/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func (r Manager) Reconcile(ctx context.Context, request ctrl.Request) (result ct

return
}
// Ensuring Roles resources
r.Log.Info("Ensuring Roles for Owners and Tenant")

if err = r.syncRoles(ctx, instance); err != nil {
r.Log.Error(err, "Cannot sync Roles items")

return
}
// Ensuring RoleBinding resources
r.Log.Info("Ensuring RoleBindings for Owners and Tenant")

Expand Down
69 changes: 69 additions & 0 deletions controllers/tenant/roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tenant

import (
"context"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

capsulev1beta1 "github.com/clastix/capsule/api/v1beta1"
)

const (
ImpersonatorRoleName = "capsule-tenant-impersonator"
)

// Sync the Tenant Owner specific cluster-roles.
// When the Tenant is configured GitOpsReady additional (Cluster)Roles are created, then bound.
func (r *Manager) syncRoles(ctx context.Context, tenant *capsulev1beta1.Tenant) (err error) {

// If the Tenant will be reconciled the GitOps-way,
// Tenant Owners might be machine GitOps reconciler identities.
if tenant.Spec.GitOpsReady {
for _, owner := range tenant.Spec.Owners {
if err = r.ensureOwnerRole(ctx, tenant, &owner, ImpersonatorRoleName); err != nil {
r.Log.Error(err, "Reconciliation for ClusterRole failed", "ClusterRole", ImpersonatorRoleName)
return err
}
}
}

return
}

func (r *Manager) ensureOwnerRole(ctx context.Context, tenant *capsulev1beta1.Tenant, owner *capsulev1beta1.OwnerSpec, roleName string) (err error) {
switch roleName {
case ImpersonatorRoleName:
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: roleName + "-" + tenant.Name + "-" + owner.Name,
},
}

resource := "users"
if owner.Kind == capsulev1beta1.GroupOwner {
resource = "groups"
}

resourceName := owner.Name
if owner.Kind == capsulev1beta1.ServiceAccountOwner {
resourceName = "system:serviceaccount:" + tenant.Namespace + ":" + owner.Name
}

_, err = controllerutil.CreateOrUpdate(ctx, r.Client, clusterRole, func() error {
clusterRole.Rules = []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{resource},
Verbs: []string{"impersonate"},
ResourceNames: []string{resourceName},
},
}

return nil
})
}

return
}

0 comments on commit ff53cc2

Please sign in to comment.