Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multi namespace RBAC manifests #897

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions traefik/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ Users can provide an override for an explicit service they want bound via `.Valu
Construct a comma-separated list of whitelisted namespaces
*/}}
{{- define "providers.kubernetesIngress.namespaces" -}}
{{- default .Release.Namespace (join "," .Values.providers.kubernetesIngress.namespaces) }}
{{- default (include "traefik.namespace" .) (join "," .Values.providers.kubernetesIngress.namespaces) }}
{{- end -}}
{{- define "providers.kubernetesCRD.namespaces" -}}
{{- default .Release.Namespace (join "," .Values.providers.kubernetesCRD.namespaces) }}
{{- default (include "traefik.namespace" .) (join "," .Values.providers.kubernetesCRD.namespaces) }}
{{- end -}}

{{/*
Expand Down
23 changes: 15 additions & 8 deletions traefik/templates/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{{- if and .Values.rbac.enabled .Values.rbac.namespaced }}
{{- $ingressNamespaces := default (include "traefik.namespace" . | list) .Values.providers.kubernetesIngress.namespaces -}}
{{- $CRDNamespaces := default (include "traefik.namespace" . | list) .Values.providers.kubernetesCRD.namespaces -}}
{{- $allNamespaces := uniq (concat $ingressNamespaces $CRDNamespaces) -}}

{{- if and .Values.rbac.enabled .Values.rbac.namespaced -}}
{{- range $allNamespaces }}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "traefik.fullname" . }}
namespace: {{ template "traefik.namespace" . }}
name: {{ template "traefik.fullname" $ }}
namespace: {{ . }}
labels:
{{- include "traefik.labels" . | nindent 4 }}
{{- include "traefik.labels" $ | nindent 4 }}
rules:
- apiGroups:
- ""
Expand All @@ -17,7 +23,7 @@ rules:
- get
- list
- watch
{{- if .Values.providers.kubernetesIngress.enabled }}
{{- if (and (has . $ingressNamespaces) $.Values.providers.kubernetesIngress.enabled) }}
- apiGroups:
- extensions
- networking.k8s.io
Expand All @@ -35,7 +41,7 @@ rules:
verbs:
- update
{{- end -}}
{{- if .Values.providers.kubernetesCRD.enabled }}
{{- if (and (has . $CRDNamespaces) $.Values.providers.kubernetesCRD.enabled) }}
- apiGroups:
- traefik.io
{{- if semverCompare "<3.0.0-0" (default $.Chart.AppVersion $.Values.image.tag) }}
Expand All @@ -59,14 +65,15 @@ rules:
- list
- watch
{{- end -}}
{{- if .Values.podSecurityPolicy.enabled }}
{{- if $.Values.podSecurityPolicy.enabled }}
- apiGroups:
- extensions
resourceNames:
- {{ template "traefik.fullname" . }}
- {{ template "traefik.fullname" $ }}
resources:
- podsecuritypolicies
verbs:
- use
{{- end -}}
{{- end -}}
{{- end -}}
19 changes: 13 additions & 6 deletions traefik/templates/rbac/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
{{- $ingressNamespaces := default (include "traefik.namespace" . | list) .Values.providers.kubernetesIngress.namespaces -}}
{{- $CRDNamespaces := default (include "traefik.namespace" . | list) .Values.providers.kubernetesCRD.namespaces -}}
{{- $allNamespaces := uniq (concat $ingressNamespaces $CRDNamespaces) -}}

{{- if and .Values.rbac.enabled .Values.rbac.namespaced }}
{{- range $allNamespaces }}
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "traefik.fullname" . }}
namespace: {{ template "traefik.namespace" . }}
name: {{ template "traefik.fullname" $ }}
namespace: {{ . }}
labels:
{{- include "traefik.labels" . | nindent 4 }}
{{- include "traefik.labels" $ | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ template "traefik.fullname" . }}
name: {{ template "traefik.fullname" $ }}
subjects:
- kind: ServiceAccount
name: {{ include "traefik.serviceAccountName" . }}
namespace: {{ template "traefik.namespace" . }}
name: {{ include "traefik.serviceAccountName" $ }}
namespace: {{ template "traefik.namespace" $ }}
{{- end -}}
{{- end -}}
50 changes: 50 additions & 0 deletions traefik/tests/rbac-config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,56 @@ tests:
path: metadata.namespace
value: NAMESPACE
template: rbac/serviceaccount.yaml
- it: should use multiple namespaces if provided
set:
providers:
kubernetesCRD:
namespaces:
- default
- foo
kubernetesIngress:
namespaces:
- default
- bar
rbac:
namespaced: true
asserts:
- hasDocuments:
count: 3
template: rbac/role.yaml
- hasDocuments:
count: 3
template: rbac/rolebinding.yaml
- equal:
path: metadata.namespace
value: default
template: rbac/role.yaml
documentIndex: 0
- equal:
path: metadata.namespace
value: bar
template: rbac/role.yaml
documentIndex: 1
- equal:
path: metadata.namespace
value: foo
template: rbac/role.yaml
documentIndex: 2
- equal:
path: metadata.namespace
value: default
template: rbac/rolebinding.yaml
documentIndex: 0
- equal:
path: metadata.namespace
value: bar
template: rbac/rolebinding.yaml
documentIndex: 1
- equal:
path: metadata.namespace
value: foo
template: rbac/rolebinding.yaml
documentIndex: 2
- it: should accept overridden namespace
set:
namespaceOverride: "traefik-ns-override"
Expand Down