control/controlclient: replace a status func with Observer interface

For now the method has only one interface (the same as the func it's
replacing) but it will grow, eventually with the goal to remove the
controlclient.Status type for most purposes.

Updates #1909

Change-Id: I715c8bf95e3f5943055a94e76af98d988558a2f2
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2023-08-28 15:27:39 -07:00
committed by Brad Fitzpatrick
parent a64593d7ef
commit 55bb7314f2
5 changed files with 34 additions and 21 deletions

View File

@@ -888,9 +888,9 @@ func (b *LocalBackend) SetDecompressor(fn func() (controlclient.Decompressor, er
b.newDecompressor = fn
}
// setClientStatus is the callback invoked by the control client whenever it posts a new status.
// SetControlClientStatus is the callback invoked by the control client whenever it posts a new status.
// Among other things, this is where we update the netmap, packet filters, DNS and DERP maps.
func (b *LocalBackend) setClientStatus(st controlclient.Status) {
func (b *LocalBackend) SetControlClientStatus(st controlclient.Status) {
// The following do not depend on any data for which we need to lock b.
if st.Err != nil {
// TODO(crawshaw): display in the UI.
@@ -947,7 +947,7 @@ func (b *LocalBackend) setClientStatus(st controlclient.Status) {
// Call ourselves with the current status again; the logic in
// setClientStatus will take care of updating the expired field
// of peers in the netmap.
b.setClientStatus(st)
b.SetControlClientStatus(st)
})
}
}
@@ -1457,7 +1457,7 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
OnClientVersion: b.onClientVersion,
OnControlTime: b.em.onControlTime,
Dialer: b.Dialer(),
Status: b.setClientStatus,
Observer: b,
C2NHandler: http.HandlerFunc(b.handleC2N),
DialPlan: &b.dialPlan, // pointer because it can't be copied

View File

@@ -29,6 +29,12 @@ import (
"tailscale.com/util/must"
)
type observerFunc func(controlclient.Status)
func (f observerFunc) SetControlClientStatus(s controlclient.Status) {
f(s)
}
func fakeControlClient(t *testing.T, c *http.Client) *controlclient.Auto {
hi := hostinfo.New()
ni := tailcfg.NetInfo{LinkType: "wired"}
@@ -43,7 +49,7 @@ func fakeControlClient(t *testing.T, c *http.Client) *controlclient.Auto {
},
HTTPTestClient: c,
NoiseTestClient: c,
Status: func(controlclient.Status) {},
Observer: observerFunc(func(controlclient.Status) {}),
}
cc, err := controlclient.NewNoStart(opts)

View File

@@ -162,7 +162,7 @@ func (cc *mockControl) send(err error, url string, loginFinished bool, nm *netma
cc.authBlocked = false
cc.mu.Unlock()
}
if cc.opts.Status != nil {
if cc.opts.Observer != nil {
pv := cc.persist.View()
s := controlclient.Status{
URL: url,
@@ -175,7 +175,7 @@ func (cc *mockControl) send(err error, url string, loginFinished bool, nm *netma
} else if url == "" && err == nil && nm == nil {
s.LogoutFinished = &empty.Message{}
}
cc.opts.Status(s)
cc.opts.Observer.SetControlClientStatus(s)
}
}