Extract opstate and dmsnap into subpackages

Two leaves of the daemon package that carry no back-references to Daemon
move out:

- internal/daemon/opstate: generic Registry[T AsyncOp]. The AsyncOp
  interface methods are capitalised (ID, IsDone, UpdatedAt, Cancel);
  vmCreateOperationState and imageBuildOperationState implement it.
- internal/daemon/dmsnap: Create, Cleanup, Remove plus the Handles type
  for device-mapper snapshot lifecycle. Takes an explicit Runner
  interface. The daemon-package snapshot.go keeps thin forwarders and a
  type alias so existing call sites and tests are untouched.

Skipped on purpose: tap_pool has too many Daemon-scoped dependencies
(config, store, closing, createTap) for a clean extraction at this
stage; nat.go is already a thin facade over internal/hostnat;
dns_routing.go tests tightly couple to package internals, so extraction
would be more churn than payoff. Each can be revisited when a
subsystem-level refactor forces the boundary.

All tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Thales Maciel 2026-04-15 16:02:43 -03:00
parent 59f2766139
commit fdab4a7e68
No known key found for this signature in database
GPG key ID: 33112E6833C34679
7 changed files with 214 additions and 170 deletions

View file

@ -11,10 +11,10 @@ import (
"banger/internal/model"
)
func (op *vmCreateOperationState) opID() string { return op.snapshot().ID }
func (op *vmCreateOperationState) opIsDone() bool { return op.snapshot().Done }
func (op *vmCreateOperationState) opUpdatedAt() time.Time { return op.snapshot().UpdatedAt }
func (op *vmCreateOperationState) opCancel() { op.cancelOperation() }
func (op *vmCreateOperationState) ID() string { return op.snapshot().ID }
func (op *vmCreateOperationState) IsDone() bool { return op.snapshot().Done }
func (op *vmCreateOperationState) UpdatedAt() time.Time { return op.snapshot().UpdatedAt }
func (op *vmCreateOperationState) Cancel() { op.cancelOperation() }
type vmCreateProgressKey struct{}
@ -153,7 +153,7 @@ func (d *Daemon) BeginVMCreate(_ context.Context, params api.VMCreateParams) (ap
}
createCtx, cancel := context.WithCancel(context.Background())
op.setCancel(cancel)
d.createOps.insert(op)
d.createOps.Insert(op)
go d.runVMCreateOperation(withVMCreateProgress(createCtx, op), op, params)
return op.snapshot(), nil
}
@ -168,7 +168,7 @@ func (d *Daemon) runVMCreateOperation(ctx context.Context, op *vmCreateOperation
}
func (d *Daemon) VMCreateStatus(_ context.Context, id string) (api.VMCreateOperation, error) {
op, ok := d.createOps.get(strings.TrimSpace(id))
op, ok := d.createOps.Get(strings.TrimSpace(id))
if !ok {
return api.VMCreateOperation{}, fmt.Errorf("vm create operation not found: %s", id)
}
@ -176,7 +176,7 @@ func (d *Daemon) VMCreateStatus(_ context.Context, id string) (api.VMCreateOpera
}
func (d *Daemon) CancelVMCreate(_ context.Context, id string) error {
op, ok := d.createOps.get(strings.TrimSpace(id))
op, ok := d.createOps.Get(strings.TrimSpace(id))
if !ok {
return fmt.Errorf("vm create operation not found: %s", id)
}
@ -185,5 +185,5 @@ func (d *Daemon) CancelVMCreate(_ context.Context, id string) error {
}
func (d *Daemon) pruneVMCreateOperations(olderThan time.Time) {
d.createOps.prune(olderThan)
d.createOps.Prune(olderThan)
}