{{ if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ printf "%s-key-admin" (include "sealed-secrets.fullname" .) }}
  namespace: {{ include "sealed-secrets.namespace" . }}
  labels: {{- include "sealed-secrets.labels" . | nindent 4 }}
    {{- if .Values.rbac.labels }}
    {{- include "sealed-secrets.render" ( dict "value" .Values.rbac.labels "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonLabels }}
    {{- include "sealed-secrets.render" (dict "value" .Values.commonLabels "context" $) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "sealed-secrets.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ printf "%s-key-admin" (include "sealed-secrets.fullname" .) }}
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "sealed-secrets.serviceAccountName" . }}
    namespace: {{ include "sealed-secrets.namespace" . }}
---
{{ end }}
{{ if and (and .Values.rbac.create .Values.rbac.serviceProxier.create) .Values.rbac.serviceProxier.bind }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ printf "%s-service-proxier" (include "sealed-secrets.fullname" .) }}
  namespace: {{ include "sealed-secrets.namespace" . }}
  labels: {{- include "sealed-secrets.labels" . | nindent 4 }}
    {{- if .Values.rbac.labels }}
    {{- include "sealed-secrets.render" ( dict "value" .Values.rbac.labels "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonLabels }}
    {{- include "sealed-secrets.render" (dict "value" .Values.commonLabels "context" $) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "sealed-secrets.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ printf "%s-service-proxier" (include "sealed-secrets.fullname" .) }}
subjects:
  {{- include "sealed-secrets.render" (dict "value" .Values.rbac.serviceProxier.subjects "context" $) | nindent 2 }}
---
{{ end }}
{{ if and (and .Values.rbac.create .Values.rbac.namespacedRoles) (not $.Values.rbac.clusterRole) }}
  {{- range $additionalNamespace := $.Values.additionalNamespaces }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "sealed-secrets.fullname" $ }}
  namespace: {{ $additionalNamespace }}
  labels: {{- include "sealed-secrets.labels" $ | nindent 4 }}
    {{- if $.Values.rbac.labels }}
    {{- include "sealed-secrets.render" ( dict "value" $.Values.rbac.labels "context" $) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if $.Values.commonAnnotations }}
    {{- include "sealed-secrets.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ $.Values.rbac.namespacedRolesName }}
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "sealed-secrets.serviceAccountName" $ }}
    namespace: {{ include "sealed-secrets.namespace" $ }}
---
  {{ end }}
{{ end }}
