{{- $ctx := dict "helm" . }}
{{- $fullname := include "vm.plain.fullname" $ctx }}
{{- $ns := include "vm.namespace" $ctx }}
{{- $sa := .Values.serviceAccount }}
{{- if $sa.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ $sa.name | default $fullname }}
  namespace: {{ $ns }}
  {{- $_ := set $ctx "extraLabels" .Values.extraLabels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- with .Values.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
{{- end }}
{{- if and .Values.crds.enabled .Values.crds.cleanup.enabled }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ $fullname }}-cleanup-hook
  namespace: {{ $ns }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  annotations: {{ include "vm-operator.cleanup.annotations" (dict "hookWeight" "-5") | nindent 4 }}
{{- end }}
