{{- if .Values.spark.enabled -}}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "jaeger.fullname" . }}-spark
  namespace: {{ .Release.Namespace  }}
  labels:
    {{- include "jaeger.labels" . | nindent 4 }}
    app.kubernetes.io/component: spark
{{- if .Values.spark.annotations }}
  annotations:
    {{- toYaml .Values.spark.annotations | nindent 4 }}
{{- end }}
spec:
  schedule: {{ .Values.spark.schedule | quote }}
  successfulJobsHistoryLimit: {{ .Values.spark.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.spark.failedJobsHistoryLimit }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            {{- include "jaeger.selectorLabels" . | nindent 12 }}
            app.kubernetes.io/component: spark
            {{- if .Values.spark.podLabels }}
            {{- toYaml .Values.spark.podLabels | nindent 12 }}
            {{- end }}
        spec:
          serviceAccountName: {{ template "jaeger.spark.serviceAccountName" . }}
          {{- with .Values.spark.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          containers:
          - name: {{ include "jaeger.fullname" . }}-spark
            image: {{ .Values.spark.image }}:{{ .Values.spark.tag }}
            imagePullPolicy: {{ .Values.spark.pullPolicy }}
            args:
              {{- range $key, $value := .Values.spark.cmdlineParams }}
              {{- if $value }}
              - --{{ $key }}={{ $value }}
              {{- else }}
              - --{{ $key }}
              {{- end }}
              {{- end }}
            env:
              - name: STORAGE
                value: {{ .Values.storage.type }}
              {{- include "storage.env" . | nindent 14 }}
            {{- if .Values.spark.extraEnv }}
              {{- toYaml .Values.spark.extraEnv | nindent 14 }}
            {{- end }}
              - name: CASSANDRA_CONTACT_POINTS
                value: {{ include "cassandra.contact_points" . }}
              - name: ES_NODES
                value: {{ include "elasticsearch.client.url" . }}
              - name: ES_NODES_WAN_ONLY
                value: {{ .Values.storage.elasticsearch.nodesWanOnly | quote }}
            resources:
              {{- toYaml .Values.spark.resources | nindent 14 }}
            volumeMounts:
            {{- range .Values.spark.extraConfigmapMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- range .Values.spark.extraSecretMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
          restartPolicy: OnFailure
          volumes:
          {{- range .Values.spark.extraConfigmapMounts }}
            - name: {{ .name }}
              configMap:
                name: {{ .configMap }}
          {{- end }}
          {{- range .Values.spark.extraSecretMounts }}
            - name: {{ .name }}
              secret:
                secretName: {{ .secretName }}
          {{- end }}
        {{- with .Values.spark.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- with .Values.spark.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
        {{- end }}
        {{- with .Values.spark.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
        {{- end }}
{{- end -}}
