{{- if and .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  {{- with .Values.rbac.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
  name: crd-controller
  labels:
    app.kubernetes.io/instance: {{ .Release.Namespace | quote }}
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote  }}
    app.kubernetes.io/part-of: flux
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
rules:
- apiGroups: ['source.toolkit.fluxcd.io']
  resources: ['*']
  verbs: ['*']
- apiGroups: ['kustomize.toolkit.fluxcd.io']
  resources: ['*']
  verbs: ['*']
- apiGroups: ['helm.toolkit.fluxcd.io']
  resources: ['*']
  verbs: ['*']
- apiGroups: ['notification.toolkit.fluxcd.io']
  resources: ['*']
  verbs: ['*']
- apiGroups: ['image.toolkit.fluxcd.io']
  resources: ['*']
  verbs: ['*']
- apiGroups:
  - ""
  resources:
  - namespaces
  - secrets
  - configmaps
  - serviceaccounts
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
# required by leader election
- apiGroups:
    - ""
  resources:
    - configmaps
  verbs:
    - get
    - list
    - watch
    - create
    - update
    - patch
    - delete
- apiGroups:
    - ""
  resources:
    - configmaps/status
  verbs:
    - get
    - update
    - patch
- apiGroups:
  - "coordination.k8s.io"
  resources:
  - leases
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - patch
  - delete
{{- end }}
