{{- if .Values.alertmanager.enabled }}
apiVersion: {{ printf "%s/v1" (.Values.prometheusOperator.crdApiGroup | default "monitoring.coreos.com") }}
kind: Alertmanager
metadata:
  name: {{ template "prometheus-operator.fullname" . }}-alertmanager
  labels:
    app: {{ template "prometheus-operator.name" . }}-alertmanager
{{ include "prometheus-operator.labels" . | indent 4 }}
spec:
{{- if .Values.alertmanager.alertmanagerSpec.image }}
  baseImage: {{ .Values.alertmanager.alertmanagerSpec.image.repository }}
  version: {{ .Values.alertmanager.alertmanagerSpec.image.tag }}
{{- end }}
  replicas: {{ .Values.alertmanager.alertmanagerSpec.replicas }}
  listenLocal: {{ .Values.alertmanager.alertmanagerSpec.listenLocal }}
  serviceAccountName: {{ template "prometheus-operator.alertmanager.serviceAccountName" . }}
{{- if .Values.alertmanager.alertmanagerSpec.externalUrl }}
  externalUrl: "{{ .Values.alertmanager.alertmanagerSpec.externalUrl }}"
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.externalUrl }}
  externalUrl: "{{ .Values.alertmanager.alertmanagerSpec.externalUrl }}"
{{- else if .Values.alertmanager.ingress.enabled }}
  externalUrl: "http://{{ index .Values.alertmanager.ingress.hosts 0 }}{{ .Values.alertmanager.alertmanagerSpec.routePrefix }}"
{{- else }}
  externalUrl: http://{{ template "prometheus-operator.fullname" . }}-alertmanager.{{ .Release.Namespace }}:9093
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.nodeSelector }}
  nodeSelector:
{{ toYaml .Values.alertmanager.alertmanagerSpec.nodeSelector | indent 4 }}
{{- end }}
  paused: {{ .Values.alertmanager.alertmanagerSpec.paused }}
  logLevel:  {{ .Values.alertmanager.alertmanagerSpec.logLevel | quote  }}
  retention: {{ .Values.alertmanager.alertmanagerSpec.retention | quote  }}
{{- if .Values.alertmanager.alertmanagerSpec.secrets }}
  secrets:
{{ toYaml .Values.alertmanager.alertmanagerSpec.secrets | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.configMaps }}
  configMaps:
{{ toYaml .Values.alertmanager.alertmanagerSpec.configMaps | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.resources }}
  resources:
{{ toYaml .Values.alertmanager.alertmanagerSpec.resources | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.routePrefix }}
  routePrefix: "{{ .Values.alertmanager.alertmanagerSpec.routePrefix }}"
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.securityContext }}
  securityContext:
{{ toYaml .Values.alertmanager.alertmanagerSpec.securityContext | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.storage }}
  storage:
{{ toYaml .Values.alertmanager.alertmanagerSpec.storage | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.podMetadata }}
  podMetadata:
{{ toYaml .Values.alertmanager.alertmanagerSpec.podMetadata | indent 4 }}
{{- end }}
{{- if eq .Values.alertmanager.alertmanagerSpec.podAntiAffinity "hard" }}
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - topologyKey: {{ .Values.alertmanager.alertmanagerSpec.podAntiAffinityTopologyKey }}
        labelSelector:
          matchLabels:
            app: alertmanager
            alertmanager: {{ template "prometheus-operator.fullname" . }}-alertmanager
{{- else if eq .Values.alertmanager.alertmanagerSpec.podAntiAffinity "soft" }}
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          topologyKey: {{ .Values.alertmanager.alertmanagerSpec.podAntiAffinityTopologyKey }}
          labelSelector:
            matchLabels:
              app: alertmanager
              alertmanager: {{ template "prometheus-operator.fullname" . }}-alertmanager
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.tolerations }}
  tolerations:
{{ toYaml .Values.alertmanager.alertmanagerSpec.tolerations | indent 4 }}
{{- end }}
{{- if .Values.global.imagePullSecrets }}
  imagePullSecrets:
{{ toYaml .Values.global.imagePullSecrets | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.containers }}
  containers:
{{ toYaml .Values.alertmanager.alertmanagerSpec.containers | indent 4 }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.priorityClassName }}
  priorityClassName: {{.Values.alertmanager.alertmanagerSpec.priorityClassName }}
{{- end }}
{{- if .Values.alertmanager.alertmanagerSpec.additionalPeers }}
  additionalPeers: {{.Values.alertmanager.alertmanagerSpec.additionalPeers }}
{{- end }}
{{- end }}
