feature/conn25: use routecheck to pick amongst connectors

Fixes tailscale/corp#47251

Signed-off-by: Fran Bull <fran@tailscale.com>
This commit is contained in:
Fran Bull authored and franbull committed 2026-11-19 09:42:20 -08:00
1 parent cc0716a2f8
commit f6483cc55a
2 files changed
+62 -4

No files matched your search

+23 -3
View File
@@ -970,7 +970,8 @@ func makePeerAPIReq(ctx context.Context, httpClient *http.Client, urlBase string
func (e *extension) pickConnectorURLBase(app appctype.AppConnectorAttr) string {
nb := e.host.NodeBackend()
peers := pickConnector(nb, app)
rp := e.getRouteCheckReport()
peers := pickConnector(nb, app, rp)
for _, p := range peers {
if urlBase := nb.PeerAPIBase(p); urlBase != "" {
return urlBase
@@ -1345,6 +1346,25 @@ func rewriteHTTPSResponse(hdr dnsmessage.Header, questions []dnsmessage.Question
const packetFilterAllowReason = "app connector transit IP"
func (e *extension) getRouteCheckReport() ipnlocal.RouteCheckReport {
b, ok := e.backend.(*ipnlocal.LocalBackend)
if !ok {
return nil
}
hook := ipnlocal.HookRouteCheckReport.GetOrNil()
if hook == nil {
return nil
}
return hook(b)
}
func peerIsReachable(rp ipnlocal.RouteCheckReport, n tailcfg.NodeView) bool {
if rp == nil {
return n.Online().Get()
}
return rp.IsReachable(n.ID()).IsReachable()
}
func isPeerEligibleConnector(peer tailcfg.NodeView) bool {
if !peer.Valid() || !peer.Hostinfo().Valid() {
return false
@@ -1365,13 +1385,13 @@ func sortByPreference(self tailcfg.NodeView, ns []tailcfg.NodeView) {
// pickConnector returns peers the backend knows about that match the app, in order of preference to use as
// a connector.
func pickConnector(nb ipnext.NodeBackend, app appctype.AppConnectorAttr) []tailcfg.NodeView {
func pickConnector(nb ipnext.NodeBackend, app appctype.AppConnectorAttr, routecheckReport ipnlocal.RouteCheckReport) []tailcfg.NodeView {
appTagsSet := set.SetOf(app.Connectors)
matches := nb.AppendMatchingPeers(nil, func(n tailcfg.NodeView) bool {
if !isPeerEligibleConnector(n) {
return false
}
if !n.Online().Get() {
if !peerIsReachable(routecheckReport, n) {
return false
}
for _, t := range n.Tags().All() {
+39 -1
View File
@@ -29,6 +29,7 @@ import (
"tailscale.com/net/dns"
"tailscale.com/net/dns/resolver"
"tailscale.com/net/netmon"
"tailscale.com/net/routecheck"
"tailscale.com/net/tsdial"
"tailscale.com/net/tstun"
"tailscale.com/tailcfg"
@@ -3738,12 +3739,21 @@ func TestPickConnector(t *testing.T) {
return nvWithConnectorSet(id, true, tags...)
}
makeReport := func(ids ...tailcfg.NodeID) *routecheck.Report {
rp := &routecheck.Report{Reachable: routecheck.NodeSet{}}
for _, id := range ids {
rp.Reachable[id] = routecheck.Node{ID: id}
}
return rp
}
self := (&tailcfg.Node{ID: 100}).View()
for _, tt := range []struct {
name string
candidates []tailcfg.NodeView
app appctype.AppConnectorAttr
report ipnlocal.RouteCheckReport
want []tailcfg.NodeView
}{
{
@@ -3855,9 +3865,37 @@ func TestPickConnector(t *testing.T) {
app: exampleApp,
want: []tailcfg.NodeView{nv(2, "tag:example")},
},
{
name: "report-replaces-online",
candidates: []tailcfg.NodeView{nv(1, "tag:example"), nvWithOnlineSet(2, false, "tag:example")},
app: exampleApp,
report: makeReport(2),
want: []tailcfg.NodeView{nvWithOnlineSet(2, false, "tag:example")},
},
{
name: "report-drops-online-not-in-report",
candidates: []tailcfg.NodeView{nv(1, "tag:example"), nv(2, "tag:example")},
app: exampleApp,
report: makeReport(1),
want: []tailcfg.NodeView{nv(1, "tag:example")},
},
{
name: "report-empty-drops-all",
candidates: []tailcfg.NodeView{nv(1, "tag:example"), nv(2, "tag:example")},
app: exampleApp,
report: makeReport(),
want: nil,
},
{
name: "report-multiple-reachable",
candidates: []tailcfg.NodeView{nv(2, "tag:example"), nvWithOnlineSet(1, false, "tag:example")},
app: exampleApp,
report: makeReport(1, 2),
want: []tailcfg.NodeView{nvWithOnlineSet(1, false, "tag:example"), nv(2, "tag:example")},
},
} {
t.Run(tt.name, func(t *testing.T) {
got := pickConnector(&testNodeBackend{self: self, peers: tt.candidates}, tt.app)
got := pickConnector(&testNodeBackend{self: self, peers: tt.candidates}, tt.app, tt.report)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Fatalf("PickConnectors (-want, +got):\n%s", diff)
}