Skip to content

Commit 70fc7ed

Browse files
author
ajruckman
committed
OpenShift compat
1 parent e44cff1 commit 70fc7ed

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

valkey/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ A Helm chart for Kubernetes
1313
| affinity | object | `{}` | |
1414
| auth.aclConfig | string | `"# Users and permissions can be defined here\n# Example:\n# user default off\n# user default on >defaultpassword ~* &* +@all \n"` | |
1515
| auth.enabled | bool | `false` | |
16+
| compat.openshift.adaptSecurityContext | string | `"auto"` | |
1617
| dataStorage.accessModes[0] | string | `"ReadWriteOnce"` | |
1718
| dataStorage.annotations | object | `{}` | |
1819
| dataStorage.className | string | `nil` | |
@@ -47,11 +48,13 @@ A Helm chart for Kubernetes
4748
| nodeSelector | object | `{}` | |
4849
| podAnnotations | object | `{}` | |
4950
| podLabels | object | `{}` | |
51+
| podSecurityContext.enabled | bool | `true` | |
5052
| podSecurityContext.fsGroup | int | `1000` | |
5153
| podSecurityContext.runAsGroup | int | `1000` | |
5254
| podSecurityContext.runAsUser | int | `1000` | |
5355
| replicaCount | int | `1` | |
5456
| resources | object | `{}` | |
57+
| securityContext.enabled | bool | `true` | |
5558
| securityContext.capabilities.drop[0] | string | `"ALL"` | |
5659
| securityContext.readOnlyRootFilesystem | bool | `true` | |
5760
| securityContext.runAsNonRoot | bool | `true` | |

valkey/templates/_helpers.tpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,31 @@ Creating Image Pull Secrets
7878
{{- end }}
7979
{{- end }}
8080

81+
{{/*
82+
Detect if running on OpenShift by checking for the security.openshift.io/v1 API
83+
*/}}
84+
{{- define "valkey.compat.isOpenshift" -}}
85+
{{- if .Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
86+
{{- true -}}
87+
{{- end -}}
88+
{{- end -}}
89+
90+
{{/*
91+
Render a securityContext that is compatible with the target
92+
Returns nothing if the resulting map is empty after adaptation
93+
*/}}
94+
{{- define "valkey.compat.renderSecurityContext" -}}
95+
{{- $adaptedContext := .secContext -}}
96+
{{- if (((.context.Values).compat).openshift) -}}
97+
{{- if or (eq .context.Values.compat.openshift.adaptSecurityContext "force") (and (eq .context.Values.compat.openshift.adaptSecurityContext "auto") (include "valkey.compat.isOpenshift" .context)) -}}
98+
{{- $adaptedContext = omit $adaptedContext "fsGroup" "runAsUser" "runAsGroup" -}}
99+
{{- if not .secContext.seLinuxOptions -}}
100+
{{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
101+
{{- end -}}
102+
{{- end -}}
103+
{{- end -}}
104+
{{- $final := omit $adaptedContext "enabled" -}}
105+
{{- if gt (len $final) 0 -}}
106+
{{- $final | toYaml -}}
107+
{{- end -}}
108+
{{- end -}}

valkey/templates/deploy_valkey.yaml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ spec:
1919
{{- with .Values.podLabels }}
2020
{{- toYaml . | nindent 8 }}
2121
{{- end }}
22-
{{- with .Values.podAnnotations }}
2322
annotations:
23+
{{- with .Values.podAnnotations }}
2424
{{- toYaml . | nindent 8 }}
25-
{{- end }}
25+
{{- end }}
2626
checksum/initconfig: {{ include (print $.Template.BasePath "/init_config.yaml") . | sha256sum | trunc 32 }}
2727
spec:
2828
{{- with .Values.imagePullSecrets }}
@@ -31,16 +31,26 @@ spec:
3131
{{- end }}
3232
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
3333
serviceAccountName: {{ include "valkey.serviceAccountName" . }}
34+
{{- $psc := include "valkey.compat.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | trim -}}
35+
{{- $pscVals := .Values.podSecurityContext -}}
36+
{{- $pscHasEnabled := hasKey $pscVals "enabled" -}}
37+
{{- $pscEnabled := ternary true (get $pscVals "enabled") (not $pscHasEnabled) -}}
38+
{{- if and $pscEnabled ($psc) }}
3439
securityContext:
35-
{{- toYaml .Values.podSecurityContext | nindent 8 }}
36-
40+
{{- $psc | nindent 8 }}
41+
{{- end }}
42+
3743
initContainers:
3844
- name: {{ .Chart.Name }}-init
3945
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
4046
imagePullPolicy: {{ .Values.image.pullPolicy }}
41-
{{- with .Values.securityContext }}
47+
{{- $cscInit := include "valkey.compat.renderSecurityContext" (dict "secContext" .Values.securityContext "context" $) | trim -}}
48+
{{- $cscVals := .Values.securityContext -}}
49+
{{- $cscHasEnabled := hasKey $cscVals "enabled" -}}
50+
{{- $cscEnabled := ternary true (get $cscVals "enabled") (not $cscHasEnabled) -}}
51+
{{- if and $cscEnabled ($cscInit) }}
4252
securityContext:
43-
{{- toYaml . | nindent 12 }}
53+
{{- $cscInit | nindent 12 }}
4454
{{- end }}
4555
command: [ "/scripts/init.sh" ]
4656
volumeMounts:
@@ -71,8 +81,14 @@ spec:
7181
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
7282
command: [ "valkey-server" ]
7383
args: [ "/data/conf/valkey.conf" ]
84+
{{- $csc := include "valkey.compat.renderSecurityContext" (dict "secContext" .Values.securityContext "context" $) | trim -}}
85+
{{- $cscVals2 := .Values.securityContext -}}
86+
{{- $cscHasEnabled2 := hasKey $cscVals2 "enabled" -}}
87+
{{- $cscEnabled2 := ternary true (get $cscVals2 "enabled") (not $cscHasEnabled2) -}}
88+
{{- if and $cscEnabled2 ($csc) }}
7489
securityContext:
75-
{{- toYaml .Values.securityContext | nindent 12 }}
90+
{{- $csc | nindent 12 }}
91+
{{- end }}
7692
env:
7793
{{- range $key, $val := .Values.env }}
7894
- name: {{ $key }}

valkey/values.schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"type": "object",
44
"properties": {
5+
"compat": {
6+
"type": "object",
7+
"properties": {
8+
"openshift": {
9+
"type": "object",
10+
"properties": {
11+
"adaptSecurityContext": {
12+
"type": "string",
13+
"enum": ["auto", "force", "disabled"]
14+
}
15+
}
16+
}
17+
}
18+
},
519
"affinity": {
620
"type": "object"
721
},
@@ -123,6 +137,9 @@
123137
"podSecurityContext": {
124138
"type": "object",
125139
"properties": {
140+
"enabled": {
141+
"type": "boolean"
142+
},
126143
"fsGroup": {
127144
"type": "integer"
128145
},
@@ -143,6 +160,9 @@
143160
"securityContext": {
144161
"type": "object",
145162
"properties": {
163+
"enabled": {
164+
"type": "boolean"
165+
},
146166
"capabilities": {
147167
"type": "object",
148168
"properties": {

valkey/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,30 @@ podLabels: {}
3232

3333
# Security context for the pod (applies to all containers)
3434
podSecurityContext:
35+
enabled: true
3536
fsGroup: 1000
3637
runAsUser: 1000
3738
runAsGroup: 1000
3839

3940
# Security context for the Valkey containers
4041
securityContext:
42+
enabled: true
4143
capabilities:
4244
drop:
4345
- ALL
4446
readOnlyRootFilesystem: true
4547
runAsNonRoot: true
4648
runAsUser: 1000
4749

50+
# Compatibility adaptations for Kubernetes platforms
51+
compat:
52+
# Adaptations for OpenShift
53+
openshift:
54+
# Adapt the securityContext sections to make them compatible with OpenShift restricted-v2 SCC.
55+
# Removes runAsUser, runAsGroup and fsGroup to let the platform use allowed default IDs.
56+
# Possible values: auto (detect OpenShift), force (always adapt), disabled (never adapt).
57+
adaptSecurityContext: auto
58+
4859
service:
4960
# Type of Kubernetes service (ClusterIP, NodePort, LoadBalancer)
5061
type: ClusterIP

0 commit comments

Comments
 (0)