{{- if and .Values.alertmanager.enabled (not .Values.alertmanager.statefulSet.enabled) -}}
apiVersion: {{ template "prometheus.deployment.apiVersion" . }}
kind: Deployment
metadata:
  labels:
    {{- include "prometheus.alertmanager.labels" . | nindent 4 }}
  name: {{ template "prometheus.alertmanager.fullname" . }}
{{ include "prometheus.namespace" . | indent 2 }}
spec:
  selector:
    matchLabels:
      {{- include "prometheus.alertmanager.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.alertmanager.replicaCount }}
  {{- if .Values.alertmanager.strategy }}
  strategy:
{{ toYaml .Values.alertmanager.strategy | trim | indent 4 }}
    {{ if eq .Values.alertmanager.strategy.type "Recreate" }}rollingUpdate: null{{ end }}
{{- end }}
  template:
    metadata:
    {{- if .Values.alertmanager.podAnnotations }}
      annotations:
{{ toYaml .Values.alertmanager.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        {{- include "prometheus.alertmanager.labels" . | nindent 8 }}
        {{- if .Values.alertmanager.podLabels}}
        {{ toYaml .Values.alertmanager.podLabels | nindent 8 }}
        {{- end}}
    spec:
{{- if .Values.alertmanager.schedulerName }}
      schedulerName: "{{ .Values.alertmanager.schedulerName }}"
{{- end }}
      serviceAccountName: {{ template "prometheus.serviceAccountName.alertmanager" . }}
      {{- if .Values.alertmanager.extraInitContainers }}
      initContainers:
{{ toYaml .Values.alertmanager.extraInitContainers | indent 8 }}
      {{- end }}
{{- if .Values.alertmanager.priorityClassName }}
      priorityClassName: "{{ .Values.alertmanager.priorityClassName }}"
{{- end }}
      containers:
        - name: {{ template "prometheus.name" . }}-{{ .Values.alertmanager.name }}
          image: "{{ .Values.alertmanager.image.repository }}:{{ .Values.alertmanager.image.tag }}"
          imagePullPolicy: "{{ .Values.alertmanager.image.pullPolicy }}"
          env:
            {{- range $key, $value := .Values.alertmanager.extraEnv }}
            - name: {{ $key }}
              value: {{ $value }}
            {{- end }}
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
          args:
            - --config.file=/etc/config/{{ .Values.alertmanager.configFileName }}
            - --storage.path={{ .Values.alertmanager.persistentVolume.mountPath }}
            - --cluster.advertise-address=$(POD_IP):6783
          {{- range $key, $value := .Values.alertmanager.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}
          {{- if .Values.alertmanager.baseURL }}
            - --web.external-url={{ .Values.alertmanager.baseURL }}
          {{- end }}

          ports:
            - containerPort: 9093
          readinessProbe:
            httpGet:
              path: {{ .Values.alertmanager.prefixURL }}/-/ready
              port: 9093
            initialDelaySeconds: 30
            timeoutSeconds: 30
          resources:
{{ toYaml .Values.alertmanager.resources | indent 12 }}
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
            - name: storage-volume
              mountPath: "{{ .Values.alertmanager.persistentVolume.mountPath }}"
              subPath: "{{ .Values.alertmanager.persistentVolume.subPath }}"
          {{- range .Values.alertmanager.extraSecretMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}

        {{- if .Values.configmapReload.alertmanager.enabled }}
        - name: {{ template "prometheus.name" . }}-{{ .Values.alertmanager.name }}-{{ .Values.configmapReload.alertmanager.name }}
          image: "{{ .Values.configmapReload.alertmanager.image.repository }}:{{ .Values.configmapReload.alertmanager.image.tag }}"
          imagePullPolicy: "{{ .Values.configmapReload.alertmanager.image.pullPolicy }}"
          args:
            - --volume-dir=/etc/config
            - --webhook-url=http://127.0.0.1:9093{{ .Values.alertmanager.prefixURL }}/-/reload
          resources:
{{ toYaml .Values.configmapReload.alertmanager.resources | indent 12 }}
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
              readOnly: true
        {{- end }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{ toYaml .Values.imagePullSecrets | indent 2 }}
    {{- end }}
    {{- if .Values.alertmanager.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.alertmanager.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.alertmanager.securityContext }}
      securityContext:
{{ toYaml .Values.alertmanager.securityContext | indent 8 }}
    {{- end }}
    {{- if .Values.alertmanager.tolerations }}
      tolerations:
{{ toYaml .Values.alertmanager.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.alertmanager.affinity }}
      affinity:
{{ toYaml .Values.alertmanager.affinity | indent 8 }}
    {{- end }}
      volumes:
        - name: config-volume
          {{- if empty .Values.alertmanager.configFromSecret }}
          configMap:
            name: {{ if .Values.alertmanager.configMapOverrideName }}{{ .Release.Name }}-{{ .Values.alertmanager.configMapOverrideName }}{{- else }}{{ template "prometheus.alertmanager.fullname" . }}{{- end }}
          {{- else }}
          secret:
            secretName: {{ .Values.alertmanager.configFromSecret }}
          {{- end }}
      {{- range .Values.alertmanager.extraSecretMounts }}
        - name: {{ .name }}
          secret:
            secretName: {{ .secretName }}
      {{- end }}
        - name: storage-volume
        {{- if .Values.alertmanager.persistentVolume.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.alertmanager.persistentVolume.existingClaim }}{{ .Values.alertmanager.persistentVolume.existingClaim }}{{- else }}{{ template "prometheus.alertmanager.fullname" . }}{{- end }}
        {{- else }}
          emptyDir: {}
        {{- end -}}
{{- end }}
