---
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
{{- if eq .Values.rbac.scope "cluster" }}
kind: ClusterRole
{{- else }}
kind: Role
{{- end }}
metadata:
  name: {{ template "loadtester.fullname" . }}
  labels:
    helm.sh/chart: {{ template "loadtester.chart" . }}
    app.kubernetes.io/name: {{ template "loadtester.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
rules:
{{ toYaml .Values.rbac.rules | indent 2 }}
---
apiVersion: rbac.authorization.k8s.io/v1
{{- if eq .Values.rbac.scope "cluster" }}
kind: ClusterRoleBinding
{{- else }}
kind: RoleBinding
{{- end }}
metadata:
  name: {{ template "loadtester.fullname" . }}
  labels:
    helm.sh/chart: {{ template "loadtester.chart" . }}
    app.kubernetes.io/name: {{ template "loadtester.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  {{- if eq .Values.rbac.scope "cluster" }}
  kind: ClusterRole
  {{- else }}
  kind: Role
  {{- end }}
  name: {{ template "loadtester.fullname" . }}
subjects:
  - kind: ServiceAccount
    name: {{ template "loadtester.fullname" . }}
    namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "loadtester.fullname" . }}
  labels:
    helm.sh/chart: {{ template "loadtester.chart" . }}
    app.kubernetes.io/name: {{ template "loadtester.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
