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

[Release 1.25] Support setting control server URL for Tailscale. #7894

Merged
merged 1 commit into from
Jul 7, 2023
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
2 changes: 1 addition & 1 deletion pkg/agent/flannel/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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 @@ -157,13 +157,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