{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "external-secrets.serviceAccountName" . }}
  namespace: {{ template "external-secrets.namespace" . }}
  labels:
    {{- include "external-secrets.labels" . | nindent 4 }}
    {{- with .Values.serviceAccount.extraLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.serviceAccount.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
{{- end }}
---
{{- if and .Values.metrics.listen.auth.enabled .Values.serviceAccount.create -}}
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
  name: {{ include "external-secrets.serviceAccountName" . }}-token
  namespace: {{ template "external-secrets.namespace" . }}
  labels:
    {{- include "external-secrets.labels" . | nindent 4 }}
    {{- with .Values.serviceAccount.extraLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  annotations:
    kubernetes.io/service-account.name: {{ include "external-secrets.serviceAccountName" . }}
    {{- with .Values.serviceAccount.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
{{- end }}
