-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(base-cluster/rbac)!: allow to use the k8s default ClusterRoles (#…
…1230) The most often used rbac roles are exactly the default ones, this eases that configuration this is breaking because one might already use these names, but they are now forbidden
- Loading branch information
Showing
3 changed files
with
49 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,50 @@ | ||
{{- define "base-cluster.rbac.roles" -}} | ||
{{- $roles := dict -}} | ||
{{- $definedRoles := .roles -}} | ||
{{- $definedNamespaces := .namespaces -}} | ||
{{- range $accountName, $account := .accounts -}} | ||
{{- range $roleName, $namespaces := dig "roles" (dict) $account -}} | ||
{{- if not (has $roleName $definedRoles) -}} | ||
{{- fail (printf "Role '%s' doesn't exist, used in account '%s'" $roleName $accountName ) -}} | ||
{{- define "base-cluster.rbac.preexistingRoles" -}} | ||
{{- $preexistingRoles := list -}} | ||
{{- range $role := (lookup "rbac.authorization.k8s.io/v1" "ClusterRole" "" "").items -}} | ||
{{/* Only allow the default k8s ClusterRoles */}} | ||
{{- if eq (dig "metadata" "labels" "kubernetes.io/bootstrapping" "" $role) "rbac-defaults" -}} | ||
{{- $preexistingRoles = append $preexistingRoles $role.metadata.name -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- toYaml $preexistingRoles -}} | ||
{{- end -}} | ||
|
||
{{- $existingRole := dig $roleName (dict) $roles -}} | ||
{{- $namespaceMapping := dig "namespaceMapping" (dict) $existingRole -}} | ||
{{- range $roleNamespace := $namespaces -}} | ||
{{- if not (has $roleNamespace $definedNamespaces) -}} | ||
{{- fail (printf "Role '%s' wants to be in the undefined namespace '%s'" $roleName $roleNamespace) -}} | ||
{{- define "base-cluster.rbac.roles" -}} | ||
{{- $roles := dict -}} | ||
{{- $definedRoles := .roles -}} | ||
{{- $preexistingRoles := include "base-cluster.rbac.preexistingRoles" (dict) | fromYamlArray -}} | ||
{{- $definedNamespaces := .namespaces -}} | ||
{{- range $accountName, $account := .accounts -}} | ||
{{- range $roleName, $namespaces := dig "roles" (dict) $account -}} | ||
{{- if and (not (has $roleName $definedRoles)) (not (has $roleName $preexistingRoles)) -}} | ||
{{- fail (printf "Role '%s' doesn't exist, used in account '%s'" $roleName $accountName ) -}} | ||
{{- end -}} | ||
|
||
{{- $existingNamespace := dig $roleNamespace (list) $namespaceMapping -}} | ||
{{- $existingNamespace = append $existingNamespace $accountName -}} | ||
{{- $namespaceMapping = set $namespaceMapping $roleNamespace $existingNamespace -}} | ||
{{- end -}} | ||
{{- $existingRole = set $existingRole "namespaceMapping" $namespaceMapping -}} | ||
{{- $roles = set $roles $roleName $existingRole -}} | ||
{{- end -}} | ||
{{- range $roleName := dig "clusterRoles" (list) $account -}} | ||
{{- if not (has $roleName $definedRoles) -}} | ||
{{- fail (printf "Role '%s' doesn't exist, used in account '%s'" $roleName $accountName ) -}} | ||
{{- $existingRole := dig $roleName (dict) $roles -}} | ||
{{- $namespaceMapping := dig "namespaceMapping" (dict) $existingRole -}} | ||
{{- range $roleNamespace := $namespaces -}} | ||
{{- if not (has $roleNamespace $definedNamespaces) -}} | ||
{{- fail (printf "Role '%s' wants to be in the undefined namespace '%s'" $roleName $roleNamespace) -}} | ||
{{- end -}} | ||
|
||
{{- $existingNamespace := dig $roleNamespace (list) $namespaceMapping -}} | ||
{{- $existingNamespace = append $existingNamespace $accountName -}} | ||
{{- $namespaceMapping = set $namespaceMapping $roleNamespace $existingNamespace -}} | ||
{{- end -}} | ||
{{- $existingRole = set $existingRole "namespaceMapping" $namespaceMapping -}} | ||
{{- $roles = set $roles $roleName $existingRole -}} | ||
{{- end -}} | ||
{{- range $roleName := dig "clusterRoles" (list) $account -}} | ||
{{- if not (has $roleName $definedRoles) -}} | ||
{{- fail (printf "Role '%s' doesn't exist, used in account '%s'" $roleName $accountName ) -}} | ||
{{- end -}} | ||
|
||
{{- $existingRole := dig $roleName (dict) $roles -}} | ||
{{- $clusterMapping := dig "clusterMapping" (list) $existingRole -}} | ||
{{- $clusterMapping = append $clusterMapping $accountName -}} | ||
{{- $existingRole = set $existingRole "clusterMapping" $clusterMapping -}} | ||
{{- $roles = set $roles $roleName $existingRole -}} | ||
{{- $existingRole := dig $roleName (dict) $roles -}} | ||
{{- $clusterMapping := dig "clusterMapping" (list) $existingRole -}} | ||
{{- $clusterMapping = append $clusterMapping $accountName -}} | ||
{{- $existingRole = set $existingRole "clusterMapping" $clusterMapping -}} | ||
{{- $roles = set $roles $roleName $existingRole -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- toYaml $roles -}} | ||
{{- toYaml $roles -}} | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters