apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "appmesh-prometheus.fullname" . }}
  labels:
{{ include "appmesh-prometheus.labels" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "appmesh-prometheus.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "appmesh-prometheus.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/part-of: appmesh
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/config.yaml") . | sha256sum | quote }}
    spec:
      serviceAccountName: {{ include "appmesh-prometheus.serviceAccountName" . }}
      volumes:
      - name: config-volume
        configMap:
          name: {{ template "appmesh-prometheus.fullname" . }}
      - name: data-volume
        {{- if .Values.persistentVolumeClaim.claimName }}
        persistentVolumeClaim:
            claimName: {{ .Values.persistentVolumeClaim.claimName }}
        {{- else }}
        emptyDir: {}
        {{- end }}
      initContainers:
        - name: chown
          image: alpine:latest
          command: ["/bin/sh", "-c"]
          args:
            - chown 65534:65534 /mount;
          volumeMounts:
            - name: data-volume
              mountPath: /mount
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - "--storage.tsdb.retention={{ .Values.retention }}"
            - "--config.file=/etc/prometheus/prometheus.yml"
          ports:
            - name: http
              containerPort: 9090
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /-/healthy
              port: http
          readinessProbe:
            httpGet:
              path: /-/ready
              port: http
          securityContext:
            runAsUser: 65534
            runAsGroup: 65534
            runAsNonRoot: true
          volumeMounts:
            - name: config-volume
              mountPath: /etc/prometheus
            - name: data-volume
              mountPath: /prometheus/data
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
