Skip to content

Commit

Permalink
Extract the nextcloud container definition to a helper template
Browse files Browse the repository at this point in the history
So that we can keep as much as possible in sync between the deployment
and the cronJob.

Signed-off-by: Martin Kirchner <martin.kirchner@cas.de>
  • Loading branch information
Martin Kirchner committed Mar 4, 2025
1 parent e788e63 commit 266d128
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 64 deletions.
46 changes: 46 additions & 0 deletions charts/nextcloud/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,50 @@ serviceAccountName: {{ .Values.rbac.serviceaccount.name }}
dnsConfig:
{{- toYaml . | nindent 2 }}
{{- end }}
{{- end -}}


{{/*
Create nextcloud container definition for deployment and cronjob.
Pass these parameters in the dict:
- containerName: name of the container
- context: Pointer to the context in the values.yaml where "resources, lifecycle, securityContext" can be found
- rootContext: $ (Inside a template the scope changes, i.e. you cannot access variables of the parent context or its parents.
Unfortunately this is also the case for the root context, this means .Values, .Release, .Chart cannot be accessed.
However the other templates need values from the objects. That's why the caller has to pass on reference to the root context which this template in turn passes on.)
- command: Optional
*/}}
{{- define "nextcloud.container" -}}
- name: {{ .containerName }}
image: {{ include "nextcloud.image" .rootContext }}
imagePullPolicy: {{ .rootContext.Values.image.pullPolicy }}
{{- if .context.command }}
command:
{{- toYaml .context.command | nindent 4 }}
{{- end }}
{{- with .context.lifecycle }}
lifecycle:
{{- with .postStartCommand }}
postStart:
exec:
command:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- with .preStopCommand }}
preStop:
exec:
command:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- end }}
env:
{{- include "nextcloud.env" .rootContext | nindent 4 }}
resources:
{{- toYaml .context.resources | nindent 4 }}
{{- with .context.securityContext }}
securityContext:
{{- toYaml . | nindent 4 }}
{{- end }}
volumeMounts:
{{- include "nextcloud.volumeMounts" .rootContext | trim | nindent 4 }}
{{- end -}}
32 changes: 2 additions & 30 deletions charts/nextcloud/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,8 @@ spec:
{{- include "nextcloud.deployment.template.metadata" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}-cron
image: {{ include "nextcloud.image" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
command:
{{- toYaml .Values.cronjob.command | nindent 14 }}
{{- with .Values.cronjob.lifecycle }}
lifecycle:
{{- with .postStartCommand }}
postStart:
exec:
command:
{{- toYaml . | nindent 20 }}
{{- end }}
{{- with .preStopCommand }}
preStop:
exec:
command:
{{- toYaml . | nindent 20 }}
{{- end }}
{{- end }}
env:
{{- include "nextcloud.env" . | nindent 14 }}
resources:
{{- toYaml .Values.cronjob.resources | nindent 14 }}
{{- with .Values.cronjob.securityContext }}
securityContext:
{{- toYaml . | nindent 14 }}
{{- end }}
volumeMounts:
{{- include "nextcloud.volumeMounts" . | trim | nindent 14 }}
{{- $containerName := printf "%s-cron" .Chart.Name }}
{{- include "nextcloud.container" ( dict "containerName" $containerName "rootContext" $ "context" .Values.cronjob ) | nindent 10 }}
{{- include "nextcloud.pod.commons" . | nindent 10 }}
restartPolicy: OnFailure
{{- end }}{{/* end-if cronjob.enabled */}}
42 changes: 8 additions & 34 deletions charts/nextcloud/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,16 @@ spec:
{{- include "nextcloud.deployment.template.metadata" . | nindent 4 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ include "nextcloud.image" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- with .Values.lifecycle }}
lifecycle:
{{- with .postStartCommand }}
postStart:
exec:
command:
{{- toYaml . | nindent 18 }}
{{- $containerName := .Chart.Name }}
{{- include "nextcloud.container" ( dict "containerName" $containerName "rootContext" $ "context" .Values ) | nindent 8 }}
{{- range $hook, $shell := .Values.nextcloud.hooks }}
{{- if $shell }}
- name: nextcloud-hooks
mountPath: /docker-entrypoint-hooks.d/{{ $hook }}/helm.sh
subPath: {{ $hook }}.sh
readOnly: true
{{- end }}
{{- with .preStopCommand }}
preStop:
exec:
command:
{{- toYaml . | nindent 18 }}
{{- end }}
{{- end }}
env:
{{- include "nextcloud.env" . | nindent 12 }}
{{- if not .Values.nginx.enabled }}
ports:
- name: http
Expand Down Expand Up @@ -99,22 +89,6 @@ spec:
{{- end }}
{{- end }}
{{- end }}{{/* end-if not nginx.enabled */}}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nextcloud.securityContext }}
securityContext:
{{- toYaml . | nindent 12 }}
{{- end }}
volumeMounts:
{{- include "nextcloud.volumeMounts" . | trim | nindent 12 }}
{{- range $hook, $shell := .Values.nextcloud.hooks }}
{{- if $shell }}
- name: nextcloud-hooks
mountPath: /docker-entrypoint-hooks.d/{{ $hook }}/helm.sh
subPath: {{ $hook }}.sh
readOnly: true
{{- end }}
{{- end }}
{{- if .Values.nginx.enabled }}
- name: {{ .Chart.Name }}-nginx
image: "{{ .Values.nginx.image.repository }}:{{ .Values.nginx.image.tag }}"
Expand Down

0 comments on commit 266d128

Please sign in to comment.