{{- if .Values.rbac.create -}}
{{- $root := . -}}
{{- $watchNamespaces := .Values.watchNamespaces -}}
{{- if $root.Values.watchAnyNamespace }}
  {{- $watchNamespaces = list -}}  
{{- end }}
{{- range append $watchNamespaces .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
{{- if $root.Values.watchAnyNamespace }}
kind: ClusterRoleBinding
{{- else }}
kind: RoleBinding
{{- end }}
metadata:
  {{- if $root.Values.watchAnyNamespace }}
  name: strimzi-cluster-operator-watched
  {{- else }}
  name: strimzi-cluster-operator-watched
  {{- end }}
  {{- if not $root.Values.watchAnyNamespace }}
  namespace: {{ . }}
  {{- end }}
  labels:
    app: {{ template "strimzi.name" $root }}
    chart: {{ template "strimzi.chart" $root }}
    component: role-binding
    release: {{ $root.Release.Name }}
    heritage: {{ $root.Release.Service }}
subjects:
  - kind: ServiceAccount
    name: {{ $root.Values.serviceAccount }}
    namespace: {{ $root.Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: strimzi-cluster-operator-watched
  apiGroup: rbac.authorization.k8s.io
{{- end }}
{{- end }}
