{{- if .Values.spark.enabled -}}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "jaeger.fullname" . }}-spark
  labels:
    app.kubernetes.io/name: {{ include "jaeger.name" . }}
    helm.sh/chart: {{ include "jaeger.chart" . }}
    app.kubernetes.io/component: spark
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.spark.annotations }}
  annotations:
{{ toYaml .Values.spark.annotations | indent 4 }}
{{- end }}
spec:
  schedule: {{ .Values.spark.schedule | quote }}
  successfulJobsHistoryLimit: {{ .Values.spark.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.spark.failedJobsHistoryLimit }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app.kubernetes.io/name: {{ include "jaeger.name" . }}
            app.kubernetes.io/component: spark
            app.kubernetes.io/instance: {{ .Release.Name }}
            {{- if .Values.spark.podLabels }}
{{ toYaml .Values.spark.podLabels | indent 12 }}
            {{- end }}
        spec:
          nodeSelector:
{{ toYaml .Values.spark.nodeSelector | indent 12 }}
{{- if .Values.spark.tolerations }}
          tolerations:
{{ toYaml .Values.spark.tolerations | indent 12 }}
{{- end }}
          serviceAccountName: {{ template "jaeger.spark.serviceAccountName" . }}
          containers:
          - name: {{ include "jaeger.fullname" . }}-spark
            image: {{ .Values.spark.image }}:{{ .Values.spark.tag }}
            imagePullPolicy: {{ .Values.spark.pullPolicy }}
            env:
            - name: STORAGE
              value: {{ .Values.storage.type }}
            {{- if eq .Values.storage.type "cassandra" }}
            - name: CASSANDRA_CONTACT_POINTS
              value: {{ template "cassandra.contact_points" . }}
            - name: CASSANDRA_KEYSPACE
              value: {{ printf "%s_%s" "jaeger_v1" .Values.cassandra.config.dc_name | quote }}
              {{- end }}
            {{- if eq .Values.storage.type "elasticsearch" }}
            - name: ES_NODES
              value: {{ template "elasticsearch.client.url" . }}
            - name: ES_NODES_WAN_ONLY
              value: {{ .Values.storage.elasticsearch.nodesWanOnly | quote }}
            {{- if .Values.storage.elasticsearch.usePassword }}
            - name: ES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ if .Values.storage.elasticsearch.existingSecret }}{{ .Values.storage.elasticsearch.existingSecret }}{{- else }}{{ include "jaeger.fullname" . }}-elasticsearch{{- end }}
                  key: password
            {{- end }}
            - name: ES_USERNAME
              value: {{ .Values.storage.elasticsearch.user }}
            {{- end }}
            resources:
{{ toYaml .Values.spark.resources | indent 14 }}
          restartPolicy: OnFailure
{{- end -}}
