apiVersion: batch/v1
kind: Job
metadata:
  name: bash-influx-setup-job
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: "influxdb"
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
  annotations:
    "helm.sh/hook": pre-install
    "helm.sh/hook-weight": "15" 
spec:
  template:
    spec:
      containers:
      - name: bash-job
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: ["/bin/bash", "-c"]
        args:
        - |
          #!/bin/bash
        
          sleep 5
          # Run the influx setup command and capture any output and error
          output=$(influx setup --host http://influxdb.contoso-motors.svc.cluster.local:8086 --bucket manufacturing --org InfluxData --password $(cat /etc/secrets/password) --username admin --token secret-token --force 2>&1)
          
          # If the command was successful, print the output and exit
          if [ $? -eq 0 ]; then
            echo "$output"
            exit 0
          fi

          # If the command failed, check if it's because the instance is already set up
          if echo "$output" | grep -q "Error: instance has already been set up"; then
            echo "InfluxDB is already set up."
            exit 0
          else
            # If the command failed for any other reason, print the output and exit with a failure status
            echo "$output"
            exit 1
          fi
          
        volumeMounts:
          - name: influxdb
            mountPath: /etc/secrets
            readOnly: true
      volumes:
        - name: influxdb
          secret:
            secretName: influxdb-pass
      restartPolicy: Never

