{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.operator.enabled .Values.rbac.create -}}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: ClusterRole
metadata:
  name: {{ template "kube-prometheus.operator.fullname" . }}
  labels: {{- include "kube-prometheus.operator.labels" . | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - apiextensions.k8s.io
    resources:
      - customresourcedefinitions
    verbs:
      - create
  - apiGroups:
      - apiextensions.k8s.io
    resourceNames:
      - alertmanagers.monitoring.coreos.com
      - podmonitors.monitoring.coreos.com
      - prometheuses.monitoring.coreos.com
      - prometheusrules.monitoring.coreos.com
      - servicemonitors.monitoring.coreos.com
      - thanosrulers.monitoring.coreos.com
      - probes.monitoring.coreos.com
      - scrapeconfigs.monitoring.coreos.com
      - prometheusagents.monitoring.coreos.com
    resources:
      - customresourcedefinitions
    verbs:
      - get
      - update
  - apiGroups:
      - monitoring.coreos.com
    resources:
      - alertmanagers
      - alertmanagers/status
      - alertmanagers/finalizers
      - alertmanagerconfigs
      - prometheuses
      - prometheuses/status
      - prometheuses/finalizers
      - thanosrulers
      - thanosrulers/finalizers
      - thanosrulers/status
      - servicemonitors
      - podmonitors
      - probes
      - prometheusrules
      - scrapeconfigs
      - prometheusagents
      - prometheusagents/finalizers
      - prometheusagents/status
    verbs:
      - '*'
  - apiGroups:
      - apps
    resources:
      - statefulsets
    verbs:
      - '*'
  - apiGroups:
      - ""
    resources:
      - configmaps
      - secrets
    verbs:
      - '*'
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - list
      - delete
  - apiGroups:
      - ""
    resources:
      - services
      - services/finalizers
      - endpoints
    verbs:
      - get
      - create
      - update
      - delete
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - namespaces
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - patch
      - create
  - apiGroups:
      - networking.k8s.io
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - storage.k8s.io
    resources:
      - storageclasses
    verbs:
      - get
  {{- if and .Values.rbac.rules.operator }}
    {{- include "common.tplvalues.render" (dict "value" .Values.rbac.rules.operator "context" $) | nindent 2 }}
  {{- end }}
{{- end }}
