{{- $functionNs := default .Release.Namespace .Values.functionNamespace }}
{{- if .Values.prometheus.create }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "openfaas.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    component: prometheus
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: prometheus
  namespace: {{ .Release.Namespace | quote }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
      annotations:
        sidecar.istio.io/inject: "true"
        checksum/prometheus-config: {{ include (print $.Template.BasePath "/prometheus-cfg.yaml") . | sha256sum | quote }}
    spec:
      serviceAccountName: {{ .Release.Name }}-prometheus
      containers:
      - name: prometheus
        resources:
          {{- .Values.prometheus.resources | toYaml | nindent 12 }}
        image: {{ .Values.prometheus.image }}
        command:
          - "prometheus"
          - "--config.file=/etc/prometheus/prometheus.yml"
        imagePullPolicy: {{ .Values.openfaasImagePullPolicy }}
        livenessProbe:
          {{- if .Values.httpProbe }}
          httpGet:
            path: /-/healthy
            port: 9090
          {{- else }}
          exec:
            command:
            - wget
            - --quiet
            - --tries=1
            - --timeout=30
            - --spider
            - http://localhost:9090/-/healthy
          {{- end }}
          timeoutSeconds: 30
        readinessProbe:
          {{- if .Values.httpProbe }}
          httpGet:
            path: /-/healthy
            port: 9090
          {{- else }}
          exec:
            command:
            - wget
            - --quiet
            - --tries=1
            - --timeout=30
            - --spider
            - http://localhost:9090/-/healthy
          {{- end }}
          timeoutSeconds: 30
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/prometheus/prometheus.yml
          name: prometheus-config
          subPath: prometheus.yml
        - mountPath: /etc/prometheus/alert.rules.yml
          name: prometheus-config
          subPath: alert.rules.yml
        - mountPath: /prometheus/data
          name: prom-data
      volumes:
        - name: prometheus-config
          configMap:
            name: prometheus-config
            items:
              - key: prometheus.yml
                path: prometheus.yml
                mode: 0644
              - key: alert.rules.yml
                path: alert.rules.yml
                mode: 0644
        - name: prom-data
          emptyDir: {}
    {{- 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 }}