Skip to content

feat: Add instance delete-on-stop support#304

Draft
jedevc wants to merge 1 commit intostagingfrom
jedevc/delete-on-stop
Draft

feat: Add instance delete-on-stop support#304
jedevc wants to merge 1 commit intostagingfrom
jedevc/delete-on-stop

Conversation

@jedevc
Copy link
Copy Markdown
Member

@jedevc jedevc commented Apr 28, 2026

Signed-off-by: Justin Chadwell <justin@unikraft.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds CLI support for “delete-on-stop” instances (Docker-like --rm), and adjusts stop/restart UX to handle instances that disappear immediately after stopping.

Changes:

  • Add --rm to unikraft instance create, mapping it to the platform delete_on_stop feature.
  • Update instance stop / instance restart to tolerate “not found” after stop/start when delete-on-stop removes instances, aiming to show a meaningful diff.
  • Add a new golden test and update help output to cover --rm.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
internal/cmd/instances.go Adds --rm handling on create; adjusts stop/restart flows to treat not-found as deletion for diff output.
cmd/unikraft/testdata/TestGolden/instances/rm New golden transcript verifying --rm causes deletion after stop.
cmd/unikraft/testdata/TestGolden/instances/help Updates help golden output to include the new --rm flag.
cmd/unikraft/instances_test.go Adds a new golden test case exercising instance create --rm + instance stop.
Comments suppressed due to low confidence (1)

internal/cmd/instances.go:1297

  • In InstancesRestartCmd.Run, when startInstances returns len(started)==0 due to delete-on-stop (and startErr is group.ErrRefNotFound), the code proceeds but then builds keySet from started (empty) and fetches updated using started.Strings() (also empty). This causes before to be filtered down to empty and the diff to show no deletions, which contradicts the intent in the preceding comment. Consider using stopped (or the original targetKeys) as the key set for diffing, and skip the Get call when there are no started instances so the diff can reflect the instances being removed.
	started, startErr := startInstances(ctx, g, stopped)
	opErr = errors.Join(opErr, startErr)
	// If all stopped instances were removed (e.g. by delete-on-stop)
	// before they could be restarted, show a diff reflecting the
	// deletion instead of returning a confusing error.
	if len(started) == 0 {
		var refErr group.ErrRefNotFound
		if startErr == nil || !errors.As(startErr, &refErr) {
			return opErr
		}
	}

	updated, getErr := Instance{}.Get(ctx, started.Strings())
	// Same as above: tolerate not-found when delete-on-stop already
	// removed the instances.
	if getErr != nil && len(updated) == 0 {
		var refErr group.ErrRefNotFound
		if !errors.As(getErr, &refErr) {
			opErr = errors.Join(opErr, getErr)
			return opErr
		}
	} else {
		opErr = errors.Join(opErr, getErr)
	}

	keySet := make(map[string]struct{}, len(started))
	for _, k := range started {
		keySet[k.Canonical()] = struct{}{}
	}
	before = slices.DeleteFunc(slices.Clone(before), func(r resource.Resource) bool {
		_, ok := keySet[r.Key().Canonical()]
		return !ok
	})

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/cmd/instances.go
Comment on lines +1267 to 1275
// If all stopped instances were removed (e.g. by delete-on-stop)
// before they could be restarted, show a diff reflecting the
// deletion instead of returning a confusing error.
if len(started) == 0 {
return opErr
var refErr group.ErrRefNotFound
if startErr == nil || !errors.As(startErr, &refErr) {
return opErr
}
}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The new delete-on-stop tolerance path in InstancesRestartCmd.Run (handling group.ErrRefNotFound when startInstances/Get can’t find instances) isn’t covered by the golden CLI tests. Adding a test similar to the new rm stop test but exercising unikraft instance restart --rm (or restarting an instance created with --rm) would help prevent regressions.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants