{{- if .Values.crds.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "vertical-pod-autoscaler.crds.fullname" . }}
  labels:
    {{- include "vertical-pod-autoscaler.crds.labels" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  template:
    metadata:
      labels:
        {{- include "vertical-pod-autoscaler.crds.selectorLabels" . | nindent 8 }}
        {{- if .Values.crds.podLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.crds.podLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
      {{- if or .Values.crds.podAnnotations .Values.commonAnnotations }}
      {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.crds.podAnnotations .Values.commonAnnotations ) "context" . ) }}
      annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "common.images.pullSecrets" ( dict "images" ( list .Values.crds.image ) "global" .Values.global ) | nindent 6 }}
      serviceAccountName: {{ include "vertical-pod-autoscaler.crds.fullname" . }}
      securityContext:
        {{- toYaml .Values.crds.podSecurityContext | nindent 8 }}
      containers:
        - name: kubectl
          securityContext:
            {{- toYaml .Values.crds.securityContext | nindent 12 }}
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.crds.image "global" .Values.global ) }}
          imagePullPolicy: {{ .Values.crds.image.pullPolicy }}
          args:
            - apply
            {{- range $path, $_ := .Files.Glob  "files/crds/*" }}
            - --filename=/config/{{ base $path }}
            {{- end }}
          resources:
            {{- toYaml .Values.crds.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /config
              readOnly: true
      {{- with .Values.crds.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.crds.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.crds.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      restartPolicy: Never
      volumes:
        - name: config
          configMap:
            name: {{ include "vertical-pod-autoscaler.crds.fullname" . }}
{{- end }}
