{{- define "nplus.environment.nstorecopy" -}}
{{- if (.Values.nstoreDownloader).enabled }}
template:
  metadata:
    labels:
      {{- include "nplus.instanceLabels" . | nindent 6 }}
  spec:
    {{- include "nplus.podSecurityContext" . | nindent 4 }}
    {{- include "nplus.imagePullSecrets" . | nindent 4 }}
    containers:
    - name: downloader
      image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
      imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
      {{ include "nplus.containerSecurityContext" . | nindent 6 }}
      command: [ "/bin/sh", "-c" ]
      args:
      - |
          mkdir -p /conf/{{ .Values.nstoreDownloader.target }}
          cd /conf/{{ .Values.nstoreDownloader.target }}
          wget -r -np -nH -nc -nd -A zip -X '*/*/*/*/*/*/1*,*/*/*/*/*/*/2*,*/*/*/*/*/*/3*,*/*/*/*/*/*/4*,*/*/*/*/*/*/5*,*/*/*/*/*/*/6*,*/*/*/*/*/*/7*,*/*/*/*/*/*/8*' -nd {{ .Values.nstoreDownloader.nstore }}
      volumeMounts:
      - name: conf
        mountPath: /conf

    restartPolicy: OnFailure
    volumes:
    - name: conf
      persistentVolumeClaim:
        claimName: conf
{{- end -}}
{{- end -}}
---
{{- if .Values.nstoreDownloader.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ .component.fullName }}-nstore
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}
spec:
  schedule: "0 3 * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      {{- include "nplus.environment.nstorecopy" . | nindent 6 }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .component.fullName }}-nstore-oncreate
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}
spec:
  ttlSecondsAfterFinished: 60
  {{- include "nplus.environment.nstorecopy" . | nindent 2 }}
{{- else }}{{/* if .Values.nstoreDownloader.enabled */}}
# nstore Downloader is disabled
{{- end }}{{/* if .Values.nstoreDownloader.enabled else */}}