Skip to content

Commit

Permalink
Upd bokysan#63: Gracefully scaledown Postfix
Browse files Browse the repository at this point in the history
Make sure the queue is flushed when the Postfix container is
shutdown through Kubernetes lifecycle hooks.
  • Loading branch information
bokysan committed Jun 5, 2021
1 parent aed5624 commit 956caf3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 13 additions & 0 deletions helm/mail/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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 }}
Expand Down
7 changes: 6 additions & 1 deletion helm/mail/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {}
4 changes: 3 additions & 1 deletion scripts/common-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 956caf3

Please sign in to comment.