Skip to content

Commit

Permalink
Merge pull request #7807 from dennwc/headscale
Browse files Browse the repository at this point in the history
Support setting control server URL for Tailscale
  • Loading branch information
manuelbuil authored Jul 7, 2023
2 parents 4ab01f3 + b9a2bf1 commit 6121e8c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/agent/flannel/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (

tailscaledBackend = `{
"Type": "extension",
"PostStartupCommand": "tailscale up --accept-routes --advertise-routes=%Routes%",
"PostStartupCommand": "tailscale set --accept-routes --advertise-routes=%Routes%",
"ShutdownCommand": "tailscale down"
}`

Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ var (
}
VPNAuth = &cli.StringFlag{
Name: "vpn-auth",
Usage: "(agent/networking) (experimental) Credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>",
Usage: "(agent/networking) (experimental) Credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>[,controlServerURL=<url>]",
EnvVar: version.ProgramUpper + "_VPN_AUTH",
Destination: &AgentConfig.VPNAuth,
}
VPNAuthFile = &cli.StringFlag{
Name: "vpn-auth-file",
Usage: "(agent/networking) (experimental) File containing credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>",
Usage: "(agent/networking) (experimental) File containing credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>[,controlServerURL=<url>]",
EnvVar: version.ProgramUpper + "_VPN_AUTH_FILE",
Destination: &AgentConfig.VPNAuthFile,
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"strings"

"github.com/k3s-io/k3s/pkg/util"
Expand Down Expand Up @@ -31,8 +32,9 @@ type VPNInfo struct {

// vpnCliAuthInfo includes auth information of the VPN. It is a general struct in case we want to add more vpn integrations
type vpnCliAuthInfo struct {
Name string
JoinKey string
Name string
JoinKey string
ControlServerURL string
}

// StartVPN starts the VPN interface. General function in case we want to add more vpn integrations
Expand All @@ -45,7 +47,13 @@ func StartVPN(vpnAuthConfigFile string) error {
logrus.Infof("Starting VPN: %s", authInfo.Name)
switch authInfo.Name {
case "tailscale":
output, err := util.ExecCommand("tailscale", []string{"up", "--authkey", authInfo.JoinKey, "--reset"})
args := []string{
"up", "--authkey", authInfo.JoinKey, "--timeout=30s", "--reset",
}
if authInfo.ControlServerURL != "" {
args = append(args, "--login-server", authInfo.ControlServerURL)
}
output, err := util.ExecCommand("tailscale", args)
if err != nil {
return errors.Wrap(err, "tailscale up failed: "+output)
}
Expand Down Expand Up @@ -80,6 +88,8 @@ func getVPNAuthInfo(vpnAuth string) (vpnCliAuthInfo, error) {
authInfo.Name = vpnKeyValue[1]
case "joinKey":
authInfo.JoinKey = vpnKeyValue[1]
case "controlServerURL":
authInfo.ControlServerURL = vpnKeyValue[1]
default:
return vpnCliAuthInfo{}, fmt.Errorf("VPN Error. The passed VPN auth info includes an unknown parameter: %v", vpnKeyValue[0])
}
Expand All @@ -97,6 +107,11 @@ func isVPNConfigOK(authInfo vpnCliAuthInfo) error {
if authInfo.JoinKey == "" {
return errors.New("VPN Error. Tailscale requires a JoinKey")
}
if authInfo.ControlServerURL != "" {
if _, err := url.Parse(authInfo.ControlServerURL); err != nil {
return fmt.Errorf("VPN Error. Invalid control server URL for Tailscale: %w", err)
}
}
return nil
}

Expand Down

0 comments on commit 6121e8c

Please sign in to comment.