Skip to content

Commit 4f4a7c1

Browse files
committed
reject shrinking disk during YAML validation
Signed-off-by: Songpon Srisawai <songpon.ssw@gmail.com>
1 parent db2c41a commit 4f4a7c1

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

cmd/limactl/edit.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ func editAction(cmd *cobra.Command, args []string) error {
118118
return err
119119
}
120120
if err := limayaml.Validate(y, true); err != nil {
121-
rejectedYAML := "lima.REJECTED.yaml"
122-
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
123-
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
124-
}
125-
// TODO: may need to support editing the rejected YAML
126-
return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
121+
return saveRejectedYAML(yBytes, err)
122+
}
123+
124+
if err := limayaml.ValidateYAMLAgainstLatestConfig(yBytes, yContent); err != nil {
125+
return saveRejectedYAML(yBytes, err)
127126
}
127+
128128
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
129129
return err
130130
}
@@ -171,3 +171,13 @@ func askWhetherToStart() (bool, error) {
171171
func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
172172
return bashCompleteInstanceNames(cmd)
173173
}
174+
175+
// saveRejectedYAML writes the rejected config and returns an error.
176+
func saveRejectedYAML(y []byte, origErr error) error {
177+
rejectedYAML := "lima.REJECTED.yaml"
178+
if writeErr := os.WriteFile(rejectedYAML, y, 0o644); writeErr != nil {
179+
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, origErr)
180+
}
181+
// TODO: may need to support editing the rejected YAML
182+
return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, origErr)
183+
}

pkg/limayaml/validate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,37 @@ func warnExperimental(y *LimaYAML) {
595595
logrus.Warn("`mountInotify` is experimental")
596596
}
597597
}
598+
599+
// ValidateYAMLAgainstLatestConfig validates the values between the latest YAML and the updated(New) YAML.
600+
func ValidateYAMLAgainstLatestConfig(yNew, yLatest []byte) error {
601+
var l, n LimaYAML
602+
var err error
603+
if err = Unmarshal(yLatest, &l, "Unmarshal latest YAML bytes"); err != nil {
604+
return err
605+
}
606+
if err = Unmarshal(yNew, &n, "Unmarshal new YAML byte"); err != nil {
607+
return err
608+
}
609+
610+
// Handle editing the template without a disk value
611+
if n.Disk == nil && l.Disk == nil {
612+
return nil
613+
}
614+
615+
// Disk value must be provided, as it is required when creating an instance.
616+
nDisk, err := units.RAMInBytes(*n.Disk)
617+
if err != nil {
618+
return err
619+
}
620+
lDisk, err := units.RAMInBytes(*l.Disk)
621+
if err != nil {
622+
return err
623+
}
624+
625+
// Reject shrinking disk
626+
if nDisk < lDisk {
627+
return fmt.Errorf("field `disk`: shrinking the disk (%v --> %v) is not supported", *l.Disk, *n.Disk)
628+
}
629+
630+
return nil
631+
}

0 commit comments

Comments
 (0)