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

{{- if and .Values.pluginBarmanCloud.enabled .Values.pluginBarmanCloud.rbac.create }}
kind: ClusterRoleBinding
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 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "cloudnative-pg.plugin-barman-cloud.serviceAccountName" . }}
    namespace: {{ include "common.names.namespace" . | quote }}
---
kind: ClusterRoleBinding
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 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ printf "%s-metrics-auth" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" .) | trunc 64 | trimSuffix "-" }}
subjects:
  - kind: ServiceAccount
    name: {{ template "cloudnative-pg.plugin-barman-cloud.serviceAccountName" . }}
    namespace: {{ include "common.names.namespace" . | quote }}
---
kind: ClusterRoleBinding
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 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ printf "%s-objectstore-editor" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" .) | trunc 64 | trimSuffix "-" }}
subjects:
  - kind: ServiceAccount
    name: {{ template "cloudnative-pg.plugin-barman-cloud.serviceAccountName" . }}
    namespace: {{ include "common.names.namespace" . | quote }}
{{- if and .Values.pluginBarmanCloud.metrics.enabled .Values.pluginBarmanCloud.metrics.allowedServiceAccounts }}
{{- range $sa := .Values.pluginBarmanCloud.metrics.allowedServiceAccounts }}
---
{{- $saName := tpl $sa.name $ }}
{{- $saNamespace := tpl $sa.namespace $ }}
kind: ClusterRoleBinding
apiVersion: {{ include "common.capabilities.rbac.apiVersion" $ }}
metadata:
  name: {{ printf "%s-%s-metrics-read" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" $)  $saName | 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 }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ printf "%s-metrics-reader" (include "cloudnative-pg.plugin-barman-cloud.fullname.namespace" $) | trunc 64 | trimSuffix "-" }}
subjects:
  - kind: ServiceAccount
    name: {{ $saName }}
    namespace: {{ $saNamespace | quote }}
{{- end }}
{{- end }}
{{- end }}
