{{- if .Values.cleanUpCRDs }}
# This job is meant primarily for cleaning up on CI systems.
# Using this on production systems, especially those that have multiple releases of Velero, will be destructive.
{{/* 'securityContext' got renamed to 'podSecurityContext', merge both dicts into one for backward compatibility */}}
{{- $podSecurityContext := merge (.Values.podSecurityContext | default dict) (.Values.securityContext | default dict) -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "velero.fullname" . }}-cleanup-crds
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-delete-policy": hook-succeeded
  labels:
    app.kubernetes.io/name: {{ include "velero.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "velero.chart" . }}
spec:
  backoffLimit: 3
  template:
    metadata:
      name: velero-cleanup-crds
      {{- with .Values.kubectl.labels }}
      labels:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.kubectl.annotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
    {{- if .Values.image.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
      serviceAccountName: {{ include "velero.serverServiceAccount" . }}
      containers:
        - name: kubectl
          {{- if .Values.kubectl.image.digest }}
          image: "{{ .Values.kubectl.image.repository }}@{{ .Values.kubectl.image.digest }}"
          {{- else if .Values.kubectl.image.tag }}
          image: "{{ .Values.kubectl.image.repository }}:{{ .Values.kubectl.image.tag }}"
          {{- else }}
          image: "{{ .Values.kubectl.image.repository }}:{{ template "chart.KubernetesVersion" . }}"
          {{- end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/sh
            - -c
            - >
              kubectl delete restore --all;
              kubectl delete backup --all;
              kubectl delete backupstoragelocation --all;
              kubectl delete volumesnapshotlocation --all;
              kubectl delete podvolumerestore --all;
              kubectl delete crd -l component=velero;
          {{- with .Values.kubectl.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.kubectl.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      restartPolicy: OnFailure
      {{- with $podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
