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

{{- if and .Values.pluginBarmanCloud.enabled .Values.pluginBarmanCloud.rbac.create }}
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" . }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - create
      - delete
      - get
      - list
      - watch
  - apiGroups:
      - barmancloud.cnpg.io
    resources:
      - objectstores
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - barmancloud.cnpg.io
    resources:
      - objectstores/finalizers
    verbs:
      - update
  - apiGroups:
      - barmancloud.cnpg.io
    resources:
      - objectstores/status
    verbs:
      - get
      - patch
      - update
  - apiGroups:
      - postgresql.cnpg.io
    resources:
      - backups
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - rolebindings
      - roles
    verbs:
      - create
      - get
      - list
      - patch
      - update
      - watch
  {{- if .Values.pluginBarmanCloud.rbac.rules }}
  {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.rbac.rules "context" $ ) | nindent 2 }}
  {{- end }}
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-metrics-auth" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" .) | trunc 64 | trimSuffix "-" }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - authentication.k8s.io
    resources:
      - tokenreviews
    verbs:
      - create
  - apiGroups:
      - authorization.k8s.io
    resources:
     - subjectaccessreviews
    verbs:
      - create
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-metrics-reader" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" .) | trunc 64 | trimSuffix "-" }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - nonResourceURLs:
      - "/metrics"
    verbs:
      - get
---
kind: ClusterRole
apiVersion: {{ include "common.capabilities.rbac.apiVersion" . }}
metadata:
  name: {{ printf "%s-objectstore-editor" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" .) | trunc 64 | trimSuffix "-" }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - barmancloud.cnpg.io
    resources:
      - objectstores
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - barmancloud.cnpg.io
    resources:
      - objectstores/status
    verbs:
      - get
{{- end }}
