apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "pomerium.authenticate.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.authenticate.serviceAccountName" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: authenticate
  annotations:
{{- if .Values.annotations }}
  {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
{{- if .Values.authenticate.serviceAccount.annotations }}
  {{- range $key, $value := .Values.authenticate.serviceAccount.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "pomerium.authorize.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.authorize.serviceAccountName" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: authorize
  annotations:
{{- if .Values.annotations }}
  {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
{{- if .Values.authorize.serviceAccount.annotations }}
  {{- range $key, $value := .Values.authorize.serviceAccount.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "pomerium.databroker.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.databroker.serviceAccountName" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: databroker
  annotations:
{{- if .Values.annotations }}
  {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
{{- if .Values.databroker.serviceAccount.annotations }}
  {{- range $key, $value := .Values.databroker.serviceAccount.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "pomerium.proxy.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.proxy.serviceAccountName" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: proxy
  annotations:
{{- if .Values.annotations }}
  {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
{{- if .Values.proxy.serviceAccount.annotations }}
  {{- range $key, $value := .Values.proxy.serviceAccount.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}

{{ if .Values.ingressController.enabled -}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "pomerium.ingressController.serviceAccountName" . }}
  labels:
    app.kubernetes.io/name: {{ template "pomerium.ingressController.name" . }}
    helm.sh/chart: {{ template "pomerium.chart" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: ingressController
  annotations:
{{- if .Values.ingressController.serviceAccount.annotations }}
  {{- range $key, $value := .Values.ingressController.serviceAccount.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- else if .Values.annotations }}
  {{- range $key, $value := .Values.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
{{- end -}}
