Skip to content

Commit

Permalink
Change "Machine" to a pointer type & make "Type" optional
Browse files Browse the repository at this point in the history
- Change "Machine" to pointer type as it's optional.

- Make "Type" optional. Go bug did not enforce mandatory
fields, and therefore our example VMIs, which has an empty
type defined for them, are needed to be accepted as valid
to allow backwards compatability.

Signed-off-by: Itamar Holder <[email protected]>
  • Loading branch information
iholder101 committed May 20, 2021
1 parent 0542941 commit 89be6b3
Showing 47 changed files with 43 additions and 111 deletions.
3 changes: 0 additions & 3 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
@@ -10549,9 +10549,6 @@
},
"v1.Machine": {
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"description": "QEMU machine type is the actual chipset of the VirtualMachineInstance.",
2 changes: 0 additions & 2 deletions examples/vm-alpine-datavolume.yaml
Original file line number Diff line number Diff line change
@@ -33,8 +33,6 @@ spec:
- disk:
bus: virtio
name: datavolumedisk1
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vm-alpine-multipvc.yaml
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ spec:
- disk:
bus: virtio
name: pvcdisk2
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vm-cirros.yaml
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ spec:
- disk:
bus: virtio
name: cloudinitdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vm-priorityclass.yaml
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ spec:
- disk:
bus: virtio
name: cloudinitdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vm-template-fedora.yaml
Original file line number Diff line number Diff line change
@@ -45,8 +45,6 @@ objects:
name: default
networkInterfaceMultiqueue: true
rng: {}
machine:
type: ""
resources:
requests:
memory: ${MEMORY}
2 changes: 0 additions & 2 deletions examples/vm-template-rhel7.yaml
Original file line number Diff line number Diff line change
@@ -41,8 +41,6 @@ objects:
model: virtio
name: default
networkInterfaceMultiqueue: true
machine:
type: ""
resources:
requests:
memory: ${MEMORY}
2 changes: 0 additions & 2 deletions examples/vm-template-windows2012r2.yaml
Original file line number Diff line number Diff line change
@@ -63,8 +63,6 @@ objects:
vapic: {}
firmware:
uuid: 5d307ca9-b3ef-428c-8861-06e72d69f223
machine:
type: ""
resources:
requests:
memory: ${MEMORY}
2 changes: 0 additions & 2 deletions examples/vmi-alpine-efi.yaml
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@ spec:
bootloader:
efi:
secureBoot: false
machine:
type: ""
resources:
requests:
memory: 1Gi
2 changes: 0 additions & 2 deletions examples/vmi-block-pvc.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: virtio
name: blockpvcdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-ephemeral.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: virtio
name: containerdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-fedora.yaml
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@ spec:
bus: virtio
name: cloudinitdisk
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-flavor-small.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: virtio
name: containerdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-gpu.yaml
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ spec:
- deviceName: nvidia.com/GP102GL_Tesla_P40
name: gpu1
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-host-disk.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: virtio
name: host-disk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-macvtap.yaml
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ spec:
- macvtap: {}
name: macvtap
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-masquerade.yaml
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@ spec:
port: 80
protocol: TCP
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-migratable.yaml
Original file line number Diff line number Diff line change
@@ -15,8 +15,6 @@ spec:
interfaces:
- masquerade: {}
name: default
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-multus-multiple-net.yaml
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ spec:
- bridge: {}
name: ptp
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-multus-ptp.yaml
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ spec:
- bridge: {}
name: ptp
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-nocloud.yaml
Original file line number Diff line number Diff line change
@@ -18,8 +18,6 @@ spec:
- disk:
bus: virtio
name: emptydisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-preset-small.yaml
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@ metadata:
spec:
domain:
devices: {}
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-pvc.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: virtio
name: pvcdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-replicaset-cirros.yaml
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ spec:
- disk:
bus: virtio
name: containerdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-sata.yaml
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ spec:
- disk:
bus: sata
name: containerdisk
machine:
type: ""
resources:
requests:
memory: 128Mi
2 changes: 0 additions & 2 deletions examples/vmi-secureboot.yaml
Original file line number Diff line number Diff line change
@@ -20,8 +20,6 @@ spec:
bootloader:
efi:
secureBoot: true
machine:
type: ""
resources:
requests:
memory: 1Gi
2 changes: 0 additions & 2 deletions examples/vmi-slirp.yaml
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@ spec:
protocol: TCP
slirp: {}
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-sriov.yaml
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ spec:
- name: sriov-net
sriov: {}
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
2 changes: 0 additions & 2 deletions examples/vmi-windows.yaml
Original file line number Diff line number Diff line change
@@ -38,8 +38,6 @@ spec:
vapic: {}
firmware:
uuid: 5d307ca9-b3ef-428c-8861-06e72d69f223
machine:
type: ""
resources:
requests:
memory: 2Gi
2 changes: 0 additions & 2 deletions examples/vmi-with-sidecar-hook.yaml
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ spec:
bus: virtio
name: cloudinitdisk
rng: {}
machine:
type: ""
resources:
requests:
memory: 1024M
10 changes: 8 additions & 2 deletions pkg/virt-api/webhooks/mutating-webhook/mutators/vm-mutator.go
Original file line number Diff line number Diff line change
@@ -101,7 +101,13 @@ func (mutator *VMsMutator) setDefaultMachineType(vm *v1.VirtualMachine) {
// nothing to do, let's the validating webhook fail later
return
}
if vm.Spec.Template.Spec.Domain.Machine.Type == "" {
vm.Spec.Template.Spec.Domain.Machine.Type = mutator.ClusterConfig.GetMachineType()
machineType := mutator.ClusterConfig.GetMachineType()

if machine := vm.Spec.Template.Spec.Domain.Machine; machine != nil {
if machine.Type == "" {
machine.Type = machineType
}
} else {
vm.Spec.Template.Spec.Domain.Machine = &v1.Machine{Type: machineType}
}
}
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ var _ = Describe("VirtualMachine Mutator", func() {
},
})

vm.Spec.Template.Spec.Domain.Machine.Type = "q35"
vm.Spec.Template.Spec.Domain.Machine = &v1.Machine{Type: "q35"}

vmSpec, _ := getVMSpecMetaFromResponse()
Expect(vmSpec.Template.Spec.Domain.Machine.Type).To(Equal(vm.Spec.Template.Spec.Domain.Machine.Type))
10 changes: 8 additions & 2 deletions pkg/virt-api/webhooks/mutating-webhook/mutators/vmi-mutator.go
Original file line number Diff line number Diff line change
@@ -238,8 +238,14 @@ func (mutator *VMIsMutator) setDefaultGuestCPUTopology(vmi *v1.VirtualMachineIns
}

func (mutator *VMIsMutator) setDefaultMachineType(vmi *v1.VirtualMachineInstance) {
if vmi.Spec.Domain.Machine.Type == "" {
vmi.Spec.Domain.Machine.Type = mutator.ClusterConfig.GetMachineType()
machineType := mutator.ClusterConfig.GetMachineType()

if machine := vmi.Spec.Domain.Machine; machine != nil {
if machine.Type == "" {
machine.Type = machineType
}
} else {
vmi.Spec.Domain.Machine = &v1.Machine{Type: machineType}
}
}

Original file line number Diff line number Diff line change
@@ -479,7 +479,7 @@ var _ = Describe("VirtualMachineInstance Mutator", func() {
k8sv1.ResourceMemory: resource.MustParse("512Mi"),
}
vmi.Spec.Domain.CPU = &v1.CPU{Model: "EPYC"}
vmi.Spec.Domain.Machine.Type = "q35"
vmi.Spec.Domain.Machine = &v1.Machine{Type: "q35"}

vmiSpec, _ := getVMISpecMetaFromResponse()
Expect(vmiSpec.Domain.CPU.Model).To(Equal(vmi.Spec.Domain.CPU.Model))
Original file line number Diff line number Diff line change
@@ -1184,12 +1184,11 @@ func validateFirmwareSerial(field *k8sfield.Path, spec *v1.VirtualMachineInstanc
}

func validateEmulatedMachine(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec, config *virtconfig.ClusterConfig) (causes []metav1.StatusCause) {
if len(spec.Domain.Machine.Type) > 0 {
machine := spec.Domain.Machine.Type
if machine := spec.Domain.Machine; machine != nil && len(machine.Type) > 0 {
supportedMachines := config.GetEmulatedMachines()
var match = false
for _, val := range supportedMachines {
if regexp.MustCompile(val).MatchString(machine) {
if regexp.MustCompile(val).MatchString(machine.Type) {
match = true
}
}
@@ -1198,7 +1197,7 @@ func validateEmulatedMachine(field *k8sfield.Path, spec *v1.VirtualMachineInstan
Type: metav1.CauseTypeFieldValueInvalid,
Message: fmt.Sprintf("%s is not supported: %s (allowed values: %v)",
field.Child("domain", "machine", "type").String(),
machine,
machine.Type,
supportedMachines,
),
Field: field.Child("domain", "machine", "type").String(),
Original file line number Diff line number Diff line change
@@ -451,19 +451,19 @@ var _ = Describe("Validating VMICreate Admitter", func() {
It("should accept valid machine type", func() {
vmi := v1.NewMinimalVMI("testvmi")
if webhooks.IsPPC64() {
vmi.Spec.Domain.Machine.Type = "pseries"
vmi.Spec.Domain.Machine = &v1.Machine{Type: "pseries"}
} else if webhooks.IsARM64() {
vmi.Spec.Domain.Machine.Type = "virt"
vmi.Spec.Domain.Machine = &v1.Machine{Type: "virt"}
} else {
vmi.Spec.Domain.Machine.Type = "q35"
vmi.Spec.Domain.Machine = &v1.Machine{Type: "q35"}
}

causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(len(causes)).To(Equal(0))
})
It("should reject invalid machine type", func() {
vmi := v1.NewMinimalVMI("testvmi")
vmi.Spec.Domain.Machine.Type = "test"
vmi.Spec.Domain.Machine = &v1.Machine{Type: "test"}

causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(len(causes)).To(Equal(1))
Loading
Oops, something went wrong.

0 comments on commit 89be6b3

Please sign in to comment.