{{- $containerMode := .Values.containerMode }}
{{- $hasCustomResourceMeta := (and .Values.resourceMeta .Values.resourceMeta.kubernetesModeRoleBinding) }}
{{- if and (eq $containerMode.type "kubernetes") (not .Values.template.spec.serviceAccountName) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "gha-runner-scale-set.kubeModeRoleBindingName" . }}
  namespace: {{ include "gha-runner-scale-set.namespace" . }}
  labels:
    {{- with .Values.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- if $hasCustomResourceMeta }}
    {{- with .Values.resourceMeta.kubernetesModeRoleBinding.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- end }}
    {{- include "gha-runner-scale-set.labels" . | nindent 4 }}

  annotations:
    {{- with .Values.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- if $hasCustomResourceMeta }}
    {{- with .Values.resourceMeta.kubernetesModeRoleBinding.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- end }}
  finalizers:
    - actions.github.com/cleanup-protection
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "gha-runner-scale-set.kubeModeRoleName" . }}
subjects:
- kind: ServiceAccount
  name: {{ include "gha-runner-scale-set.kubeModeServiceAccountName" . }}
  namespace: {{ include "gha-runner-scale-set.namespace" . }}
{{- end }}
