apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
---
# Grant permissions to read secrets inside the cluster to allow to have issuer in another namespace than the webhook
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:secrets-reader
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  - apiGroups:
      - ''
    resources:
      - '*'
    verbs:
      - 'get'
      - 'list'
      - 'watch'
---
# Bind the previously created role to the webhook service account to allow reading from secrets in all namespaces
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:secrets-reader
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:secrets-reader
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "cert-manager-webhook-regru.fullname" . }}
    namespace: {{ .Release.Namespace }}
---
# Grant the webhook permission to read the ConfigMap containing the Kubernetes
# apiserver's requestheader-ca-certificate.
# This ConfigMap is automatically created by the Kubernetes apiserver.
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:webhook-authentication-reader
  namespace: kube-system
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: extension-apiserver-authentication-reader
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "cert-manager-webhook-regru.fullname" . }}
    namespace: {{ .Release.Namespace }}
---
# apiserver gets the auth-delegator role to delegate auth decisions to
# the core apiserver
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:auth-delegator
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "cert-manager-webhook-regru.fullname" . }}
    namespace: {{ .Release.Namespace }}
---
# Grant cert-manager permission to validate using our apiserver
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:domain-solver
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  - apiGroups:
      - {{ .Values.groupName.name }}
    resources:
      - '*'
    verbs:
      - 'create'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:domain-solver
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:domain-solver
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ .Values.certManager.serviceAccountName }}
    namespace: {{ .Values.certManager.namespace }}
{{- if .Values.clusterRoleApiGroups.flowControlResources }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:flowcontrol
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
rules:
  - apiGroups:
    - flowcontrol.apiserver.k8s.io
    resources:
    - flowschemas
    - prioritylevelconfigurations
    verbs:
    - get
    - watch
    - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:flowcontrol
  labels:
    app: {{ include "cert-manager-webhook-regru.name" . }}
    chart: {{ include "cert-manager-webhook-regru.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "cert-manager-webhook-regru.fullname" . }}:flowcontrol
subjects:
  - apiGroup: ""
    kind: ServiceAccount
    name: {{ include "cert-manager-webhook-regru.fullname" . }}
    namespace: {{ .Release.Namespace | quote }}
{{- end }}
