{{- if and .Values.instanceType (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterInstancetype" "" .Values.instanceType)) }}
{{-   fail (printf "Specified instancetype not exists in cluster: %s" .Values.instanceType) }}
{{- end }}
{{- if and .Values.instanceProfile (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterPreference" "" .Values.instanceProfile)) }}
{{-   fail (printf "Specified profile not exists in cluster: %s" .Values.instanceProfile) }}
{{- end }}

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: {{ include "virtual-machine.fullname" . }}
  labels:
    {{- include "virtual-machine.labels" . | nindent 4 }}
spec:
  running: {{ .Values.running | default "true" }}

  {{- with .Values.instanceType }}
  instancetype:
    kind: VirtualMachineClusterInstancetype
    name: {{ . }}
  {{- end }}
  {{- with .Values.instanceProfile }}
  preference:
    kind: VirtualMachineClusterPreference
    name: {{ . }}
  {{- end }}

  dataVolumeTemplates:
  - metadata:
      name: {{ include "virtual-machine.fullname" . }}
      labels:
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      storage:
        resources:
          requests:
            storage: {{ .Values.systemDisk.storage | quote }}
        {{- with .Values.systemDisk.storageClass }}
        storageClassName: {{ . }}
        {{- end }}
      source:
        http:
          {{- if eq .Values.systemDisk.image "cirros" }}
          url: https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
          {{- else if eq .Values.systemDisk.image "ubuntu" }}
          url: https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
          {{- else if eq .Values.systemDisk.image "fedora" }}
          url: https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2
          {{- else if eq .Values.systemDisk.image "alpine" }}
          url: https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-tiny-r0.qcow2
          {{- else if eq .Values.systemDisk.image "talos" }}
          url: https://github.com/siderolabs/talos/releases/download/v1.7.6/nocloud-amd64.raw.xz
          {{- end }}

  template:
    metadata:
      annotations:
        kubevirt.io/allow-pod-bridge-network-live-migration: "true"
      labels:
        {{- include "virtual-machine.labels" . | nindent 8 }}
    spec:
      domain:
        {{- if and .Values.resources .Values.resources.cpu }}
        cpu:
          cores: {{ .Values.resources.cpu }}
        {{- end }}
        {{- if and .Values.resources .Values.resources.memory }}
        resources:
          requests:
            memory: {{ .Values.resources.memory | quote }}
        {{- end }}
        firmware:
          uuid: {{ include "virtual-machine.stableUuid" . }}
        devices:
          {{- if .Values.gpus }}
          gpus:
          {{- range $i, $gpu := .Values.gpus }}
          - name: gpu{{ add $i 1 }}
            deviceName: {{ $gpu.name }}
          {{- end }}
          {{- end }}

          disks:
          - disk:
              bus: scsi
            name: systemdisk
          {{- if .Values.sshKeys }}
          - disk:
              bus: virtio
            name: cloudinitdisk
          {{- end }}

          interfaces:
          - name: default
            bridge: {}

        machine:
          type: ""

      {{- if .Values.sshKeys }}
      accessCredentials:
      - sshPublicKey:
          source:
            secret:
              secretName: {{ include "virtual-machine.fullname" $ }}-ssh-keys
          propagationMethod:
            # keys will be injected into metadata part of cloud-init disk
            noCloud: {}
      {{- end }}

      terminationGracePeriodSeconds: 30

      volumes:
        - name: systemdisk
          dataVolume:
            name: {{ include "virtual-machine.fullname" . }}

        {{- if and .Values.sshKeys .Values.cloudInit }}
        - name: cloudinitdisk
          cloudInitNoCloud:
            secretRef:
              name: {{ include "virtual-machine.fullname" . }}-cloud-init
        {{- else if .Values.sshKeys }}
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              {{ printf "%s" "#cloud-config" }}
              ssh_authorized_keys:
                {{- range .Values.sshKeys }}
                - {{ . }}
                {{- end }}
              chpasswd:
                expire: false
        {{- else }}
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              {{ printf "%s" "#cloud-config" }}
        {{- end }}

      networks:
      - name: default
        pod: {}
