{{- if .Values.uninstallVPA }}
apiVersion: v1
kind: ServiceAccount
metadata:
  annotations:
    "helm.sh/hook": "pre-upgrade,post-delete"
    "helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation,hook-failed"
    "helm.sh/hook-weight": "-250"
  name: {{ include "goldilocks.fullname" . }}-vpa-uninstall
  namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "goldilocks.fullname" . }}-vpa-uninstall
  labels:
    app.kubernetes.io/name: {{ include "goldilocks.name" . }}
    helm.sh/chart: {{ include "goldilocks.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: vpa-uninstall
  annotations:
    "helm.sh/hook": "pre-upgrade,post-delete"
    "helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation,hook-failed"
    "helm.sh/hook-weight": "-240"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: {{ include "goldilocks.fullname" . }}-vpa-uninstall
    namespace: {{ .Release.Namespace }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "goldilocks.fullname" . }}-vpa-uninstall
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "goldilocks.name" . }}
    helm.sh/chart: {{ include "goldilocks.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: vpa-uninstall
  annotations:
    "helm.sh/hook": "pre-upgrade,post-delete"
    "helm.sh/hook-delete-policy": "before-hook-creation"
    "helm.sh/hook-weight": "-230"
spec:
  template:
    metadata:
      name: {{ include "goldilocks.fullname" . }}-vpa-uninstall
      labels:
        app.kubernetes.io/name: {{ include "goldilocks.name" . }}
        helm.sh/chart: {{ include "goldilocks.chart" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/component: vpa-uninstall
    spec:
      restartPolicy: Never
      serviceAccountName: {{ include "goldilocks.fullname" . }}-vpa-uninstall
      containers:
      - name: vpa-uninstall
        image: quay.io/reactiveops/ci-images:v9-alpine
        command: ["bash"]
        args:
          - -c
          - |
            apk --update add git openssl
            git clone "https://github.com/kubernetes/autoscaler.git"
            cd autoscaler/vertical-pod-autoscaler
            git checkout {{ .Values.vpaVersion }}
            ./hack/vpa-down.sh
{{- end }}
