Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug fix] Reject VM defined with volume with no matching disk #5585

Merged
merged 2 commits into from May 6, 2021

Conversation

iholder101
Copy link
Contributor

What this PR does / why we need it:
Reject VM defined with volume with no matching disk.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Bugzilla bug: https://bugzilla.redhat.com/show_bug.cgi?id=1954667
Github issue: #5556

Release note:

[bugfix] - reject VM defined with volume with no matching disk

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. size/M labels May 3, 2021
}

// Validate that volumes match disks correctly
for idx, volume := range spec.Volumes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! this ensures there's always a disk if a volume exists.

Is the reflexive case required? Do we need a volume if a disk exists?

Copy link
Contributor Author

@iholder101 iholder101 May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reflexive case is already being checked a few lines above (line 877) and there's also a test covering it

@stu-gott
Copy link
Member

stu-gott commented May 3, 2021

This appears to be something we could explicitly test for at the functional test level as well.


// Validate that volumes match disks correctly
for idx, volume := range spec.Volumes {
if _, machingDiskExists := diskNames[volume.Name]; !machingDiskExists {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iholder-redhat volumes can also match spec.Domain.Devices.Filesystems

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
diskNames is now diskAndFilesystemNames and contain both fs and disk names :)

@iholder101
Copy link
Contributor Author

This appears to be something we could explicitly test for at the functional test level as well.

@stu-gott I've added functional tests :)
p.s. do we have a policy / rule of thumb on when to add a functional test?

@iholder101
Copy link
Contributor Author

/retest

@iholder101
Copy link
Contributor Author

/test pull-kubevirt-e2e-k8s-1.17-rook-ceph

Signed-off-by: Itamar Holder <iholder@redhat.com>
@iholder101
Copy link
Contributor Author

/test pull-kubevirt-unit-test

@@ -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 / fs names
diskAndFilesystemNames := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it's somewhat more idiomatic/common to use a map[string]struct{}, and then "signal the presence" of a disk/fs by having a diskAndFilesystemNames[disk.Name] = struct{}{}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I'm still new to Go, so good to know :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a related post by the prophet Dave Cheney:
https://dave.cheney.net/2014/03/25/the-empty-struct


// Validate that volumes match disks and filesystems correctly
for idx, volume := range spec.Volumes {
if _, machingDiskExists := diskAndFilesystemNames[volume.Name]; !machingDiskExists {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo-nit: machingDiskExists --> matchingDiskExists

Addresses this bugzilla:
https://bugzilla.redhat.com/show_bug.cgi?id=1954667

Signed-off-by: Itamar Holder <iholder@redhat.com>
@iholder101
Copy link
Contributor Author

/retest

@zcahana
Copy link
Contributor

zcahana commented May 6, 2021

It's worthy to mention that for plain Kubernetes pods, it's perfectly legal to define a pod volume but no volumeMount. The justification for that is unclear to me, and still it has been like that for years and there seem to be no open/closed bug about it.

Nevertheless, we can do better than pods. Thanks @iholder-redhat!
/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label May 6, 2021
@vladikr
Copy link
Member

vladikr commented May 6, 2021

Thank @iholder-redhat
/lgtm
/approve

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vladikr

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 6, 2021
@kubevirt-bot kubevirt-bot merged commit f4321c2 into kubevirt:master May 6, 2021
@stu-gott
Copy link
Member

/cherry-pick release-0.41

@xpivarc
Copy link
Member

xpivarc commented Nov 22, 2021

/cherry-pick release-0.36

@kubevirt-bot
Copy link
Contributor

@xpivarc: #5585 failed to apply on top of branch "release-0.36":

Applying: Add tests to ensure VM volume with missing disk / fs is rejected
Applying: Reject VM defined with volume with no matching disk / fs
Using index info to reconstruct a base tree...
M	pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go
M	pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter_test.go
A	tests/storage/datavolume.go
Falling back to patching base and 3-way merge...
Auto-merging tests/datavolume_test.go
Auto-merging pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter_test.go
Auto-merging pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go
CONFLICT (content): Merge conflict in pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0002 Reject VM defined with volume with no matching disk / fs
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

In response to this:

/cherry-pick release-0.36

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants