-
Notifications
You must be signed in to change notification settings - Fork 672
Reject shrinking disk during YAML validation #3596
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements early YAML validation to reject configurations that attempt to shrink the disk size rather than waiting until instance startup.
- Added a ValidateYAMLAgainstLatest function that compares disk sizes from the new and latest YAML configurations.
- Integrated the new validation into the limactl edit flow to save and report rejected configurations.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
pkg/limayaml/validate.go | Added a YAML validation function to prevent disk size shrinking. |
cmd/limactl/edit.go | Integrated validation into the edit command and added a function to handle rejected YAML. |
func saveRejectedYAML(y []byte, origErr error) error { | ||
rejectedYAML := "lima.REJECTED.yaml" | ||
if writeErr := os.WriteFile(rejectedYAML, y, 0o644); writeErr != nil { | ||
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, origErr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message wraps two errors using '%w: %w', which might be confusing. Consider simplifying the error wrapping to clearly associate the cause of failure with a single wrapped error.
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, origErr) | |
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w", rejectedYAML, writeErr) |
Copilot uses AI. Check for mistakes.
8cb23be
to
4532c86
Compare
Signed-off-by: Songpon Srisawai <[email protected]>
4532c86
to
4f4a7c1
Compare
The disk value can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good, but there is some nitpicking I think should be addressed.
func saveRejectedYAML(y []byte, origErr error) error { | ||
rejectedYAML := "lima.REJECTED.yaml" | ||
if writeErr := os.WriteFile(rejectedYAML, y, 0o644); writeErr != nil { | ||
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, origErr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As Copilot suggested above, you cannot use 2 %w
formatting codes in an error; only the last one will be wrapped; the other will be treated as %v
.
So either explicitly choose which one should be wrapped, and use %v
for the other one explicitly. Or combine both errors with errors.Join()
:
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, origErr) | |
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w", rejectedYAML, errors.Join(writeErr, origErr)) |
if err = Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil { | ||
return err | ||
} | ||
if err = Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if err = Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil { | |
if err = Unmarshal(yNew, &n, "Unmarshal new YAML bytes); err != nil { |
} | ||
|
||
// Handle editing the template without a disk value | ||
if n.Disk == nil && l.Disk == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are dereferencing both, so you need to bail if either one is nil
.
if n.Disk == nil && l.Disk == nil { | |
if n.Disk == nil || l.Disk == nil { |
var err error | ||
if err = Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil { | ||
return err | ||
} | ||
if err = Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid err
declaration:
var err error | |
if err = Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil { | |
return err | |
} | |
if err = Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil { | |
if err := Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil { | |
return err | |
} | |
if err := Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil { |
@@ -595,3 +595,37 @@ func warnExperimental(y *LimaYAML) { | |||
logrus.Warn("`mountInotify` is experimental") | |||
} | |||
} | |||
|
|||
// ValidateYAMLAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML. | |||
func ValidateYAMLAgainstLatestConfig(yNew, yLatest []byte) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's cover this function with unit tests.
As @jandubois suggested in #3533.
Currently, rejecting shrinking disk size happens during instance startup.
This PR adds validation earlier during YAML validation.
The function compares values between the new config and the latest config YAML files to reject disk size shrinking during validation.