{{- if and .Values.webhooksServer.enabled .Values.rbac.installClusterRoleBindings }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kargo-webhooks-server
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.webhooksServer.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kargo-webhooks-server
subjects:
- kind: ServiceAccount
  namespace: {{ .Release.Namespace }}
  name: kargo-webhooks-server
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kargo-webhooks-server-ns-controller
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.webhooksServer.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kargo-webhooks-server-ns-controller
subjects:
- kind: ServiceAccount
  namespace: kube-system
  name: namespace-controller
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kargo-webhooks-server-generic-gc
  labels:
    {{- include "kargo.labels" . | nindent 4 }}
    {{- include "kargo.webhooksServer.labels" . | nindent 4 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kargo-webhooks-server-generic-gc
subjects:
- kind: ServiceAccount
  namespace: kube-system
  name: generic-garbage-collector
{{- end }}
