package hostedctl import ( "context" "errors" "fmt" "amadan.net/oficina/equipo/internal/isolatedjob" ) // boundedRunner is the runner the controller calls through. Each provider // call gets a deadline derived from the caller's context (ruling R27): // ProviderLaunchTimeout for Launch (R21) and ProviderCallTimeout for the // rest, retries included. A caller's earlier deadline, or its cancellation, // still ends the call first. Results are recorded by the caller on a // context of its own. type boundedRunner struct{ r isolatedjob.Runner } // Every Ref a runner hands back is checked with isolatedjob.Ref.Valid here, // the one way into the controller, so no Ref it stores or logs can carry // other text (ruling R36). A Launch with a bad Ref is a provider failure; a // List holding one is incomplete. var errBadRef = errors.New("hostedctl: the runner returned a malformed VM ref") func (b boundedRunner) Launch(ctx context.Context, s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) { ctx, cancel := context.WithTimeout(ctx, ProviderLaunchTimeout) defer cancel() ref, err := b.r.Launch(ctx, s) if err == nil && !ref.Valid() { return "", errBadRef } return ref, err } func (b boundedRunner) Get(ctx context.Context, ref isolatedjob.Ref) (isolatedjob.Job, error) { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() j, err := b.r.Get(ctx, ref) if err == nil && !j.Ref.Valid() { return isolatedjob.Job{}, errBadRef } return j, err } func (b boundedRunner) Suspend(ctx context.Context, ref isolatedjob.Ref) error { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() return b.r.Suspend(ctx, ref) } func (b boundedRunner) Resume(ctx context.Context, ref isolatedjob.Ref) error { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() return b.r.Resume(ctx, ref) } func (b boundedRunner) Stop(ctx context.Context, ref isolatedjob.Ref, reason string) error { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() return b.r.Stop(ctx, ref, reason) } func (b boundedRunner) List(ctx context.Context) ([]isolatedjob.Job, error) { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() jobs, err := b.r.List(ctx) if err != nil { return nil, err } for _, j := range jobs { if !j.Ref.Valid() { return nil, fmt.Errorf("%w: %w", isolatedjob.ErrIncomplete, errBadRef) } } return jobs, nil } func (b boundedRunner) Limits() isolatedjob.Limits { return b.r.Limits() } // boundedAuditor gives each network audit, every read in it included, // ProviderCallTimeout (R27). type boundedAuditor struct{ a isolatedjob.NetworkAuditor } func (b boundedAuditor) AuditNetwork(ctx context.Context) (isolatedjob.NetworkAudit, error) { ctx, cancel := context.WithTimeout(ctx, ProviderCallTimeout) defer cancel() return b.a.AuditNetwork(ctx) }