From 5b2614741995b7aea77db76365680078a81ef517 Mon Sep 17 00:00:00 2001 From: Jonathon Beauregard II Date: Thu, 28 Dec 2023 12:27:46 -0800 Subject: [PATCH 1/4] https://github.com/bokysan/docker-postfix/issues/162 --- helm/mail/templates/service-monitor.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/helm/mail/templates/service-monitor.yaml b/helm/mail/templates/service-monitor.yaml index 4b139f8..a6e162c 100644 --- a/helm/mail/templates/service-monitor.yaml +++ b/helm/mail/templates/service-monitor.yaml @@ -24,6 +24,5 @@ spec: selector: matchLabels: app.kubernetes.io/instance: {{ $chart }} - prometheus: unknown {{- end -}} {{- end -}} From f95b96f8191f1cf63f2e2521f49851de07688b0c Mon Sep 17 00:00:00 2001 From: Jonathon Beauregard II Date: Thu, 28 Dec 2023 14:12:01 -0800 Subject: [PATCH 2/4] test generating hostname --- helm/mail/templates/statefulset.yaml | 31 +++++++++++++++++++++++++++- helm/mail/values.yaml | 7 +++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/helm/mail/templates/statefulset.yaml b/helm/mail/templates/statefulset.yaml index 7b3ef7e..47cfb56 100644 --- a/helm/mail/templates/statefulset.yaml +++ b/helm/mail/templates/statefulset.yaml @@ -48,14 +48,31 @@ spec: {{ with .Values.affinity }}affinity: {{- toYaml . | nindent 8 }} {{- end }} {{ with .Values.tolerations }}tolerations: {{- toYaml . | nindent 8 }} {{- end }} - {{- if .Values.extraInitContainers }} + {{- if or .Values.extraInitContainers .Values.GenerateHostName.enabled }} # # Init containers # initContainers: + {{- end }} + {{- if .Values.extraInitContainers}} {{- tpl .Values.extraInitContainers . | nindent 6 }} + {{- else if .Values.GenerateHostName.enabled }} + - name: get-public-ip + image: curlimages/curl:latest + command: ["/bin/sh", "-c"] + args: + - > + IP=$(curl http://ipinfo.io/ip); + PREFIX={{- .Values.GenerateHostName.prefix -}}; + DOMAIN={{- .Values.GenerateHostName.domain -}}; + echo "${PREFIX}-${IP}.${DOMAIN}" > /var/worker-ip/hostname.txt; + volumeMounts: + - mountPath: "/var/worker-ip" + name: {{ $fullName | quote }} + subPath: ip {{- end }} + # Allow up to 2 minutes for Postfix to flush / empty the queue before shutting down the container terminationGracePeriodSeconds: 120 containers: @@ -81,6 +98,13 @@ spec: {{- if .Values.lifecycle.postStart }} postStart: {{- toYaml .Values.lifecycle.postStart | nindent 14 }} {{- end }} + {{- if .Values.GenerateHostName.enabled }} + env: + - name: POSTFIX_myhostname + valueFrom: + fieldRef: + fieldPath: "/var/worker-ip/hostname.txt" + {{- end }} envFrom: - configMapRef: name: {{ $fullName | quote }} @@ -132,6 +156,11 @@ spec: readOnly: true {{- end }} {{- if .Values.extraVolumeMounts }}{{- toYaml .Values.extraVolumeMounts | nindent 12 }}{{ end }} + {{- if .Values.GenerateHostName.enabled }} + - mountPath: "/var/worker-ip" + name: {{ $fullName | quote }} + subPath: ip + {{- end }} resources: {{ toYaml .Values.resources | nindent 12 }} {{- if .Values.metrics.enabled }} - name: exporter diff --git a/helm/mail/values.yaml b/helm/mail/values.yaml index a8608ea..61ae8ea 100644 --- a/helm/mail/values.yaml +++ b/helm/mail/values.yaml @@ -151,6 +151,13 @@ mountSecret: # < redacted > # -----END RSA PRIVATE KEY----- + +# if the following is true, config.prefix.myhostname should not be set +GenerateHostName: + enabled: false + domain: example.com + prefix: smtp + config: general: {} # e.g. From e60b98b93d72c59859fbb621da72a54de6a55db4 Mon Sep 17 00:00:00 2001 From: Jonathon Beauregard II Date: Thu, 28 Dec 2023 14:46:08 -0800 Subject: [PATCH 3/4] new attempt --- helm/mail/templates/hostname-script.yaml | 20 ++++++++++++++++++++ helm/mail/templates/statefulset.yaml | 14 +++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 helm/mail/templates/hostname-script.yaml diff --git a/helm/mail/templates/hostname-script.yaml b/helm/mail/templates/hostname-script.yaml new file mode 100644 index 0000000..cd0a742 --- /dev/null +++ b/helm/mail/templates/hostname-script.yaml @@ -0,0 +1,20 @@ +{{- $chart := "mail" -}} +{{- $fullName := include (print $chart ".fullname") . -}} +{{- $labels := include (print $chart ".labels") . -}} +apiVersion: v1 +kind: ConfigMap +metadata: + name: hostname-init + labels: + {{- $labels | nindent 4 }} +data: + set_hostname.sh: | + #!/usr/bin/env bash + + # Load the hostname from the file + if [ -f /var/worker-ip/hostname.txt ]; then + export POSTFIX_myhostname=$(cat /var/worker-ip/hostname.txt) + fi + + # Execute the main container command + /scripts/run.sh diff --git a/helm/mail/templates/statefulset.yaml b/helm/mail/templates/statefulset.yaml index 47cfb56..a4f0d11 100644 --- a/helm/mail/templates/statefulset.yaml +++ b/helm/mail/templates/statefulset.yaml @@ -99,11 +99,7 @@ spec: postStart: {{- toYaml .Values.lifecycle.postStart | nindent 14 }} {{- end }} {{- if .Values.GenerateHostName.enabled }} - env: - - name: POSTFIX_myhostname - valueFrom: - fieldRef: - fieldPath: "/var/worker-ip/hostname.txt" + command: ["/var/generate-hostname/set_hostname.sh"] {{- end }} envFrom: - configMapRef: @@ -160,6 +156,8 @@ spec: - mountPath: "/var/worker-ip" name: {{ $fullName | quote }} subPath: ip + - mountPath: "/var/generate-hostname" + name: hostname-init {{- end }} resources: {{ toYaml .Values.resources | nindent 12 }} {{- if .Values.metrics.enabled }} @@ -247,6 +245,12 @@ spec: name: {{ print $fullName "-scripts" | quote }} defaultMode: 0777 {{- end }} + {{- if .Values.GenerateHostName.enabled }} + - name: hostname-init + configMap: + name: hostname-init + defaultMode: 0777 + {{- end }} {{- if .Values.extraVolumes }}{{- toYaml .Values.extraVolumes | nindent 8 }}{{ end }} {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }} volumeClaimTemplates: From 3f1889290a450c1b802190e549e3fdeda3d9b008 Mon Sep 17 00:00:00 2001 From: Jonathon Beauregard II Date: Thu, 28 Dec 2023 15:24:40 -0800 Subject: [PATCH 4/4] change from . to - --- helm/mail/templates/statefulset.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/helm/mail/templates/statefulset.yaml b/helm/mail/templates/statefulset.yaml index a4f0d11..aa8b794 100644 --- a/helm/mail/templates/statefulset.yaml +++ b/helm/mail/templates/statefulset.yaml @@ -63,6 +63,7 @@ spec: args: - > IP=$(curl http://ipinfo.io/ip); + IP=$(echo "$IP" | tr '.' '-') PREFIX={{- .Values.GenerateHostName.prefix -}}; DOMAIN={{- .Values.GenerateHostName.domain -}}; echo "${PREFIX}-${IP}.${DOMAIN}" > /var/worker-ip/hostname.txt;