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

{{- if and .Values.elasticsearch.hosts .Values.elasticsearch.port -}}
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 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- $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 }}
  template:
    metadata:
      {{- if or .Values.podAnnotations (include "kibana.createTlsSecret" .) }}
      annotations:
        {{- if (include "kibana.createTlsSecret" .) }}
        checksum/tls: {{ include (print $.Template.BasePath "/tls-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app: kibana
    spec:
      {{- include "kibana.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- 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 "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "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.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.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ include "kibana.serviceAccountName" . }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "kibana.volumePermissions.image" . }}
          imagePullPolicy: {{ default "" .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/kibana
              chown {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }} /bitnami/kibana
              find /bitnami/kibana -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{ toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
          - name: kibana-data
            mountPath: /bitnami/kibana
            {{- if .Values.persistence.subPath }}
            subPath: {{ .Values.persistence.subPath }}
            {{- end }}
        {{- end }}
        - name: copy-default-plugins
          image: {{ include "kibana.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.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 }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
                #!/bin/bash

                . /opt/bitnami/scripts/libfs.sh
                . /opt/bitnami/scripts/kibana-env.sh

                if ! is_dir_empty "$SERVER_DEFAULT_PLUGINS_DIR"; then
                    cp -nr "$SERVER_DEFAULT_PLUGINS_DIR"/* /plugins
                fi
          volumeMounts:
            - name: empty-dir
              mountPath: /plugins
              subPath: app-plugins-dir
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: kibana
          image: {{ include "kibana.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- 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 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else 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: KIBANA_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: KIBANA_ELASTICSEARCH_URL
              value: {{ include "kibana.elasticsearch.url" . | quote }}
            - name: KIBANA_ELASTICSEARCH_PORT_NUMBER
              value: {{ include "kibana.elasticsearch.port" . | quote }}
            - name: KIBANA_FORCE_INITSCRIPTS
              value: {{ .Values.forceInitScripts | quote }}
            - name: KIBANA_SERVER_ENABLE_TLS
              value: {{ ternary "true" "false" .Values.tls.enabled | quote }}
            {{- if or .Values.tls.usePemCerts (include "kibana.createTlsSecret" . ) }}
            - name: KIBANA_SERVER_TLS_USE_PEM
              value: "true"
            {{- end }}
            {{- if and .Values.tls.enabled .Values.tls.usePemCerts (or .Values.tls.keyPassword .Values.tls.passwordsSecret) }}
            {{- if .Values.usePasswordFiles }}
            - name: KIBANA_SERVER_KEY_PASSWORD_FILE
              value: "/opt/bitnami/kibana/secrets/kibana-key-password"
            {{- else }}
            - name: KIBANA_SERVER_KEY_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.tls.secretName" . }}
                  key: kibana-key-password
            {{- end }}
            {{- end }}
            {{- if and .Values.tls.enabled (not .Values.tls.usePemCerts) (or .Values.tls.keystorePassword .Values.tls.passwordsSecret) }}
            {{- if .Values.usePasswordFiles }}
            - name: KIBANA_SERVER_KEYSTORE_PASSWORD_FILE
              value: "/opt/bitnami/kibana/secrets/kibana-keystore-password"
            {{- else }}
            - name: KIBANA_SERVER_KEYSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.tls.secretName" . }}
                  key: kibana-keystore-password
            {{- end }}
            {{- end }}
            {{- if .Values.elasticsearch.security.auth.enabled }}
            {{- if .Values.usePasswordFiles }}
            - name: KIBANA_PASSWORD_FILE
              value: "/opt/bitnami/kibana/secrets/kibana-password"
            {{- else }}
            - name: KIBANA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.elasticsearch.auth.secretName" . }}
                  key: kibana-password
            {{- end }}
            {{- end }}
            {{- if and .Values.elasticsearch.security.auth.enabled .Values.elasticsearch.security.auth.createSystemUser }}
            {{- if .Values.usePasswordFiles }}
            - name: KIBANA_ELASTICSEARCH_PASSWORD_FILE
              value: "/opt/bitnami/kibana/secrets/elasticsearch-password"
            {{- else }}
            - name: KIBANA_ELASTICSEARCH_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ tpl .Values.elasticsearch.security.auth.elasticsearchPasswordSecret . }}
                  key: elasticsearch-password
            {{- end }}
            - name: KIBANA_CREATE_USER
              value: "true"
            {{- end }}
            - name: KIBANA_ELASTICSEARCH_ENABLE_TLS
              value: {{ ternary "true" "false" .Values.elasticsearch.security.tls.enabled | quote }}
            - name: KIBANA_ELASTICSEARCH_TLS_USE_PEM
              value: {{ ternary "true" "false" .Values.elasticsearch.security.tls.usePemCerts | quote }}
            - name: KIBANA_ELASTICSEARCH_TLS_VERIFICATION_MODE
              value: {{ .Values.elasticsearch.security.tls.verificationMode | quote }}
            {{- if and .Values.elasticsearch.security.tls.enabled (not .Values.elasticsearch.security.tls.usePemCerts) (or .Values.elasticsearch.security.tls.truststorePassword .Values.elasticsearch.security.tls.passwordsSecret) }}
            {{- if .Values.usePasswordFiles }}
            - name: KIBANA_ELASTICSEARCH_TRUSTSTORE_PASSWORD_FILE
              value: "/opt/bitnami/kibana/secrets/elasticsearch-truststore-password"
            {{- else }}
            - name: KIBANA_ELASTICSEARCH_TRUSTSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.elasticsearch.tls.secretName" . }}
                  key: elasticsearch-truststore-password
            {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | 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: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: {{ printf "%s/status" (default "" .Values.configuration.server.basePath) }}
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.tls.enabled }}
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.resources "context" $) | 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
            - name: empty-dir
              mountPath: /opt/bitnami/kibana/config
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/kibana/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/kibana/logs
              subPath: app-logs-dir
            - name: empty-dir
              mountPath: /opt/bitnami/kibana/plugins
              subPath: app-plugins-dir
            - name: kibana-data
              mountPath: /bitnami/kibana
            - name: kibana-config
              mountPath: /bitnami/kibana/conf
            {{- if and .Values.usePasswordFiles (or .Values.elasticsearch.security.auth.enabled (and .Values.tls.enabled (or .Values.tls.passwordsSecret (ternary .Values.tls.keyPassword .Values.tls.keystorePassword .Values.tls.usePemCerts)))) }}
            - name: kibana-secrets
              mountPath: /opt/bitnami/kibana/secrets
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: kibana-certificates
              mountPath: /opt/bitnami/kibana/config/certs/server
              readOnly: true
            {{- end }}
            {{- if and .Values.elasticsearch.security.tls.enabled (not (eq .Values.elasticsearch.security.tls.verificationMode "none" )) }}
            - name: elasticsearch-certificates
              mountPath: /opt/bitnami/kibana/config/certs/elasticsearch
              readOnly: true
            {{- end }}
            {{- if .Values.plugins }}
            - name: plugins-init-scripts
              mountPath: /docker-entrypoint-initdb.d/plugin-install
            {{- end }}
            {{- if (include "kibana.importSavedObjects" .) }}
            - name: saved-objects-init-scripts
              mountPath: /docker-entrypoint-initdb.d/saved-objects-import
            {{- end }}
            {{- if .Values.savedObjects.configmap }}
            - name: saved-objects-configmap
              mountPath: /bitnami/kibana/saved-objects
            {{- end }}
            {{- if .Values.initScriptsCM }}
            - name: custom-init-scripts-cm
              mountPath: /docker-entrypoint-initdb.d/cm
            {{- end }}
            {{- if .Values.initScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/secret
            {{- 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" $) | nindent 8 }}
      {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if and .Values.usePasswordFiles (or .Values.elasticsearch.security.auth.enabled
        (and .Values.elasticsearch.security.tls.enabled (not .Values.elasticsearch.security.tls.usePemCerts) (or .Values.elasticsearch.security.tls.truststorePassword .Values.elasticsearch.security.tls.passwordsSecret))
        (and .Values.tls.enabled (or .Values.tls.passwordsSecret (ternary .Values.tls.keyPassword .Values.tls.keystorePassword .Values.tls.usePemCerts)))) }}
        - name: kibana-secrets
          projected:
            sources:
                {{- if and .Values.tls.enabled (or .Values.tls.passwordsSecret (ternary .Values.tls.keyPassword .Values.tls.keystorePassword .Values.tls.usePemCerts)) }}
                - secret:
                    name: {{ include "kibana.tls.secretName" . }}
                {{- end }}
                {{- if .Values.elasticsearch.security.auth.enabled }}
                - secret:
                    name: {{ include "kibana.elasticsearch.auth.secretName" . }}
                {{- end }}
                {{- if and .Values.elasticsearch.security.auth.enabled .Values.elasticsearch.security.auth.createSystemUser }}
                - secret:
                    name: {{ tpl .Values.elasticsearch.security.auth.elasticsearchPasswordSecret . }}
                {{- end }}
                {{- if and .Values.elasticsearch.security.tls.enabled (not .Values.elasticsearch.security.tls.usePemCerts) (or .Values.elasticsearch.security.tls.truststorePassword .Values.elasticsearch.security.tls.passwordsSecret) }}
                - secret:
                    name: {{ include "kibana.elasticsearch.tls.secretName" . }}
                {{- end }}
        {{- end }}
        - name: kibana-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "kibana.pvc" . }}
        {{- else }}
          emptyDir: {}
        {{ end }}
        {{- if .Values.tls.enabled }}
        - name: kibana-certificates
          secret:
            secretName: {{ include "kibana.tlsSecretName" . }}
            defaultMode: 256
        {{- end }}
        {{- if and .Values.elasticsearch.security.tls.enabled (ne .Values.elasticsearch.security.tls.verificationMode "none" ) }}
        - name: elasticsearch-certificates
          secret:
            secretName: {{ required "A secret containing the Truststore or CA certificate for Elasticsearch is required" .Values.elasticsearch.security.tls.existingSecret }}
            defaultMode: 256
        {{- end }}
        - name: kibana-config
          configMap:
            name: {{ include "kibana.configurationCM" . }}
        {{- if (include "kibana.importSavedObjects" .) }}
        - name: saved-objects-init-scripts
          configMap:
            name: {{ include "common.names.fullname" . }}-saved-objects
            defaultMode: 0755
        {{- end }}
        {{- if .Values.plugins }}
        - name: plugins-init-scripts
          configMap:
            name: {{ include "common.names.fullname" . }}-plugins
            defaultMode: 0755
        {{- end }}
        {{- if .Values.initScriptsCM }}
        - name: custom-init-scripts-cm
          configMap:
            name: {{ template "kibana.initScriptsCM" . }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.initScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            name: {{ template "kibana.initScriptsSecret" . }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.savedObjects.configmap }}
        - name: saved-objects-configmap
          configMap:
            name: {{ template "kibana.savedObjectsCM" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
