{{- if .Values.deployment.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "chart.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "chart.labels" . | nindent 4 }}
{{- with .Values.extraLabels }}
{{ toYaml . | indent 4 }}
{{- end }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- with .Values.deployment.strategy }}
  strategy:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "chart.selectorLabels" . | nindent 8 }}
{{- with .Values.extraLabels }}
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.podLabels }}
{{ toYaml . | indent 8 }}
{{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "chart.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}"
          workingDir: {{ .Values.containerWorkingDir }}
          args:
            - -promscrape.config=/config/scrape.yml
            - -remoteWrite.tmpDataPath=/tmpData
          {{- range .Values.remoteWriteUrls }}
            - -remoteWrite.url={{ . }}
          {{- end }}
          {{- range .Values.multiTenantUrls }}
            - -remoteWrite.multitenantURL={{ . }}
          {{- end }}
          {{- range $key, $value := .Values.extraArgs }}
            - -{{ $key }}={{ $value }}
          {{- end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8429
          {{- if .Values.extraArgs.graphiteListenAddr }}
            - name: graphite-tcp
              protocol: TCP
              containerPort: {{ include "split-host-port" .Values.extraArgs.graphiteListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.graphiteListenAddr }}
            - name: graphite-udp
              protocol: UDP
              containerPort: {{ include "split-host-port" .Values.extraArgs.graphiteListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.influxListenAddr }}
            - name: influx-tcp
              protocol: TCP
              containerPort: {{ include "split-host-port" .Values.extraArgs.influxListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.influxListenAddr }}
            - name: influx-udp
              protocol: UDP
              containerPort: {{ include "split-host-port" .Values.extraArgs.influxListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.opentsdbHTTPListenAddr }}
            - name: opentsdbhttp
              protocol: TCP
              containerPort: {{ include "split-host-port" .Values.extraArgs.opentsdbHTTPListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.opentsdbListenAddr }}
            - name: opentsdb-tcp
              protocol: TCP
              containerPort: {{ include "split-host-port" .Values.extraArgs.opentsdbListenAddr }}
          {{- end }}
          {{- if .Values.extraArgs.opentsdbListenAddr }}
            - name: opentsdb-udp
              protocol: UDP
              containerPort: {{ include "split-host-port" .Values.extraArgs.opentsdbListenAddr }}
          {{- end }}
          {{- with .Values.env }}
          env: {{ toYaml . | nindent 10 }}
          {{- end }}
          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 5
            periodSeconds: 15
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: 5
            periodSeconds: 15
            timeoutSeconds: 5
          volumeMounts:
            - name: tmpdata
              mountPath: /tmpData
            - name: config
              mountPath: /config
          {{- range .Values.extraHostPathMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- with .Values.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
        {{- with .Values.extraContainers }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{ toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      volumes:
        - name: tmpdata
      {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "chart.fullname" .) }}
      {{- else }}
          emptyDir: {}
      {{- end }}
        - name: config
          configMap:
            name: {{ include "chart.configname" .}}
      {{- range .Values.extraHostPathMounts }}
        - name: {{ .name }}
          hostPath:
            path: {{ .hostPath }}
      {{- end }}
      {{- with .Values.extraVolumes }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
    {{- end }}
{{- end }}
