Skip to content

Commit 976361c

Browse files
committed
add support for compatibility to run this chart in openshift
1 parent a3382d8 commit 976361c

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

charts/keycloakx/templates/_helpers.tpl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,39 @@ Create the service DNS name.
7474
key: {{ .Values.database.existingSecretKey | default "password" }}
7575
{{- end }}
7676
{{- end -}}
77+
78+
{{/*
79+
Return true if the detected platform is Openshift
80+
Usage:
81+
{{- include "common.compatibility.isOpenshift" . -}}
82+
*/}}
83+
{{- define "keycloak.isOpenshift" -}}
84+
{{- if .Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
85+
{{- true -}}
86+
{{- end -}}
87+
{{- end -}}
88+
89+
{{/*
90+
Render a compatible securityContext depending on the platform. By default it is maintained as it is. In other platforms like Openshift we remove default user/group values that do not work out of the box with the restricted-v1 SCC
91+
Usage:
92+
{{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) -}}
93+
*/}}
94+
{{- define "keycloak.renderSecurityContext" -}}
95+
{{- $adaptedContext := .secContext -}}
96+
97+
{{- if (((.context.Values).compatibility).openshift) -}}
98+
{{- if or (eq .context.Values.compatibility.openshift.adaptSecurityContext "force") (and (eq .context.Values.compatibility.openshift.adaptSecurityContext "auto") (include "keycloak.isOpenshift" .context)) -}}
99+
{{/* Remove incompatible user/group values that do not work in Openshift out of the box */}}
100+
{{- $adaptedContext = omit $adaptedContext "fsGroup" "runAsUser" "runAsGroup" -}}
101+
{{- if not .secContext.seLinuxOptions -}}
102+
{{/* If it is an empty object, we remove it from the resulting context because it causes validation issues */}}
103+
{{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
104+
{{- end -}}
105+
{{- end -}}
106+
{{- end -}}
107+
{{/* Remove fields that are disregarded when running the container in privileged mode */}}
108+
{{- if $adaptedContext.privileged -}}
109+
{{- $adaptedContext = omit $adaptedContext "capabilities" "seLinuxOptions" -}}
110+
{{- end -}}
111+
{{- omit $adaptedContext "enabled" | toYaml -}}
112+
{{- end -}}

charts/keycloakx/templates/statefulset.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ spec:
4646
- name: dbchecker
4747
image: "{{ .Values.dbchecker.image.repository }}{{- if (.Values.dbchecker.image.digest) -}}@{{ .Values.dbchecker.image.digest }}{{- else -}}:{{ .Values.dbchecker.image.tag }} {{- end }}"
4848
imagePullPolicy: {{ .Values.dbchecker.image.pullPolicy }}
49-
securityContext:
50-
{{- toYaml .Values.dbchecker.securityContext | nindent 12 }}
49+
{{- if .Values.podSecurityContext.enabled }}
50+
securityContext: {{- include "keycloak.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 12 }}
51+
{{- end }}
5152
command:
5253
- sh
5354
- -c
@@ -69,8 +70,9 @@ spec:
6970
{{- end }}
7071
containers:
7172
- name: keycloak
72-
securityContext:
73-
{{- toYaml .Values.securityContext | nindent 12 }}
73+
{{- if .Values.securityContext.enabled }}
74+
securityContext: {{- include "keycloak.renderSecurityContext" (dict "secContext" .Values.securityContext "context" $) | nindent 12 }}
75+
{{- end }}
7476
image: "{{ .Values.image.repository }}{{- if (.Values.image.digest) -}}@{{ .Values.image.digest }}{{- else -}}:{{ .Values.image.tag | default .Chart.AppVersion }} {{- end }}"
7577
imagePullPolicy: {{ .Values.image.pullPolicy }}
7678
{{- if .Values.command }}
@@ -190,8 +192,9 @@ spec:
190192
{{- toYaml . | nindent 8 }}
191193
{{- end }}
192194
serviceAccountName: {{ include "keycloak.serviceAccountName" . }}
193-
securityContext:
194-
{{- toYaml .Values.podSecurityContext | nindent 8 }}
195+
{{- if .Values.podSecurityContext.enabled }}
196+
securityContext: {{- include "keycloak.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
197+
{{- end }}
195198
{{- with .Values.hostAliases }}
196199
hostAliases:
197200
{{- toYaml . | nindent 8 }}

charts/keycloakx/values.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,22 @@ rbac:
6868
# - get
6969
# - list
7070

71+
compatibility:
72+
## Compatibility adaptations for Openshift
73+
##
74+
openshift:
75+
## @param compatibility.openshift.adaptSecurityContext Adapt the securityContext sections of the deployment to make them compatible with Openshift restricted-v2 SCC: remove runAsUser, runAsGroup and fsGroup and let the platform use their allowed default IDs. Possible values: auto (apply if the detected running cluster is Openshift), force (perform the adaptation always), disabled (do not perform adaptation)
76+
##
77+
adaptSecurityContext: auto
78+
7179
# SecurityContext for the entire Pod. Every container running in the Pod will inherit this SecurityContext. This might be relevant when other components of the environment inject additional containers into running Pods (service meshes are the most prominent example for this)
7280
podSecurityContext:
81+
enabled: true
7382
fsGroup: 1000
7483

7584
# SecurityContext for the Keycloak container
7685
securityContext:
86+
enabled: true
7787
runAsUser: 1000
7888
runAsNonRoot: true
7989

0 commit comments

Comments
 (0)