|
| 1 | +package tenant |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "hash/fnv" |
| 7 | + |
| 8 | + "golang.org/x/sync/errgroup" |
| 9 | + rbacv1 "k8s.io/api/rbac/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 12 | + |
| 13 | + capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" |
| 14 | +) |
| 15 | + |
| 16 | +// Sync the dynamic Tenant Owner specific cluster-roles and additional ClusterRole Bindings, which can be used in many ways: |
| 17 | +// applying Pod Security Policies or giving access to CRDs or specific API groups. |
| 18 | +func (r *Manager) syncClusterRoleBindings(ctx context.Context, tenant *capsulev1beta1.Tenant) (err error) { |
| 19 | + // hashing the ClusterRoleBinding name due to DNS RFC-1123 applied to Kubernetes labels |
| 20 | + hashFn := func(binding capsulev1beta1.AdditionalRoleBindingsSpec) string { |
| 21 | + h := fnv.New64a() |
| 22 | + |
| 23 | + _, _ = h.Write([]byte(binding.ClusterRoleName)) |
| 24 | + |
| 25 | + for _, sub := range binding.Subjects { |
| 26 | + _, _ = h.Write([]byte(sub.Kind + sub.Name)) |
| 27 | + } |
| 28 | + |
| 29 | + return fmt.Sprintf("%x", h.Sum64()) |
| 30 | + } |
| 31 | + // getting requested Role Binding keys |
| 32 | + keys := make([]string, 0, len(tenant.Spec.Owners)) |
| 33 | + // Generating for dynamic tenant owners cluster roles |
| 34 | + for _, owner := range tenant.Spec.Owners { |
| 35 | + for _, clusterRoleName := range owner.GetClusterRoles(*tenant) { |
| 36 | + |
| 37 | + cr := r.ownerClusterRoleBindings(owner, clusterRoleName) |
| 38 | + |
| 39 | + keys = append(keys, hashFn(cr)) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + group := new(errgroup.Group) |
| 44 | + |
| 45 | + group.Go(func() error { |
| 46 | + return r.syncAdditionalClusterRoleBinding(ctx, tenant, keys, hashFn) |
| 47 | + }) |
| 48 | + |
| 49 | + return group.Wait() |
| 50 | +} |
| 51 | + |
| 52 | +func (r *Manager) syncAdditionalClusterRoleBinding(ctx context.Context, tenant *capsulev1beta1.Tenant, keys []string, hashFn func(binding capsulev1beta1.AdditionalRoleBindingsSpec) string) (err error) { |
| 53 | + |
| 54 | + var tenantLabel string |
| 55 | + |
| 56 | + var clusterRoleBindingLabel string |
| 57 | + |
| 58 | + if tenantLabel, err = capsulev1beta1.GetTypeLabel(&capsulev1beta1.Tenant{}); err != nil { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if clusterRoleBindingLabel, err = capsulev1beta1.GetTypeLabel(&rbacv1.ClusterRoleBinding{}); err != nil { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if err = r.pruningResources(ctx, "capsule-system", keys, &rbacv1.ClusterRoleBinding{}); err != nil { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + var clusterRoleBindings []capsulev1beta1.AdditionalRoleBindingsSpec |
| 71 | + |
| 72 | + for _, owner := range tenant.Spec.Owners { |
| 73 | + for _, clusterRoleName := range owner.GetClusterRoles(*tenant) { |
| 74 | + clusterRoleBindings = append(clusterRoleBindings, r.ownerClusterRoleBindings(owner, clusterRoleName)) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for i, clusterRoleBinding := range clusterRoleBindings { |
| 79 | + |
| 80 | + clusterRoleBindingHashLabel := hashFn(clusterRoleBinding) |
| 81 | + |
| 82 | + target := &rbacv1.ClusterRoleBinding{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: fmt.Sprintf("capsule-%s-%d-%s", tenant.Name, i, clusterRoleBinding.ClusterRoleName), |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + var res controllerutil.OperationResult |
| 89 | + res, err = controllerutil.CreateOrUpdate(ctx, r.Client, target, func() error { |
| 90 | + target.ObjectMeta.Labels = map[string]string{ |
| 91 | + tenantLabel: tenant.Name, |
| 92 | + |
| 93 | + clusterRoleBindingLabel: clusterRoleBindingHashLabel, |
| 94 | + } |
| 95 | + target.RoleRef = rbacv1.RoleRef{ |
| 96 | + APIGroup: rbacv1.GroupName, |
| 97 | + Kind: "ClusterRole", |
| 98 | + Name: clusterRoleBinding.ClusterRoleName, |
| 99 | + } |
| 100 | + target.Subjects = clusterRoleBinding.Subjects |
| 101 | + |
| 102 | + return controllerutil.SetControllerReference(tenant, target, r.Client.Scheme()) |
| 103 | + }) |
| 104 | + |
| 105 | + r.emitEvent(tenant, target.GetNamespace(), res, fmt.Sprintf("Ensuring ClusterRoleBinding %s", target.GetName()), err) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + r.Log.Error(err, "Cannot sync ClusterRoleBinding") |
| 109 | + } |
| 110 | + |
| 111 | + r.Log.Info(fmt.Sprintf("ClusterRoleBinding sync result: %s", string(res)), "name", target.Name, "namespace", target.Namespace) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
0 commit comments