apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ template "kibana.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "kibana.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "kibana.name" . }}
        release: "{{ .Release.Name }}"
    spec:
      {{- if .Values.serviceAccountName }}
      serviceAccountName: {{ .Values.serviceAccountName }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
{{- if .Values.dashboardImport.dashboards }}
      initContainers:
      - name: {{ .Chart.Name }}-dashboardimport
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: ["/bin/bash"]
        args:
          - "-c"
          - "/tmp/dashboardImport.sh"
{{- if .Values.commandline.args }}
{{ toYaml .Values.commandline.args | indent 10 }}
{{- end }}
        env:
        {{- range $key, $value := .Values.env }}
        - name: "{{ $key }}"
          value: "{{ $value }}"
        {{- end }}
        ports:
        - containerPort: {{ .Values.service.internalPort }}
          name: {{ template "kibana.name" . }}
          protocol: TCP
        volumeMounts:
        - name: {{ template "kibana.fullname" . }}-dashboards
          mountPath: "/kibanadashboards"
        - name: {{ template "kibana.fullname" . }}-importscript
          mountPath: "/tmp/dashboardImport.sh"
          subPath: dashboardImport.sh
        {{- range $configFile := (keys .Values.files) }}
        - name: {{ template "kibana.name" $ }}
          mountPath: "/usr/share/kibana/config/{{ $configFile }}"
          subPath: {{ $configFile }}
        {{- end }}
{{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.commandline.args }}
        args:
          - "/bin/bash"
          - "/usr/local/bin/kibana-docker"
{{ toYaml .Values.commandline.args | indent 10 }}
        {{- end }}
        env:
        {{- range $key, $value := .Values.env }}
        - name: "{{ $key }}"
          value: "{{ $value }}"
        {{- end }}
{{- if (not .Values.authProxyEnabled) }}
        ports:
        - containerPort: {{ .Values.service.internalPort }}
          name: {{ template "kibana.name" . }}
          protocol: TCP
{{- end }}
{{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /status
            port: {{ .Values.service.internalPort }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
{{- end }}
{{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /status
            port: {{ .Values.service.internalPort }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
{{- end }}
{{- with .Values.extraContainers }}
{{ tpl . $ | indent 6 }}
{{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        {{- range $configFile := (keys .Values.files) }}
        - name: {{ template "kibana.name" $ }}
          mountPath: "/usr/share/kibana/config/{{ $configFile }}"
          subPath: {{ $configFile }}
        {{- end }}
    {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      volumes:
        - name: {{ template "kibana.name" . }}
          configMap:
            name: {{ template "kibana.fullname" . }}
{{- if .Values.dashboardImport.dashboards }}
        - name: {{ template "kibana.fullname" . }}-dashboards
          configMap:
            name: {{ template "kibana.fullname" . }}-dashboards
        - name: {{ template "kibana.fullname" . }}-importscript
          configMap:
            name: {{ template "kibana.fullname" . }}-importscript
            defaultMode: 0777
{{- end }}
