{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" (dict "customLabels" .Values.commonLabels "context" .) | nindent 4 }}
    app.kubernetes.io/part-of: influxdb
    app.kubernetes.io/component: influxdb
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" .) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (ne .Values.objectStore "file") (ne .Values.objectStore "memory") (not .Values.autoscaling.hpa.enabled) }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" (dict "customLabels" $podLabels "context" .) | nindent 6 }}
      app.kubernetes.io/part-of: influxdb
      app.kubernetes.io/component: influxdb
  template:
    metadata:
      annotations:
        {{- if include "influxdb.store.secret.create" . }}
        checksum/store-secret: {{ include (print .Template.BasePath "/store-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" .) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" (dict "customLabels" $podLabels "context" .) | nindent 8 }}
        app.kubernetes.io/part-of: influxdb
        app.kubernetes.io/component: influxdb
    spec:
      {{- include "influxdb.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" .) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "influxdb" "customLabels" $podLabels "context" .) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "influxdb" "customLabels" $podLabels "context" .) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" .) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "influxdb.serviceAccountName" . }}
      initContainers:
        {{- if and .Values.defaultInitContainers.volumePermissions.enabled .Values.persistence.enabled (eq .Values.objectStore "file") }}
        {{- include "influxdb.defaultInitContainers.volumePermissions" . | nindent 8 }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" .) | nindent 8 }}
        {{- end }}
      containers:
        - name: influxdb
          image: {{ include "influxdb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" .) | 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.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" .) | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" .) | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" .) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: INFLUXDB_OBJECT_STORE
              value: {{ .Values.objectStore | quote }}
            - name: INFLUXDB_HTTP_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: INFLUXDB_HTTP_AUTH_ENABLED
              value: {{ ternary "true" "false" .Values.auth.enabled | quote }}
            {{- if eq .Values.objectStore "file" }}
            - name: INFLUXDB_DATA_DIR
              value: {{ printf "%s/data" .Values.persistence.mountPath | quote }}
            {{- end }}
            - name: INFLUXDB_NODE_ID
              value: {{ .Values.nodeId | quote }}
            {{- if and .Values.auth.enabled (ne .Values.objectStore "memory") (or .Values.auth.existingSecret .Values.createAdminTokenJob.enabled) }}
            {{- if .Values.usePasswordFiles }}
            - name: INFLUXDB_ADMIN_TOKEN_FILE
              value: {{ printf "/opt/bitnami/influxdb/secrets/%s" (include "influxdb.secret.adminTokenKey" .) }}
            {{- else }}
            - name: INFLUXDB_ADMIN_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ template "influxdb.secret.name" . }}
                  key: {{ template "influxdb.secret.adminTokenKey" . }}
            {{- end }}
            {{- if .Values.databases }}
            - name: INFLUXDB_DATABASES
              value: {{ .Values.databases | quote }}
            {{- end }}
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: INFLUXDB3_TLS_CERT
              value: "/opt/bitnami/influxdb/certs/server/tls.crt"
            - name: INFLUXDB3_TLS_KEY
              value: "/opt/bitnami/influxdb/certs/server/tls.key"
            {{- end }}
            {{- if or (eq .Values.objectStore "s3") (eq .Values.objectStore "google") (eq .Values.objectStore "azure") }}
            - name: INFLUXDB3_BUCKET
              value: {{ .Values.bucket | quote }}
            {{- end }}
            {{- if eq .Values.objectStore "s3" }}
            {{- if .Values.s3.defaultRegion }}
            - name: AWS_DEFAULT_REGION
              value: {{ .Values.s3.defaultRegion | quote }}
            {{- end }}
            {{- if .Values.s3.endpoint }}
            - name: AWS_ENDPOINT
              value: {{ .Values.s3.endpoint | quote }}
            - name: AWS_ALLOW_HTTP
              value: {{ ternary "false" "true" (contains "https" .Values.s3.endpoint) | quote }}
            {{- end }}
            {{- if .Values.usePasswordFiles }}
            - name: AWS_ACCESS_KEY_ID_FILE
              value: "/opt/bitnami/influxdb/store-secrets/s3-access-key-id"
            - name: AWS_SECRET_ACCESS_KEY_FILE
              value: "/opt/bitnami/influxdb/store-secrets/s3-secret-access-key"
            {{- else }}
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: {{ template "influxdb.store.secret.name" . }}
                  key: s3-access-key-id
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "influxdb.store.secret.name" . }}
                  key: s3-secret-access-key
            {{- end }}
            {{- else if eq .Values.objectStore "google" }}
            {{- if .Values.usePasswordFiles }}
            - name: GOOGLE_SERVICE_ACCOUNT_FILE
              value: "/opt/bitnami/influxdb/store-secrets/google-service-account-key"
            {{- else }}
            - name: GOOGLE_SERVICE_ACCOUNT
              valueFrom:
                secretKeyRef:
                  name: {{ template "influxdb.store.secret.name" . }}
                  key: google-service-account-key
            {{- end }}
            {{- else if eq .Values.objectStore "azure" }}
            - name: AZURE_STORAGE_ACCOUNT
              value: {{ .Values.azure.account | quote }}
            {{- if .Values.usePasswordFiles }}
            - name: AZURE_STORAGE_ACCESS_KEY_FILE
              value: "/opt/bitnami/influxdb/store-secrets/azure-storage-access-key"
            {{- else }}
            - name: AZURE_STORAGE_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "influxdb.store.secret.name" . }}
                  key: azure-storage-access-key
            {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" .) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsCM .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
              protocol: TCP
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" .) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" .) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- omit .Values.startupProbe "enabled" | toYaml | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" .) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- omit .Values.livenessProbe "enabled" | toYaml | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" .) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- omit .Values.readinessProbe "enabled" | toYaml | nindent 12 }}
            {{- if and .Values.auth.createAdminToken (ne .Values.objectStore "memory") }}
            {{- $readinessTimeout := max (sub (int .Values.readinessProbe.timeoutSeconds) 1) 1 }}
            exec:
              command:
                - bash
                - -c
                - |
                  {{- if .Values.usePasswordFiles }}
                  INFLUXDB_ADMIN_TOKEN="$(<INFLUXDB_ADMIN_TOKEN_FILE)"
                  {{- end -}}
                  timeout {{ $readinessTimeout }}s curl -sSL -H "Authorization: Bearer $INFLUXDB_ADMIN_TOKEN" http://127.0.0.1:{{ .Values.containerPorts.http }}/health
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if and .Values.auth.enabled (ne .Values.objectStore "memory") .Values.usePasswordFiles (or .Values.auth.existingSecret .Values.createAdminTokenJob.enabled) }}
            - name: secret
              mountPath: /opt/bitnami/influxdb/secrets
              readOnly: true
            {{- end }}
            {{- if and (or (eq .Values.objectStore "s3") (eq .Values.objectStore "google") (eq .Values.objectStore "azure")) .Values.usePasswordFiles }}
            - name: store-secret
              mountPath: /opt/bitnami/influxdb/store-secrets
              readOnly: true
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: ca-certs
              mountPath: /opt/bitnami/influxdb/certs/ca
              readOnly: true
            - name: server-certs
              mountPath: /opt/bitnami/influxdb/certs/server
              readOnly: true
            {{- end }}
            {{- if or .Values.initdbScripts .Values.initdbScriptsCM }}
            - name: custom-init-scripts
              mountPath: /docker-entrypoint-initdb.d
            {{- end }}
            {{- if .Values.initdbScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/secret
            {{- end }}
            {{- if eq .Values.objectStore "file" }}
            - mountPath: {{ .Values.persistence.mountPath }}              
              name: data
            {{- end }}
          {{- if .Values.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" .) | nindent 12 }}
          {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" .) | trim | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if and .Values.auth.enabled (ne .Values.objectStore "memory") .Values.usePasswordFiles (or .Values.auth.existingSecret .Values.createAdminTokenJob.enabled) }}
        - name: secret
          secret:
            secretName: {{ template "influxdb.secret.name" . }}
            defaultMode: 256
        {{- end }}
        {{- if and (or (eq .Values.objectStore "s3") (eq .Values.objectStore "google") (eq .Values.objectStore "azure")) .Values.usePasswordFiles }}
        - name: store-secret
          secret:
            secretName: {{ template "influxdb.store.secret.name" . }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: ca-certs
          secret:
            secretName: {{ template "influxdb.tls.ca.secretName" . }}
            defaultMode: 256
        - name: server-certs
          secret:
            secretName: {{ template "influxdb.tls.server.secretName" . }}
            defaultMode: 256
        {{- end }}
        {{- if or .Values.initdbScripts .Values.initdbScriptsCM }}
        - name: custom-init-scripts
          configMap:
            name: {{ include "influxdb.initdbScriptsConfigmapName" . }}
        {{- end }}
        {{- if .Values.initdbScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ template "influxdb.initdbScriptsSecret" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" .) | nindent 8 }}
        {{- end }}
        {{- if eq .Values.objectStore "file" }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ (tpl .Values.persistence.existingClaim .) | default (include "common.names.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- end }}
