{{- include "nplus.init" $ -}}
# Component: {{ .component.chartName }}
# will connect to:
{{- if (.this.nappl).host }}
#   nappl: {{ if ($.this.nappl).ssl -}}https{{- else -}}http{{- end -}}://{{ ($.this.nappl).host }}:{{ (.this.nappl).port }}/{{ (.this.nappl).instance }}
{{- else }}
#   defined by config file in conf PV.
{{- end }}
#
{{- if (.this.utils).maintenance -}}
# Job must not be running, as we are in maintenance mode and there might not even be a nappl service
{{- else }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .component.fullName }}
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}
spec:
  # Deletion ist done by Operator when successful, so no ttl necessary.
  # ttlSecondsAfterFinished: 60
  template:
    metadata:
      labels:
        app.kubernetes.io/version: {{ .Chart.AppVersion }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/instance: {{ .instance.group | default .instance.name | default .Release.Name }}
        app.kubernetes.io/component: {{ .component.chartName }}
        {{- include "nplus.templateLabels" . | nindent 8 }}
    spec:
      # hostname: {{ .component.fullName }}
      {{- include "nplus.imagePullSecrets" . | nindent 6 }}
      {{- include "nplus.podSecurityContext" . | nindent 6 }}
      {{- include "nplus.terminationGracePeriodSeconds" . | nindent 6 }}
      
      initContainers:

      {{- if (or .this.utils.disableWave (not (and .component.isArgo .this.meta.wave))) }}
      {{- include "nplus.waitOneMinute" . | nindent 6 }}
      {{- else }}
      # -- wait-one-minute - not waiting as {{ .this.utils.disableWave }} {{ .component.isArgo }} {{ .this.meta.wave }}
      {{- end }}
      {{- include "nplus.waitFor" . | nindent 6 }}
      
      containers:
      - name: run
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        env:
        # -- NAPPL Connection Credentials
          {{- include "nplus.envCredentials" (list
            "APP_AL_USER"       ($.this.nappl).account
            "APP_AL_PASSWORD"   ($.this.nappl).password
                                ($.this.nappl).secret
          ) | nindent 10 }}

        {{- include "nplus.environment" . | nindent 8 }}

        command: ["/bin/sh", "-c", "/config/run"]

        {{- include "nplus.resources" . | nindent 8 }}

        volumeMounts:
        {{- include "nplus.defaultMounts" . | nindent 8 }}
        - name: config
          mountPath: /config
        - name: conf
          subPath: {{ .this.instance.name }}
          mountPath: /instance

      volumes:
      {{- include "nplus.defaultVolumes" . | nindent 6 }}
      - name: config
        configMap:
          name: {{ .component.fullName }}-config
          defaultMode: 0777

      restartPolicy: Never
  backoffLimit: 0
{{- end }}
