# Clean up the AppRepository resources used by this Kubeapps instance
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "kubeapps.apprepository-jobs-cleanup.fullname" . }}
  annotations:
    helm.sh/hook: post-delete
    helm.sh/hook-delete-policy: hook-succeeded
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.apprepository-jobs-cleanup.fullname" . }}
spec:
  template:
    metadata:
      labels:
        app: {{ template "kubeapps.apprepository-jobs-cleanup.fullname" . }}
        app.kubernetes.io/name: {{ template "kubeapps.name" . }}
        release: {{ .Release.Name }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
{{- include "kubeapps.imagePullSecrets" . | indent 6 }}
      {{- if .Values.hooks.affinity }}
      affinity: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.hooks.nodeSelector }}
      nodeSelector: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.hooks.tolerations }}
      tolerations: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      restartPolicy: OnFailure
      serviceAccountName: {{ template "kubeapps.apprepository-jobs-cleanup.fullname" . }}
      containers:
        - name: kubectl
          image: {{ template "kubeapps.image" (list .Values.hooks.image .Values.global) }}
          imagePullPolicy: {{ .Values.hooks.image.pullPolicy | quote }}
          command:
            - /bin/sh
          args:
            - -ec
            - |
              kubectl delete apprepositories.kubeapps.com -n {{ .Release.Namespace }} --all
              kubectl delete secrets -n {{ .Release.Namespace }} -l app={{ template "kubeapps.name" $ }},release={{ $.Release.Name }}
