Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add config option for node hostname #43

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 93 additions & 0 deletions module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,99 @@ func Test_GetAuthKey(t *testing.T) {
}
}

func Test_GetControlURL(t *testing.T) {
const nodeName = "node"
tests := map[string]struct {
env map[string]string // env vars to set
defaultURL string // default control_url in caddy config
nodeURL string // node control_url in caddy config
want string
}{
"default empty URL": {
want: "",
},
"custom URL from app config": {
defaultURL: "http://custom.example.com",
want: "http://custom.example.com",
},
"custom URL from node config": {
defaultURL: "xxx",
nodeURL: "http://custom.example.com",
want: "http://custom.example.com",
},
"custom URL from env on app config": {
env: map[string]string{"CONTROL_URL": "http://env.example.com"},
defaultURL: "{env.CONTROL_URL}",
want: "http://env.example.com",
},
"custom URL from env on node config": {
env: map[string]string{"CONTROL_URL": "http://env.example.com"},
defaultURL: "xxx",
nodeURL: "{env.CONTROL_URL}",
want: "http://env.example.com",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
app := &App{
ControlURL: tt.defaultURL,
Nodes: make(map[string]Node),
}
if tt.nodeURL != "" {
app.Nodes[nodeName] = Node{
ControlURL: tt.nodeURL,
}
}
app.Provision(caddy.Context{})
for k, v := range tt.env {
t.Setenv(k, v)
}

got, _ := getControlURL(nodeName, app)
if got != tt.want {
t.Errorf("GetControlURL() = %v, want %v", got, tt.want)
}
})
}
}
func Test_GetHostname(t *testing.T) {
const nodeName = "node"
tests := map[string]struct {
env map[string]string // env vars to set
hostname string // hostname value 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