{{ if .Values.modeling.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: modeling
spec:
  schedule: "{{ .Values.modeling.schedule }}"
  suspend: {{ .Values.modeling.suspend }}
  timeZone: "{{ .Values.timeZone }}"

  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
#      ttlSecondsAfterFinished: 12000
      ttlSecondsAfterFinished: 604800 # 7 days
      backoffLimit: 0
      completions: 1
      parallelism: 1
      template:
        spec:
          restartPolicy: Never
          terminationGracePeriodSeconds: 1
          containers:
          - name: modeling
            image: {{.Values.modeling.image}}
            imagePullPolicy: Always
            volumeMounts:
            - mountPath: /work
              name: work
            - mountPath: /in/int
              name: in-int
            ports:
              - containerPort: 9091
            args:
              - bash
              - /app/run.sh
              - --cluster
              - {{ .Values.modeling.cluster }}
              - --region
              - {{ .Values.modeling.region }}
            env:
            - name: ADMIN_LOGIN
              valueFrom:
                secretKeyRef:
                  name: secret-admin
                  key: username
                  optional: false
            - name: ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-admin
                  key: password
                  optional: false
            - name: SERVICE_LOGIN
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: username
                  optional: false
            - name: SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: password
                  optional: false
            - name: S3_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: s3-access
                  key: keyid
                  optional: false
            - name: S3_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: s3-access
                  key: key
                  optional: false
            - name: S3_STORAGE_ENDPOINT
              value: "{{.Values.modeling.s3_storage_endpoint}}"

    {{ if .Values.local_user_id }}
            - name: LOCAL_USER_ID
              value: "{{.Values.local_user_id}}"
    {{ end }}
    {{ if .Values.local_user_name }}
            - name: LOCAL_USER_NAME
              value: "{{.Values.local_user_name}}"
    {{ end }}

          imagePullSecrets:
          - name: regcred

          volumes:
          - name: work
            hostPath:
              path: /data/{{.Values.customer}}/modeling
              type: Directory
#              readOnly: true
          - name: in-int
            hostPath:
              path: /data/{{.Values.customer}}/in/int
              type: Directory
          {{ include "app.affinity" . | nindent 10}}
{{ end }}