{{- if .Values.reportsController.enabled -}}
{{- if .Values.reportsController.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.reports-controller.roleName" . }}
  labels:
    {{- include "kyverno.reports-controller.labels" . | nindent 4 }}
aggregationRule:
  clusterRoleSelectors:
    - matchLabels:
        {{- include "kyverno.reports-controller.matchLabels" . | nindent 8 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.reports-controller.roleName" . }}:core
  labels:
    {{- include "kyverno.reports-controller.labels" . | nindent 4 }}
rules:
  - apiGroups:
      - '*'
    resources:
      - '*'
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - kyverno.io
    resources:
      - admissionreports
      - clusteradmissionreports
      - backgroundscanreports
      - clusterbackgroundscanreports
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
      - deletecollection
  - apiGroups:
      - wgpolicyk8s.io
    resources:
      - policyreports
      - policyreports/status
      - clusterpolicyreports
      - clusterpolicyreports/status
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
      - deletecollection
  - apiGroups:
      - ''
      - events.k8s.io
    resources:
      - events
    verbs:
      - create
      - patch
{{- with .Values.reportsController.rbac.clusterRole.extraResources }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "kyverno.reports-controller.roleName" $ }}:additional
  labels:
    {{- include "kyverno.reports-controller.labels" $ | nindent 4 }}
rules:
  {{- range . }}
  - apiGroups:
      {{- toYaml .apiGroups | nindent 6 }}
    resources:
      {{- toYaml .resources | nindent 6 }}
    verbs:
      - get
      - list
      - watch
  {{- end }}
{{- end }}
{{- end }}
{{- end }}
