{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "vault-secrets-webhook.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
  {{- with .Values.serviceAccount.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    helm.sh/chart: {{ include "vault-secrets-webhook.chart" . }}
    app.kubernetes.io/name: {{ include "vault-secrets-webhook.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- if .Values.serviceAccount.labels }}
{{ toYaml .Values.serviceAccount.labels | indent 4 }}
  {{- end }}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "vault-secrets-webhook.fullname" . }}
rules:
  - apiGroups:
      - ""
    resources:
      - secrets
      - configmaps
    verbs:
      - "get"
    {{- if .Values.secretsMutation }}
      - "update"
    {{- end }}
  - apiGroups:
      - ""
    resources:
      - serviceaccounts
    verbs:
      - "get"
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - "create"
      - "update"
  - apiGroups:
      - ""
    resources:
      - serviceaccounts/token
    verbs:
      - "create"
{{- if .Values.rbac.psp.enabled }}
  - apiGroups:
      - extensions
    resources:
      - podsecuritypolicies
    verbs:
      - use
    resourceNames:
      - {{ template "vault-secrets-webhook.fullname" . }}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "vault-secrets-webhook.fullname" . }}-limited
roleRef:
  kind: ClusterRole
  apiGroup: rbac.authorization.k8s.io
  name: {{ template "vault-secrets-webhook.fullname" . }}
subjects:
- kind: ServiceAccount
  namespace: {{ .Release.Namespace }}
  name: {{ template "vault-secrets-webhook.serviceAccountName" . }}
{{- if .Values.rbac.authDelegatorRole.enabled }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "vault-secrets-webhook.fullname" . }}-auth-delegator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
  - kind: ServiceAccount
    name: {{ template "vault-secrets-webhook.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}

