-
Notifications
You must be signed in to change notification settings - Fork 669
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -125,6 +125,11 @@ func editAction(cmd *cobra.Command, args []string) error { | |||||
// TODO: may need to support editing the rejected YAML | ||||||
return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err) | ||||||
} | ||||||
|
||||||
if err := limayaml.ValidateYAMLAgainstLatest(yBytes, yContent); err != nil { | ||||||
return saveRejectedYAML(yBytes, err) | ||||||
} | ||||||
|
||||||
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -171,3 +176,12 @@ func askWhetherToStart() (bool, error) { | |||||
func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||||||
return bashCompleteInstanceNames(cmd) | ||||||
} | ||||||
|
||||||
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 commentThe 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.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
// TODO: may need to support editing the rejected YAML | ||||||
return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, origErr) | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -595,3 +595,27 @@ func warnExperimental(y *LimaYAML) { | |||||||||||||||||||||
logrus.Warn("`mountInotify` is experimental") | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// ValidateYAMLAgainstLatest validates the values between the latest YAML and the updated(New) YAML. | ||||||||||||||||||||||
func ValidateYAMLAgainstLatest(yNew, yLatest []byte) error { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does "latest" mean here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refers to the current (or existing) YAML file for the instance. Basically, I want to compare the new YAML (edited YAML) to the old YAML (current/existing YAML). |
||||||||||||||||||||||
var l, n LimaYAML | ||||||||||||||||||||||
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 { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Skip validation if both fields are unset. | ||||||||||||||||||||||
if n.Disk == nil && l.Disk == nil { | ||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current nil check only skips validation when both disk fields are nil, which may lead to a nil pointer dereference if only one disk value is set. Consider adding explicit checks to handle cases where one of the disk fields is nil.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
nDisk, _ := units.RAMInBytes(*n.Disk) | ||||||||||||||||||||||
lDisk, _ := units.RAMInBytes(*l.Disk) | ||||||||||||||||||||||
if nDisk < lDisk { | ||||||||||||||||||||||
return fmt.Errorf("field `disk`: shrinking the disk (%v --> %v) is not supported", *l.Disk, *n.Disk) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return 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.
This is same as
saveRejectedYAML
and should be deduplicated