{{- if .Values.cleanupController.enabled -}}
{{- if .Values.cleanupController.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.cleanup-controller.roleName" . }}
  labels:
    {{- include "kyverno.cleanup-controller.labels" . | nindent 4 }}
aggregationRule:
  clusterRoleSelectors:
    - matchLabels:
        {{- include "kyverno.cleanup-controller.matchLabels" . | nindent 8 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.cleanup-controller.roleName" . }}:core
  labels:
    {{- include "kyverno.cleanup-controller.labels" . | nindent 4 }}
rules:
  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - validatingwebhookconfigurations
    verbs:
      - create
      - delete
      - get
      - list
      - update
      - watch
  - apiGroups:
      - ''
    resources:
      - namespaces
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - kyverno.io
    resources:
      - clustercleanuppolicies
      - cleanuppolicies
      - clustercleanuppolicies/*
      - cleanuppolicies/*
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
      - deletecollection
  - apiGroups:
      - batch
    resources:
      - cronjobs
    verbs:
      - create
      - delete
      - get
      - list
      - update
      - watch
  - apiGroups:
    - ''
    - events.k8s.io
    resources:
    - events
    verbs:
    - create
    - patch
    - update
  - apiGroups:
      - authorization.k8s.io
    resources:
      - subjectaccessreviews
    verbs:
      - create
{{- with .Values.cleanupController.rbac.clusterRole.extraResources }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.cleanup-controller.roleName" $ }}:additional
  labels:
    {{- include "kyverno.cleanup-controller.labels" $ | nindent 4 }}
rules:
  {{- range . }}
  - apiGroups:
      {{- toYaml .apiGroups | nindent 6 }}
    resources:
      {{- toYaml .resources | nindent 6 }}
    verbs:
      - delete
      - list
  {{- end }}
{{- end }}
{{- end }}
{{- end }}
