Skip to content

Commit

Permalink
Add OpenShift beta support (#319)
Browse files Browse the repository at this point in the history
* Initial commit

* Added openshift flag

* added self signed certificate for service annotation

* added OpenShift flag

* Added OpenShift flag

* cleanup

* Cleanup

* Further cleanup

* Further cleanup

* reverted security context on injector

* Extra corrections

* cleanup

* Removed Raft config for OpenShift, removed generated certs for ha and standby services

* Add openshift flag to global block, route disabled by default, condition for injector in network policy

* Added Unit tests for OpenShift

* Fixed unit test for HA statefulset for OpenShift

* Removed debug log level from stateful set

* Added port 8201 to networkpolicy

* Updated injector image

* Add openshift beta support

* Add openshift beta support

* Remove comments from configs

* Remove vault-k8s note from values

* Change route to use active service when HA

Co-authored-by: Radu Domnu <[email protected]>
Co-authored-by: Radu Domnu <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2020
1 parent 7f7fb7b commit 853cb06
Show file tree
Hide file tree
Showing 23 changed files with 382 additions and 49 deletions.
15 changes: 15 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ Sets extra ingress annotations
{{- end }}
{{- end -}}

{{/*
Sets extra route annotations
*/}}
{{- define "vault.route.annotations" -}}
{{- if .Values.server.route.annotations }}
annotations:
{{- $tp := typeOf .Values.server.route.annotations }}
{{- if eq $tp "string" }}
{{- tpl .Values.server.route.annotations . | nindent 4 }}
{{- else }}
{{- toYaml .Values.server.route.annotations | nindent 4 }}
{{- end }}
{{- end }}
{{- end -}}

{{/*
Sets extra vault server Service annotations
*/}}
Expand Down
6 changes: 6 additions & 0 deletions templates/injector-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ spec:
priorityClassName: {{ .Values.injector.priorityClassName }}
{{- end }}
serviceAccountName: "{{ template "vault.fullname" . }}-agent-injector"
{{- if not .Values.global.openshift }}
securityContext:
runAsNonRoot: true
runAsGroup: {{ .Values.injector.gid | default 1000 }}
runAsUser: {{ .Values.injector.uid | default 100 }}
{{- end }}
containers:
- name: sidecar-injector
{{ template "injector.resources" . }}
Expand Down Expand Up @@ -70,6 +72,10 @@ spec:
value: {{ .Values.injector.logFormat | default "standard" }}
- name: AGENT_INJECT_REVOKE_ON_SHUTDOWN
value: "{{ .Values.injector.revokeOnShutdown | default false }}"
{{- if .Values.global.openshift }}
- name: AGENT_INJECT_SET_SECURITY_CONTEXT
value: "false"
{{- end }}
{{- include "vault.extraEnvironmentVars" .Values.injector | nindent 12 }}
args:
- agent-inject
Expand Down
21 changes: 21 additions & 0 deletions templates/injector-network-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- if .Values.global.openshift }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ template "vault.fullname" . }}-agent-injector
labels:
app.kubernetes.io/name: {{ template "vault.name" . }}-agent-injector
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: {{ template "vault.name" . }}-agent-injector
app.kubernetes.io/instance: {{ .Release.Name }}
component: webhook
ingress:
- from:
- namespaceSelector: {}
ports:
- port: 8080
protocol: TCP
{{ end }}
2 changes: 2 additions & 0 deletions templates/server-ingress.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .Values.global.openshift }}
{{ template "vault.mode" . }}
{{- if ne .mode "external" }}
{{- if .Values.server.ingress.enabled -}}
Expand Down Expand Up @@ -49,3 +50,4 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- end }}
22 changes: 22 additions & 0 deletions templates/server-network-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{- if .Values.global.openshift }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ template "vault.fullname" . }}
labels:
app.kubernetes.io/name: {{ template "vault.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: {{ template "vault.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
ingress:
- from:
- namespaceSelector: {}
ports:
- port: 8200
protocol: TCP
- port: 8201
protocol: TCP
{{ end }}
33 changes: 33 additions & 0 deletions templates/server-route.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{- if .Values.global.openshift }}
{{- if ne .mode "external" }}
{{- if .Values.server.route.enabled -}}
{{- $serviceName := include "vault.fullname" . -}}
{{- if eq .mode "ha" }}
{{- $serviceName = printf "%s-%s" $serviceName "active" -}}
{{- end }}
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: {{ template "vault.fullname" . }}
labels:
helm.sh/chart: {{ include "vault.chart" . }}
app.kubernetes.io/name: {{ include "vault.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.server.route.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- template "vault.route.annotations" . }}
spec:
host: {{ .Values.server.route.host }}
to:
kind: Service
name: {{ $serviceName }}
weight: 100
port:
targetPort: 8200
tls:
termination: passthrough
{{- end }}
{{- end }}
{{- end }}
8 changes: 8 additions & 0 deletions templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ spec:
{{ if .Values.server.shareProcessNamespace }}
shareProcessNamespace: true
{{ end }}
{{- if not .Values.global.openshift }}
securityContext:
runAsNonRoot: true
runAsGroup: {{ .Values.server.gid | default 1000 }}
runAsUser: {{ .Values.server.uid | default 100 }}
fsGroup: {{ .Values.server.gid | default 1000 }}
{{- end }}
volumes:
{{ template "vault.volumes" . }}
- name: home
emptyDir: {}
{{- if .Values.server.extraInitContainers }}
initContainers:
{{ toYaml .Values.server.extraInitContainers | nindent 8}}
Expand Down Expand Up @@ -100,11 +104,15 @@ spec:
fieldRef:
fieldPath: metadata.name
{{- end }}
- name: HOME
value: "/home/vault"
{{ template "vault.envs" . }}
{{- include "vault.extraEnvironmentVars" .Values.server | nindent 12 }}
{{- include "vault.extraSecretEnvironmentVars" .Values.server | nindent 12 }}
volumeMounts:
{{ template "vault.mounts" . }}
- name: home
mountPath: /home/vault
ports:
- containerPort: 8200
name: {{ include "vault.scheme" . }}
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/injector-test/pg-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spec:
- name: POSTGRES_PASSWORD
value: password
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
- mountPath: "/var/lib/postgresql"
name: "pgdata"
- mountPath: "/docker-entrypoint-initdb.d"
name: "pgconf"
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/server-dev.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ load _helpers
# Volume Mounts
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.containers[0].volumeMounts | length')
[ "${volumeCount}" == "0" ]
[ "${volumeCount}" == "1" ]

# Service
local service=$(kubectl get service "$(name_prefix)" --output json |
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/server-ha-enterprise-dr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load _helpers

helm install "$(name_prefix)-east" \
--set='server.image.repository=hashicorp/vault-enterprise' \
--set='server.image.tag=1.4.0_ent' \
--set='server.image.tag=1.4.2_ent' \
--set='injector.enabled=false' \
--set='server.ha.enabled=true' \
--set='server.ha.raft.enabled=true' .
Expand Down Expand Up @@ -76,7 +76,7 @@ load _helpers
helm install "$(name_prefix)-west" \
--set='injector.enabled=false' \
--set='server.image.repository=hashicorp/vault-enterprise' \
--set='server.image.tag=1.4.0_ent' \
--set='server.image.tag=1.4.2_ent' \
--set='server.ha.enabled=true' \
--set='server.ha.raft.enabled=true' .
wait_for_running "$(name_prefix)-west-0"
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/server-ha-enterprise-perf.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load _helpers
helm install "$(name_prefix)-east" \
--set='injector.enabled=false' \
--set='server.image.repository=hashicorp/vault-enterprise' \
--set='server.image.tag=1.4.0_ent' \
--set='server.image.tag=1.4.2_ent' \
--set='server.ha.enabled=true' \
--set='server.ha.raft.enabled=true' .
wait_for_running "$(name_prefix)-east-0"
Expand Down Expand Up @@ -76,7 +76,7 @@ load _helpers
helm install "$(name_prefix)-west" \
--set='injector.enabled=false' \
--set='server.image.repository=hashicorp/vault-enterprise' \
--set='server.image.tag=1.4.0_ent' \
--set='server.image.tag=1.4.2_ent' \
--set='server.ha.enabled=true' \
--set='server.ha.raft.enabled=true' .
wait_for_running "$(name_prefix)-west-0"
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/server-ha-raft.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ load _helpers
# Volume Mounts
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.containers[0].volumeMounts | length')
[ "${volumeCount}" == "2" ]
[ "${volumeCount}" == "3" ]

# Volumes
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes | length')
[ "${volumeCount}" == "1" ]
[ "${volumeCount}" == "2" ]

local volume=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes[0].configMap.name')
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/server-ha.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ load _helpers
# Volume Mounts
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.containers[0].volumeMounts | length')
[ "${volumeCount}" == "1" ]
[ "${volumeCount}" == "2" ]

# Volumes
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes | length')
[ "${volumeCount}" == "1" ]
[ "${volumeCount}" == "2" ]

local volume=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes[0].configMap.name')
Expand Down
9 changes: 2 additions & 7 deletions test/acceptance/server.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ load _helpers
# Volume Mounts
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.containers[0].volumeMounts | length')
[ "${volumeCount}" == "2" ]
[ "${volumeCount}" == "3" ]

local mountName=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.containers[0].volumeMounts[0].name')
Expand All @@ -47,17 +47,12 @@ load _helpers
# Volumes
local volumeCount=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes | length')
[ "${volumeCount}" == "1" ]
[ "${volumeCount}" == "2" ]

local volume=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.volumes[0].configMap.name')
[ "${volume}" == "$(name_prefix)-config" ]

# Security Context
local fsGroup=$(kubectl get statefulset "$(name_prefix)" --output json |
jq -r '.spec.template.spec.securityContext.fsGroup')
[ "${fsGroup}" == "1000" ]

# Service
local service=$(kubectl get service "$(name_prefix)" --output json |
jq -r '.spec.clusterIP')
Expand Down
35 changes: 35 additions & 0 deletions test/unit/injector-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,19 @@ load _helpers
[ "${actual}" = "true" ]
}

@test "injector/deployment: disable security context when openshift enabled" {
cd `chart_dir`
local object=$(helm template \
--show-only templates/injector-deployment.yaml \
--set 'global.openshift=true' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers[0].env' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.[9].name' | tee /dev/stderr)
[ "${actual}" = "AGENT_INJECT_SET_SECURITY_CONTEXT" ]
}

#--------------------------------------------------------------------
# extraEnvironmentVars

Expand Down Expand Up @@ -447,3 +460,25 @@ load _helpers
yq '.spec.template.spec | .priorityClassName == "armaggeddon"' | tee /dev/stderr)
[ "${actual}" = "true" ]
}
#--------------------------------------------------------------------
# OpenShift

@test "injector/deployment: OpenShift - runAsUser disabled" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/injector-deployment.yaml \
--set 'global.openshift=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.securityContext.runAsUser | length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "injector/deployment: OpenShift - runAsGroup disabled" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/injector-deployment.yaml \
--set 'global.openshift=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.securityContext.runAsGroup | length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}
22 changes: 12 additions & 10 deletions test/unit/server-dev-statefulset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,19 @@ load _helpers
yq -r '.spec.template.spec.containers[0].env' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.[11].name' | tee /dev/stderr)
yq -r '.[12].name' | tee /dev/stderr)
[ "${actual}" = "FOO" ]

local actual=$(echo $object |
yq -r '.[11].value' | tee /dev/stderr)
yq -r '.[12].value' | tee /dev/stderr)
[ "${actual}" = "bar" ]

local actual=$(echo $object |
yq -r '.[12].name' | tee /dev/stderr)
yq -r '.[13].name' | tee /dev/stderr)
[ "${actual}" = "FOOBAR" ]

local actual=$(echo $object |
yq -r '.[12].value' | tee /dev/stderr)
yq -r '.[13].value' | tee /dev/stderr)
[ "${actual}" = "foobar" ]
}

Expand All @@ -282,23 +282,25 @@ load _helpers
yq -r '.spec.template.spec.containers[0].env' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.[10].name' | tee /dev/stderr)
yq -r '.[11].name' | tee /dev/stderr)
[ "${actual}" = "ENV_FOO_0" ]
local actual=$(echo $object |
yq -r '.[10].valueFrom.secretKeyRef.name' | tee /dev/stderr)
yq -r '.[11].valueFrom.secretKeyRef.name' | tee /dev/stderr)
[ "${actual}" = "secret_name_0" ]
local actual=$(echo $object |
yq -r '.[10].valueFrom.secretKeyRef.key' | tee /dev/stderr)
yq -r '.[11].valueFrom.secretKeyRef.key' | tee /dev/stderr)
[ "${actual}" = "secret_key_0" ]

local actual=$(echo $object |
yq -r '.[11].name' | tee /dev/stderr)
yq -r '.[12].name' | tee /dev/stderr)
[ "${actual}" = "ENV_FOO_1" ]

local actual=$(echo $object |
yq -r '.[11].valueFrom.secretKeyRef.name' | tee /dev/stderr)
yq -r '.[12].valueFrom.secretKeyRef.name' | tee /dev/stderr)
[ "${actual}" = "secret_name_1" ]

local actual=$(echo $object |
yq -r '.[11].valueFrom.secretKeyRef.key' | tee /dev/stderr)
yq -r '.[12].valueFrom.secretKeyRef.key' | tee /dev/stderr)
[ "${actual}" = "secret_key_1" ]
}

Expand Down
Empty file modified test/unit/server-ha-active-service.bats
100644 → 100755
Empty file.
Empty file modified test/unit/server-ha-standby-service.bats
100644 → 100755
Empty file.
Loading

0 comments on commit 853cb06

Please sign in to comment.