{{- if and .Values.prometheusOperator.enabled .Values.prometheusOperator.cleanupCustomResource }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "prometheus-operator.fullname" . }}-operator-cleanup
  namespace: {{ template "prometheus-operator.namespace" . }}
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "3"
    "helm.sh/hook-delete-policy": hook-succeeded
  labels:
    app: {{ template "prometheus-operator.name" . }}-operator
{{ include "prometheus-operator.labels" . | indent 4 }}
spec:
  template:
    metadata:
      name: {{ template "prometheus-operator.fullname" . }}-operator-cleanup
      labels:
        app: {{ template "prometheus-operator.name" . }}-operator
{{ include "prometheus-operator.labels" . | indent 8 }}
    spec:
    {{- if .Values.global.rbac.create }}
      serviceAccountName: {{ template "prometheus-operator.operator.serviceAccountName" . }}
    {{- end }}
      containers:
        - name: kubectl
          {{- if .Values.prometheusOperator.hyperkubeImage.sha }}
          image: {{ .Values.prometheusOperator.hyperkubeImage.repository }}:{{ .Values.prometheusOperator.hyperkubeImage.tag }}@sha256:{{ .Values.prometheusOperator.hyperkubeImage.sha }}
          {{- else }}
          image: "{{ .Values.prometheusOperator.hyperkubeImage.repository }}:{{ .Values.prometheusOperator.hyperkubeImage.tag }}"
          {{- end }}
          imagePullPolicy: "{{ .Values.prometheusOperator.hyperkubeImage.pullPolicy }}"
          command:
          - /bin/sh
          - -c
          - >
              kubectl delete alertmanager   --all;
              kubectl delete prometheus     --all;
              kubectl delete prometheusrule --all;
              kubectl delete servicemonitor --all;
              sleep 10;
              kubectl delete crd alertmanagers.monitoring.coreos.com;
              kubectl delete crd prometheuses.monitoring.coreos.com;
              kubectl delete crd prometheusrules.monitoring.coreos.com;
              kubectl delete crd servicemonitors.monitoring.coreos.com;
              kubectl delete crd podmonitors.monitoring.coreos.com;
              kubectl delete crd thanosrulers.monitoring.coreos.com;
      restartPolicy: OnFailure
{{- end }}
