apiVersion: extensions/v1beta1
kind: Deployment
metadata:
{{- if .Values.server.deploymentAnnotations }}
  annotations:
{{ toYaml .Values.server.deploymentAnnotations | indent 4 }}
{{- end }}
  labels:
    app: {{ template "prometheus.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    component: "{{ .Values.server.name }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "prometheus.server.fullname" . }}
spec:
  replicas: {{ .Values.server.replicaCount }}
  {{- if .Values.server.strategy }}
  strategy:
{{ toYaml .Values.server.strategy | indent 4 }}
  {{- end }}
  template:
    metadata:
    {{- if .Values.server.podAnnotations }}
      annotations:
{{ toYaml .Values.server.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "prometheus.name" . }}
        component: "{{ .Values.server.name }}"
        release: {{ .Release.Name }}
    spec:
{{- if .Values.server.affinity }}
      affinity:
{{ toYaml .Values.server.affinity | indent 8 }}
{{- end }}
{{- if .Values.server.priorityClassName }}
      priorityClassName: "{{ .Values.server.priorityClassName }}"
{{- end }}
{{- if .Values.server.schedulerName }}
      schedulerName: "{{ .Values.server.schedulerName }}"
{{- end }}
      serviceAccountName: {{ template "prometheus.serviceAccountName.server" . }}
      {{- if .Values.initChownData.enabled }}
      initContainers:
      - name: "{{ .Values.initChownData.name }}"
        image: "{{ .Values.initChownData.image.repository }}:{{ .Values.initChownData.image.tag }}"
        imagePullPolicy: "{{ .Values.initChownData.image.pullPolicy }}"
        resources:
{{ toYaml .Values.initChownData.resources | indent 12 }}
        # 65534 is the nobody user that prometheus uses.
        command: ["chown", "-R", "65534:65534", "{{ .Values.server.persistentVolume.mountPath }}"]
        volumeMounts:
        - name: storage-volume
          mountPath: {{ .Values.server.persistentVolume.mountPath }}
          subPath: "{{ .Values.server.persistentVolume.subPath }}"
      {{- end }}
      containers:
        - name: {{ template "prometheus.name" . }}-{{ .Values.server.name }}-{{ .Values.configmapReload.name }}
          image: "{{ .Values.configmapReload.image.repository }}:{{ .Values.configmapReload.image.tag }}"
          imagePullPolicy: "{{ .Values.configmapReload.image.pullPolicy }}"
          args:
            - --volume-dir=/etc/config
            - --webhook-url=http://127.0.0.1:9090{{ .Values.server.prefixURL }}/-/reload
          {{- range $key, $value := .Values.configmapReload.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}
          resources:
{{ toYaml .Values.configmapReload.resources | indent 12 }}
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
              readOnly: true
          {{- range .Values.configmapReload.extraConfigmapMounts }}
            - name: {{ $.Values.configmapReload.name }}-{{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
          {{- end }}

        - name: {{ template "prometheus.name" . }}-{{ .Values.server.name }}
          image: "{{ .Values.server.image.repository }}:{{ .Values.server.image.tag }}"
          imagePullPolicy: "{{ .Values.server.image.pullPolicy }}"
          args:
          {{- if .Values.server.retention }}
            - --storage.tsdb.retention={{ .Values.server.retention }}
          {{- end }}
            - --config.file=/etc/config/prometheus.yml
            - --storage.tsdb.path={{ .Values.server.persistentVolume.mountPath }}
            - --web.console.libraries=/etc/prometheus/console_libraries
            - --web.console.templates=/etc/prometheus/consoles
            - --web.enable-lifecycle
          {{- range $key, $value := .Values.server.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}
          {{- if .Values.server.baseURL }}
            - --web.external-url={{ .Values.server.baseURL }}
          {{- end }}
          {{- if .Values.server.enableAdminApi }}
            - --web.enable-admin-api
          {{- end }}
          ports:
            - containerPort: 9090
          readinessProbe:
            httpGet:
              path: {{ .Values.server.prefixURL }}/-/ready
              port: 9090
            initialDelaySeconds: 30
            timeoutSeconds: 30
          livenessProbe:
            httpGet:
              path: {{ .Values.server.prefixURL }}/-/healthy
              port: 9090
            initialDelaySeconds: 30
            timeoutSeconds: 30
          resources:
{{ toYaml .Values.server.resources | indent 12 }}
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
            - name: storage-volume
              mountPath: {{ .Values.server.persistentVolume.mountPath }}
              subPath: "{{ .Values.server.persistentVolume.subPath }}"
          {{- range .Values.server.extraHostPathMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.server.extraConfigmapMounts }}
            - name: {{ $.Values.server.name }}-{{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.server.extraSecretMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
    {{- if .Values.server.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.server.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.server.securityContext }}
      securityContext:
{{ toYaml .Values.server.securityContext | indent 8 }}
    {{- end }}
    {{- if .Values.server.tolerations }}
      tolerations:
{{ toYaml .Values.server.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.server.affinity }}
      affinity:
{{ toYaml .Values.server.affinity | indent 8 }}
    {{- end }}
      terminationGracePeriodSeconds: {{ .Values.server.terminationGracePeriodSeconds }}
      volumes:
        - name: config-volume
          configMap:
            name: {{ if .Values.server.configMapOverrideName }}{{ .Release.Name }}-{{ .Values.server.configMapOverrideName }}{{- else }}{{ template "prometheus.server.fullname" . }}{{- end }}
        - name: storage-volume
        {{- if .Values.server.persistentVolume.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.server.persistentVolume.existingClaim }}{{ .Values.server.persistentVolume.existingClaim }}{{- else }}{{ template "prometheus.server.fullname" . }}{{- end }}
        {{- else }}
          emptyDir: {}
        {{- end -}}
      {{- range .Values.server.extraHostPathMounts }}
        - name: {{ .name }}
          hostPath:
            path: {{ .hostPath }}
      {{- end }}
      {{- range .Values.configmapReload.extraConfigmapMounts }}
        - name: {{ $.Values.configmapReload.name }}-{{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
      {{- range .Values.server.extraConfigmapMounts }}
        - name: {{ $.Values.server.name }}-{{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
      {{- range .Values.server.extraSecretMounts }}
        - name: {{ .name }}
          secret:
            secretName: {{ .secretName }}
      {{- end }}
      {{- range .Values.configmapReload.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
