{{- if and .Values.controller.enabled .Values.rbac.installClusterRoles }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kargo-controller
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.controller.labels" . | nindent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
{{- if .Values.controller.serviceAccount.clusterWideSecretReadingEnabled }}
- apiGroups:
  - ""
  resources:
  - secrets
  verbs:
  - get
  - list
  - watch
{{- end }}
- apiGroups:
  - kargo.akuity.io
  resources:
  - projects
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - kargo.akuity.io
  resources:
  - freights
  - promotions
  verbs:
  - create
  - get
  - list
  - watch
- apiGroups:
  - kargo.akuity.io
  resources:
  - stages
  verbs:
  - get
  - list
  - patch
  - promote
  - watch
- apiGroups:
  - kargo.akuity.io
  resources:
  - warehouses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - kargo.akuity.io
  resources:
  - freights/status
  - promotions/status
  - stages/status
  - warehouses/finalizers
  - warehouses/status
  verbs:
  - patch
{{- if not .Values.controller.serviceAccount.clusterWideSecretReadingEnabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kargo-controller-read-secrets
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.controller.labels" . | nindent 4 }}
rules:
- apiGroups:
  - ""
  resources:
  - secrets
  verbs:
  - get
  - list
  - watch
{{- end }}
{{- if and .Values.controller.argocd.integrationEnabled (not .Values.controller.argocd.watchArgocdNamespaceOnly) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kargo-controller-argocd
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.controller.labels" . | nindent 4 }}
rules:
- apiGroups:
  - argoproj.io
  resources:
  - applications
  verbs:
  - get
  - list
  - patch
  - watch
{{- end }}
{{- if .Values.controller.rollouts.integrationEnabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kargo-controller-rollouts
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.controller.labels" . | nindent 4 }}
rules:
- apiGroups:
  - argoproj.io
  resources:
  - analysistemplates
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - argoproj.io
  resources:
  - analysisruns
  verbs:
  - create
  - get
  - list
  - patch
  - watch
  - deletecollection
{{- end }}
{{- end }}
