{{- if .Values.workflows.serviceAccount.create }}
{{- $namespaces := list .Release.Namespace }}
{{- if and (not .Values.rbac.singleNamespace) .Values.controller.workflowNamespaces }}
{{- $namespaces = .Values.controller.workflowNamespaces }}
{{- end }}
{{- range $namespaces }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "argo-workflows.workflows.serviceAccountName" $ }}
  namespace: {{ . | quote }}
  labels: {{- include "common.labels.standard" $ | nindent 4 }}
    app.kubernetes.io/component: controller
    app.kubernetes.io/part-of: argo-workflows
    {{- if $.Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if $.Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if $.Values.workflows.serviceAccount.annotations }}
    {{- include "common.tplvalues.render" (dict "value" $.Values.workflows.serviceAccount.annotations "context" $) | nindent 4 }}
    {{- end }}
automountServiceAccountToken: {{ $.Values.workflows.serviceAccount.automountServiceAccountToken }}
---
{{- end }}
{{- end }}
