console: the instance list's egress is a member projection with no box diagnostics (#328)
A box's egress Detail can name another tenant's host and unit, and its refusals name the box's own addresses and prefixes. The member-visible instance list now carries only the held addresses and a member state (pending, failing or not_available) with a fixed reason; the full read-back stays in status --layout. The CLI shows the member words in EGRESS and the reason in REASON.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+152
−32
instances_cli.go+22 −7instances_cli_test.go+13 −3internal/console/apiinstances.go+56 −12internal/console/apiinstances_test.go+61 −10
diff --git a/instances_cli.go b/instances_cli.go| index 2844c6212..b124789d2 100644 |
| --- a/instances_cli.go |
| +++ b/instances_cli.go |
| @@ -455,13 +455,16 @@ func upstreamsCell(ups []apiUpstreamState) string { |
| // apiEgress mirrors the console's apiEgress (internal/console/apistatuslayout.go): |
| // one host's egress allow as root read it back from its slice, plus the held |
| -// addresses the console's fleet-address report found on a fleet box. |
| +// addresses the console's fleet-address report found on a fleet box. The |
| +// instance list sends the member projection instead (the console's |
| +// memberEgress): Allowed, a member State and its fixed Reason, nothing else. |
| type apiEgress struct { |
| Allowed []string `json:"allowed,omitempty"` |
| Conflicts []string `json:"conflicts,omitempty"` // allowed addresses a fleet box now holds |
| Refused []string `json:"refused,omitempty"` |
| State string `json:"state,omitempty"` |
| Detail string `json:"detail,omitempty"` |
| + Reason string `json:"reason,omitempty"` // the member projection's fixed reason |
| } |
| // Copy: drafts for plan 2 Task 17's review (#328). egressStateWords maps a |
| @@ -474,6 +477,9 @@ var egressStateWords = map[string]string{ |
| "expired": "policy expired", |
| "not_nested": "box not wholly nested", |
| "under_claims": "not yet supported under claims", |
| + // The member projection's words (instances list). |
| + "pending": "pending", |
| + "not_available": "not available", |
| } |
| const ( |
| @@ -514,16 +520,18 @@ func egressCell(e *apiEgress) string { |
| return cell |
| } |
| -// egressReason is what status --layout's REASON cell adds for the host's |
| -// egress: "egress: " and the entries the box refused, then its detail, or "" |
| -// when there are none. |
| +// egressReason is what a REASON cell adds for the host's egress: "egress: " |
| +// and the entries the box refused, then its detail (status --layout), or the |
| +// member projection's fixed reason (instances list); "" when there are none. |
| func egressReason(e *apiEgress) string { |
| if e == nil { |
| return "" |
| } |
| parts := slices.Clone(e.Refused) |
| - if e.Detail != "" { |
| - parts = append(parts, e.Detail) |
| + for _, p := range []string{e.Detail, e.Reason} { |
| + if p != "" { |
| + parts = append(parts, p) |
| + } |
| } |
| if len(parts) == 0 { |
| return "" |
| @@ -644,9 +652,16 @@ func printInstancesList(out io.Writer, recs []apiInstanceRecord) { |
| if rowHealth == "" { |
| rowHealth = "—" // an old console: the field predates it, not "unhealthy" |
| } |
| + reason := reasonCell(r) |
| + if er := egressReason(r.Egress); er != "" { |
| + if reason != "" { |
| + reason += "; " |
| + } |
| + reason += er |
| + } |
| fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", r.Host, r.Region, r.Channel, r.Pool, |
| installedCell(r), servingCell(r), rowHealth, upstreamsCell(r.Upstreams), humanBytes(r.DBBytes), |
| - egressCell(r.Egress), r.CreatedAt, r.CreatedBy, reasonCell(r), seedCell(r)) |
| + egressCell(r.Egress), r.CreatedAt, r.CreatedBy, reason, seedCell(r)) |
| } |
| w.Flush() |
| } |
diff --git a/instances_cli_test.go b/instances_cli_test.go| index eb03e052b..9f31c039c 100644 |
| --- a/instances_cli_test.go |
| +++ b/instances_cli_test.go |
| @@ -125,6 +125,8 @@ func TestEgressCellTable(t *testing.T) { |
| {"an unknown state shows as sent", &apiEgress{State: "later"}, "- !later"}, |
| {"refused only", &apiEgress{Allowed: []string{"10.0.5.20"}, Refused: []string{"10.0.5.1: an address of this box"}}, "10.0.5.20 !refused"}, |
| {"a state outranks refused", &apiEgress{Refused: []string{"x"}, State: "failing"}, "- !failing"}, |
| + {"member: pending", &apiEgress{Allowed: []string{"10.0.5.20"}, State: "pending", Reason: "r"}, "10.0.5.20 !pending"}, |
| + {"member: not available", &apiEgress{State: "not_available", Reason: "r"}, "- !not available"}, |
| } |
| for _, c := range cases { |
| if got := egressCell(c.e); got != c.want { |
| @@ -136,13 +138,15 @@ func TestEgressCellTable(t *testing.T) { |
| // TestInstancesListShowsEgress: the list's EGRESS column sits right after DB |
| // and before CREATED (plan 3 puts SIZE between DB and EGRESS), and an old |
| // console's record, which has no egress key, reads "-". The records are the |
| -// console's shape: the member-visible list carries no conflicts. |
| +// console's member projection: no conflicts, a member state word, and its |
| +// fixed reason in REASON. |
| func TestInstancesListShowsEgress(t *testing.T) { |
| var recs []apiInstanceRecord |
| body := `[ |
| {"host":"shop-a.example.com","channel":"stable","health":"running","db_bytes":2048, |
| "egress":{"allowed":["10.0.5.20","fd00:5::20"]}}, |
| - {"host":"shop-b.example.com","channel":"stable","health":"running","egress":{"state":"expired"}}, |
| + {"host":"shop-b.example.com","channel":"stable","health":"running", |
| + "egress":{"state":"not_available","reason":"the allow is not in force on this box"}}, |
| {"host":"shop-c.example.com","channel":"stable","health":"running"} |
| ]` |
| if err := json.Unmarshal([]byte(body), &recs); err != nil { |
| @@ -170,7 +174,7 @@ func TestInstancesListShowsEgress(t *testing.T) { |
| } |
| for _, c := range []struct{ host, db, egress string }{ |
| {"shop-a.example.com", "2.0KB", "10.0.5.20,fd00:5::20"}, |
| - {"shop-b.example.com", "-", "- !policy expired"}, |
| + {"shop-b.example.com", "-", "- !not available"}, |
| {"shop-c.example.com", "-", "-"}, |
| } { |
| if got := cell(c.host, "DB", "EGRESS"); got != c.db { |
| @@ -180,6 +184,12 @@ func TestInstancesListShowsEgress(t *testing.T) { |
| t.Errorf("%s EGRESS = %q, want %q\n%s", c.host, got, c.egress, buf.String()) |
| } |
| } |
| + if got := cell("shop-b.example.com", "REASON", "SEED"); got != "egress: the allow is not in force on this box" { |
| + t.Errorf("shop-b REASON = %q", got) |
| + } |
| + if got := cell("shop-a.example.com", "REASON", "SEED"); got != "" { |
| + t.Errorf("shop-a REASON = %q, want empty", got) |
| + } |
| } |
| // TestParseAddrList pins the given-vs-absent split -addr/-addrs share |
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index 591d3ae5a..a164fa9af 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -210,14 +210,58 @@ type instanceListRecord struct { |
| // CLI can tell an instance on a box that ignores settings from one that |
| // never asked for any. |
| SettingsDefault bool `json:"settings_default"` |
| - // Egress is this host's egress read-back as its box last sent it |
| - // (status.BoxMetrics.Egress, #328), joined under DBBytes' rules. Absent |
| - // when no fresh box reports one, which is most hosts: a host with no |
| - // allow and nothing to say is not reported. It never carries Conflicts: |
| - // this list is a member's, and that an address belongs to a fleet box |
| - // is fleet topology a member does not learn. The operator views |
| - // (status --layout, the policy GET) carry the marks. |
| - Egress *apiEgress `json:"egress,omitempty"` |
| + // Egress is this host's egress read-back (status.BoxMetrics.Egress, |
| + // #328), joined under DBBytes' rules, as a member may see it |
| + // (memberEgressView). Absent when no fresh box reports one, which is |
| + // most hosts: a host with no allow and nothing to say is not reported. |
| + Egress *memberEgress `json:"egress,omitempty"` |
| +} |
| + |
| +// memberEgress is a host's egress read-back as a member sees it on the |
| +// instance list: the addresses its slice holds, and when the allow is not |
| +// simply in force a member state word with a fixed reason. A box's own |
| +// Detail and Refused entries are operator diagnostics and are never copied: |
| +// they can name another tenant's host and unit, or the box's own addresses |
| +// and prefixes. Nor does it carry the fleet-address report's conflicts: |
| +// that an address belongs to a fleet box is fleet topology. The operator |
| +// view (status --layout) carries all of it. |
| +type memberEgress struct { |
| + Allowed []string `json:"allowed,omitempty"` |
| + State string `json:"state,omitempty"` |
| + Reason string `json:"reason,omitempty"` |
| +} |
| + |
| +// The member states and their fixed reasons. Copy: drafts for plan 2 |
| +// Task 17's review. |
| +const ( |
| + memberEgressPending = "pending" |
| + memberEgressFailing = "failing" |
| + memberEgressNotAvailable = "not_available" |
| + |
| + memberEgressPendingReason = "the allow takes effect when the instance next starts" |
| + memberEgressFailingReason = "the box could not apply the allow, and tries again" |
| + memberEgressNotAvailableReason = "the allow is not in force on this box" |
| +) |
| + |
| +// memberEgressView projects a box's read-back for host onto memberEgress, |
| +// nil when no fresh box reported one. Root's "failing" and "pending" keep |
| +// their word; every other state (the policy unreadable, unverifiable or |
| +// expired, the box not wholly nested, claims on, or one this console does |
| +// not know) and a refusal with no state are "not_available". |
| +func memberEgressView(h status.HostEgress, ok bool) *memberEgress { |
| + if !ok { |
| + return nil |
| + } |
| + e := &memberEgress{Allowed: h.Allowed} |
| + switch { |
| + case h.State == memberEgressFailing: |
| + e.State, e.Reason = memberEgressFailing, memberEgressFailingReason |
| + case h.State == memberEgressPending: |
| + e.State, e.Reason = memberEgressPending, memberEgressPendingReason |
| + case h.State != "" || len(h.Refused) > 0: |
| + e.State, e.Reason = memberEgressNotAvailable, memberEgressNotAvailableReason |
| + } |
| + return e |
| } |
| // instanceHealth computes each record host's health word the same way the |
| @@ -472,11 +516,11 @@ func (j boxJoin) dbSize(host string) *int64 { |
| return &n |
| } |
| -// egressFor is the join's egress view for host, nil when no fresh box |
| -// reported one. It marks no conflicts (instanceListRecord.Egress). |
| -func (j boxJoin) egressFor(host string) *apiEgress { |
| +// egressFor is the join's member view of host's egress, nil when no fresh |
| +// box reported one (memberEgressView). |
| +func (j boxJoin) egressFor(host string) *memberEgress { |
| h, ok := j.egress[host] |
| - return egressView(h, ok, nil) |
| + return memberEgressView(h, ok) |
| } |
| // loadBoxJoin builds boxJoin from every NON-STALE box's own |
diff --git a/internal/console/apiinstances_test.go b/internal/console/apiinstances_test.go| index 82fb31d82..1946a4423 100644 |
| --- a/internal/console/apiinstances_test.go |
| +++ b/internal/console/apiinstances_test.go |
| @@ -2635,9 +2635,11 @@ func TestAPIInstanceListDBBytes(t *testing.T) { |
| // TestAPIInstancesJoinsEgress (#328): egress joins from a FRESH box's |
| // Metrics.Egress under db_bytes' rules: absent for a host no box reports, |
| // and dropped for a host whose only reporting box has gone stale. The list |
| -// is a member's, so it never carries the fleet-address report's conflicts, |
| -// even for a held address the report found on a box: that an address is a |
| -// fleet box's is fleet topology a member does not learn. |
| +// is a member's, so it carries the member projection only: the held |
| +// addresses and a member state word with its fixed reason. The box's own |
| +// detail (which can name another tenant's host and unit), its refusals |
| +// (which can name the box's addresses and prefixes) and the fleet-address |
| +// report's conflicts never reach it. |
| func TestAPIInstancesJoinsEgress(t *testing.T) { |
| t.Parallel() |
| fx, token := instanceFixture(t, "code-inst-egress") |
| @@ -2647,15 +2649,26 @@ func TestAPIInstancesJoinsEgress(t *testing.T) { |
| enableInstances(t, fx, "shop", provision.Manifest{Schema: 1, App: "shop", Domain: "example.com", |
| Channel: "stable", Regions: []string{"ie"}, Seed: []string{"seed"}}) |
| u := instancesURL(fx, "bab", "shop") |
| - for _, h := range []string{"shop-a.example.com", "shop-b.example.com", "shop-c.example.com"} { |
| + hosts := []string{"shop-a.example.com", "shop-b.example.com", "shop-c.example.com", "shop-d.example.com", "shop-e.example.com"} |
| + for _, h := range hosts { |
| if code, _, out := rawReq(t, http.MethodPost, u, token, |
| fmt.Sprintf(`{"host":%q,"region":"ie"}`, h)); code != http.StatusCreated { |
| t.Fatalf("create %s = %d %s", h, code, out) |
| } |
| } |
| + // What the producers put in Detail and Refused: another tenant's host |
| + // and unit (internal/host/egress_source.go's boxWhollyNested reason) |
| + // and the box's own address with its prefix (instancectl's isLocal |
| + // refusal). |
| + const otherTenant = "blog-z.example.org runs as carlos-named@blog-z.service, which runs as the agent's user" |
| + const localRefusal = "10.0.9.7: an address of this box (10.0.9.0/24)" |
| fresh := status.CollectBox(nil, status.Probes{MachineID: "box1"}, time.Now()) |
| fresh.Metrics = &status.BoxMetrics{Egress: map[string]status.HostEgress{ |
| - "shop-a.example.com": {Allowed: []string{"10.0.5.20", "fd00:5::20"}, State: "failing"}, |
| + "shop-a.example.com": {Allowed: []string{"10.0.5.20", "fd00:5::20"}, State: "failing", |
| + Detail: "restarted to close 10.0.5.21", Refused: []string{localRefusal}}, |
| + "shop-b.example.com": {State: "not_nested", Detail: otherTenant, Refused: []string{localRefusal}}, |
| + "shop-d.example.com": {Allowed: []string{"10.0.5.20"}, Refused: []string{localRefusal}}, |
| + "shop-e.example.com": {Allowed: []string{"10.0.5.20"}}, |
| }} |
| if err := status.WriteBox(context.Background(), fx.rel, fresh); err != nil { |
| t.Fatal(err) |
| @@ -2673,6 +2686,11 @@ func TestAPIInstancesJoinsEgress(t *testing.T) { |
| if code != http.StatusOK { |
| t.Fatalf("list = %d %s", code, out) |
| } |
| + for _, leak := range []string{"blog-z", "carlos-named", "10.0.9.", "10.0.5.21", "conflicts", "box-b", "refused", "detail"} { |
| + if strings.Contains(out, leak) { |
| + t.Fatalf("a member's list carries %q: %s", leak, out) |
| + } |
| + } |
| var list struct { |
| Instances []struct { |
| Host string `json:"host"` |
| @@ -2682,13 +2700,13 @@ func TestAPIInstancesJoinsEgress(t *testing.T) { |
| if err := json.Unmarshal([]byte(out), &list); err != nil { |
| t.Fatalf("list body %s: %v", out, err) |
| } |
| - if strings.Contains(out, "conflicts") || strings.Contains(out, "box-b") { |
| - t.Fatalf("a member's list names a fleet-address finding: %s", out) |
| - } |
| + notAvailable := fmt.Sprintf(`"state":%q,"reason":%q}`, memberEgressNotAvailable, memberEgressNotAvailableReason) |
| want := map[string]string{ |
| - "shop-a.example.com": `{"allowed":["10.0.5.20","fd00:5::20"],"state":"failing"}`, |
| - "shop-b.example.com": ``, |
| + "shop-a.example.com": fmt.Sprintf(`{"allowed":["10.0.5.20","fd00:5::20"],"state":%q,"reason":%q}`, memberEgressFailing, memberEgressFailingReason), |
| + "shop-b.example.com": `{` + notAvailable, |
| "shop-c.example.com": ``, |
| + "shop-d.example.com": `{"allowed":["10.0.5.20"],` + notAvailable, |
| + "shop-e.example.com": `{"allowed":["10.0.5.20"]}`, |
| } |
| if len(list.Instances) != len(want) { |
| t.Fatalf("instances = %s", out) |
| @@ -2700,6 +2718,39 @@ func TestAPIInstancesJoinsEgress(t *testing.T) { |
| } |
| } |
| +// TestMemberEgressViewStates pins the member projection's state words: root's |
| +// failing and pending keep theirs, every other state and a refusal alone |
| +// are not_available, and nothing but the held addresses is copied. |
| +func TestMemberEgressViewStates(t *testing.T) { |
| + t.Parallel() |
| + if memberEgressView(status.HostEgress{Allowed: []string{"10.0.5.20"}}, false) != nil { |
| + t.Fatal("no report, yet a view") |
| + } |
| + for _, c := range []struct { |
| + in status.HostEgress |
| + state string |
| + }{ |
| + {status.HostEgress{Allowed: []string{"10.0.5.20"}}, ""}, |
| + {status.HostEgress{State: "failing", Detail: "restarted to close 10.0.5.21"}, memberEgressFailing}, |
| + {status.HostEgress{State: "pending"}, memberEgressPending}, |
| + {status.HostEgress{State: "unreadable"}, memberEgressNotAvailable}, |
| + {status.HostEgress{State: "unverifiable"}, memberEgressNotAvailable}, |
| + {status.HostEgress{State: "expired"}, memberEgressNotAvailable}, |
| + {status.HostEgress{State: "not_nested", Detail: "x"}, memberEgressNotAvailable}, |
| + {status.HostEgress{State: "under_claims", Detail: "not yet supported under claims"}, memberEgressNotAvailable}, |
| + {status.HostEgress{State: "something_new"}, memberEgressNotAvailable}, |
| + {status.HostEgress{Refused: []string{"10.0.9.7: an address of this box (10.0.9.0/24)"}}, memberEgressNotAvailable}, |
| + } { |
| + got := memberEgressView(c.in, true) |
| + if got.State != c.state || !slices.Equal(got.Allowed, c.in.Allowed) { |
| + t.Errorf("%+v: view %+v, want state %q", c.in, got, c.state) |
| + } |
| + if (got.State == "") != (got.Reason == "") { |
| + t.Errorf("%+v: state %q with reason %q", c.in, got.State, got.Reason) |
| + } |
| + } |
| +} |
| + |
| // TestAPIInstanceCreateReplicaFields pins the create door's replica rails |
| // (#162): role and placement travel together or not at all; a valid replica |
| // create lands role on the record, in the bucket, canonicalized; the home's |