Skip to content

Commit

Permalink
chore: Add security improvements, openshift & resource presets to cha…
Browse files Browse the repository at this point in the history
…rt template (bitnami#24759)
  • Loading branch information
juan131 authored Apr 1, 2024
1 parent 7e7a609 commit ca757c9
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 93 deletions.
25 changes: 18 additions & 7 deletions template/CHART_NAME/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ spec:
labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
app.kubernetes.io/component: %%COMPONENT_NAME%%
spec:
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
{{- include "%%TEMPLATE_NAME%%.imagePullSecrets" . | nindent 6 }}
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
automountServiceAccountToken: {{ .Values.%%MAIN_OBJECT_BLOCK%%.automountServiceAccountToken }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases }}
hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases "context" $) | nindent 8 }}
{{- end }}
Expand Down Expand Up @@ -65,13 +66,16 @@ spec:
imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
command:
- %%commands%%
securityContext:
runAsUser: 0
{{- if .Values.volumePermissions.containerSecurityContext.enabled }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.volumePermissions.resources }}
resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
{{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
Expand All @@ -85,7 +89,7 @@ spec:
image: {{ template "%%TEMPLATE_NAME%%.image" . }}
imagePullPolicy: {{ .Values.%%MAIN_OBJECT_BLOCK%%.image.pullPolicy }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "enabled" | toYaml | nindent 12 }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.diagnosticMode.enabled }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
Expand Down Expand Up @@ -119,6 +123,8 @@ spec:
protocol: bar
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.resources }}
resources: {{- toYaml .Values.%%MAIN_OBJECT_BLOCK%%.resources | nindent 12 }}
{{- else if ne .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset) | nindent 12 }}
{{- end }}
{{- if not .Values.diagnosticMode.enabled }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.customLivenessProbe }}
Expand All @@ -136,20 +142,25 @@ spec:
{{- end }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
readOnly: true
- name: empty-dir
mountPath: /tmp
subPath: tmp-dir
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.sidecars }}
{{- include "common.tplvalues.render" ( dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.sidecars "context" $) | nindent 8 }}
{{- end }}
volumes:
- name: foo
- name: empty-dir
emptyDir: {}
- name: data
{{- if .Values.persistence.enabled }}
persistentVolumeClaim:
claimName: {{ default (include "common.names.fullname" .) .Values.persistence.existingClaim }}
Expand Down
25 changes: 18 additions & 7 deletions template/CHART_NAME/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ spec:
labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
app.kubernetes.io/component: %%COMPONENT_NAME%%
spec:
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
{{- include "%%TEMPLATE_NAME%%.imagePullSecrets" . | nindent 6 }}
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
automountServiceAccountToken: {{ .Values.%%MAIN_OBJECT_BLOCK%%.automountServiceAccountToken }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases }}
hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases "context" $) | nindent 8 }}
{{- end }}
Expand Down Expand Up @@ -73,13 +74,16 @@ spec:
image: {{ include "%%TEMPLATE_NAME%%.volumePermissions.image" . }}
imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
command:
- %%commands%%
securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
{{- if .Values.volumePermissions.containerSecurityContext.enabled }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.volumePermissions.resources }}
resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
{{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
Expand All @@ -93,7 +97,7 @@ spec:
image: {{ template "%%TEMPLATE_NAME%%.image" . }}
imagePullPolicy: {{ .Values.%%MAIN_OBJECT_BLOCK%%.image.pullPolicy }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "enabled" | toYaml | nindent 12 }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.diagnosticMode.enabled }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
Expand Down Expand Up @@ -124,6 +128,8 @@ spec:
{{- end }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.resources }}
resources: {{- toYaml .Values.%%MAIN_OBJECT_BLOCK%%.resources | nindent 12 }}
{{- else if ne .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset) | nindent 12 }}
{{- end }}
ports:
- name: http
Expand Down Expand Up @@ -154,19 +160,24 @@ spec:
lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.lifecycleHooks "context" $) | nindent 12 }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
- name: empty-dir
mountPath: /tmp
subPath: tmp-dir
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.sidecars }}
{{- include "common.tplvalues.render" ( dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.sidecars "context" $) | nindent 8 }}
{{- end }}
volumes:
- name: foo
- name: empty-dir
emptyDir: {}
- name: data
{{- if .Values.persistence.enabled }}
persistentVolumeClaim:
claimName: {{ default (include "common.names.fullname" .) .Values.persistence.existingClaim }}
Expand Down
68 changes: 68 additions & 0 deletions template/CHART_NAME/templates/networkpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{{- /*
Copyright VMware, Inc.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.networkPolicy.enabled }}
kind: NetworkPolicy
apiVersion: {{ include "common.capabilities.networkPolicy.apiVersion" . }}
metadata:
name: {{ template "common.names.fullname" . }}
namespace: {{ include "common.names.namespace" . | quote }}
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
app.kubernetes.io/component: %%COMPONENT_NAME%%
{{- if .Values.commonAnnotations }}
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
spec:
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.%%MAIN_OBJECT_BLOCK%%.podLabels .Values.commonLabels ) "context" . ) }}
podSelector:
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
app.kubernetes.io/component: %%COMPONENT_NAME%%
policyTypes:
- Ingress
- Egress
egress:
{{- if .Values.networkPolicy.allowExternalEgress }}
- {}
{{- else }}
- ports:
# Allow dns resolution
- port: 53
protocol: UDP
- port: 53
protocol: TCP
{{- if .Values.networkPolicy.extraEgress }}
{{- include "common.tplvalues.render" ( dict "value" .Values.networkPolicy.extraEgress "context" $ ) | nindent 4 }}
{{- end }}
{{- end }}
ingress:
- ports:
- port: {{ .Values.%%MAIN_OBJECT_BLOCK%%.containerPorts.http }}
- port: {{ .Values.%%MAIN_OBJECT_BLOCK%%.containerPorts.https }}
{{- if not .Values.networkPolicy.allowExternal }}
from:
- podSelector:
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 14 }}
- podSelector:
matchLabels:
{{ template "common.names.fullname" . }}-client: "true"
{{- if .Values.networkPolicy.ingressNSMatchLabels }}
- namespaceSelector:
matchLabels:
{{- range $key, $value := .Values.networkPolicy.ingressNSMatchLabels }}
{{ $key | quote }}: {{ $value | quote }}
{{- end }}
{{- if .Values.networkPolicy.ingressNSPodMatchLabels }}
podSelector:
matchLabels:
{{- range $key, $value := .Values.networkPolicy.ingressNSPodMatchLabels }}
{{ $key | quote }}: {{ $value | quote }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.networkPolicy.extraIngress }}
{{- include "common.tplvalues.render" ( dict "value" .Values.networkPolicy.extraIngress "context" $ ) | nindent 4 }}
{{- end }}
{{- end }}
24 changes: 17 additions & 7 deletions template/CHART_NAME/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ spec:
labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
app.kubernetes.io/component: %%COMPONENT_NAME%%
spec:
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
{{- include "%%TEMPLATE_NAME%%.imagePullSecrets" . | nindent 6 }}
serviceAccountName: {{ template "%%TEMPLATE_NAME%%.serviceAccountName" . }}
automountServiceAccountToken: {{ .Values.%%MAIN_OBJECT_BLOCK%%.automountServiceAccountToken }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases }}
hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.hostAliases "context" $) | nindent 8 }}
{{- end }}
Expand Down Expand Up @@ -76,14 +77,16 @@ spec:
imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
command:
- %%commands%%
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "enabled" | toYaml | nindent 12 }}
{{- if .Values.volumePermissions.containerSecurityContext.enabled }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.volumePermissions.resources }}
resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
{{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
Expand All @@ -97,7 +100,7 @@ spec:
image: {{ template "%%TEMPLATE_NAME%%.image" . }}
imagePullPolicy: {{ .Values.%%MAIN_OBJECT_BLOCK%%.image.pullPolicy }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "enabled" | toYaml | nindent 12 }}
securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.%%MAIN_OBJECT_BLOCK%%.containerSecurityContext "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.diagnosticMode.enabled }}
command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
Expand Down Expand Up @@ -128,6 +131,8 @@ spec:
{{- end }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.resources }}
resources: {{- toYaml .Values.%%MAIN_OBJECT_BLOCK%%.resources | nindent 12 }}
{{- else if ne .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset "none" }}
resources: {{- include "common.resources.preset" (dict "type" .Values.%%MAIN_OBJECT_BLOCK%%.resourcesPreset) | nindent 12 }}
{{- end }}
ports:
- name: http
Expand Down Expand Up @@ -158,20 +163,25 @@ spec:
lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.lifecycleHooks "context" $) | nindent 12 }}
{{- end }}
volumeMounts:
- name: foo
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- if .Values.persistence.subPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
readOnly: true
- name: empty-dir
mountPath: /tmp
subPath: tmp-dir
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts }}
{{- include "common.tplvalues.render" (dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.extraVolumeMounts "context" $) | nindent 12 }}
{{- end }}
{{- if .Values.%%MAIN_OBJECT_BLOCK%%.sidecars }}
{{- include "common.tplvalues.render" ( dict "value" .Values.%%MAIN_OBJECT_BLOCK%%.sidecars "context" $) | nindent 8 }}
{{- end }}
volumes:
- name: foo
- name: empty-dir
emptyDir: {}
- name: data
{{- if .Values.persistence.enabled }}
persistentVolumeClaim:
claimName: {{ default (include "common.names.fullname" .) .Values.persistence.existingClaim }}
Expand Down
Loading

0 comments on commit ca757c9

Please sign in to comment.