Skip to content

Commit ab75014

Browse files
committed
refactor: support for rfc 1123 for tenant owners cluster roles overrides
1 parent e237249 commit ab75014

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

api/v1beta1/owner_role.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,37 @@ const (
1212
ClusterRoleNamesAnnotation = "clusterrolenames.capsule.clastix.io"
1313
)
1414

15-
func (in OwnerSpec) GetRoles(tenant Tenant) []string {
15+
// GetRoles read the annotation available in the Tenant specification and if it matches the pattern
16+
// clusterrolenames.capsule.clastix.io/${KIND}.${NAME} returns the associated roles.
17+
// Kubernetes annotations and labels must respect RFC 1123 about DNS names and this could be cumbersome in two cases:
18+
// 1. identifying users based on their email address
19+
// 2. the overall length of the annotation key that is exceeding 63 characters
20+
// For emails, the symbol @ can be replaced with the placeholder __AT__.
21+
// For the latter one, the index of the owner can be used to force the retrieval.
22+
func (in OwnerSpec) GetRoles(tenant Tenant, index int) []string {
1623
for key, value := range tenant.GetAnnotations() {
17-
if key == fmt.Sprintf("%s/%s.%s", ClusterRoleNamesAnnotation, strings.ToLower(in.Kind.String()), strings.ToLower(in.Name)) {
24+
if !strings.HasPrefix(key, fmt.Sprintf("%s/", ClusterRoleNamesAnnotation)) {
25+
continue
26+
}
27+
28+
for symbol, replace := range in.convertMap() {
29+
key = strings.ReplaceAll(key, symbol, replace)
30+
}
31+
32+
nameBased := key == fmt.Sprintf("%s/%s.%s", ClusterRoleNamesAnnotation, strings.ToLower(in.Kind.String()), strings.ToLower(in.Name))
33+
34+
indexBased := key == fmt.Sprintf("%s/%d", ClusterRoleNamesAnnotation, index)
35+
36+
if nameBased || indexBased {
1837
return strings.Split(value, ",")
1938
}
2039
}
2140

2241
return []string{"admin", "capsule-namespace-deleter"}
2342
}
43+
44+
func (in OwnerSpec) convertMap() map[string]string {
45+
return map[string]string{
46+
"__AT__": "@",
47+
}
48+
}

controllers/tenant/rolebindings.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func (r *Manager) syncRoleBindings(ctx context.Context, tenant *capsulev1beta1.T
6161
// getting requested Role Binding keys
6262
keys := make([]string, 0, len(tenant.Spec.Owners))
6363
// Generating for dynamic tenant owners cluster roles
64-
for _, owner := range tenant.Spec.Owners {
65-
for _, clusterRoleName := range owner.GetRoles(*tenant) {
64+
for index, owner := range tenant.Spec.Owners {
65+
for _, clusterRoleName := range owner.GetRoles(*tenant, index) {
6666
cr := r.ownerClusterRoleBindings(owner, clusterRoleName)
6767

6868
keys = append(keys, hashFn(cr))
@@ -103,8 +103,8 @@ func (r *Manager) syncAdditionalRoleBinding(ctx context.Context, tenant *capsule
103103

104104
var roleBindings []capsulev1beta1.AdditionalRoleBindingsSpec
105105

106-
for _, owner := range tenant.Spec.Owners {
107-
for _, clusterRoleName := range owner.GetRoles(*tenant) {
106+
for index, owner := range tenant.Spec.Owners {
107+
for _, clusterRoleName := range owner.GetRoles(*tenant, index) {
108108
roleBindings = append(roleBindings, r.ownerClusterRoleBindings(owner, clusterRoleName))
109109
}
110110
}

0 commit comments

Comments
 (0)