Skip to content

Commit

Permalink
Reject VM defined with volume with no matching disk
Browse files Browse the repository at this point in the history
Addresses this bugzilla:
https://bugzilla.redhat.com/show_bug.cgi?id=1954667

Issue on github:
#5556

Signed-off-by: Itamar Holder <iholder@redhat.com>
  • Loading branch information
iholder101 committed May 3, 2021
1 parent 73ab884 commit 9e504ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,14 @@ func validateNetworkHasOnlyOneType(field *k8sfield.Path, cniTypesCount int, caus
func validateBootOrder(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec, volumeNameMap map[string]*v1.Volume) (bootOrderMap map[uint]bool, causes []metav1.StatusCause) {
// used to validate uniqueness of boot orders among disks and interfaces
bootOrderMap = make(map[uint]bool)
// to perform as set of volume names
diskNames := make(map[string]interface{})

for i, volume := range spec.Volumes {
volumeNameMap[volume.Name] = &spec.Volumes[i]
}

// Validate disks and volumes match up correctly
// Validate disks match volumes correctly
for idx, disk := range spec.Domain.Devices.Disks {
var matchingVolume *v1.Volume

Expand Down Expand Up @@ -901,7 +903,22 @@ func validateBootOrder(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec
}
bootOrderMap[order] = true
}

diskNames[disk.Name] = nil
}

// Validate that volumes match disks correctly
for idx, volume := range spec.Volumes {
if _, machingDiskExists := diskNames[volume.Name]; !machingDiskExists {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Message: fmt.Sprintf(nameOfTypeNotFoundMessagePattern, field.Child("domain", "volumes").Index(idx).Child("name").String(), volume.Name),
Field: field.Child("domain", "volumes").Index(idx).Child("name").String(),
})
}

}

return bootOrderMap, causes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ var _ = Describe("Validating VMICreate Admitter", func() {

causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("fake"), &vmi.Spec, config)
Expect(len(causes)).To(Equal(1))
//Expect(causes[0].Field).To(Equal("fake.domain.devices.disks[0].name"))
Expect(causes[0].Field).To(Equal("fake.domain.volumes[0].name"))
})
It("should reject multiple disks referencing same volume", func() {
vmi := v1.NewMinimalVMI("testvmi")
Expand Down

0 comments on commit 9e504ef

Please sign in to comment.