| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "log/slog" |
| 7 | "maps" |
| 8 | "net/netip" |
| 9 | "reflect" |
| 10 | "slices" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 13 | ) |
| 14 | |
| 15 | // Alert is one operator alert. At is controller time. |
| 16 | type Alert struct { |
| 17 | Kind, Detail string |
| 18 | At T |
| 19 | } |
| 20 | |
| 21 | // Alerter delivers operator alerts. |
| 22 | type Alerter interface { |
| 23 | Alert(ctx context.Context, a Alert) error |
| 24 | } |
| 25 | |
| 26 | // Config is the controller's settings. Later tasks add fields. |
| 27 | type Config struct { |
| 28 | Enabled bool // launches allowed; only EQUIPO_HOSTED_ENABLED=1 sets it |
| 29 | Images map[isolatedjob.ImageDigest]bool // image digests launches may use |
| 30 | Egress []netip.AddrPort // the exact egress set the network audit expects |
| 31 | // ProviderNamespace names where the runner's VM references live (for |
| 32 | // AWS, the account and region). New binds the database to it on first |
| 33 | // use and refuses any other (ruling R28): a reference read in another |
| 34 | // namespace is not found, which would pass for proof that its VM is gone. |
| 35 | ProviderNamespace string |
| 36 | // ProviderFingerprint names the provider settings a launch of an image |
| 37 | // at a size would use beyond the spec (for AWS, the connector and the |
| 38 | // image ARN and version). Each intent keeps the one it was saved with; |
| 39 | // a dispatch under a different one abandons the intent (ruling R29), as |
| 40 | // re-sending it would not repeat the request a lost reply answered. |
| 41 | ProviderFingerprint func(isolatedjob.ImageDigest, isolatedjob.Size) string |
| 42 | } |
| 43 | |
| 44 | // ErrProviderMismatch: the database belongs to another provider namespace |
| 45 | // than the controller's config names. |
| 46 | var ErrProviderMismatch = errors.New("hostedctl: database belongs to another provider namespace") |
| 47 | |
| 48 | // Deps are the controller's collaborators. Every one but Log is required. |
| 49 | type Deps struct { |
| 50 | Store *Store |
| 51 | Runner isolatedjob.Runner |
| 52 | Auditor isolatedjob.NetworkAuditor |
| 53 | Alerter Alerter |
| 54 | // Log gets one info record for each orphan stopped or deferred, each |
| 55 | // failed network audit and each restart re-base or elapsed_unknown |
| 56 | // stop (logs.go). Nil discards them. |
| 57 | Log *slog.Logger |
| 58 | } |
| 59 | |
| 60 | // Controller owns the job table and the VMs launched for it. |
| 61 | type Controller struct { |
| 62 | cfg Config |
| 63 | store *Store |
| 64 | runner isolatedjob.Runner |
| 65 | auditor isolatedjob.NetworkAuditor |
| 66 | alerter Alerter |
| 67 | log *slog.Logger |
| 68 | audit auditState // the last network audit result (audit.go) |
| 69 | rebased rebasedSet // jobs re-based after a restart (refresh.go) |
| 70 | attempts attemptLocks // per-attempt locks (attemptlock.go) |
| 71 | reap reapState // the reaper's in-memory state (reaper.go) |
| 72 | ownership ownershipLock // dispatches against the orphan reaper's final check (reaper.go) |
| 73 | // beforeMark, when set, runs in dispatch after its gates pass and just |
| 74 | // before the dispatch marker is recorded. Tests use it to hold the store |
| 75 | // there; production leaves it nil. |
| 76 | beforeMark func() |
| 77 | } |
| 78 | |
| 79 | // New builds a controller. It refuses a nil dependency, including a nil |
| 80 | // pointer inside a non-nil interface. It then delivers the |
| 81 | // store's startup alerts (a clock rollback found by Open) through |
| 82 | // d.Alerter, once each; if one cannot be delivered New returns that error |
| 83 | // and no controller, so a controller never runs with an undelivered rollback |
| 84 | // alert. A later New on the same store retries only the undelivered alerts. |
| 85 | // |
| 86 | // New needs cfg.ProviderNamespace and cfg.ProviderFingerprint. It binds the |
| 87 | // store to the namespace the first time and, on a store bound to another, |
| 88 | // returns ErrProviderMismatch before anything reads the provider. |
| 89 | func New(cfg Config, d Deps) (*Controller, error) { |
| 90 | switch { |
| 91 | case d.Store == nil: |
| 92 | return nil, errors.New("hostedctl: controller needs a store") |
| 93 | case isNil(d.Runner): |
| 94 | return nil, errors.New("hostedctl: controller needs a runner") |
| 95 | case isNil(d.Auditor): |
| 96 | return nil, errors.New("hostedctl: controller needs a network auditor") |
| 97 | case isNil(d.Alerter): |
| 98 | return nil, errors.New("hostedctl: controller needs an alerter") |
| 99 | case cfg.ProviderNamespace == "" || cfg.ProviderFingerprint == nil: |
| 100 | return nil, errors.New("hostedctl: controller needs a provider namespace and fingerprint") |
| 101 | } |
| 102 | if err := d.Store.bindProvider(context.Background(), cfg.ProviderNamespace); err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | if err := d.Store.deliverStartupAlerts(context.Background(), d.Alerter); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | cfg.Images = maps.Clone(cfg.Images) |
| 109 | cfg.Egress = slices.Clone(cfg.Egress) |
| 110 | log := d.Log |
| 111 | if log == nil { |
| 112 | log = slog.New(slog.DiscardHandler) |
| 113 | } |
| 114 | return &Controller{cfg: cfg, store: d.Store, runner: boundedRunner{d.Runner}, auditor: boundedAuditor{d.Auditor}, |
| 115 | alerter: d.Alerter, log: log}, nil |
| 116 | } |
| 117 | |
| 118 | // isNil reports whether v is nil or an interface holding a nil pointer, map, |
| 119 | // slice, function or channel, which would panic on first use. |
| 120 | func isNil(v any) bool { |
| 121 | if v == nil { |
| 122 | return true |
| 123 | } |
| 124 | switch rv := reflect.ValueOf(v); rv.Kind() { |
| 125 | case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan, reflect.Interface: |
| 126 | return rv.IsNil() |
| 127 | } |
| 128 | return false |
| 129 | } |
| 130 | |