From ee2d0604dcf4d1502132f0bf79bc61dd60ee9496 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sun, 29 Nov 2026 00:10:10 +0000 Subject: [PATCH] health: let a Warnable provide a PrimaryAction UnhealthyState.PrimaryAction was only set for health messages from the control plane. Add Warnable.PrimaryAction, which returns an action for the Warnable's Args, so that client Warnables can offer one too. As with control messages, Strings appends the action to the text. Signed-off-by: Nick Khyl --- health/health.go | 13 +++++++++---- health/health_test.go | 33 +++++++++++++++++++++++++++++++++ health/state.go | 14 +++++++++----- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/health/health.go b/health/health.go index 30eb31271..66230d34a 100644 --- a/health/health.go +++ b/health/health.go @@ -285,6 +285,10 @@ type Warnable struct { // on the tray icon when ImpactsConnectivity is set to true and the Warnable is unhealthy. ImpactsConnectivity bool + // PrimaryAction, if non-nil, returns an action the user can take to resolve this Warnable's + // unhealthy state, given its Args, or nil if there is none. + PrimaryAction func(args Args) *UnhealthyStateAction + // TimeToVisible is the Duration that the Warnable has to be in an unhealthy state before it // should be surfaced as unhealthy to the user. This is used to prevent transient errors from being // displayed to the user. @@ -1018,11 +1022,12 @@ func (t *Tracker) stringsLocked() []string { if t.isEffectivelyHealthyLocked(w) { continue } - if ws.Args == nil { - result = append(result, w.Text(Args{})) - } else { - result = append(result, w.Text(ws.Args)) + us := w.unhealthyState(ws) + msg := us.Text + if us.PrimaryAction != nil { + msg = msg + " " + us.PrimaryAction.Label + ": " + us.PrimaryAction.URL } + result = append(result, msg) } warnLen := len(result) diff --git a/health/health_test.go b/health/health_test.go index e027f891c..7da791c00 100644 --- a/health/health_test.go +++ b/health/health_test.go @@ -1183,3 +1183,36 @@ func TestIPForwardingState(t *testing.T) { } }) } +func TestWarnablePrimaryAction(t *testing.T) { + w := Register(&Warnable{ + Code: "test-primary-action", + Text: StaticMessage("Something is wrong."), + PrimaryAction: func(args Args) *UnhealthyStateAction { + if args[ArgError] == "" { + return nil + } + return &UnhealthyStateAction{URL: args[ArgError], Label: "Fix it"} + }, + }) + defer unregister(w) + + ht := NewTracker(eventbustest.NewBus(t)) + ht.SetUnhealthy(w, nil) + if got := ht.CurrentState().Warnings[w.Code].PrimaryAction; got != nil { + t.Errorf("got action %+v; want none", got) + } + noAction := ht.CurrentState().Warnings[w.Code].ETag + + ht.SetUnhealthy(w, Args{ArgError: "http://example.com/"}) + us := ht.CurrentState().Warnings[w.Code] + want := &UnhealthyStateAction{URL: "http://example.com/", Label: "Fix it"} + if !reflect.DeepEqual(us.PrimaryAction, want) { + t.Errorf("got action %+v; want %+v", us.PrimaryAction, want) + } + if us.ETag == noAction { + t.Error("ETag didn't change with the action") + } + if got, want := ht.Strings(), "Something is wrong. Fix it: http://example.com/"; !slices.Contains(got, want) { + t.Errorf("got strings %q; want %q in it", got, want) + } +} diff --git a/health/state.go b/health/state.go index 689876697..2d0bffcfe 100644 --- a/health/state.go +++ b/health/state.go @@ -85,11 +85,14 @@ type UnhealthyStateAction struct { // unhealthyState returns a unhealthyState of the Warnable given its current warningState. func (w *Warnable) unhealthyState(ws *warningState) *UnhealthyState { - var text string - if ws.Args != nil { - text = w.Text(ws.Args) - } else { - text = w.Text(Args{}) + args := ws.Args + if args == nil { + args = Args{} + } + text := w.Text(args) + var action *UnhealthyStateAction + if w.PrimaryAction != nil { + action = w.PrimaryAction(args) } dependsOnWarnableCodes := make([]WarnableCode, len(w.DependsOn), len(w.DependsOn)+1) @@ -112,6 +115,7 @@ func (w *Warnable) unhealthyState(ws *warningState) *UnhealthyState { Args: ws.Args, DependsOn: dependsOnWarnableCodes, ImpactsConnectivity: w.ImpactsConnectivity, + PrimaryAction: action, } }