{{- if .Values.upgrade.enabled }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  annotations:
    "helm.sh/hook": pre-upgrade, pre-rollback
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
    "helm.sh/hook-weight": "1"
rules:
- apiGroups:
  - apps
  resources:
  - deployments
  - daemonsets
  - statefulsets
  verbs:
  - 'list'
  - 'delete'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  annotations:
    "helm.sh/hook": pre-upgrade, pre-rollback
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
    "helm.sh/hook-weight": "1"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
subjects:
- kind: ServiceAccount
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  labels:
    app: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  annotations:
    "helm.sh/hook": pre-upgrade, pre-rollback
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
    "helm.sh/hook-weight": "1"
rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  labels:
    app: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  annotations:
    "helm.sh/hook": pre-upgrade, pre-rollback
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
    "helm.sh/hook-weight": "1"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
subjects:
- kind: ServiceAccount
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.fullname" . }}-upgrade
  annotations:
    "helm.sh/hook": pre-upgrade, pre-rollback
    "helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded, hook-failed
    "helm.sh/hook-weight": "1"
{{- end }}