# helm_charts_crd-cleanup-job.yaml
{{ if .Values.installCrds }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "sparkoperator.fullname" . }}-crd-cleanup
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-delete-policy": hook-succeeded
  labels:
    app.kubernetes.io/name: {{ include "sparkoperator.name" . }}
    helm.sh/chart: {{ include "sparkoperator.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  template:
    metadata:
      name: {{ include "sparkoperator.fullname" . }}-crd-cleanup
      {{- if .Values.istio.enabled }}
      annotations:
        "sidecar.istio.io/inject": "false"
      {{- end }}
    spec:
      serviceAccountName: {{ include "sparkoperator.serviceAccountName" . }}
      restartPolicy: OnFailure
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | trim | indent 8 }}
      containers:
        - name: delete-sparkapp-crd
          image: {{ .Values.operatorImageName }}:{{ .Values.operatorVersion }}
          imagePullPolicy: {{ .Values.imagePullPolicy }}
          {{- if .Values.securityContext }}
          securityContext:
            {{- range $securityPolicy, $value := .Values.securityContext }}
              {{ $securityPolicy }}: {{ $value }}
              {{- end }}
          {{- end }}
          command:
            - "/bin/sh"
            - "-c"
            - "curl -ik \
          -X DELETE \
          -H \"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)\" \
          -H \"Accept: application/json\" \
          -H \"Content-Type: application/json\" \
          https://kubernetes.default.svc/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/sparkapplications.sparkoperator.k8s.io"
        - name: delete-scheduledsparkapp-crd
          image: {{ .Values.operatorImageName }}:{{ .Values.operatorVersion }}
          imagePullPolicy: {{ .Values.imagePullPolicy }}
          {{- if .Values.securityContext }}
          securityContext:
            {{- range $securityPolicy, $value := .Values.securityContext }}
              {{ $securityPolicy }}: {{ $value }}
              {{- end }}
          {{- end }}
          command:
            - "/bin/sh"
            - "-c"
            - "curl -ik \
          -X DELETE \
          -H \"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)\" \
          -H \"Accept: application/json\" \
          -H \"Content-Type: application/json\" \
          https://kubernetes.default.svc/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/scheduledsparkapplications.sparkoperator.k8s.io"
{{ end }}