mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-02 14:42:34 -08:00
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 <[email protected]>
This commit is contained in:
1 parent
e2aa441e81
commit
ee2d0604dc
3 files changed
+51
-9
No files matched your search
+9
-4
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+9
-5
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user