mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-26 11:36:26 -08:00
ipn/ipnlocal: update netstack's active Services on every prefs change (#22517)
A config file reload sets prefs through setPrefsLocked, but netstack's set of active Tailscale Services was only updated from onEditPrefsLocked and setNetMapLocked. After the operator changed AdvertiseServices in a ProxyGroup config Secret, netstack kept the old set until the next netmap. The node still served a withdrawn Service locally instead of sending that traffic to another host, and did not serve a newly added one. Move the update into setPrefsLocked so every path that changes prefs applies it, and add a netstack test that exercises ReloadConfig. Fixes #22498 Signed-off-by: Brendan Creane <[email protected]>
This commit is contained in:
1 parent
0422c46eb0
commit
fcef8e2384
2 files changed
+102
-6
No files matched your search
@@ -5725,12 +5725,6 @@ func (b *LocalBackend) onEditPrefsLocked(_ ipnauth.Actor, mp *ipn.MaskedPrefs, o
|
||||
}
|
||||
}
|
||||
|
||||
if mp.AdvertiseServicesSet {
|
||||
if ns, ok := b.sys.Netstack.GetOK(); ok {
|
||||
ns.UpdateActiveVIPServices(newPrefs.AdvertiseServices())
|
||||
}
|
||||
}
|
||||
|
||||
// This is recorded here in the EditPrefs path, not the setPrefs path on purpose.
|
||||
// recordForEdit records metrics related to edits and changes, not the final state.
|
||||
// If, in the future, we want to record gauge-metrics related to the state of prefs,
|
||||
@@ -5946,6 +5940,15 @@ func (b *LocalBackend) setPrefsLocked(newp *ipn.Prefs) ipn.PrefsView {
|
||||
b.updateWarnSync(prefs)
|
||||
b.updateNoSNATExitNodeWarning(prefs)
|
||||
|
||||
// Netstack handles traffic to a Tailscale Service IP locally only when
|
||||
// this node advertises that Service. Update its set here so that every
|
||||
// path that changes prefs keeps it current, including config file reloads.
|
||||
if buildfeatures.HasServe && (!oldp.Valid() || !views.SliceEqual(oldp.AdvertiseServices(), prefs.AdvertiseServices())) {
|
||||
if ns, ok := b.sys.Netstack.GetOK(); ok {
|
||||
ns.UpdateActiveVIPServices(prefs.AdvertiseServices())
|
||||
}
|
||||
}
|
||||
|
||||
if oldp.ShieldsUp() != newp.ShieldsUp || hostInfoChanged {
|
||||
b.doSetHostinfoFilterServicesLocked()
|
||||
}
|
||||
|
||||
@@ -5,10 +5,13 @@ package netstack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -24,7 +27,9 @@ import (
|
||||
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
|
||||
"gvisor.dev/gvisor/pkg/waiter"
|
||||
"tailscale.com/envknob"
|
||||
"tailscale.com/feature/buildfeatures"
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/conffile"
|
||||
"tailscale.com/ipn/ipnlocal"
|
||||
"tailscale.com/ipn/store/mem"
|
||||
"tailscale.com/metrics"
|
||||
@@ -33,6 +38,7 @@ import (
|
||||
"tailscale.com/net/tsaddr"
|
||||
"tailscale.com/net/tsdial"
|
||||
"tailscale.com/net/tstun"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tsd"
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/types/ipproto"
|
||||
@@ -109,9 +115,19 @@ func getMemStats() (ms runtime.MemStats) {
|
||||
}
|
||||
|
||||
func makeNetstack(tb testing.TB, config func(*Impl)) *Impl {
|
||||
return makeNetstackWithSys(tb, nil, config)
|
||||
}
|
||||
|
||||
// makeNetstackWithSys is like makeNetstack, but calls configSys on the
|
||||
// tsd.System before the LocalBackend is created, so tests can set fields
|
||||
// like InitialConfig that NewLocalBackend reads.
|
||||
func makeNetstackWithSys(tb testing.TB, configSys func(*tsd.System), config func(*Impl)) *Impl {
|
||||
tunDev := tstun.NewFake()
|
||||
sys := tsd.NewSystem()
|
||||
sys.Set(new(mem.Store))
|
||||
if configSys != nil {
|
||||
configSys(sys)
|
||||
}
|
||||
dialer := new(tsdial.Dialer)
|
||||
logf := tstest.WhileTestRunningLogger(tb)
|
||||
eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{
|
||||
@@ -2234,3 +2250,80 @@ func TestInjectLoopback(t *testing.T) {
|
||||
t.Errorf("got %q, want %q", got, "loopback test")
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadConfigUpdatesActiveVIPServices verifies that a change to
|
||||
// AdvertiseServices applied through a config file reload reaches netstack's
|
||||
// set of active Tailscale Services, not only the prefs.
|
||||
//
|
||||
// Regression test for tailscale/tailscale#22498.
|
||||
func TestReloadConfigUpdatesActiveVIPServices(t *testing.T) {
|
||||
if !buildfeatures.HasServe {
|
||||
t.Skip("netstack only tracks active Services when serve is built in")
|
||||
}
|
||||
const svcA, svcB = "svc:a", "svc:b"
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "tailscale.conf")
|
||||
writeConf := func(services ...string) {
|
||||
t.Helper()
|
||||
raw, err := json.Marshal(ipn.ConfigVAlpha{
|
||||
Version: "alpha0",
|
||||
AdvertiseServices: services,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, raw, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Start with no Services advertised. The initial config is applied at
|
||||
// construction without going through setPrefsLocked, so netstack's set is
|
||||
// empty at boot regardless; every Service below is added by a reload.
|
||||
writeConf()
|
||||
initial, err := conffile.Load(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ns := makeNetstackWithSys(t, func(sys *tsd.System) {
|
||||
sys.InitialConfig = initial
|
||||
}, nil)
|
||||
|
||||
active := func() []string {
|
||||
var got []string
|
||||
for s := range ns.atomicActiveVIPServices.Load() {
|
||||
got = append(got, s.String())
|
||||
}
|
||||
return got
|
||||
}
|
||||
isActive := func(name string) bool {
|
||||
return ns.atomicActiveVIPServices.Load().Contains(tailcfg.ServiceName(name))
|
||||
}
|
||||
reload := func(services ...string) {
|
||||
t.Helper()
|
||||
writeConf(services...)
|
||||
if ok, err := ns.lb.ReloadConfig(); !ok || err != nil {
|
||||
t.Fatalf("ReloadConfig() = %v, %v; want true, nil", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add a Service.
|
||||
reload(svcA)
|
||||
if !isActive(svcA) {
|
||||
t.Errorf("after adding %q, active services = %v; want it active", svcA, active())
|
||||
}
|
||||
|
||||
// Swap the advertised Service from A to B. This checks that a reload
|
||||
// removes a Service that an earlier reload added.
|
||||
reload(svcB)
|
||||
if isActive(svcA) || !isActive(svcB) {
|
||||
t.Errorf("after swapping to %q, active services = %v; want only %q", svcB, active(), svcB)
|
||||
}
|
||||
|
||||
// Withdraw every Service.
|
||||
reload()
|
||||
if got := active(); len(got) != 0 {
|
||||
t.Errorf("after withdrawing all services, active services = %v; want none", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user