{{- if and .Values.rbac.create .Values.clusterAgent.enabled -}}
apiVersion: {{ template "rbac.apiVersion" . }}
kind: ClusterRole
metadata:
  labels:
    app: "{{ template "datadog.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
  name: {{ template "datadog.fullname" . }}-cluster-agent
rules:
- apiGroups:
  - ""
  resources:
  - services
  - events
  - endpoints
  - pods
  - nodes
  - componentstatuses
  verbs:
  - get
  - list
  - watch
- apiGroups: ["quota.openshift.io"]
  resources:
  - clusterresourcequotas
  verbs:
  - get
  - list
- apiGroups:
  - "autoscaling"
  resources:
  - horizontalpodautoscalers
  verbs:
  - list
  - watch
{{- if .Values.datadog.collectEvents }}
- apiGroups:
  - ""
  resources:
  - configmaps
  resourceNames:
  - datadogtoken  # Kubernetes event collection state
  verbs:
  - get
  - update
{{- end }}
- apiGroups:
  - ""
  resources:
  - configmaps
  resourceNames:
  - datadog-leader-election  # Leader election token
{{- if .Values.clusterAgent.metricsProvider.enabled }}
  - datadog-custom-metrics
  - extension-apiserver-authentication
{{- end }}
  verbs:
  - get
  - update
- apiGroups:  # To create the leader election token
  - ""
  resources:
  - configmaps
  verbs:
  - create
- nonResourceURLs:
  - "/version"
  - "/healthz"
  verbs:
  - get
---
apiVersion: {{ template "rbac.apiVersion" . }}
kind: ClusterRoleBinding
metadata:
  labels:
    app: "{{ template "datadog.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
  name: {{ template "datadog.fullname" . }}-cluster-agent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ template "datadog.fullname" . }}-cluster-agent
subjects:
  - kind: ServiceAccount
    name: {{ template "datadog.fullname" . }}-cluster-agent
    namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: "{{ template "datadog.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
  name: {{ template "datadog.fullname" . }}-cluster-agent
{{- end }}

{{- if and .Values.rbac.create .Values.clusterAgent.enabled .Values.clusterAgent.metricsProvider.enabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: "{{ template "datadog.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
  name: {{ template "datadog.fullname" . }}-cluster-agent:system:auth-delegator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
  - kind: ServiceAccount
    name: {{ template "datadog.fullname" . }}-cluster-agent
    namespace: {{ .Release.Namespace }}
{{- end -}}
