feat(labs): add the labd provisioner with clone pool, time to live and limits
This commit is contained in:
1 parent
9fdaa62b9b
commit
9385b2ddd4
53 files changed
+14675
-6
No files matched your search
@@ -0,0 +1,50 @@
|
||||
// Package reaper runs the periodic work that makes lab deletion
|
||||
// guaranteed: it ends labs at their timers, retries unfinished deletions,
|
||||
// and removes LXD objects that no lab record owns.
|
||||
package reaper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Target is the lab manager.
|
||||
type Target interface {
|
||||
// Sweep ends expired labs and retries pending deletions.
|
||||
Sweep(ctx context.Context)
|
||||
// Reconcile deletes orphaned LXD objects.
|
||||
Reconcile(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Config holds the intervals of the reaper.
|
||||
type Config struct {
|
||||
SweepInterval time.Duration
|
||||
ReconcileInterval time.Duration
|
||||
}
|
||||
|
||||
// Run sweeps every SweepInterval and reconciles every ReconcileInterval until
|
||||
// ctx ends. The first sweep runs at once; the caller reconciles once before
|
||||
// it serves. A failed reconciliation is logged and retried on schedule;
|
||||
// reconciled reports each outcome, for readiness.
|
||||
func Run(ctx context.Context, target Target, cfg Config, reconciled func(error), logger *slog.Logger) {
|
||||
sweep := time.NewTicker(cfg.SweepInterval)
|
||||
defer sweep.Stop()
|
||||
reconcile := time.NewTicker(cfg.ReconcileInterval)
|
||||
defer reconcile.Stop()
|
||||
target.Sweep(ctx)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-sweep.C:
|
||||
target.Sweep(ctx)
|
||||
case <-reconcile.C:
|
||||
err := target.Reconcile(ctx)
|
||||
if err != nil && ctx.Err() == nil {
|
||||
logger.WarnContext(ctx, "lab reconciliation failed; retrying on schedule", slog.String("error", err.Error()))
|
||||
}
|
||||
reconciled(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package reaper_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/labd/internal/reaper"
|
||||
)
|
||||
|
||||
type target struct {
|
||||
sweeps atomic.Int32
|
||||
reconciles atomic.Int32
|
||||
}
|
||||
|
||||
func (t *target) Sweep(context.Context) { t.sweeps.Add(1) }
|
||||
|
||||
func (t *target) Reconcile(context.Context) error {
|
||||
t.reconciles.Add(1)
|
||||
return errors.New("lxd unreachable")
|
||||
}
|
||||
|
||||
func TestRunSweepsAndReconcilesUntilCancelled(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
work := &target{}
|
||||
var failures atomic.Int32
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
reaper.Run(ctx, work, reaper.Config{SweepInterval: time.Millisecond, ReconcileInterval: 2 * time.Millisecond},
|
||||
func(err error) {
|
||||
if err != nil {
|
||||
failures.Add(1)
|
||||
}
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
}()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for work.sweeps.Load() < 3 || failures.Load() < 2 {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("sweeps %d, failed reconciliations %d", work.sweeps.Load(), failures.Load())
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Run() did not return after cancellation")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user