{{- 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" . | nindent 4 }}
    {{- 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: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | 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" . | nindent 8 }}
        app: kibana
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "kibana.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 "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "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: {{- omit .Values.podSecurityContext "enabled" | toYaml | 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
          resources: {{ toYaml .Values.volumePermissions.resources | nindent 12 }}
          volumeMounts:
          - name: kibana-data
            mountPath: /bitnami/kibana
            {{- if .Values.persistence.subPath }}
            subPath: {{ .Values.persistence.subPath }}
            {{- end }}
        {{- end }}
        {{- 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: {{- omit .Values.containerSecurityContext "enabled" | toYaml | 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) }}
            - name: KIBANA_SERVER_KEY_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.tls.secretName" . }}
                  key: kibana-key-password
            {{- end }}
            {{- if and .Values.tls.enabled (not .Values.tls.usePemCerts) (or .Values.tls.keystorePassword .Values.tls.passwordsSecret) }}
            - name: KIBANA_SERVER_KEYSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.tls.secretName" . }}
                  key: kibana-keystore-password
            {{- end }}
            {{- if .Values.elasticsearch.security.auth.enabled }}
            - name: KIBANA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.elasticsearch.auth.secretName" . }}
                  key: kibana-password
            {{- end }}
            {{- if and .Values.elasticsearch.security.auth.enabled .Values.elasticsearch.security.auth.createSystemUser }}
            - name: KIBANA_ELASTICSEARCH_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ tpl .Values.elasticsearch.security.auth.elasticsearchPasswordSecret . }}
                  key: elasticsearch-password
            - 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) }}
            - name: KIBANA_ELASTICSEARCH_TRUSTSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kibana.elasticsearch.tls.secretName" . }}
                  key: elasticsearch-truststore-password
            {{- 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.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: {{ printf "%s/login" (default "" .Values.configuration.server.basePath) }}
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.tls.enabled }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- 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 }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.resources "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: kibana-data
              mountPath: /bitnami/kibana
            - name: kibana-config
              mountPath: /bitnami/kibana/conf
            {{- 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: 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 }}
