{{- if eq .Values.architecture "high-availability" }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "influxdb.fullname" . }}
  labels: 
    {{- include "influxdb.labels" . | nindent 4 }}
    app.kubernetes.io/component: influxdb
spec:
  serviceName: {{ include "influxdb.fullname" . }}-headless
  podManagementPolicy: {{ .Values.influxdb.podManagementPolicy }}
  replicas: {{ .Values.influxdb.replicaCount }}
  updateStrategy:
    type: {{ .Values.influxdb.updateStrategy }}
    {{- if (eq "OnDelete" .Values.influxdb.updateStrategy) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: 
      {{- include "influxdb.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: influxdb
  template:
    metadata:
      labels: 
        {{ include "influxdb.labels" . | nindent 8 }}
        app.kubernetes.io/component: influxdb
    spec:
{{- include "influxdb.imagePullSecrets" . | nindent 6 }}
      affinity:
        {{- if eq .Values.influxdb.antiAffinity "hard" }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels: 
                  {{- include "influxdb.matchLabels" . | nindent 18 }}
                  app.kubernetes.io/component: influxdb
        {{- else if eq .Values.influxdb.antiAffinity "soft" }}
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              podAffinityTerm:
                topologyKey: kubernetes.io/hostname
                labelSelector:
                  matchLabels: 
                    {{- include "influxdb.matchLabels" . | nindent 20 }}
                    app.kubernetes.io/component: influxdb
        {{- end }}
        {{- if .Values.influxdb.nodeAffinity }}
        nodeAffinity: {{- toYaml .Values.influxdb.nodeAffinity | nindent 10 }}
        {{- end }}
      {{- if .Values.influxdb.nodeSelector }}
      nodeSelector: {{- include "influxdb.tplValue" (dict "value" .Values.influxdb.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.influxdb.tolerations }}
      tolerations: {{- include "influxdb.tplValue" (dict "value" .Values.influxdb.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.influxdb.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.influxdb.securityContext.fsGroup }}
      {{- end }}
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
      initContainers:
        - name: init-chmod-data
          image: {{ include "influxdb.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -c
            - |
              mkdir -p /bitnami/influxdb/{data,meta,wal}
              chmod 700 /bitnami/influxdb/{data,meta,wal}
              {{- if eq ( toString ( .Values.volumePermissions.securityContext.runAsUser )) "auto" }}
                chown -R `id -u`:`id -G` /bitnami/influxdb/{data,meta,wal}
              {{- else }}
                chown -R {{ .Values.influxdb.securityContext.runAsUser }}:{{ .Values.influxdb.securityContext.fsGroup }} /bitnami/influxdb/{data,meta,wal}
              {{- end }}
          {{- if eq ( toString ( .Values.volumePermissions.securityContext.runAsUser )) "auto" }}
          securityContext:
          {{- else }}
          securityContext:
            runAsUser: {{ .Values.volumePermissions.securityContext.runAsUser }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/influxdb
      {{- end }}
      containers:
        - name: influxdb
          image: {{ include "influxdb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.influxdb.securityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.influxdb.securityContext.runAsUser }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: INFLUXDB_HTTP_AUTH_ENABLED
              value: {{ .Values.authEnabled | quote }}
            {{- if .Values.adminUser.name }}
            - name: INFLUXDB_ADMIN_USER
              value: {{ .Values.adminUser.name | quote }}
            {{- if .Values.adminUser.usePasswordFile }}
            - name: INFLUXDB_ADMIN_USER_PASSWORD_FILE
              value: "/opt/bitnami/influxdb/secrets/admin-user-password"
            {{- else }}
            - name: INFLUXDB_ADMIN_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "influxdb.secretName" . }}
                  key: admin-user-password
            {{- end }}
            {{- end }}
            {{- if .Values.user.name }}
            - name: INFLUXDB_USER
              value: {{ .Values.user.name | quote }}
            {{- if .Values.user.usePasswordFile }}
            - name: INFLUXDB_USER_PASSWORD_FILE
              value: "/opt/bitnami/influxdb/secrets/user-password"
            {{- else }}
            - name: INFLUXDB_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "influxdb.secretName" . }}
                  key: user-password
            {{- end }}
            {{- end }}
            {{- if .Values.readUser.name }}
            - name: INFLUXDB_READ_USER
              value: {{ .Values.readUser.name | quote }}
            {{- if .Values.readUser.usePasswordFile }}
            - name: INFLUXDB_READ_USER_PASSWORD_FILE
              value: "/opt/bitnami/influxdb/secrets/read-user-password"
            {{- else }}
            - name: INFLUXDB_READ_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "influxdb.secretName" . }}
                  key: read-user-password
            {{- end }}
            {{- end }}
            {{- if .Values.writeUser.name }}
            - name: INFLUXDB_WRITE_USER
              value: {{ .Values.writeUser.name | quote }}
            {{- if .Values.writeUser.usePasswordFile }}
            - name: INFLUXDB_WRITE_USER_PASSWORD_FILE
              value: "/opt/bitnami/influxdb/secrets/write-user-password"
            {{- else }}
            - name: INFLUXDB_WRITE_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "influxdb.secretName" . }}
                  key: write-user-password
            {{- end }}
            {{- end }}
            {{- if .Values.database }}
            - name: INFLUXDB_DB
              value: {{ .Values.database | quote }}
            {{- end }}
            {{- if .Values.influxdb.extraEnvVars }}
            {{- tpl (toYaml .Values.influxdb.extraEnvVars) $ | nindent 12 }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.influxdb.containerPorts.http }}
              protocol: TCP
            - name: rpc
              containerPort: {{ .Values.influxdb.containerPorts.rpc }}
              protocol: TCP
          {{- if .Values.influxdb.livenessProbe.enabled }}
          {{- $livenessTimeout := sub (int .Values.influxdb.livenessProbe.timeoutSeconds) 1 }}
          livenessProbe:
            exec:
              command:
                - bash
                - -c
                - |
                  if [[ -f "${INFLUXDB_ADMIN_USER_PASSWORD_FILE:-}" ]]; then
                      export INFLUXDB_ADMIN_USER_PASSWORD="$(< "${INFLUXDB_ADMIN_USER_PASSWORD_FILE}")"
                  fi
                  {{ if .Values.authEnabled }}INFLUX_USERNAME="$INFLUXDB_ADMIN_USER" INFLUX_PASSWORD="$INFLUXDB_ADMIN_USER_PASSWORD"{{ end }} timeout {{ $livenessTimeout }}s influx -host $POD_IP -port {{ .Values.influxdb.containerPorts.http }} -execute "SHOW DATABASES"
            initialDelaySeconds: {{ .Values.influxdb.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.influxdb.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.influxdb.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.influxdb.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.influxdb.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.influxdb.readinessProbe.enabled }}
          {{- $readinessTimeout := sub (int .Values.influxdb.readinessProbe.timeoutSeconds) 1 }}
          readinessProbe:
            exec:
              command:
                - bash
                - -c
                - |
                  if [[ -f "${INFLUXDB_ADMIN_USER_PASSWORD_FILE:-}" ]]; then
                      export INFLUXDB_ADMIN_USER_PASSWORD="$(< "${INFLUXDB_ADMIN_USER_PASSWORD_FILE}")"
                  fi
                  {{ if .Values.authEnabled }}INFLUX_USERNAME="$INFLUXDB_ADMIN_USER" INFLUX_PASSWORD="$INFLUXDB_ADMIN_USER_PASSWORD"{{ end }} timeout {{ $readinessTimeout }}s influx -host $POD_IP -port {{ .Values.influxdb.containerPorts.http }} -execute "SHOW DATABASES"
            initialDelaySeconds: {{ .Values.influxdb.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.influxdb.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.influxdb.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.influxdb.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.influxdb.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.influxdb.resources }}
          resources: {{- toYaml .Values.influxdb.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if or (.Files.Glob "files/conf/influxdb.conf") .Values.influxdb.configuration .Values.influxdb.existingConfiguration  }}
            - name: influxdb-config
              mountPath: /opt/bitnami/influxdb/etc
            {{- end }}
            {{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,txt}") .Values.influxdb.initdbScripts .Values.influxdb.initdbScriptsCM }}
            - name: custom-init-scripts
              mountPath: /docker-entrypoint-initdb.d/
            {{- end }}
            {{- if .Values.influxdb.initdbScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/secret
            {{- end }}
            {{- if or .Values.adminUser.usePasswordFile .Values.user.usePasswordFile .Values.readUser.usePasswordFile .Values.writeUser.usePasswordFile }}
            - name: influxdb-credentials
              mountPath: /opt/bitnami/influxdb/secrets/
            {{- end }}
            {{- if .Values.persistence.enabled }}
            - name: data
              mountPath: /bitnami/influxdb
            {{- end }}
      volumes:
        {{- if or (.Files.Glob "files/conf/influxdb.conf") .Values.influxdb.configuration .Values.influxdb.existingConfiguration }}
        - name: influxdb-config
          configMap:
            name: {{ include "influxdb.configmapName" . }}
        {{- end }}
        {{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,txt}") .Values.influxdb.initdbScripts .Values.influxdb.initdbScriptsCM }}
        - name: custom-init-scripts
          configMap:
            name: {{ include "influxdb.initdbScriptsConfigmapName" . }}
        {{- end }}
        {{- if .Values.influxdb.initdbScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ template "influxdb.initdbScriptsSecret" . }}
        {{- end }}
        {{- if or .Values.adminUser.usePasswordFile .Values.user.usePasswordFile .Values.readUser.usePasswordFile .Values.writeUser.usePasswordFile }}
        - name: influxdb-credentials
          secret:
            secretName: {{ include "influxdb.secretName" . }}
            items:
              {{- if .Values.adminUser.usePasswordFile }}
              - key: admin-user-password
                path: admin-user-password
              {{- end }}
              {{- if .Values.user.usePasswordFile }}
              - key: user-password
                path: user-password
              {{- end }}
              {{- if .Values.readUser.usePasswordFile }}
              - key: read-user-password
                path: read-user-password
              {{- end }}
              {{- if .Values.writeUser.usePasswordFile }}
              - key: write-user-password
                path: write-user-password
              {{- end }}
        {{- end }}
  {{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim }}
  {{- else if not .Values.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
        {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
{{- include "influxdb.storageClass" . | nindent 8 }}
  {{- end }}
{{- end }}
