Skip to content

Commit

Permalink
Consolidate controller ClusterRole mods for enablePodSecurityPolicies
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Nov 3, 2022
1 parent 71045d9 commit d247baa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
38 changes: 15 additions & 23 deletions charts/consul/templates/api-gateway-controller-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ metadata:
release: {{ .Release.Name }}
component: api-gateway-controller
rules:
{{- if .Values.global.enablePodSecurityPolicies }}
- apiGroups:
- rbac.authorization.k8s.io
resources:
- roles
- rolebindings
verbs:
- create
- get
- list
- watch
- apiGroups:
- policy
resources:
- podsecuritypolicies
verbs:
- use
{{- end}}
- apiGroups:
- api-gateway.consul.hashicorp.com
resources:
Expand Down Expand Up @@ -263,11 +245,21 @@ rules:
- patch
- update
{{- if .Values.global.enablePodSecurityPolicies }}
- apiGroups: ["policy"]
resources: ["podsecuritypolicies"]
resourceNames:
- {{ template "consul.fullname" . }}-api-gateway-controller
- apiGroups:
- policy
resources:
- podsecuritypolicies
verbs:
- use
- apiGroups:
- rbac.authorization.k8s.io
resources:
- roles
- rolebindings
verbs:
- use
- create
- get
- list
- watch
{{- end }}
{{- end }}
12 changes: 12 additions & 0 deletions charts/consul/test/unit/api-gateway-controller-clusterrole.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ load _helpers
yq '.rules[] | select((.resourceNames[0] == "release-name-consul-api-gateway-controller") and (.resources[0] == "podsecuritypolicies")) | length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "apiGateway/ClusterRole: uses PodSecurityPolicy with apiGateway.enabled=true and global.enablePodSecurityPolicies=true" {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-controller-clusterrole.yaml \
--set 'global.enablePodSecurityPolicies=true' \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
. | tee /dev/stderr |
yq '.rules[] | select((.resourceNames[0] == "release-name-consul-api-gateway-controller") and (.resources[0] == "podsecuritypolicies")) | length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

0 comments on commit d247baa

Please sign in to comment.