package hostedctl import ( "context" "errors" "log/slog" "maps" "net/netip" "reflect" "slices" "amadan.net/oficina/equipo/internal/isolatedjob" ) // Alert is one operator alert. At is controller time. type Alert struct { Kind, Detail string At T } // Alerter delivers operator alerts. type Alerter interface { Alert(ctx context.Context, a Alert) error } // Config is the controller's settings. Later tasks add fields. type Config struct { Enabled bool // launches allowed; only EQUIPO_HOSTED_ENABLED=1 sets it Images map[isolatedjob.ImageDigest]bool // image digests launches may use Egress []netip.AddrPort // the exact egress set the network audit expects // ProviderNamespace names where the runner's VM references live (for // AWS, the account and region). New binds the database to it on first // use and refuses any other (ruling R28): a reference read in another // namespace is not found, which would pass for proof that its VM is gone. ProviderNamespace string // ProviderFingerprint names the provider settings a launch of an image // at a size would use beyond the spec (for AWS, the connector and the // image ARN and version). Each intent keeps the one it was saved with; // a dispatch under a different one abandons the intent (ruling R29), as // re-sending it would not repeat the request a lost reply answered. ProviderFingerprint func(isolatedjob.ImageDigest, isolatedjob.Size) string } // ErrProviderMismatch: the database belongs to another provider namespace // than the controller's config names. var ErrProviderMismatch = errors.New("hostedctl: database belongs to another provider namespace") // Deps are the controller's collaborators. Every one but Log is required. type Deps struct { Store *Store Runner isolatedjob.Runner Auditor isolatedjob.NetworkAuditor Alerter Alerter // Log gets one info record for each orphan stopped or deferred, each // failed network audit and each restart re-base or elapsed_unknown // stop (logs.go). Nil discards them. Log *slog.Logger } // Controller owns the job table and the VMs launched for it. type Controller struct { cfg Config store *Store runner isolatedjob.Runner auditor isolatedjob.NetworkAuditor alerter Alerter log *slog.Logger audit auditState // the last network audit result (audit.go) rebased rebasedSet // jobs re-based after a restart (refresh.go) attempts attemptLocks // per-attempt locks (attemptlock.go) reap reapState // the reaper's in-memory state (reaper.go) ownership ownershipLock // dispatches against the orphan reaper's final check (reaper.go) // beforeMark, when set, runs in dispatch after its gates pass and just // before the dispatch marker is recorded. Tests use it to hold the store // there; production leaves it nil. beforeMark func() } // New builds a controller. It refuses a nil dependency, including a nil // pointer inside a non-nil interface. It then delivers the // store's startup alerts (a clock rollback found by Open) through // d.Alerter, once each; if one cannot be delivered New returns that error // and no controller, so a controller never runs with an undelivered rollback // alert. A later New on the same store retries only the undelivered alerts. // // New needs cfg.ProviderNamespace and cfg.ProviderFingerprint. It binds the // store to the namespace the first time and, on a store bound to another, // returns ErrProviderMismatch before anything reads the provider. func New(cfg Config, d Deps) (*Controller, error) { switch { case d.Store == nil: return nil, errors.New("hostedctl: controller needs a store") case isNil(d.Runner): return nil, errors.New("hostedctl: controller needs a runner") case isNil(d.Auditor): return nil, errors.New("hostedctl: controller needs a network auditor") case isNil(d.Alerter): return nil, errors.New("hostedctl: controller needs an alerter") case cfg.ProviderNamespace == "" || cfg.ProviderFingerprint == nil: return nil, errors.New("hostedctl: controller needs a provider namespace and fingerprint") } if err := d.Store.bindProvider(context.Background(), cfg.ProviderNamespace); err != nil { return nil, err } if err := d.Store.deliverStartupAlerts(context.Background(), d.Alerter); err != nil { return nil, err } cfg.Images = maps.Clone(cfg.Images) cfg.Egress = slices.Clone(cfg.Egress) log := d.Log if log == nil { log = slog.New(slog.DiscardHandler) } return &Controller{cfg: cfg, store: d.Store, runner: boundedRunner{d.Runner}, auditor: boundedAuditor{d.Auditor}, alerter: d.Alerter, log: log}, nil } // isNil reports whether v is nil or an interface holding a nil pointer, map, // slice, function or channel, which would panic on first use. func isNil(v any) bool { if v == nil { return true } switch rv := reflect.ValueOf(v); rv.Kind() { case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan, reflect.Interface: return rv.IsNil() } return false }