Files
cozystack/packages/apps/vm-instance/templates/vm.yaml
T
Timofei Larkin f7a52ed4f6 feat(vm-instance): keep a VPC VM's IP and MAC
From cozyplane v0.1.3 a pod without a PortClaim gets a new Port, and so
a new IP and MAC, on every launcher pod, live migration included. A
guest whose network config is pinned to its MAC then loses its network
after a restart. A VM in a VPC now renders a PortClaim named after the
VM and binds its launcher pods to it, so the address survives restarts
and migrations and stays reserved while the VM is stopped. The name
matches the VM so cozyplane adopts the Port a running VM already has.

Assisted-by: LLM
Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
2026-11-26 16:11:47 +01:00

203 lines
8.8 KiB
YAML

{{- /* The effective type, not the raw value: once complete resources size the
VM the matcher is gone, and a stale or misspelled instanceType that the
dashboard gives no way to clear must not fail a render that never
references it. */}}
{{- $instanceType := include "virtual-machine.effectiveInstanceType" . }}
{{- if and $instanceType (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterInstancetype" "" $instanceType)) }}
{{- fail (printf "Specified instanceType does not exist in the cluster: %s" $instanceType) }}
{{- end }}
{{- if and .Values.instanceProfile (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterPreference" "" .Values.instanceProfile)) }}
{{- fail (printf "Specified instanceProfile does not exist in the cluster: %s" .Values.instanceProfile) }}
{{- end }}
{{- /* Raw values here, deliberately. This gate asks whether the user supplied
a sizing source at all, which is a question about their input; every
other read asks what gets rendered, which is a question about the
output. The raw instanceType and the effective one cannot disagree on
this gate's outcome -- a truthy raw value with an empty effective one
means the resources are complete, so the VM is sized either way -- but
swapping this one to $instanceType would quietly change what it means. */}}
{{- if and (not .Values.instanceType) (not (and .Values.resources .Values.resources.cpu .Values.resources.sockets .Values.resources.memory)) }}
{{- fail "Either instanceType or resources (cpu, sockets, memory) must be specified" }}
{{- end }}
{{- $networks := .Values.networks | default .Values.subnets }}
{{- /* On the cozyplane networking variant a VM joins its VPC through a pod
annotation: no multus, no secondary interfaces, and exactly one VPC per
pod — so more than one network cannot be honored and must not render. */ -}}
{{- $cozyplaneSDN := .Capabilities.APIVersions.Has "sdn.cozystack.io/v1alpha1/VPC" }}
{{- if and $cozyplaneSDN (gt (len ($networks | default list)) 1) }}
{{- fail (printf "the cozyplane networking variant joins a VM to exactly one VPC, got %d networks" (len $networks)) }}
{{- end }}
{{- $routeTable := .Values.routeTable | default "" }}
{{- if $routeTable }}
{{- if not $cozyplaneSDN }}
{{- fail (printf "routeTable %q names a cozyplane RouteTable, but the cluster does not serve sdn.cozystack.io: route tables need the cozyplane networking variant" $routeTable) }}
{{- end }}
{{- if not $networks }}
{{- fail (printf "routeTable %q needs the VM to join a VPC: name the VirtualPrivateCloud in networks" $routeTable) }}
{{- end }}
{{- end }}
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: {{ include "virtual-machine.fullname" . }}
labels:
{{- include "virtual-machine.labels" . | nindent 4 }}
spec:
{{- if hasKey .Values "runStrategy" }}
runStrategy: {{ .Values.runStrategy }}
{{- else }}
runStrategy: {{ ternary "Always" "Halted" (.Values.running | default true) }}
{{- end }}
{{- with $instanceType }}
instancetype:
kind: VirtualMachineClusterInstancetype
name: {{ . }}
{{- end }}
{{- with .Values.instanceProfile }}
preference:
kind: VirtualMachineClusterPreference
name: {{ . }}
{{- end }}
template:
metadata:
annotations:
kubevirt.io/allow-pod-bridge-network-live-migration: "true"
{{- if .Capabilities.APIVersions.Has "kubeovn.io/v1/IP" }}
{{- $ovnIPName := printf "%s.%s" (include "virtual-machine.fullname" .) .Release.Namespace }}
{{- $ovnIP := lookup "kubeovn.io/v1" "IP" "" $ovnIPName }}
{{- if $ovnIP }}
ovn.kubernetes.io/mac_address: {{ $ovnIP.spec.macAddress | quote }}
ovn.kubernetes.io/ip_address: {{ $ovnIP.spec.ipAddress | quote }}
{{- end }}
{{- end }}
{{- if and $cozyplaneSDN (gt (len ($networks | default list)) 0) }}
{{- /* The VPC lives in the VM's own namespace, so the bare release name
resolves; <owner-ns>/<vpc> is only for foreign VPCs. */}}
sdn.cozystack.io/vpc: {{ printf "virtualprivatecloud-%s" (index $networks 0).name | quote }}
sdn.cozystack.io/port-claim: {{ include "virtual-machine.fullname" . }}
{{- with $routeTable }}
sdn.cozystack.io/route-table: {{ . | quote }}
{{- end }}
{{- end }}
labels:
{{- include "virtual-machine.labels" . | nindent 8 }}
spec:
domain:
{{- $domainRes := include "virtual-machine.domainResources" . | fromJson -}}
{{- $fw := .Values.firmware | default dict }}
{{- with $domainRes.cpu }}
cpu: {{- . | toYaml | nindent 10 }}
{{- end }}
{{- with $domainRes.resources }}
resources: {{- . | toYaml | nindent 10 }}
{{- end }}
firmware:
uuid: {{ include "virtual-machine.stableUuid" . }}
{{- if eq (toString $fw.bootloader) "uefi" }}
bootloader:
efi:
secureBoot: {{ $fw.secureBoot | default false }}
persistent: {{ $fw.efiPersistent | default false }}
{{- else if eq (toString $fw.bootloader) "bios" }}
bootloader:
bios: {}
{{- end }}
{{- if and (eq (toString $fw.bootloader) "uefi") ($fw.secureBoot | default false) }}
features:
smm:
enabled: true
{{- end }}
devices:
{{- if .Values.gpus }}
gpus:
{{- range $i, $gpu := .Values.gpus }}
- name: gpu{{ add $i 1 }}
deviceName: {{ $gpu.name }}
{{- end }}
{{- end }}
disks:
{{- /* KubeVirt rejects two devices sharing a bootOrder, so a clash is
reported here with the disk names rather than surfacing as a
webhook denial on the VirtualMachine. */}}
{{- $bootOrderOwners := dict }}
{{- range $i, $disk := .Values.disks }}
{{- $bootOrder := int ($disk.bootOrder | default (add $i 1)) }}
{{- with get $bootOrderOwners (toString $bootOrder) }}
{{- fail (printf "disks %q and %q both have bootOrder %d; boot orders must be unique" . $disk.name $bootOrder) }}
{{- end }}
{{- $_ := set $bootOrderOwners (toString $bootOrder) $disk.name }}
- name: disk-{{ $disk.name }}
{{- $dv := lookup "cdi.kubevirt.io/v1beta1" "DataVolume" $.Release.Namespace (printf "vm-disk-%s" $disk.name) }}
{{- if $dv }}
{{- $isOptical := and (hasKey $dv.metadata.annotations "vm-disk.cozystack.io/optical") (eq (index $dv.metadata.annotations "vm-disk.cozystack.io/optical") "true") }}
{{- $deviceKey := ternary "cdrom" "disk" $isOptical }}
{{- if $disk.bus }}
{{ $deviceKey }}:
bus: {{ $disk.bus }}
{{- else }}
{{ $deviceKey }}: {}
{{- end }}
bootOrder: {{ $bootOrder }}
{{- else }}
{{- fail (printf "Specified disk not exists in cluster: %s" .name) }}
{{- end }}
{{- end }}
{{- if or .Values.cloudInit .Values.sshKeys .Values.netplanDhcp }}
- name: cloudinitdisk
disk: {}
{{- end }}
interfaces:
- name: default
bridge: {}
{{- if not $cozyplaneSDN }}
{{- range $i, $net := $networks }}
- name: {{ $net.name }}
bridge: {}
{{- end }}
{{- end }}
machine:
type: ""
{{- with .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
{{- include "virtual-machine.nodeAffinity" . | nindent 6 }}
volumes:
{{- range .Values.disks }}
- name: disk-{{ .name }}
dataVolume:
name: vm-disk-{{ .name }}
{{- end }}
{{- if or .Values.cloudInit .Values.sshKeys .Values.netplanDhcp }}
- name: cloudinitdisk
cloudInitNoCloud:
{{- if or .Values.cloudInit .Values.sshKeys }}
secretRef:
name: {{ include "virtual-machine.fullname" . }}-cloud-init
{{- end }}
{{- if .Values.netplanDhcp }}
networkDataSecretRef:
name: {{ include "virtual-machine.fullname" . }}-network-config
{{- end }}
{{- end }}
networks:
- name: default
pod: {}
{{- if not $cozyplaneSDN }}
{{- range $i, $net := $networks }}
- name: {{ $net.name }}
multus:
networkName: {{ $.Release.Namespace }}/{{ $net.name }}
{{- end }}
{{- end }}