make update / install a dedicated job on k8s

This commit is contained in:
Jonas Leder 2026-08-08 21:13:04 +02:00
commit 1b02e245ce
8 changed files with 293 additions and 155 deletions

View file

@ -60,7 +60,10 @@ COPY --chown=root:$SYSTEM_USERNAME --chmod=0640 opcache.ini /usr/local/etc/php/c
COPY --chown=root:root --chmod=0644 Caddyfile /etc/caddy/Caddyfile
COPY --from=download nextcloud/ /var/www/html/
COPY --chown=root:$SYSTEM_USERNAME --chmod=0640 ./config-patch.php /config-patch.php
COPY --chown=root:$SYSTEM_USERNAME --chmod=0640 common.sh /usr/local/bin/common.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 start.sh /usr/local/bin/start.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 setup.sh /usr/local/bin/setup.sh
COPY --chown=root:$SYSTEM_USERNAME --chmod=0750 serve.sh /usr/local/bin/serve.sh
RUN rm -rf /var/www/html/config && \
chown -R root:$SYSTEM_USERNAME /var/www/html && \

126
common.sh Normal file
View file

@ -0,0 +1,126 @@
#!/bin/ash
set -eu
umask 027
NEXTCLOUD_OVERWRITE_PROTOCOL="${NEXTCLOUD_OVERWRITE_PROTOCOL:-https}"
NC_SERVERID="${POD_NAME:+${POD_NAME##*-}}"
NC_SERVERID="${NC_SERVERID:-0}"
export NC_SERVERID
write_php_ini() {
cat > /usr/local/etc/php/conf.d/zz-nextcloud.ini << EOF
output_buffering = Off
display_errors = ${PHP_DISPLAY_ERRORS}
display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}
error_reporting = ${PHP_ERROR_REPORTING}
memory_limit = ${PHP_MEMORY_LIMIT}
upload_max_filesize = ${PHP_MAX_UPLOAD}
max_file_uploads = ${PHP_MAX_FILE_UPLOAD}
post_max_size = ${PHP_MAX_POST}
expose_php = ${PHP_EXPOSE_INFO}
apc.enable_cli = 1
apc.shm_size = ${PHP_APC_CACHE_SIZE}
cgi.fix_pathinfo = 0
session.save_handler = redis
session.save_path = "tcp://${REDIS_HOST}:6379/${REDIS_NEXTCLOUD_DBINDEX}"
EOF
}
ensure_data_dirs() {
mkdir -p /data/config
mkdir -p /data/data
mkdir -p /data/apps
mkdir -p /data/themes
}
install_nextcloud() {
if [ -f "/data/config/config.php" ]; then
return 0
fi
echo "Installing nextcloud, please wait one moment"
touch /data/config/CAN_INSTALL
while ! nc -z "$NEXTCLOUD_DB_HOST" "5432"; do
echo "Waiting for the postgres db to be available..."
sleep 1
done
sleep 5
./occ maintenance:install --database='pgsql' --database-name="$NEXTCLOUD_DB_NAME" --database-user="$NEXTCLOUD_DB_USER" \
--database-pass="$NEXTCLOUD_DB_PASS" --database-host="$NEXTCLOUD_DB_HOST" --admin-user="$NEXTCLOUD_ADMIN_USER" \
--admin-pass="$NEXTCLOUD_ADMIN_PASS" --data-dir="/data/data"
./occ config:system:set maintenance_window_start --type=integer --value=1
./occ background:cron
php /config-patch.php
./occ app:install notify_push
echo "Install done"
}
upgrade_nextcloud() {
echo "Executing Nextcloud Update"
./occ upgrade
echo "Patching nextcloud config file"
./occ config:system:set trusted_domains 0 --value="$NEXTCLOUD_DOMAIN"
php /config-patch.php
echo "Executing nextcloud repair jobs"
./occ maintenance:repair --include-expensive
./occ db:add-missing-indices
./occ maintenance:mode --off
}
setup_notify_push() {
if [ "${NOTIFY_PUSH_DAEMON_ENABLED:-0}" -ne 1 ]; then
return 0
fi
notify_push_url="${NEXTCLOUD_OVERWRITE_PROTOCOL}://${NEXTCLOUD_DOMAIN}/push"
attempts="${NOTIFY_PUSH_SETUP_ATTEMPTS:-20}"
delay_seconds="${NOTIFY_PUSH_SETUP_DELAY_SECONDS:-3}"
attempt=1
setup_log="$(mktemp)"
while [ "$attempt" -le "$attempts" ]; do
if ./occ notify_push:setup "$notify_push_url" >"$setup_log" 2>&1; then
cat "$setup_log"
rm -f "$setup_log"
return 0
fi
echo "notify_push setup not ready (${attempt}/${attempts}), retrying in ${delay_seconds}s..."
attempt=$((attempt + 1))
sleep "$delay_seconds"
done
echo "Warning: notify_push setup failed after ${attempts} attempts; continuing startup"
cat "$setup_log"
rm -f "$setup_log"
return 0
}
protect_secrets() {
unset NEXTCLOUD_DB_PASS
unset NEXTCLOUD_ADMIN_PASS
unset NEXTCLOUD_ADDITIONAL_CONFIG
}
run_server() {
echo "Init done, starting FrankenPHP"
supercronic -prometheus-listen-address 0.0.0.0:8081 /etc/crontabs/nextcloud &
if [ "${NOTIFY_PUSH_DAEMON_ENABLED:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php &
setup_notify_push &
fi
exec /usr/local/bin/frankenphp run --config /etc/caddy/Caddyfile
}
run_sidecar_if_requested() {
if [ "${SIDECAR_NOTIFY_PUSH:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php
exit 1
fi
if [ "${SIDECAR_FULLTEXTSEARCH:-0}" -eq 1 ]; then
./occ fulltextsearch:stop
./occ fulltextsearch:live
exit 1
fi
}

View file

@ -60,3 +60,62 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
{{/*
Nextcloud container image
*/}}
{{- define "nextcloud.image" -}}
{{- if .Values.image.digest }}
{{- printf "%s@%s" .Values.image.repository .Values.image.digest }}
{{- else }}
{{- printf "%s:%s" .Values.image.repository (.Values.image.tag | default .Chart.AppVersion) }}
{{- end }}
{{- end }}
{{/*
Shared env vars for the nextcloud container/job
*/}}
{{- define "nextcloud.env" -}}
- name: REDIS_HOST
{{- if .Values.valkey.enabled }}
value: {{ include "nextcloud.fullname" . }}-valkey
{{- else }}
value: {{ .Values.valkey.externalHostname }}
{{- end }}
- name: REDIS_PHP_DBINDEX
value: {{ .Values.valkey.databasePhp | quote}}
- name: REDIS_NEXTCLOUD_DBINDEX
value: {{ .Values.valkey.databaseNextcloud | quote}}
- name: NEXTCLOUD_DB_HOST
{{- if .Values.postgresql.enabled }}
value: {{ include "nextcloud.fullname" . }}-postgres
{{- else }}
value: {{ .Values.postgresql.externalHostname }}
{{- end }}
- name: NEXTCLOUD_DB_NAME
value: {{ .Values.postgresql.auth.database }}
- name: NEXTCLOUD_DB_USER
value: {{ .Values.postgresql.auth.username }}
- name: NEXTCLOUD_DB_PASS
valueFrom:
secretKeyRef:
name: {{ .Values.postgresql.auth.password.secretName }}
key: {{ .Values.postgresql.auth.password.secretKey }}
- name: NEXTCLOUD_ADDITIONAL_CONFIG
value: "{{ .Values.nextcloud.additionalConfig | toJson }}"
- name: NEXTCLOUD_DOMAIN
value: {{ .Values.nextcloud.hostname }}
- name: NEXTCLOUD_ADMIN_USER
value: {{ .Values.nextcloud.admin.user }}
- name: NEXTCLOUD_ADMIN_PASS
valueFrom:
secretKeyRef:
{{- if .Values.nextcloud.admin.existingSecret }}
name: {{ .Values.nextcloud.admin.existingSecret }}
{{- else }}
name: {{ template "nextcloud.fullname" . }}
{{- end }}
key: password
- name: NEXTCLOUD_SERVICE_NAME
value: {{ include "nextcloud.fullname" . }}
{{- end }}

View file

@ -0,0 +1,59 @@
apiVersion: batch/v1
kind: Job
metadata:
# Suffixed with the release revision: Job pod templates are immutable, so
# each install/upgrade needs a fresh object rather than patching one in place.
name: {{ include "nextcloud.fullname" . }}-setup-{{ .Release.Revision }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "nextcloud.labels" . | nindent 4 }}
spec:
backoffLimit: 3
ttlSecondsAfterFinished: 3600
template:
metadata:
labels:
{{- include "nextcloud.labels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
restartPolicy: Never
serviceAccountName: {{ include "nextcloud.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}-setup
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ include "nextcloud.image" . }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["ash", "/usr/local/bin/setup.sh"]
env:
{{- include "nextcloud.env" . | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: nextcloud-data
mountPath: /data
volumes:
- name: nextcloud-data
persistentVolumeClaim:
{{ if .Values.storage.useExistingPvc }}
claimName: {{ .Values.storage.existingPvc }}
{{ else }}
claimName: {{ include "nextcloud.fullname" . }}
{{ end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}

View file

@ -36,61 +36,15 @@ spec:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
{{ if .Values.image.digest }}
image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
{{ else }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
{{ end }}
image: "{{ include "nextcloud.image" . }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["ash", "/usr/local/bin/serve.sh"]
env:
- name: REDIS_HOST
{{- if .Values.valkey.enabled }}
value: {{ include "nextcloud.fullname" . }}-valkey
{{- else }}
value: {{ .Values.valkey.externalHostname }}
{{- end }}
- name: REDIS_PHP_DBINDEX
value: {{ .Values.valkey.databasePhp | quote}}
- name: REDIS_NEXTCLOUD_DBINDEX
value: {{ .Values.valkey.databaseNextcloud | quote}}
- name: NEXTCLOUD_DB_HOST
{{- if .Values.postgresql.enabled }}
value: {{ include "nextcloud.fullname" . }}-postgres
{{- else }}
value: {{ .Values.postgresql.externalHostname }}
{{- end }}
- name: NEXTCLOUD_DB_NAME
value: {{ .Values.postgresql.auth.database }}
- name: NEXTCLOUD_DB_USER
value: {{ .Values.postgresql.auth.username }}
- name: NEXTCLOUD_DB_PASS
valueFrom:
secretKeyRef:
name: {{ .Values.postgresql.auth.password.secretName }}
key: {{ .Values.postgresql.auth.password.secretKey }}
- name: NEXTCLOUD_ADDITIONAL_CONFIG
value: "{{ .Values.nextcloud.additionalConfig | toJson }}"
- name: NEXTCLOUD_DOMAIN
value: {{ .Values.nextcloud.hostname }}
- name: NEXTCLOUD_ADMIN_USER
value: {{ .Values.nextcloud.admin.user }}
- name: NEXTCLOUD_ADMIN_PASS
valueFrom:
secretKeyRef:
{{- if .Values.nextcloud.admin.existingSecret }}
name: {{ .Values.nextcloud.admin.existingSecret }}
{{- else }}
name: {{ template "nextcloud.fullname" . }}
{{- end }}
key: password
- name: NEXTCLOUD_SERVICE_NAME
value: {{ include "nextcloud.fullname" . }}
{{- include "nextcloud.env" . | nindent 12 }}
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- name: http
containerPort: 8080

19
serve.sh Normal file
View file

@ -0,0 +1,19 @@
#!/bin/ash
# Starts the nextcloud webserver only. Install/upgrade is handled by a
# separate k8s setup Job that runs before this pod starts.
set -eu
. /usr/local/bin/common.sh
write_php_ini
ensure_data_dirs
run_sidecar_if_requested
while [ ! -f "/data/config/config.php" ]; do
echo "Waiting for nextcloud setup job to finish installing..."
sleep 2
done
protect_secrets
run_server

16
setup.sh Normal file
View file

@ -0,0 +1,16 @@
#!/bin/ash
# Runs nextcloud install/upgrade only. Used as the k8s setup Job so the
# StatefulSet pods never race each other to install/upgrade on startup.
set -eu
. /usr/local/bin/common.sh
write_php_ini
ensure_data_dirs
install_nextcloud
upgrade_nextcloud
protect_secrets
echo "Setup complete"

114
start.sh
View file

@ -1,117 +1,19 @@
#!/bin/ash
set -eu
umask 027
NEXTCLOUD_OVERWRITE_PROTOCOL="${NEXTCLOUD_OVERWRITE_PROTOCOL:-https}"
# StatefulSet pod names are "<statefulset-name>-<ordinal>"; use the ordinal as the server id
NC_SERVERID="${POD_NAME:+${POD_NAME##*-}}"
NC_SERVERID="${NC_SERVERID:-0}"
export NC_SERVERID
. /usr/local/bin/common.sh
setup_notify_push() {
if [ "${NOTIFY_PUSH_DAEMON_ENABLED:-0}" -ne 1 ]; then
return 0
fi
write_php_ini
ensure_data_dirs
run_sidecar_if_requested
notify_push_url="${NEXTCLOUD_OVERWRITE_PROTOCOL}://${NEXTCLOUD_DOMAIN}/push"
attempts="${NOTIFY_PUSH_SETUP_ATTEMPTS:-20}"
delay_seconds="${NOTIFY_PUSH_SETUP_DELAY_SECONDS:-3}"
attempt=1
setup_log="$(mktemp)"
install_nextcloud
upgrade_nextcloud
while [ "$attempt" -le "$attempts" ]; do
if ./occ notify_push:setup "$notify_push_url" >"$setup_log" 2>&1; then
cat "$setup_log"
rm -f "$setup_log"
return 0
fi
echo "notify_push setup not ready (${attempt}/${attempts}), retrying in ${delay_seconds}s..."
attempt=$((attempt + 1))
sleep "$delay_seconds"
done
echo "Warning: notify_push setup failed after ${attempts} attempts; continuing startup"
cat "$setup_log"
rm -f "$setup_log"
return 0
}
cat > /usr/local/etc/php/conf.d/zz-nextcloud.ini << EOF
output_buffering = Off
display_errors = ${PHP_DISPLAY_ERRORS}
display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}
error_reporting = ${PHP_ERROR_REPORTING}
memory_limit = ${PHP_MEMORY_LIMIT}
upload_max_filesize = ${PHP_MAX_UPLOAD}
max_file_uploads = ${PHP_MAX_FILE_UPLOAD}
post_max_size = ${PHP_MAX_POST}
expose_php = ${PHP_EXPOSE_INFO}
apc.enable_cli = 1
apc.shm_size = ${PHP_APC_CACHE_SIZE}
cgi.fix_pathinfo = 0
session.save_handler = redis
session.save_path = "tcp://${REDIS_HOST}:6379/${REDIS_NEXTCLOUD_DBINDEX}"
EOF
mkdir -p /data/config
mkdir -p /data/data
mkdir -p /data/apps
mkdir -p /data/themes
if [ "${SIDECAR_NOTIFY_PUSH:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php
exit 1
fi
if [ "${SIDECAR_FULLTEXTSEARCH:-0}" -eq 1 ]; then
./occ fulltextsearch:stop
./occ fulltextsearch:live
exit 1
fi
if [ ! -f "/data/config/config.php" ]; then
echo "Installing nextcloud, please wait one moment"
touch /data/config/CAN_INSTALL
# Install the nextcloud
while ! nc -z "$NEXTCLOUD_DB_HOST" "5432"; do
echo "Waiting for the postgres db to be available..."
sleep 1
done
sleep 5
./occ maintenance:install --database='pgsql' --database-name="$NEXTCLOUD_DB_NAME" --database-user="$NEXTCLOUD_DB_USER" \
--database-pass="$NEXTCLOUD_DB_PASS" --database-host="$NEXTCLOUD_DB_HOST" --admin-user="$NEXTCLOUD_ADMIN_USER" \
--admin-pass="$NEXTCLOUD_ADMIN_PASS" --data-dir="/data/data"
./occ config:system:set maintenance_window_start --type=integer --value=1
./occ background:cron
php /config-patch.php
./occ app:install notify_push
echo "Install done"
fi
echo "Executing Nextcloud Update"
./occ upgrade
echo "Patching nextcloud config file"
./occ config:system:set trusted_domains 0 --value="$NEXTCLOUD_DOMAIN"
php /config-patch.php
echo "Executing nextcloud repair jobs"
./occ maintenance:repair --include-expensive
./occ db:add-missing-indices
./occ maintenance:mode --off
# Protect the passwords from beeing exposed
unset NEXTCLOUD_DB_PASS
unset NEXTCLOUD_ADMIN_PASS
unset NEXTCLOUD_ADDITIONAL_CONFIG
protect_secrets
if [ "${EXIT_AFTER_INSTALL:-0}" -eq 1 ]; then
exit 0
fi
echo "Init done, starting FrankenPHP"
supercronic -prometheus-listen-address 0.0.0.0:8081 /etc/crontabs/nextcloud &
if [ "${NOTIFY_PUSH_DAEMON_ENABLED:-0}" -eq 1 ]; then
"$NOTIFY_PUSH_BINPATH" --port 7867 /data/config/config.php &
setup_notify_push &
fi
exec /usr/local/bin/frankenphp run --config /etc/caddy/Caddyfile
run_server