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

Create Admin Partitions when enabled #696

Merged
merged 5 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions charts/consul/templates/client-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ spec:
{{- if (and .Values.global.metrics.enabled .Values.global.metrics.enableAgentMetrics) }}
-hcl='telemetry { prometheus_retention_time = "{{ .Values.global.metrics.agentMetricsRetentionTime }}" }' \
{{- end }}
{{- if .Values.global.adminPartitions.enabled }}
-hcl='partition = "{{ .Values.global.adminPartitions.name }}"' \
{{- end }}
-config-dir=/consul/config \
{{- if .Values.global.acls.manageSystemACLs }}
-config-dir=/consul/aclconfig \
Expand Down
92 changes: 92 additions & 0 deletions charts/consul/templates/partition-init-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{{- $serverEnabled := (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) -}}
{{- if (and .Values.global.adminPartitions.enabled (not $serverEnabled)) }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "consul.fullname" . }}-partition-init
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "2"
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
spec:
template:
metadata:
name: {{ template "consul.fullname" . }}-partition-init
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: partition-init
annotations:
"consul.hashicorp.com/connect-inject": "false"
spec:
restartPolicy: Never
serviceAccountName: {{ template "consul.fullname" . }}-partition-init
{{- if .Values.global.tls.enabled }}
volumes:
- name: consul-ca-cert
secret:
{{- if .Values.global.tls.caCert.secretName }}
secretName: {{ .Values.global.tls.caCert.secretName }}
{{- else }}
secretName: {{ template "consul.fullname" . }}-ca-cert
{{- end }}
items:
- key: {{ default "tls.crt" .Values.global.tls.caCert.secretKey }}
path: tls.crt
{{- end }}
containers:
- name: post-install-job
image: {{ .Values.global.imageK8S }}
env:
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
{{- if .Values.global.tls.enabled }}
volumeMounts:
- name: consul-ca-cert
mountPath: /consul/tls/ca
readOnly: true
{{- end }}
command:
- "/bin/sh"
- "-ec"
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"

consul-k8s-control-plane partition-init \
-log-level={{ .Values.global.logLevel }} \
-log-json={{ .Values.global.logJSON }} \

{{- if and .Values.externalServers.enabled (not .Values.externalServers.hosts) }}{{ fail "externalServers.hosts must be set if externalServers.enabled is true" }}{{ end -}}
{{- range .Values.externalServers.hosts }}
-server-address={{ quote . }} \
{{- end }}
-server-port={{ .Values.externalServers.httpsPort }} \

{{- if .Values.global.tls.enabled }}
-use-https \
-consul-ca-cert=/consul/tls/ca/tls.crt \
{{- if not .Values.externalServers.enabled }}
-server-port=8501 \
{{- end }}
{{- if .Values.externalServers.tlsServerName }}
-consul-tls-server-name={{ .Values.externalServers.tlsServerName }} \
{{- end }}
{{- end }}
-partition-name={{ .Values.global.adminPartitions.name }}
resources:
requests:
memory: "50Mi"
cpu: "50m"
limits:
memory: "50Mi"
cpu: "50m"
{{- end }}
38 changes: 38 additions & 0 deletions charts/consul/templates/partition-init-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- $serverEnabled := (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) -}}
{{- if (and .Values.global.adminPartitions.enabled (not $serverEnabled)) }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: {{ template "consul.fullname" . }}-partition-init
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation
spec:
privileged: false
# Allow core volume types.
volumes:
- 'secret'
allowPrivilegeEscalation: false
# This is redundant with non-root + disallow privilege escalation,
# but we can provide it for defense in depth.
requiredDropCapabilities:
- ALL
hostNetwork: false
hostIPC: false
hostPID: false
runAsUser:
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
supplementalGroups:
rule: 'RunAsAny'
fsGroup:
rule: 'RunAsAny'
readOnlyRootFilesystem: false
{{- end }}
40 changes: 40 additions & 0 deletions charts/consul/templates/partition-init-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{- $serverEnabled := (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) -}}
{{- if (and .Values.global.adminPartitions.enabled (not $serverEnabled)) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ template "consul.fullname" . }}-partition-init
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation
rules:
- apiGroups: [""]
resources:
- secrets
verbs:
- create
- get
{{- if .Values.connectInject.enabled }}
- apiGroups: [""]
resources:
- serviceaccounts
resourceNames:
- {{ template "consul.fullname" . }}-connect-injector-authmethod-svc-account
verbs:
- get
{{- end }}
{{- if .Values.global.enablePodSecurityPolicies }}
- apiGroups: ["policy"]
resources: ["podsecuritypolicies"]
resourceNames:
- {{ template "consul.fullname" . }}-partition-init
verbs:
- use
{{- end }}
{{- end }}
23 changes: 23 additions & 0 deletions charts/consul/templates/partition-init-rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{- $serverEnabled := (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) -}}
{{- if (and .Values.global.adminPartitions.enabled (not $serverEnabled)) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ template "consul.fullname" . }}-partition-init
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ template "consul.fullname" . }}-partition-init
subjects:
- kind: ServiceAccount
name: {{ template "consul.fullname" . }}-partition-init
{{- end }}
22 changes: 22 additions & 0 deletions charts/consul/templates/partition-init-serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{- $serverEnabled := (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) -}}
{{- if (and .Values.global.adminPartitions.enabled (not $serverEnabled)) }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ template "consul.fullname" . }}-partition-init
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation
{{- with .Values.global.imagePullSecrets }}
imagePullSecrets:
{{- range . }}
- name: {{ .name }}
{{- end }}
{{- end }}
{{- end }}
23 changes: 23 additions & 0 deletions charts/consul/test/unit/client-daemonset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1332,3 +1332,26 @@ rollingUpdate:
yq -c -r '.spec.template.spec.containers[0].command | join(" ") | contains("-recursor=\"1.2.3.4\"")' | tee /dev/stderr)
[ "${actual}" = "true" ]
}
#--------------------------------------------------------------------
# partitions

@test "client/DaemonSet: -partitions can be set by global.adminPartition.enabled" {
cd `chart_dir`
local actual=$(helm template \
-s templates/client-daemonset.yaml \
--set 'global.adminPartitions.enabled=true' \
. | tee /dev/stderr |
yq -c -r '.spec.template.spec.containers[0].command | join(" ") | contains("partition = \"default\"")' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "client/DaemonSet: -partitions can be overridden by global.adminPartition.name" {
cd `chart_dir`
local actual=$(helm template \
-s templates/client-daemonset.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'global.adminPartitions.name=test' \
. | tee /dev/stderr |
yq -c -r '.spec.template.spec.containers[0].command | join(" ") | contains("partition = \"test\"")' | tee /dev/stderr)
[ "${actual}" = "true" ]
}
96 changes: 96 additions & 0 deletions charts/consul/test/unit/partition-init-job.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bats

load _helpers

@test "partitionInit/Job: disabled by default" {
cd `chart_dir`
assert_empty helm template \
-s templates/partition-init-job.yaml \
.
}

@test "partitionInit/Job: enabled with global.adminPartitions.enabled=true and servers = false" {
cd `chart_dir`
local actual=$(helm template \
-s templates/partition-init-job.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "partitionInit/Job: disabled with global.adminPartitions.enabled=true and servers = true" {
cd `chart_dir`
assert_empty helm template \
-s templates/partition-init-job.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'server.enabled=true' \
.
}

@test "partitionInit/Job: disabled with global.adminPartitions.enabled=true and global.enabled = true" {
cd `chart_dir`
assert_empty helm template \
-s templates/partition-init-job.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'global.enabled=true' \
.
}

@test "partitionInit/Job: disabled with global.adminPartitions.enabled=false" {
cd `chart_dir`
assert_empty helm template \
-s templates/partition-init-job.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'server.enabled=true' \
.
}

#--------------------------------------------------------------------
# global.tls.enabled

@test "partitionInit/Job: sets TLS flags when global.tls.enabled" {
cd `chart_dir`
local command=$(helm template \
-s templates/partition-init-job.yaml \
--set 'global.enabled=false' \
--set 'global.adminPartitions.enabled=true' \
--set 'global.tls.enabled=true' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers[0].command' | tee /dev/stderr)

local actual
actual=$(echo $command | jq -r '. | any(contains("-use-https"))' | tee /dev/stderr)
[ "${actual}" = "true" ]

actual=$(echo $command | jq -r '. | any(contains("-consul-ca-cert=/consul/tls/ca/tls.crt"))' | tee /dev/stderr)
[ "${actual}" = "true" ]

actual=$(echo $command | jq -r '. | any(contains("-server-port=8501"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "partitionInit/Job: can overwrite CA secret with the provided one" {
cd `chart_dir`
local ca_cert_volume=$(helm template \
-s templates/partition-init-job.yaml \
--set 'global.enabled=false' \
--set 'global.adminPartitions.enabled=true' \
--set 'global.tls.enabled=true' \
--set 'global.tls.caCert.secretName=foo-ca-cert' \
--set 'global.tls.caCert.secretKey=key' \
--set 'global.tls.caKey.secretName=foo-ca-key' \
--set 'global.tls.caKey.secretKey=key' \
. | tee /dev/stderr |
yq '.spec.template.spec.volumes[] | select(.name=="consul-ca-cert")' | tee /dev/stderr)

# check that the provided ca cert secret is attached as a volume
local actual
actual=$(echo $ca_cert_volume | jq -r '.secret.secretName' | tee /dev/stderr)
[ "${actual}" = "foo-ca-cert" ]

# check that the volume uses the provided secret key
actual=$(echo $ca_cert_volume | jq -r '.secret.items[0].key' | tee /dev/stderr)
[ "${actual}" = "key" ]
}
Loading