apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: influxdb
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  {{- if .Values.influxdb.updateStrategy }}
  strategy: {{- toYaml .Values.influxdb.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: influxdb
  template:
    metadata:
      {{- if .Values.influxdb.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: influxdb
        {{- if .Values.influxdb.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "influxdb.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.influxdb.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.influxdb.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.influxdb.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.influxdb.podAffinityPreset "component" "influxdb" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.influxdb.podAntiAffinityPreset "component" "influxdb" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.influxdb.nodeAffinityPreset.type "key" .Values.influxdb.nodeAffinityPreset.key "values" .Values.influxdb.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.influxdb.schedulerName }}
      schedulerName: {{ .Values.influxdb.schedulerName }}
      {{- end }}
      {{- if .Values.influxdb.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      priorityClassName: {{ .Values.influxdb.priorityClassName | quote }}
      {{- if .Values.influxdb.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.influxdb.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.influxdb.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.influxdb.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "influxdb.serviceAccountName" . }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - 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 | cut -d " " -f2` /bitnami/influxdb
              {{- else }}
              chown -R {{ .Values.influxdb.containerSecurityContext.runAsUser }}:{{ .Values.influxdb.podSecurityContext.fsGroup }} /bitnami/influxdb
              {{- 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 }}
        {{- if .Values.influxdb.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
      containers:
        - name: influxdb
          image: {{ include "influxdb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.influxdb.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.influxdb.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.influxdb.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.influxdb.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.influxdb.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: INFLUXDB_HTTP_AUTH_ENABLED
              value: {{ .Values.auth.enabled | quote }}
            - name: INFLUXDB_CREATE_USER_TOKEN
              value: {{ ternary "yes" "no" .Values.auth.createUserToken | quote }}
            {{- if .Values.auth.admin.username }}
            - name: INFLUXDB_ADMIN_USER
              value: {{ .Values.auth.admin.username | quote }}
            {{- end }}
            {{- if .Values.auth.usePasswordFiles }}
            - 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 }}
            {{- if .Values.auth.usePasswordFiles }}
            - name: INFLUXDB_ADMIN_USER_TOKEN_FILE
              value: "/opt/bitnami/influxdb/secrets/admin-user-token"
            {{- else }}
            - name: INFLUXDB_ADMIN_USER_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ include "influxdb.secretName" . }}
                  key: admin-user-token
            {{- end }}
            - name: INFLUXDB_ADMIN_BUCKET
              value: {{ .Values.auth.admin.bucket | quote }}
            - name: INFLUXDB_ADMIN_ORG
              value: {{ .Values.auth.admin.org | quote }}
            {{- if .Values.auth.user.username }}
            - name: INFLUXDB_USER
              value: {{ .Values.auth.user.username | quote }}
            {{- if .Values.auth.usePasswordFiles }}
            - 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.auth.user.bucket }}
            - name: INFLUXDB_USER_BUCKET
              value: {{ .Values.auth.user.bucket | quote }}
            {{- end }}
            {{- if .Values.auth.user.org }}
            - name: INFLUXDB_USER_ORG
              value: {{ .Values.auth.user.org | quote }}
            {{- end }}
            {{- if .Values.auth.readUser.username }}
            - name: INFLUXDB_READ_USER
              value: {{ .Values.auth.readUser.username | quote }}
            {{- if .Values.auth.usePasswordFiles }}
            - 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.auth.writeUser.username }}
            - name: INFLUXDB_WRITE_USER
              value: {{ .Values.auth.writeUser.username | quote }}
            {{- if .Values.auth.usePasswordFiles }}
            - 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.auth.user.bucket }}
            - name: INFLUXDB_DB
              value: {{ .Values.auth.user.bucket | quote }}
            {{- end }}
            {{- if .Values.influxdb.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.influxdb.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.influxdb.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.influxdb.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.influxdb.extraEnvVarsSecret }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.influxdb.containerPorts.http }}
              protocol: TCP
            - name: rpc
              containerPort: {{ .Values.influxdb.containerPorts.rpc }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.influxdb.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.influxdb.startupProbe.enabled }}
          {{- $startupTimeout := sub (int .Values.influxdb.startupProbe.timeoutSeconds) 1 }}
          startupProbe: {{- omit .Values.influxdb.startupProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - bash
                - -c
                - |
                  . /opt/bitnami/scripts/libinfluxdb.sh

                  influxdb_env

                  {{- if .Values.auth.enabled }}
                  export INFLUX_USERNAME="$INFLUXDB_ADMIN_USER"
                  export INFLUX_PASSWORD="$INFLUXDB_ADMIN_USER_PASSWORD"
                  {{- end }}

                  timeout {{ $startupTimeout }}s influx --host http://$POD_IP:{{ .Values.influxdb.containerPorts.http }} ping
          {{- end }}
          {{- if .Values.influxdb.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.influxdb.livenessProbe.enabled }}
          {{- $livenessTimeout := sub (int .Values.influxdb.livenessProbe.timeoutSeconds) 1 }}
          livenessProbe: {{- omit .Values.influxdb.livenessProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - bash
                - -c
                - |
                  . /opt/bitnami/scripts/libinfluxdb.sh

                  influxdb_env

                  {{- if .Values.auth.enabled }}
                  export INFLUX_USERNAME="$INFLUXDB_ADMIN_USER"
                  export INFLUX_PASSWORD="$INFLUXDB_ADMIN_USER_PASSWORD"
                  {{- end }}

                  timeout {{ $livenessTimeout }}s influx ping --host http://$POD_IP:{{ .Values.influxdb.containerPorts.http }}
          {{- end }}
          {{- if .Values.influxdb.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.influxdb.readinessProbe.enabled }}
          {{- $readinessTimeout := sub (int .Values.influxdb.readinessProbe.timeoutSeconds) 1 }}
          readinessProbe: {{- omit .Values.influxdb.readinessProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - bash
                - -c
                - |
                  . /opt/bitnami/scripts/libinfluxdb.sh

                  influxdb_env

                  {{- if .Values.auth.enabled }}
                  export INFLUX_USERNAME="$INFLUXDB_ADMIN_USER"
                  export INFLUX_PASSWORD="$INFLUXDB_ADMIN_USER_PASSWORD"
                  {{- end }}

                  timeout {{ $readinessTimeout }}s influx ping --host http://$POD_IP:{{ .Values.influxdb.containerPorts.http }}
          {{- end }}
          {{- end }}
          {{- if .Values.influxdb.resources }}
          resources: {{- toYaml .Values.influxdb.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if or .Values.influxdb.configuration .Values.influxdb.existingConfiguration  }}
            - name: influxdb-config
              mountPath: /opt/bitnami/influxdb/etc
            {{- end }}
            {{- if or .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  .Values.auth.usePasswordFiles }}
            - name: influxdb-credentials
              mountPath: /opt/bitnami/influxdb/secrets/
            {{- end }}
            {{- if .Values.persistence.enabled }}
            - name: data
              mountPath: /bitnami/influxdb
            {{- end }}
          {{- if .Values.influxdb.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.influxdb.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      volumes:
        {{- if or .Values.influxdb.configuration .Values.influxdb.existingConfiguration }}
        - name: influxdb-config
          configMap:
            name: {{ include "influxdb.configmapName" . }}
        {{- end }}
        {{- if or .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 .Values.auth.usePasswordFiles }}
        - name: influxdb-credentials
          secret:
            secretName: {{ include "influxdb.secretName" . }}
        {{- end }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "influxdb.claimName" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- if .Values.influxdb.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.influxdb.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
