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

Fix: panic and crash when size parameter of a disk is not parseable into a Quantity #111

Merged
merged 2 commits into from
Dec 30, 2024

Conversation

m-ildefons
Copy link
Contributor

Description

The harvester_virtualmachine and its subresources are parsed and converted into a kubevirtv1.VirtualMachine object using a VMBuilder factory. This VMBuilder assumes that parameters given to it have some semblance of sanity. This means amongst other things that the size parameter of a volume must be a parseable into a resource.Quantity using resource.MustParse, e.g. "10Gi".
The size parameter isn't always required, but when it is required, if a user omits it the Terraform provider will just fill it with a default value.
Since the size parameter of a disk subresource of a harvester_virtualmachine is user input, it must be sanitized before being passed on to the VMBuilder. In case it isn't parseable, a sensible error must be emitted and further processing of the virtual machine resource must be stopped.
To do this, the parsing function of the disk subresource of harvester_virtualmachine resources is re-ordered such that the different cases where the size parameter matters and where it is ignored are more clearly defined.
When it matters, the size parameter is checked before being passed on to the VMBuilder.
This also allows a default value to be used when the size parameter isn't given, but is required.

Related Issues:

Fix crash if the disk block in the virtualmachine resource does not
contain a `size` parameter and also doesn't contain either of the
`existing_volume_name` or the `container_image_name` parameters.

The Terraform provider relied on the `builder` package always behaving
nicely, but the `builder` package relied on all users checking inputs
before and only giving valid inputs. Therefore error handling in case a
non-parsable size has been given for a disk was not done, causing a
crash.
The size parameter is not mandatory in all cases and defaults to an
empty string, which is not parsable into a `resource.Quantity` object.

The fix here is to differentiate better when the `size` parameter is
taken into account at all. In case it is not taken into account, it is
completely ignored. If it is taken into account, a check is performed
that ensures it's parsable. If it isn't parsable, the Terraform provider
will return an error directly, indicating to the user where the problem
is.

related-to: harvester/harvester#7139

Signed-off-by: Moritz Röhrich <[email protected]>
Add test to ensure that a non-parseable `size` parameter in a `disk`
subresource of a `virtualmachine` resource generates an appropriate
error message.
The `size` parameter is not mandatory in all configurations, but when it
is, it must be a parseable `resource.Quantity`, otherwise a panic and
crash would ensue.

Signed-off-by: Moritz Röhrich <[email protected]>
@m-ildefons m-ildefons added the bug Something isn't working label Dec 17, 2024
@m-ildefons m-ildefons self-assigned this Dec 17, 2024
Copy link
Member

@FrankYang0529 FrankYang0529 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the patch.

@FrankYang0529 FrankYang0529 merged commit 416edd7 into harvester:master Dec 30, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants