{{- $upgrade := .Values.upgrade }}
{{- if and $upgrade.enabled $upgrade.serviceAccount.create }}
{{- $ctx := dict "helm" . }}
{{- $fullname := include "vm.plain.fullname" $ctx }}
{{- $ns := include "vm.namespace" $ctx }}
{{- $sa := $upgrade.serviceAccount }}
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: {{ $sa.automountServiceAccountToken }}
metadata:
  name: {{ include "crds.upgrade.name" . }}
  namespace: {{ $ns }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade,pre-rollback
    "helm.sh/hook-weight": "-4"
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
    {{- with $sa.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- $extraLabels := $sa.labels | default dict }}
  {{- $_ := set $extraLabels "app.kubernetes.io/component" "upgrade-crds" }}
  {{- $_ := set $ctx "extraLabels" $extraLabels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- $_ := unset $ctx "extraLabels" }}
{{- end }}
