| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/netip" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 10 | ) |
| 11 | |
| 12 | // Operator log records, at info, through Deps.Log. Each message is a fixed |
| 13 | // string and each attribute a value the controller holds itself: a VM ref, |
| 14 | // an attempt ID, controller times and durations, or a fixed kind word. No |
| 15 | // record carries provider error text, request bodies or key material. |
| 16 | |
| 17 | func (c *Controller) logOrphanStopped(o orphan, now T) { |
| 18 | c.log.Info("hosted controller: orphan VM stopped", |
| 19 | "vm_ref", string(o.ref), "first_seen_ns", int64(o.firstSeen), "unclaimed_for", time.Duration(now-o.firstSeen), |
| 20 | "grace", OrphanGrace, "seen_before_restart", c.store.Restarted() && o.firstSeen < c.store.OpenedAt()) |
| 21 | } |
| 22 | |
| 23 | func (c *Controller) logOrphanStopFailed(o orphan) { |
| 24 | c.log.Info("hosted controller: orphan VM stop call failed; sent again next pass", |
| 25 | "vm_ref", string(o.ref), "first_seen_ns", int64(o.firstSeen), "grace", OrphanGrace) |
| 26 | } |
| 27 | |
| 28 | func (c *Controller) logOrphanDeferred(o orphan, now T, dispatched int) { |
| 29 | c.log.Info("hosted controller: orphan VM left running for a dispatched launch intent", |
| 30 | "vm_ref", string(o.ref), "first_seen_ns", int64(o.firstSeen), "unclaimed_for", time.Duration(now-o.firstSeen), |
| 31 | "grace", OrphanGrace, "dispatched_intents", dispatched) |
| 32 | } |
| 33 | |
| 34 | func (c *Controller) logAuditFailed(at T, kinds []string) { |
| 35 | c.log.Info("hosted controller: network audit failed; launches refused", "began_ns", int64(at), "kinds", kinds) |
| 36 | } |
| 37 | |
| 38 | func (c *Controller) logRebased(attempt string, age, launchedBy, stopBy time.Duration) { |
| 39 | c.log.Info("hosted controller: restart re-based a job from the provider's age", |
| 40 | "attempt", attempt, "provider_age", age, "launched_moved_back", launchedBy, "stop_moved_back", stopBy) |
| 41 | } |
| 42 | |
| 43 | func (c *Controller) logElapsedUnknown(attempt string) { |
| 44 | c.log.Info("hosted controller: restart stop requested, elapsed time unknown", |
| 45 | "attempt", attempt, "reason", reasonElapsedUnknown) |
| 46 | } |
| 47 | |
| 48 | // auditCallKinds names, in fixed words, why reading the network settings |
| 49 | // failed. |
| 50 | func auditCallKinds(err error) []string { |
| 51 | kinds := []string{"provider_read_failed"} |
| 52 | switch { |
| 53 | case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded): |
| 54 | kinds = append(kinds, "cancelled_or_timed_out") |
| 55 | case errors.Is(err, isolatedjob.ErrThrottled): |
| 56 | kinds = append(kinds, "throttled") |
| 57 | case errors.Is(err, isolatedjob.ErrNotFound): |
| 58 | kinds = append(kinds, "not_found") |
| 59 | } |
| 60 | return kinds |
| 61 | } |
| 62 | |
| 63 | // auditKinds names, in fixed words, each property of a that fails the |
| 64 | // audit against want (see isolatedjob.NetworkAudit.OK). |
| 65 | func auditKinds(a isolatedjob.NetworkAudit, want []netip.AddrPort) []string { |
| 66 | var kinds []string |
| 67 | if !a.ResolverDisabled { |
| 68 | kinds = append(kinds, "resolver_enabled") |
| 69 | } |
| 70 | if !a.IngressClosed { |
| 71 | kinds = append(kinds, "ingress_open") |
| 72 | } |
| 73 | if !a.RouteOutAbsent { |
| 74 | kinds = append(kinds, "route_out_present") |
| 75 | } |
| 76 | a.ResolverDisabled, a.IngressClosed, a.RouteOutAbsent = true, true, true |
| 77 | if a.OK(want) != nil { |
| 78 | kinds = append(kinds, "egress_differs") |
| 79 | } |
| 80 | return kinds |
| 81 | } |
| 82 | |