{{- if .Values.statefulset.enabled }}
apiVersion: apps/v1
kind: StatefulSet
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 }}
  serviceName: {{ include "chart.fullname" . }}
  {{- if .Values.statefulset.updateStrategy }}
  updateStrategy:
    {{- toYaml .Values.statefulset.updateStrategy | 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
          {{- if .Values.statefulset.clusterMode }}
            - -promscrape.cluster.membersCount={{ .Values.replicaCount }}
            - -promscrape.cluster.replicationFactor={{ .Values.statefulset.replicationFactor }}
            - -promscrape.cluster.memberNum=$(POD_NAME)
          {{- end }}
          {{- 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 }}
          {{- if or .Values.env .Values.statefulset.clusterMode}}
          env:
            {{- if .Values.statefulset.clusterMode}}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- end -}}
            {{- with .Values.env  }}
            {{ toYaml .| nindent 12 }}
            {{- end }}
          {{- 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:
      {{- if not .Values.persistence.enabled }}
        - name: tmpdata
          emptyDir: {}
      {{- else if .Values.persistence.existingClaim }}
        - name: tmpdata
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim }}
      {{- 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 }}
  {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
  volumeClaimTemplates:
  - metadata:
      name: tmpdata
      {{- with .Values.persistence.extraLabels }}
      labels:
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.persistence.annotations  }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      accessModes:
        {{- range .Values.persistence.accessModes }}
        - {{ . | quote }}
        {{- end }}
      {{- if .Values.persistence.storageClassName }}
      storageClassName: {{ .Values.persistence.storageClassName }}
      {{- end }}
      {{- with .Values.persistence.matchLabels }}
      selector:
        matchLabels:
          {{- toYaml . | nindent 10 }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.persistence.size | quote }}
  {{- end }}
{{- end }}
