apiVersion: batch/v1
kind: Job
metadata:
  name: pre-install-{{ template "kibana.fullname" . }}
  labels: {{ include "kibana.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": hook-succeeded
    {{- if .Values.annotations }}
    {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
    {{- end }}
spec:
  backoffLimit: 20
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: create-kibana-token
          image: "{{ .Values.image }}:{{ .Values.imageTag }}"
          imagePullPolicy: "{{ .Values.imagePullPolicy }}"
          command: ["{{ template "kibana.home_dir" . }}/node/bin/node"]
          args:
           - {{ template "kibana.home_dir" . }}/helm-scripts/manage-es-token.js
           - create
          env:
            - name: "ELASTICSEARCH_USERNAME"
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.elasticsearchCredentialSecret }}
                  key: username
            - name: "ELASTICSEARCH_PASSWORD"
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.elasticsearchCredentialSecret }}
                  key: password
            - name: ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES
              value: "{{ template "kibana.home_dir" . }}/config/certs/{{ .Values.elasticsearchCertificateAuthoritiesFile }}"
          volumeMounts:
            - name: elasticsearch-certs
              mountPath: {{ template "kibana.home_dir" . }}/config/certs
              readOnly: true
            - name: kibana-helm-scripts
              mountPath: {{ template "kibana.home_dir" . }}/helm-scripts
      serviceAccount: pre-install-{{ template "kibana.fullname" . }}
      volumes:
        - name: elasticsearch-certs
          secret:
            secretName: {{ .Values.elasticsearchCertificateSecret }}
        - name: kibana-helm-scripts
          configMap:
            name: {{ template "kibana.fullname" . }}-helm-scripts
            defaultMode: 0755
