{{- define "cleanupHookName.annotations" -}}
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": hook-succeeded
{{- end }}
{{- if .Values.cleanupCRD }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "vm-operator.cleanupHookName" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{ include "vm-operator.labels" . | nindent 4 }}
  annotations: {{ include "cleanupHookName.annotations" . | nindent 4 }}
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}"
      labels: {{ include "vm-operator.labels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "vm-operator.cleanupHookName" . }}
      containers:
        - name: kubectl
          image: "{{ (index .Values "cleanupImage" "repository") }}:{{ (index .Values "cleanupImage" "tag") }}"
          imagePullPolicy: "{{ (index .Values "cleanupImage" "pullPolicy") }}"
          resources:
            limits:
              cpu: "500m"
              memory: "256Mi"
            requests:
              cpu: "100m"
              memory: "56Mi"
          command:
            - /bin/sh
            - -c
            - >
              kubectl delete vmalerts --all --ignore-not-found=true;
              kubectl delete vmagents --all --ignore-not-found=true;
              kubectl delete vmsingles --all --ignore-not-found=true;
              kubectl delete vmalertmanagers --all --ignore-not-found=true;
              kubectl delete vmclusters --all --ignore-not-found=true;
      restartPolicy: OnFailure
    {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "vm-operator.cleanupHookName" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "vm-operator.labels" . | nindent 4 }}
  annotations: {{ include "cleanupHookName.annotations" . | nindent 4 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "vm-operator.cleanupHookName" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "vm-operator.labels" . | nindent 4 }}
  annotations: {{ include "cleanupHookName.annotations" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "vm-operator.cleanupHookName" . }}
subjects:
  - kind: ServiceAccount
    name: {{ include "vm-operator.cleanupHookName" . }}
    namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "vm-operator.cleanupHookName" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "vm-operator.labels" . | nindent 4 }}
  annotations: {{ include "cleanupHookName.annotations" . | nindent 4 }}
rules:
  - apiGroups: ["operator.victoriametrics.com"]
    resources:
      - vmagents
      - vmalerts
      - vmsingles
      - vmalertmanagers
      - vmclusters
    verbs: ["get", "list", "watch","delete"]
---
{{- end }}

