diff --git a/helm/mail/templates/statefulset.yaml b/helm/mail/templates/statefulset.yaml index cb4fe27..c190918 100644 --- a/helm/mail/templates/statefulset.yaml +++ b/helm/mail/templates/statefulset.yaml @@ -56,6 +56,8 @@ spec: {{- tpl .Values.extraInitContainers . | nindent 6 }} {{- end }} + # Allow up to 2 minutes for Postfix to flush / empty the queue before shutting down the container + terminationGracePeriodSeconds: 120 containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" @@ -68,6 +70,17 @@ spec: readinessProbe: {{- toYaml .Values.readinessProbe | nindent 12 }} livenessProbe: {{- toYaml .Values.livenessProbe | nindent 12 }} startupProbe: {{- toYaml .Values.startupProbe | nindent 12 }} + lifecycle: + # If a container has a preStop hook configured, that runs before the container enters the Terminated state. + preStop: + exec: + command: + - bash + - -c + - touch /tmp/container_is_terminating && while ! [[ "`mailq`" == *empty* ]]; do echo "Flushing queue..." && postfix flush; sleep 1; done + {{- if .Values.lifecycle.postStart }} + postStart: {{- toYaml .Values.lifecycle.postStart | nindent 14 }} + {{- end }} envFrom: - configMapRef: name: {{ $fullName | quote }} diff --git a/helm/mail/values.yaml b/helm/mail/values.yaml index 2fe6140..1dcb526 100644 --- a/helm/mail/values.yaml +++ b/helm/mail/values.yaml @@ -131,7 +131,9 @@ readinessProbe: command: - sh - -c - - 'printf "EHLO healthcheck\n" | nc 127.0.0.1 587 | grep -qE "^220.*ESMTP Postfix"' + - >- + [ ! -f /tmp/container_is_terminating ] && + printf "EHLO healthcheck\n" | nc 127.0.0.1 587 | grep -qE "^220.*ESMTP Postfix" livenessProbe: initialDelaySeconds: 5 periodSeconds: 5 @@ -156,3 +158,6 @@ startupProbe: ps axf | fgrep -v grep | egrep -q '\{supervisord\}|/usr/bin/supervisord' && ps axf | fgrep -v grep | fgrep -q "postfix-script" && ps axf | fgrep -v grep | fgrep -q 'opendkim.sh' + +lifecycle: + postStart: {} \ No newline at end of file diff --git a/scripts/common-run.sh b/scripts/common-run.sh index 1cbf61b..5678d07 100644 --- a/scripts/common-run.sh +++ b/scripts/common-run.sh @@ -55,7 +55,9 @@ reown_folders() { chown root: /var/spool/postfix/pid do_postconf -e "manpage_directory=/usr/share/man" - postfix -c /etc/postfix/ set-permissions || true + + # postfix set-permissions complains if documentation files do not exist + postfix -c /etc/postfix/ set-permissions > /dev/null 2>&1 || true } postfix_upgrade_conf() {