apiVersion: v1
kind: ServiceAccount
metadata:
  name: dapr-injector
  namespace: {{ .Release.Namespace }}
  labels:
    {{- range $key, $value := .Values.global.k8sLabels }}
    {{ $key }}: {{ tpl $value $ }}
    {{- end }}
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dapr-injector
  labels:
    {{- range $key, $value := .Values.global.k8sLabels }}
    {{ $key }}: {{ tpl $value $ }}
    {{- end }}
rules:
  - apiGroups: [""]
    resources: ["serviceaccounts"]
    verbs: ["get", "list"]
  - apiGroups: ["admissionregistration.k8s.io"]
    resources: ["mutatingwebhookconfigurations"]
    verbs: ["patch"]
    resourceNames: ["dapr-sidecar-injector"]
{{- if not .Values.global.rbac.namespaced }}
  - apiGroups: ["dapr.io"]
    resources: ["components"]
    verbs: [ "get", "list"]
{{- end }}
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dapr-injector
  labels:
    {{- range $key, $value := .Values.global.k8sLabels }}
    {{ $key }}: {{ tpl $value $ }}
    {{- end }}
subjects:
- kind: ServiceAccount
  name: dapr-injector
  namespace: {{ .Release.Namespace }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dapr-injector
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dapr-injector
  namespace: {{ .Release.Namespace }}
  labels:
    {{- range $key, $value := .Values.global.k8sLabels }}
    {{ $key }}: {{ tpl $value $ }}
    {{- end }}
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
    resourceNames: ["dapr-trust-bundle"]
{{- if eq .Values.global.rbac.namespaced true }}
  - apiGroups: ["dapr.io"]
    resources: ["components"]
    verbs: [ "get", "list"]
{{- end }}
  - apiGroups: ["dapr.io"]
    resources: ["configurations"]
    verbs: [ "get" ]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dapr-injector
  labels:
    {{- range $key, $value := .Values.global.k8sLabels }}
    {{ $key }}: {{ tpl $value $ }}
    {{- end }}
subjects:
- kind: ServiceAccount
  name: dapr-injector
  namespace: {{ .Release.Namespace }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: dapr-injector
