# helm_charts_post-install-set-auth.yaml
{{- if .Values.setDefaultUser.enabled -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "influxdb.fullname" . }}-set-auth
  labels:
    {{- include "influxdb.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-delete-policy": {{ .Values.setDefaultUser.hookDeletePolicy }}
spec:
  activeDeadlineSeconds: {{ .Values.setDefaultUser.activeDeadlineSeconds }}
  backoffLimit: {{ .Values.setDefaultUser.backoffLimit }}
  template:
    metadata:
      labels:
        {{- include "influxdb.selectorLabels" . | nindent 8 }}
    spec:
      containers:
      - name: {{ include "influxdb.fullname" . }}-set-auth
        image: "{{ .Values.setDefaultUser.image }}"
        env:
          - name: INFLUXDB_USER
            valueFrom:
              secretKeyRef:
                {{- if .Values.setDefaultUser.user.existingSecret }}
                name: {{ .Values.setDefaultUser.user.existingSecret -}}
                {{ else }}
                name: {{ include "influxdb.fullname" . }}-auth
                {{- end }}
                key: influxdb-user
          - name: INFLUXDB_PASSWORD
            valueFrom:
              secretKeyRef:
                {{- if .Values.setDefaultUser.user.existingSecret }}
                name: {{ .Values.setDefaultUser.user.existingSecret -}}
                {{ else }}
                name: {{ include "influxdb.fullname" . }}-auth
                {{- end }}
                key: influxdb-password
        args:
          - "/bin/sh"
          - "-c"
          - |
             curl -X POST http://{{ include "influxdb.fullname" . }}:{{ .Values.config.http.bind_address | default 8086 }}/query \
             --data-urlencode \
             "q=CREATE USER \"${INFLUXDB_USER}\" WITH PASSWORD '${INFLUXDB_PASSWORD}' {{ .Values.setDefaultUser.user.privileges }}"
      restartPolicy: {{ .Values.setDefaultUser.restartPolicy }}
{{- end -}}