cmd/tailscale/cli: export advertised Services without serve config

get-config left them out of --all, and for --service wrote {}, which
set-config read as the legacy format. Write them with "endpoints": {}.

Updates #18381

Signed-off-by: Kristoffer Dalby <[email protected]>
This commit is contained in:
Kristoffer Dalby committed 2026-11-18 14:27:17 +00:00
1 parent a9caaf54a4
commit d581b22e15
4 files changed
+202 -12

No files matched your search

@@ -53,6 +53,60 @@ func TestServeConfigExistingFiles(t *testing.T) {
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://DB.Example.com:5432"}}}}`,
means: []string{"--service=svc:foo --tcp=5432 tcp://DB.Example.com:5432"},
},
// Advertisement.
{
name: "advertised_false",
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false}}}`,
means: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"drain svc:foo",
},
},
{
name: "advertised_true_is_default",
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": true}}}`,
means: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
},
{
name: "no_endpoints",
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`,
means: []string{"advertise svc:foo"},
},
{
name: "no_endpoints_not_advertised",
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}, "advertised": false}}}`,
export: `{"version": "0.0.1"}`,
},
{
name: "no_services",
file: `{"version": "0.0.1", "services": {}}`,
export: `{"version": "0.0.1"}`,
},
{
name: "version_only",
file: `{"version": "0.0.1"}`,
},
// One Service.
{
name: "service",
flag: "--service=svc:foo",
file: `{"version": "0.0.1", "endpoints": {"tcp:443": "https://localhost:8443"}}`,
means: []string{"--service=svc:foo --https=443 https://localhost:8443"},
},
{
name: "service_no_endpoints",
flag: "--service=svc:foo",
file: `{"version": "0.0.1", "endpoints": {}}`,
means: []string{"advertise svc:foo"},
},
{
name: "service_no_endpoints_not_advertised",
flag: "--service=svc:foo",
file: `{"version": "0.0.1", "endpoints": {}, "advertised": false}`,
},
})
}
@@ -0,0 +1,89 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package cli
import (
"strings"
"testing"
)
// TestServeConfigMixed runs sequences that mix tailscale serve commands and
// config files, and checks each lands on the state the commands in want
// build, in state and in every read-out. A step is one of:
//
// "<args>" tailscale serve <args>
// "set-config <flag> <json>" set-config <flag> of that file
// "set-config <flag> $FROM" set-config <flag> of what get-config <flag> writes on the from node
// "set-config --all $FROM_STATUS" set-config --all of what serve status --json writes on the from node
func TestServeConfigMixed(t *testing.T) {
tests := []struct {
name string
from []string // tailscale serve commands that build the from node
steps []string
want []string // tailscale serve commands that build the same state
}{
// One node's export applied over another's state. See
// TestServeConfigRoundTrip for --all.
{
name: "export_of_advertised_service_without_config",
from: []string{"advertise svc:foo"},
steps: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"set-config --service=svc:foo $FROM",
},
want: []string{"advertise svc:foo"},
},
// set-config reads serve status --json as the legacy format. It
// carries what the config file format cannot, but has no advertised
// state: every service it names comes back advertised.
{
name: "status_json_loses_drain",
from: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"drain svc:foo",
},
steps: []string{"set-config --all $FROM_STATUS"},
want: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
},
{
name: "status_json_loses_advertised_without_config",
from: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"advertise svc:bar",
},
steps: []string{"set-config --all $FROM_STATUS"},
want: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
from := buildState(t, dir, tt.from)
got := newServiceHostClient()
for _, step := range tt.steps {
verb, rest, _ := strings.Cut(step, " ")
if verb != "set-config" {
runServeCmd(t, got, cmd(step)...)
continue
}
flag, file, _ := strings.Cut(rest, " ")
switch file {
case "$FROM":
file = runServeCmd(t, from, "get-config", flag)
case "$FROM_STATUS":
// set-config warns that serve status --json is deprecated
// as input; the warning is pinned in TestRunServeSetConfig.
if _, stderr, err := serveRun(got, "set-config", flag, writeTmpServeConfig(t, runServeCmd(t, from, "status", "--json"))); err != nil {
t.Fatalf("step %q: %v\n%s", step, err, stderr)
}
continue
}
setConfig(t, got, flag, file)
}
assertSameState(t, buildState(t, dir, tt.want), got)
})
}
}
@@ -22,6 +22,19 @@ import (
"tailscale.com/tailcfg"
)
// The tests in this file, serve_config_files_test.go and
// serve_config_mixed_test.go treat a node's Services (its ipn.ServeConfig
// Services plus which of them it advertises) as the state, and check that
// every way in and out of it agrees:
//
// tailscale serve ... -> state <- set-config <file>
// state -> get-config (the file)
// state -> serve status --json
//
// Each row of TestServeConfigRoundTrip is one state spelled three ways side by
// side: the tailscale serve commands that build it, the file get-config writes
// for it, and the state itself.
// TestServeConfigRoundTrip checks, for each row, that the commands build the
// state; that the file builds it with set-config, on a fresh node and over one
// serving something else; that get-config writes the file back whichever way
@@ -70,6 +83,33 @@ func TestServeConfigRoundTrip(t *testing.T) {
}},
advertised: []string{"svc:foo"},
},
// Advertisement.
{
name: "drained",
cli: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"drain svc:foo",
},
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false}}}`,
services: services{"svc:foo": {TCP: tcp{5432: {TCPForward: "localhost:5432"}}}},
},
{
name: "advertised_without_config",
cli: []string{"advertise svc:foo"},
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`,
advertised: []string{"svc:foo"},
},
{
// Turning off a Service's last port leaves it advertised.
name: "last_port_turned_off",
cli: []string{
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
"--service=svc:foo --tcp=5432 off",
},
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`,
advertised: []string{"svc:foo"},
},
}
for _, tt := range tests {
+19 -12
View File
@@ -686,7 +686,8 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
magicDNSSuffix := st.CurrentTailnet.MagicDNSSuffix
handleService := func(svcName tailcfg.ServiceName, serviceConfig *ipn.ServiceConfig) (*conffile.ServiceDetailsFile, error) {
var sdf conffile.ServiceDetailsFile
// "endpoints" is required, so a Service without any writes {}.
sdf := conffile.ServiceDetailsFile{Endpoints: map[*tailcfg.ProtoPortRange]*conffile.Target{}}
// Leave unset for true case since that's the default.
if !advertised.Contains(svcName.String()) {
sdf.Advertised.Set(false)
@@ -793,6 +794,12 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
} else if e.allServices {
var scf conffile.ServicesConfigFile
scf.Version = "0.0.1"
// An advertised Service without serve config is still state to write.
for _, svc := range prefs.AdvertiseServices {
if _, ok := sc.Services[tailcfg.ServiceName(svc)]; !ok {
mak.Set(&sc.Services, tailcfg.ServiceName(svc), new(ipn.ServiceConfig))
}
}
for svcName, serviceConfig := range sc.Services {
sdf, err := handleService(svcName, serviceConfig)
if err != nil {
@@ -807,17 +814,17 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
} else if forSingleService {
serviceConfig, ok := sc.Services[e.service]
if !ok {
j = []byte("{}")
} else {
sdf, err := handleService(e.service, serviceConfig)
if err != nil {
return err
}
sdf.Version = "0.0.1"
j, err = json.MarshalIndent(sdf, "", " ")
if err != nil {
return err
}
// No serve config is still a state the file can say.
serviceConfig = new(ipn.ServiceConfig)
}
sdf, err := handleService(e.service, serviceConfig)
if err != nil {
return err
}
sdf.Version = "0.0.1"
j, err = json.MarshalIndent(sdf, "", " ")
if err != nil {
return err
}
} else {
return errors.New("must specify either --service=svc:<service-name> or --all")