{{- $functionNs := default .Release.Namespace .Values.functionNamespace }}
{{- if .Values.alertmanager.create }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "openfaas.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    component: alertmanager
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: alertmanager
  namespace: {{ .Release.Namespace | quote }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: alertmanager
  template:
    metadata:
      labels:
        app: alertmanager
      annotations:
        sidecar.istio.io/inject: "true"
        checksum/alertmanager-config: {{ include (print $.Template.BasePath "/alertmanager-cfg.yaml") . | sha256sum | quote  }}
    spec:
      containers:
      - name: alertmanager
        image: {{ .Values.alertmanager.image }}
        imagePullPolicy: {{ .Values.openfaasImagePullPolicy }}
        command:
          - "alertmanager"
          - "--config.file=/alertmanager.yml"
          - "--storage.path=/alertmanager"
          - "--cluster.listen-address="
        livenessProbe:
          {{- if .Values.httpProbe }}
          httpGet:
            path: /-/ready
            port: 9093
          {{- else }}
          exec:
            command:
            - wget
            - --quiet
            - --tries=1
            - --timeout=30
            - --spider
            - http://localhost:9093/-/ready
          {{- end }}
          timeoutSeconds: 30
        readinessProbe:
          {{- if .Values.httpProbe }}
          httpGet:
            path: /-/ready
            port: 9093
          {{- else }}
          exec:
            command:
            - wget
            - --quiet
            - --tries=1
            - --timeout=30
            - --spider
            - http://localhost:9093/-/ready
          {{- end }}
          timeoutSeconds: 30
        ports:
        - containerPort: 9093
          protocol: TCP
        resources:
          {{- .Values.alertmanager.resources | toYaml | nindent 12 }}
        volumeMounts:
        - mountPath: /alertmanager.yml
          name: alertmanager-config
          subPath: alertmanager.yml
        {{- if .Values.basic_auth }}
        - name: auth
          readOnly: true
          mountPath: "/var/secrets"
        {{- end }}
      volumes:
        - name: alertmanager-config
          configMap:
            name: alertmanager-config
            items:
              - key: alertmanager.yml
                path: alertmanager.yml
                mode: 0644
        {{- if .Values.basic_auth }}
        - name: auth
          secret:
            secretName: basic-auth
        {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- end }}