{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.alertmanager.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
  name: {{ template "kube-prometheus.alertmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.alertmanager.labels" . | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.alertmanager.replicaCount }}
  serviceAccountName: {{ template "kube-prometheus.alertmanager.serviceAccountName" . }}
  {{- if .Values.alertmanager.image }}
  image: {{ template "kube-prometheus.alertmanager.image" . }}
  imagePullPolicy: {{ .Values.alertmanager.image.pullPolicy }}
  {{- end }}
  listenLocal: {{ .Values.alertmanager.listenLocal }}
  {{- if index .Values.alertmanager "externalUrl" }}
  externalUrl: {{ .Values.alertmanager.externalUrl | quote }}
  {{- else if and .Values.alertmanager.ingress.enabled }}
  externalUrl: {{ printf "%s://%s%s" (ternary "https" "http" .Values.alertmanager.ingress.tls) .Values.alertmanager.ingress.hostname .Values.alertmanager.routePrefix }}
  {{- else }}
  externalUrl: http://{{ template "kube-prometheus.alertmanager.fullname" . }}.{{ include "common.names.namespace" . }}:{{ .Values.alertmanager.service.ports.http }}{{ .Values.alertmanager.routePrefix }}
  {{- end }}
  portName: "{{ .Values.alertmanager.portName }}"
  paused: {{ .Values.alertmanager.paused }}
  logFormat: {{ .Values.alertmanager.logFormat }}
  logLevel: {{ .Values.alertmanager.logLevel }}
  retention: {{ .Values.alertmanager.retention }}
  {{- if .Values.alertmanager.enableFeatures }}
  enableFeatures:
    {{- range .Values.alertmanager.enableFeatures }}
    - {{ . | quote }}
    {{- end }}
  {{- end }}
  {{- if .Values.alertmanager.secrets }}
  secrets: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.secrets "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.configMaps }}
  configMaps: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.configMaps "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.resources }}
  resources: {{- toYaml .Values.alertmanager.resources | nindent 4 }}
  {{- else if ne .Values.alertmanager.resourcesPreset "none" }}
  resources: {{- include "common.resources.preset" (dict "type" .Values.alertmanager.resourcesPreset) | nindent 4 }}
  {{- end }}
  routePrefix: "{{ .Values.alertmanager.routePrefix }}"
  {{- if .Values.alertmanager.podSecurityContext.enabled }}
  securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.alertmanager.podSecurityContext "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.storageSpec }}
  storage: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.storageSpec "context" $) | nindent 4 }}
  {{- else }}
  {{- if .Values.alertmanager.persistence.enabled }}
  storage:
    volumeClaimTemplate:
      {{- if or .Values.alertmanager.persistence.annotations .Values.commonAnnotations .Values.commonLabels }}
      metadata:
        {{- if or .Values.alertmanager.persistence.annotations .Values.commonAnnotations }}
        {{- $claimAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.alertmanager.persistence.annotations .Values.commonAnnotations ) "context" . ) }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" $claimAnnotations "context" $ ) | nindent 10 }}
        {{- end }}
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
      {{- end }}
      spec:
        accessModes:
          {{- range .Values.alertmanager.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.alertmanager.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.alertmanager.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
  {{- end }}
  podMetadata:
    labels: {{- include "kube-prometheus.alertmanager.matchLabels" . | nindent 6 }}
    {{- if .Values.alertmanager.podMetadata.annotations }}
    annotations: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.podMetadata.annotations "context" $) | nindent 6 }}
    {{- end }}
  {{- if .Values.alertmanager.affinity }}
  affinity: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.affinity "context" $) | nindent 4 }}
  {{- else }}
  affinity:
    {{- if not (empty .Values.alertmanager.podAffinityPreset) }}
    podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.alertmanager.podAffinityPreset "component" "alertmanager" "context" $) | nindent 6 }}
    {{- end }}
    {{- if not (empty .Values.alertmanager.podAntiAffinityPreset) }}
    podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.alertmanager.podAntiAffinityPreset "component" "alertmanager" "context" $) | nindent 6 }}
    {{- end }}
    {{- if not (empty .Values.alertmanager.nodeAffinityPreset.values) }}
    nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.alertmanager.nodeAffinityPreset.type "key" .Values.alertmanager.nodeAffinityPreset.key "values" .Values.alertmanager.nodeAffinityPreset.values) | nindent 6 }}
    {{- end }}
  {{- end }}
  {{- if .Values.alertmanager.nodeSelector }}
  nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.nodeSelector "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.tolerations }}
  tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.tolerations "context" $) | nindent 4 }}
  {{- end }}
  volumes:
    - name: empty-dir
      emptyDir: {}
    {{- if .Values.alertmanager.volumes }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.volumes "context" $) | nindent 4 }}
    {{- end }}
  volumeMounts:
    - name: empty-dir
      mountPath: /tmp
      subPath: tmp-dir
    {{- if .Values.alertmanager.volumeMounts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.volumeMounts "context" $) | nindent 4 }}
    {{- end }}
{{- include "kube-prometheus.imagePullSecrets" . | nindent 2 }}
  {{- if or .Values.alertmanager.containers .Values.alertmanager.containerSecurityContext.enabled .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled }}
  containers:
    {{- if or .Values.alertmanager.containerSecurityContext.enabled .Values.alertmanager.livenessProbe.enabled .Values.alertmanager.readinessProbe.enabled }}
    ## This monkey patching is needed until the securityContexts are
    ## directly patchable via the CRD.
    ## ref: https://github.com/prometheus-operator/prometheus-operator/issues/3947
    ## currently implemented with strategic merge
    ## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/strategic-merge-patch.md
    - name: alertmanager
      {{- if .Values.alertmanager.containerSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.alertmanager.containerSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.alertmanager.livenessProbe.enabled }}
      livenessProbe:
        httpGet:
          path: {{ .Values.alertmanager.livenessProbe.path }}
          port: web
          scheme: HTTP
        initialDelaySeconds: {{ .Values.alertmanager.livenessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.alertmanager.livenessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.alertmanager.livenessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.alertmanager.livenessProbe.failureThreshold }}
        successThreshold: {{ .Values.alertmanager.livenessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.alertmanager.readinessProbe.enabled }}
      readinessProbe:
        httpGet:
          path: {{ .Values.alertmanager.readinessProbe.path }}
          port: web
          scheme: HTTP
        initialDelaySeconds: {{ .Values.alertmanager.readinessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.alertmanager.readinessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.alertmanager.readinessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.alertmanager.readinessProbe.failureThreshold }}
        successThreshold: {{ .Values.alertmanager.readinessProbe.successThreshold }}
      {{- end }}
    {{- end }}
    {{- if or .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled .Values.operator.prometheusConfigReloader.livenessProbe.enabled .Values.operator.prometheusConfigReloader.readinessProbe.enabled }}
    ## This monkey patching is needed until the securityContexts are
    ## directly patchable via the CRD.
    ## ref: https://github.com/prometheus-operator/prometheus-operator/issues/3947
    ## currently implemented with strategic merge
    ## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/strategic-merge-patch.md
    - name: config-reloader
      {{- if .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.operator.prometheusConfigReloader.containerSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.prometheusConfigReloader.livenessProbe.enabled }}
      livenessProbe:
        tcpSocket:
          port: reloader-web
        initialDelaySeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.operator.prometheusConfigReloader.livenessProbe.failureThreshold }}
        successThreshold: {{ .Values.operator.prometheusConfigReloader.livenessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.operator.prometheusConfigReloader.readinessProbe.enabled }}
      readinessProbe:
        tcpSocket:
          port: reloader-web
        initialDelaySeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.operator.prometheusConfigReloader.readinessProbe.failureThreshold }}
        successThreshold: {{ .Values.operator.prometheusConfigReloader.readinessProbe.successThreshold }}
      {{- end }}
    {{- end }}
    {{- if .Values.alertmanager.containers }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.containers "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
  {{- if .Values.alertmanager.priorityClassName }}
  priorityClassName: {{ .Values.alertmanager.priorityClassName }}
  {{- end }}
  {{- if .Values.alertmanager.additionalPeers }}
  additionalPeers: {{ .Values.alertmanager.additionalPeers }}
  {{- end }}
  {{- if .Values.alertmanager.configNamespaceSelector }}
  alertmanagerConfigNamespaceSelector: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.configNamespaceSelector "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.configSelector }}
  alertmanagerConfigSelector: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.configSelector "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.configuration }}
  alertmanagerConfiguration: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.configuration "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.alertmanager.configMatcherStrategy }}
  alertmanagerConfigMatcherStrategy: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.configMatcherStrategy "context" $) | nindent 4 }}
  {{- end }}
{{- end }}
