{{- if and .Values.elasticsearch.hosts .Values.elasticsearch.port -}}
apiVersion: {{ template "kibana.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "kibana.fullname" . }}
  labels: {{- include "kibana.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- tpl (toYaml .Values.updateStrategy) $ | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "kibana.matchLabels" . | nindent 6 }}
  template:
    metadata:
      {{- if .Values.podAnnotations }}
      annotations: {{- include "kibana.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "kibana.labels" . | nindent 8 }}
        app: kibana
        {{- if .Values.podLabels }}
        {{- include "kibana.tplValue" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "kibana.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "kibana.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "kibana.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "kibana.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      {{- if or .Values.initContainers (and .Values.volumePermissions.enabled .Values.persistence.enabled) }}
      initContainers:
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: "{{ template "kibana.volumePermissions.image" . }}"
          imagePullPolicy: {{ default "" .Values.volumePermissions.image.pullPolicy | quote }}
          command: ["chown", "-R", "{{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }}", "/bitnami/kibana"]
          securityContext:
            runAsUser: 0
          resources: {{ toYaml .Values.volumePermissions.resources | nindent 12 }}
          volumeMounts:
          - name: kibana-data
            mountPath: /bitnami/kibana
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "kibana.tplValue" (dict "value" .Values.initContainers "context" $) | nindent 10 }}
        {{- end }}
      {{- end }}
      containers:
        - name: kibana
          image: {{ include "kibana.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.securityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.securityContext.runAsUser }}
          {{- end }}
          env:
            - name: KIBANA_ELASTICSEARCH_URL
              value: {{ include "kibana.elasticsearch.url" . | quote }}
            - name: KIBANA_ELASTICSEARCH_PORT
              value: {{ include "kibana.elasticsearch.port" . | quote }}
            - name: KIBANA_FORCE_INITSCRIPTS
              value: {{ .Values.forceInitScripts | quote }}
          {{- if .Values.extraEnvVars }}
          {{- include "kibana.tplValue" (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: 5601
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
            {{- if .Values.configuration.server.rewriteBasePath }}
              path: {{ .Values.configuration.server.basePath }}/app/kibana
            {{- else }}
              path: /app/kibana
            {{- end }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
            {{- if .Values.configuration.server.rewriteBasePath }}
              path: {{ .Values.configuration.server.basePath }}/app/kibana
            {{- else }}
              path: /app/kibana
            {{- end }}
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- include "kibana.tplValue" (dict "value" .Values.resources "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: kibana-data
              mountPath: /bitnami/kibana
            - name: kibana-config
              mountPath: /bitnami/kibana/conf
            {{- 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 "kibana.tplValue" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
      {{- if .Values.sidecars }}
      {{- include "kibana.tplValue" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
      {{- end }}
      volumes:
        - name: kibana-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "kibana.pvc" . }}
        {{- else }}
          emptyDir: {}
        {{ end }}
        - name: kibana-config
          configMap:
            name: {{ include "kibana.configurationCM" . }}
        {{- if (include "kibana.importSavedObjects" .) }}
        - name: saved-objects-init-scripts
          configMap:
            name: {{ include "kibana.fullname" . }}-saved-objects
            defaultMode: 0755
        {{- end }}
        {{- if .Values.plugins }}
        - name: plugins-init-scripts
          configMap:
            name: {{ include "kibana.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 "kibana.tplValue" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
