# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "mailhog.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "mailhog.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "mailhog.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name for the auth secret.
*/}}
{{- define "mailhog.authFileSecret" -}}
    {{- if .Values.auth.existingKeySecret -}}
        {{- .Values.auth.existingKeySecret -}}
    {{- else -}}
        {{- template "mailhog.fullname" . -}}-auth
    {{- end -}}
{{- end -}}

{{/*
Create the name for the tls secret.
*/}}
{{- define "mailhog.tlsSecret" -}}
    {{- if .Values.ingress.tls.existingSecret -}}
        {{- .Values.ingress.tls.existingSecret -}}
    {{- else -}}
        {{- template "mailhog.fullname" . -}}-tls
    {{- end -}}
{{- end -}}
 # auth-secret.yaml
{{- if and (.Values.auth.enabled) (not .Values.auth.existingSecret) -}}
apiVersion: v1
kind: Secret
metadata:
  labels:
    app: {{ template "mailhog.name" . }}
    chart: {{ template "mailhog.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "mailhog.authFileSecret" . }}
type: Opaque
data:
  {{ .Values.auth.fileName }}: {{ .Values.auth.fileContents | b64enc }}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ template "mailhog.name" . }}
    chart: {{ template "mailhog.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "mailhog.fullname" . }}
spec:
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "mailhog.name" . }}
        release: {{ .Release.Name }}
    spec:
      securityContext:
        runAsUser: 1000
      containers:
        - name: {{ template "mailhog.name" . }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.pullPolicy }}"
          env:
            - name: MH_HOSTNAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          {{- if .Values.auth.enabled }}
            - name: MH_AUTH_FILE
              value: /authdir/{{ .Values.auth.fileName }}
          {{- end }}
          {{- range $key, $value := .Values.env }}
            - name: "{{ $key }}"
              value: "{{ $value }}"
          {{- end }}
          ports:
            - name: http
              containerPort: 8025
              protocol: TCP
            - name: smtp
              containerPort: 1025
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: smtp
            initialDelaySeconds: 10
            timeoutSeconds: 1
          readinessProbe:
            tcpSocket:
              port: smtp
          {{- if .Values.auth.enabled }}
          volumeMounts:
            - name: authdir
              mountPath: /authdir
              readOnly: true
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.auth.enabled }}
      volumes:
        - name: authdir
          secret:
            secretName: {{ template "mailhog.authFileSecret" . }}
    {{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled }}
{{- $fullName := include "mailhog.fullname" . -}}
{{- $path := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
{{- if .Values.ingress.annotations }}
  annotations:
{{ toYaml .Values.ingress.annotations | indent 4 }}
{{- end }}
  labels:
    app: {{ template "mailhog.name" . }}
    chart: {{ template "mailhog.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ $fullName }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ . }}
      http:
        paths:
          - path: {{ $path }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: http
  {{- end }}
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
  labels:
    app: {{ template "mailhog.name" . }}
    chart: {{ template "mailhog.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "mailhog.fullname" . }}
spec:
  type: "{{ .Values.service.type }}"
  clusterIP: "{{ .Values.service.clusterIP }}"
{{- if .Values.service.externalIPs }}
  externalIPs:
{{ toYaml .Values.service.externalIPs | indent 4 }}
{{- end }}
{{- if .Values.service.loadBalancerIP }}
  loadBalancerIP: "{{ .Values.service.loadBalancerIP }}"
{{- end }}
{{- if .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.service.loadBalancerSourceRanges | indent 4 }}
{{- end }}
  ports:
    - name: http
      port: {{ .Values.service.port.http }}
      protocol: TCP
      targetPort: http
      {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort.http))) }}
      nodePort: {{ .Values.service.nodePort.http }}
      {{- end }}
    - name: smtp
      port: {{ .Values.service.port.smtp }}
      protocol: TCP
      targetPort: smtp
      {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort.smtp))) }}
      nodePort: {{ .Values.service.nodePort.smtp }}
      {{- end }}
  selector:
    app: {{ template "mailhog.name" . }}
    release: {{ .Release.Name }}

image:
  repository: mailhog/mailhog
  tag: v1.0.0
  pullPolicy: IfNotPresent

service:
  annotations: {}
  clusterIP: ""
  externalIPs: []
  loadBalancerIP: ""
  loadBalancerSourceRanges: []
  type: ClusterIP
  port:
    http: 8025
    smtp: 1025
  nodePort:
    http: ""
    smtp: ""

ingress:
  enabled: false
  annotations: {}
  path: /
  hosts:
    - mailhog.example.com
  tls: []
  #  - hosts:
  #      - mailhog.example.com
  #    secretName: tls-mailhog

auth:
  enabled: false
  existingSecret: ""
  fileName: auth.txt
  fileContents: ""

podAnnotations: {}
resources: {}
env: {}
