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

{{- if and .Values.operator.rbac.create (not .Values.operator.watchAllNamespaces) }}
{{- $watchNamespaces := default (list (include "common.names.namespace" .)) .Values.operator.watchNamespaces }}
{{- range $namespace := $watchNamespaces }}
---
kind: Role
apiVersion: {{ include "common.capabilities.rbac.apiVersion" $ }}
metadata:
  name: {{ include "cloudnative-pg.operator.fullname" $ | trunc 63 | trimSuffix "-" }}
  namespace: {{ $namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $.Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: operator
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - secrets
      - services
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - ""
    resources:
      - configmaps/status
      - secrets/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch
  - apiGroups:
      - ""
    resources:
      - persistentvolumeclaims
      - pods
      - pods/exec
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - watch
  - apiGroups:
      - ""
    resources:
      - pods/status
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - serviceaccounts
    verbs:
      - create
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - apps
    resources:
      - deployments
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - batch
    resources:
      - jobs
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - watch
  - apiGroups:
      - coordination.k8s.io
    resources:
      - leases
    verbs:
      - create
      - get
      - update
  - apiGroups:
      - monitoring.coreos.com
    resources:
      - podmonitors
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - watch
  - apiGroups:
      - policy
    resources:
      - poddisruptionbudgets
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - backups
      - clusters
      - databases
      - poolers
      - publications
      - scheduledbackups
      - subscriptions
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - backups/status
      - databases/status
      - publications/status
      - scheduledbackups/status
      - subscriptions/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - imagecatalogs
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - clusters/finalizers
      - poolers/finalizers
    verbs:
      - update
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - clusters/status
      - failoverquorums/status
      - poolers/status
    verbs:
      - get
      - patch
      - update
      - watch
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - failoverquorums
    verbs:
      - create
      - delete
      - get
      - list
      - watch
  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - rolebindings
      - roles
    verbs:
      - create
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - snapshot.storage.k8s.io
    resources:
      - volumesnapshots
    verbs:
      - create
      - get
      - list
      - patch
      - watch
  {{- if $.Values.operator.rbac.rules }}
  {{- include "common.tplvalues.render" ( dict "value" $.Values.operator.rbac.rules "context" $ ) | nindent 2 }}
  {{- end }}
{{- end }}
{{- end }}
