Skip to content

Commit

Permalink
feat: add rbac support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwganem committed Oct 9, 2018
1 parent 2db433e commit 6330895
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
21 changes: 21 additions & 0 deletions templates/sync-cluster-role-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- $rbacEnabled := (or (and (ne (.Values.rbac.enabled | toString) "-") .Values.rbac.enabled) (and (eq (.Values.rbac.enabled | toString) "-") .Values.rbac.enabled)) }}
{{- $syncEnabled := (or (and (ne (.Values.syncCatalog.enabled | toString) "-") .Values.syncCatalog.enabled) (and (eq (.Values.syncCatalog.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (and $rbacEnabled $syncEnabled) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: consul:sync
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: consul:sync
subjects:
- kind: Group
name: system:serviceaccounts:{{ .Release.Namespace }}
apiGroup: rbac.authorization.k8s.io
{{- end }}
25 changes: 25 additions & 0 deletions templates/sync-cluster-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{- $rbacEnabled := (or (and (ne (.Values.rbac.enabled | toString) "-") .Values.rbac.enabled) (and (eq (.Values.rbac.enabled | toString) "-") .Values.rbac.enabled)) }}
{{- $syncEnabled := (or (and (ne (.Values.syncCatalog.enabled | toString) "-") .Values.syncCatalog.enabled) (and (eq (.Values.syncCatalog.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (and $rbacEnabled $syncEnabled) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: consul:sync
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
rules:
- apiGroups: [""]
resources:
- services
- endpoints
verbs:
- get
- list
- watch
- update
- patch
- delete
{{- end }}
56 changes: 56 additions & 0 deletions test/unit/sync-cluster-role-binding.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bats

load _helpers

@test "sync/ClusterRoleBinding: disabled by default" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role-binding.yaml \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRoleBinding: enable with global.enabled false" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role-binding.yaml \
--set 'global.enabled=false' \
--set 'syncCatalog.enabled=true' \
--set 'rbac.enabled=true' \
. | tee /dev/stderr |
yq -s 'length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "sync/ClusterRoleBinding: disable with syncCatalog.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role-binding.yaml \
--set 'syncCatalog.enabled=false' \
--set 'rbac.enabled=true' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRoleBinding: disable with rbac.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role-binding.yaml \
--set 'syncCatalog.enabled=true' \
--set 'rbac.enabled=false' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRoleBinding: disable with global.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role-binding.yaml \
--set 'global.enabled=false' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}
56 changes: 56 additions & 0 deletions test/unit/sync-cluster-role.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bats

load _helpers

@test "sync/ClusterRole: disabled by default" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role.yaml \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRole: enable with global.enabled false" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role.yaml \
--set 'global.enabled=false' \
--set 'syncCatalog.enabled=true' \
--set 'rbac.enabled=true' \
. | tee /dev/stderr |
yq -s 'length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "sync/ClusterRole: disable with syncCatalog.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role.yaml \
--set 'syncCatalog.enabled=false' \
--set 'rbac.enabled=true' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRole: disable with rbac.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role.yaml \
--set 'syncCatalog.enabled=true' \
--set 'rbac.enabled=false' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "sync/ClusterRole: disable with global.enabled" {
cd `chart_dir`
local actual=$(helm template \
-x templates/sync-cluster-role.yaml \
--set 'global.enabled=false' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}
5 changes: 5 additions & 0 deletions values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ connectInject:
# defaults but can be customized if necessary.
certName: tls.crt
keyName: tls.key

# Enabling rbac will create cluster roles and cluster role bindings to allow the
# syncCatalog service to communicate with the kubernetes api to get/create services
rbac:
enabled: false

0 comments on commit 6330895

Please sign in to comment.