Skip to content

Commit f1bb75e

Browse files
pascalfreundsrfPascal Freund
andauthored
[common] Fix/set securitycontext based on targetplatform to comply with openshift clusters (CloudPirates-io#166)
* Add a helper function to render the securitycontext based on the target platform for openshift compatibility * Bump Chartversion * Fix openshift detection function * Remove unneeded indent * Add Readme to common helm chart * Add newline to common values.yaml * Revert SecurityContext changes that the github action check dosn't fails * Bump zookeeper and nginx Chart version --------- Co-authored-by: Pascal Freund <[email protected]>
1 parent b8ca89e commit f1bb75e

File tree

9 files changed

+70
-18
lines changed

9 files changed

+70
-18
lines changed

charts/common/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: common
33
description: A library chart for common templates and helper functions
44
type: library
5-
version: 1.0.0
5+
version: 1.1.0
66
appVersion: "1.0.0"
77

88
home: https://www.cloudpirates.io

charts/common/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Common Helm Chart
2+
3+
### Example config for OpenShift Clusters
4+
The `_helpers.tpl` detects for `route.openshift.io/v1` to determine the target platform.
5+
If the target platform is Openshift, following fields are beeing removed if you use `{{ include "common.renderContainerSecurityContext" . }}` or `{{ include "common.renderPodSecurityContext" . }}` in the Chart to render the SecurityContext.
6+
```yaml
7+
fsGroup:
8+
runAsUser:
9+
runAsGroup:
10+
seLinuxOptions:
11+
```
12+
13+
Example usage:
14+
```yaml
15+
apiVersion: apps/v1
16+
kind: StatefulSet
17+
spec:
18+
template:
19+
spec:
20+
securityContext: {{ include "common.renderPodSecurityContext" . }}
21+
containers:
22+
- name: {{ .Chart.Name }}
23+
securityContext: {{ include "common.renderContainerSecurityContext" . }}
24+
```

charts/common/templates/_helpers.tpl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,42 @@ imagePullSecrets:
247247
- name: {{ . }}
248248
{{- end }}
249249
{{- end }}
250-
{{- end -}}
250+
{{- end -}}
251+
252+
{{/*
253+
Detect if the target platform is OpenShift (via .Values.targetPlatform or API group).
254+
Usage: {{ include "common.isOpenshift" . }}
255+
*/}}
256+
{{- define "common.isOpenshift" -}}
257+
{{- if or (eq (lower (default "" .Values.targetPlatform)) "openshift") (.Capabilities.APIVersions.Has "route.openshift.io/v1") -}}
258+
true
259+
{{- else -}}
260+
false
261+
{{- end -}}
262+
{{- end }}
263+
264+
{{/*
265+
Render podSecurityContext, omitting runAsUser, runAsGroup, fsGroup, and seLinuxOptions if OpenShift is detected.
266+
Usage: {{ include "common.renderPodSecurityContext" . }}
267+
*/}}
268+
{{- define "common.renderPodSecurityContext" -}}
269+
{{- $isOpenshift := include "common.isOpenshift" . | trim }}
270+
{{- if eq $isOpenshift "true" }}
271+
{{- omit .Values.podSecurityContext "runAsUser" "runAsGroup" "fsGroup" "seLinuxOptions" | toYaml }}
272+
{{- else }}
273+
{{- toYaml .Values.podSecurityContext | nindent 8 }}
274+
{{- end }}
275+
{{- end }}
276+
277+
{{/*
278+
Render containerSecurityContext, omitting runAsUser, runAsGroup, and seLinuxOptions if OpenShift is detected.
279+
Usage: {{ include "common.renderContainerSecurityContext" . }}
280+
*/}}
281+
{{- define "common.renderContainerSecurityContext" -}}
282+
{{- $isOpenshift := include "common.isOpenshift" . | trim }}
283+
{{- if eq $isOpenshift "true" }}
284+
{{- omit .Values.containerSecurityContext "runAsUser" "runAsGroup" "seLinuxOptions" | toYaml }}
285+
{{- else }}
286+
{{- toYaml .Values.containerSecurityContext | nindent 12 }}
287+
{{- end }}
288+
{{- end }}

charts/common/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# This is a library chart, so it doesn't define any values.
2-
# All values are defined in the consuming charts.
2+
# All values are defined in the consuming charts.

charts/nginx/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: nginx
33
description: Nginx is a high-performance HTTP server and reverse proxy.
44
type: application
5-
version: 0.1.7
5+
version: 0.1.8
66
appVersion: "1.29.1"
77
keywords:
88
- nginx

charts/nginx/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,4 @@ spec:
263263
{{- with .Values.tolerations }}
264264
tolerations:
265265
{{- toYaml . | nindent 8 }}
266-
{{- end }}
266+
{{- end }}

charts/zookeeper/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: zookeeper
33
description: Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
44
type: application
5-
version: 0.1.1
5+
version: 0.1.2
66
appVersion: "3.9.3"
77
keywords:
88
- zookeeper

charts/zookeeper/README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ To install with custom values:
2323
helm install my-zookeeper ./charts/zookeeper -f values.yaml
2424
```
2525

26-
#### Example config for OpenShift Clusters
27-
To run this chart in an OpenShift cluster, the following security values must be empty:
28-
```yaml
29-
zookeeper:
30-
containerSecurityContext:
31-
runAsUser:
32-
runAsGroup:
33-
seLinuxOptions:
34-
podSecurityContext:
35-
fsGroup:
36-
```
37-
3826

3927
### Getting Started
4028

charts/zookeeper/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ metrics:
7878
## @param metrics.service.ports.port Zookeeper metrics service port
7979
port: 7000
8080

81+
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container
8182
## @section MariaDB Container Security Context
8283
containerSecurityContext:
8384
## @param containerSecurityContext.runAsUser Set MariaDB container's Security Context runAsUser
@@ -102,6 +103,7 @@ containerSecurityContext:
102103
seccompProfile:
103104
type: RuntimeDefault
104105

106+
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
105107
## @section Security Context
106108
podSecurityContext:
107109
## @param podSecurityContext.fsGroup Group ID for the volumes of the pod

0 commit comments

Comments
 (0)