Skip to content

Commit

Permalink
add config option for node hostname
Browse files Browse the repository at this point in the history
this overrides the name used to refer to the node in the caddy config,
and is mostly useful because it can include environment variables.

Closes #18

Co-authored-by: kdevan <kaidevan@gmail.com>
Signed-off-by: Will Norris <will@tailscale.com>
  • Loading branch information
willnorris and kdevan committed May 9, 2024
1 parent 21b5e30 commit e679b94
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Node struct {
// Ephemeral specifies whether the node should be registered as ephemeral.
Ephemeral bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`

// Hostname is the hostname to use when registering the node.
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`

name string
}

Expand Down Expand Up @@ -137,6 +140,11 @@ func parseNodeConfig(d *caddyfile.Dispenser) (Node, error) {
node.ControlURL = segment.Val()
case "ephemeral":
node.Ephemeral = true
case "hostname":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.Hostname = segment.Val()
default:
return node, segment.Errf("unrecognized subdirective: %s", segment.Val())
}
Expand Down
17 changes: 16 additions & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {

s, _, err := nodes.LoadOrNew(name, func() (caddy.Destructor, error) {
s := &tsnet.Server{
Hostname: name,
Logf: func(format string, args ...any) {
app.logger.Sugar().Debugf(format, args...)
},
Expand All @@ -116,6 +115,9 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {
if s.ControlURL, err = getControlURL(name, app); err != nil {
return nil, err
}
if s.Hostname, err = getHostname(name, app); err != nil {
return nil, err
}

if name != "" {
// Set config directory for tsnet. By default, tsnet will use the name of the
Expand Down Expand Up @@ -182,6 +184,19 @@ func getEphemeral(name string, app *App) bool {
return app.Ephemeral
}

func getHostname(name string, app *App) (string, error) {
if app == nil {
return name, nil
}
if node, ok := app.Nodes[name]; ok {
if node.Hostname != "" {
return repl.ReplaceOrErr(node.Hostname, true, true)
}
}

return name, nil
}

// tailscaleNode is a wrapper around a tsnet.Server that provides a fully self-contained Tailscale node.
// This node can listen on the tailscale network interface, or be used to connect to other nodes in the tailnet.
type tailscaleNode struct {
Expand Down
38 changes: 38 additions & 0 deletions module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ func Test_GetAuthKey(t *testing.T) {
}
}

func Test_GetHostname(t *testing.T) {
const nodeName = "node"
tests := map[string]struct {
env map[string]string // env vars to set
hostname string // host key in caddy config
want string
}{
"default hostname from node name": {
want: nodeName,
},
"custom hostname from node config": {
hostname: "custom",
want: "custom",
},
"custom hostname with env vars": {
env: map[string]string{"REGION": "eu", "ENV": "prod"},
hostname: "custom-{env.REGION}-{env.ENV}",
want: "custom-eu-prod",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
app := &App{Nodes: map[string]Node{
nodeName: {Hostname: tt.hostname},
}}
app.Provision(caddy.Context{})
for k, v := range tt.env {
t.Setenv(k, v)
}

got, _ := getHostname(nodeName, app)
if got != tt.want {
t.Errorf("GetHostname() = %v, want %v", got, tt.want)
}
})
}
}

func Test_Listen(t *testing.T) {
must.Do(caddy.Run(new(caddy.Config)))
ctx := caddy.ActiveContext()
Expand Down

0 comments on commit e679b94

Please sign in to comment.