{{- if and (.Values.serviceAccount.create) (.Values.serviceAccount.rbac.create) -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "woodpecker-agent.serviceAccountName" . }}
  {{- with .Values.env.WOODPECKER_BACKEND_K8S_NAMESPACE }}
  namespace: {{ . }}
  {{- end }}
  labels:
    {{- include "woodpecker-agent.labels" . | nindent 4 }}
    {{- with .Values.serviceAccount.rbac.roleBinding.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.serviceAccount.rbac.roleBinding.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
subjects:
  - kind: ServiceAccount
    name: {{ include "woodpecker-agent.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: Role
  name: {{ include "woodpecker-agent.serviceAccountName" . }}
  apiGroup: rbac.authorization.k8s.io
{{- end }}
