{{- if and .Values.operator.rbac.create .Values.operator.enabled }}
{{- if .Values.operator.leaderElect }}
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: Role
metadata:
  name: {{ include "common.names.fullname" . }}-leader-election
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
- apiGroups:
    - ""
  resources:
    - configmaps
  verbs:
    - get
    - list
    - watch
    - create
    - update
    - patch
    - delete
- apiGroups:
    - coordination.k8s.io
  resources:
    - leases
  verbs:
    - get
    - list
    - watch
    - create
    - update
    - patch
    - delete
- apiGroups:
    - ""
  resources:
    - events
  verbs:
    - create
    - patch
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
kind: RoleBinding
metadata:
  name: {{ include "common.names.fullname" . }}-leader-election
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "common.names.fullname" . }}-leader-election
subjects:
  - kind: ServiceAccount
    name: {{ include "grafana-operator.serviceAccountName" . }}
{{- end }}
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
{{- if or .Values.operator.scanAllNamespaces .Values.operator.scanNamespaces }}
kind: ClusterRole
{{- else }}
kind: Role
{{- end }}
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - persistentvolumeclaims
      - secrets
      - serviceaccounts
      - services
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - get
      - list
      - watch
      - patch
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - apps
      - extensions
    resources:
      - deployments
      - deployments/finalizers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - integreatly.org
    resources:
      - grafanadashboards
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - integreatly.org
    resources:
      - grafanadashboards/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - integreatly.org
    resources:
      - grafanadatasources
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - integreatly.org
    resources:
      - grafanadatasources/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - integreatly.org
    resources:
      - grafananotificationchannels
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - integreatly.org
    resources:
      - grafananotificationchannels/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - integreatly.org
    resources:
      - grafanas
      - grafanas/finalizers
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - integreatly.org
    resources:
      - grafanas/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - networking.k8s.io
    resources:
      - ingresses
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - route.openshift.io
    resources:
      - routes
      - routes/custom-host
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  {{- if or .Values.operator.scanAllNamespaces  .Values.operator.scanNamespaces }}
  - nonResourceURLs:
      - /metrics
    verbs:
      - get
  {{- end }}
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
{{- if or .Values.operator.scanAllNamespaces  .Values.operator.scanNamespaces }}
kind: ClusterRoleBinding
{{- else }}
kind: RoleBinding
{{- end }}
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
roleRef:
  name: {{ include "common.names.fullname" . }}
  {{- if or .Values.operator.scanAllNamespaces  .Values.operator.scanNamespaces }}
  kind: ClusterRole
  {{- else }}
  kind: Role
  {{- end }}
  apiGroup: rbac.authorization.k8s.io
subjects:
  - kind: ServiceAccount
    name: {{ include "grafana-operator.serviceAccountName" . }}
    {{- if or .Values.operator.scanAllNamespaces .Values.operator.scanNamespaces }}
    namespace: {{ include "common.names.namespace" . }}
    {{- end }}
{{- end }}
