Skip to content

Commit

Permalink
make the token and control urls configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Davor Gajic committed Oct 16, 2024
1 parent ff5f233 commit 7f9120a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/k8s-operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func initTSNet(zlog *zap.SugaredLogger) (*tsnet.Server, *tailscale.Client) {
var (
clientIDPath = defaultEnv("CLIENT_ID_FILE", "")
clientSecretPath = defaultEnv("CLIENT_SECRET_FILE", "")
tokenURL = defaultEnv("TOKEN_URL", "https://login.tailscale.com/api/v2/oauth/token")
controlURL = defaultEnv("CONTROL_URL", "")
hostname = defaultEnv("OPERATOR_HOSTNAME", "tailscale-operator")
kubeSecret = defaultEnv("OPERATOR_SECRET", "")
operatorTags = defaultEnv("OPERATOR_INITIAL_TAGS", "tag:k8s-operator")
Expand All @@ -140,15 +142,19 @@ func initTSNet(zlog *zap.SugaredLogger) (*tsnet.Server, *tailscale.Client) {
credentials := clientcredentials.Config{
ClientID: string(clientID),
ClientSecret: string(clientSecret),
TokenURL: "https://login.tailscale.com/api/v2/oauth/token",
TokenURL: tokenURL,
}
tsClient := tailscale.NewClient("-", nil)
if controlURL != "" {
tsClient.BaseURL = controlURL
}
tsClient.UserAgent = "tailscale-k8s-operator"
tsClient.HTTPClient = credentials.Client(context.Background())

s := &tsnet.Server{
Hostname: hostname,
Logf: zlog.Named("tailscaled").Debugf,
ControlURL: controlURL,
Hostname: hostname,
Logf: zlog.Named("tailscaled").Debugf,
}
if kubeSecret != "" {
st, err := kubestore.New(logger.Discard, kubeSecret)
Expand Down Expand Up @@ -271,6 +277,7 @@ func runReconcilers(opts reconcilerOpts) {
proxyImage: opts.proxyImage,
proxyPriorityClassName: opts.proxyPriorityClassName,
tsFirewallMode: opts.proxyFirewallMode,
controlUrl: opts.tsServer.ControlURL,
}
err = builder.
ControllerManagedBy(mgr).
Expand Down
11 changes: 11 additions & 0 deletions cmd/k8s-operator/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type tailscaleSTSConfig struct {
Hostname string
Tags []string // if empty, use defaultTags

ControlURL string
// Connector specifies a configuration of a Connector instance if that's
// what this StatefulSet should be created for.
Connector *connector
Expand Down Expand Up @@ -150,6 +151,7 @@ type tailscaleSTSReconciler struct {
proxyImage string
proxyPriorityClassName string
tsFirewallMode string
controlUrl string
}

func (sts tailscaleSTSReconciler) validate() error {
Expand Down Expand Up @@ -186,6 +188,10 @@ func (a *tailscaleSTSReconciler) Provision(ctx context.Context, logger *zap.Suga
}
sts.ProxyClass = proxyClass

if a.controlUrl != "" {
sts.ControlURL = a.controlUrl
}

secretName, tsConfigHash, configs, err := a.createOrGetSecret(ctx, logger, sts, hsvc)
if err != nil {
return nil, fmt.Errorf("failed to create or get API key secret: %w", err)
Expand Down Expand Up @@ -830,6 +836,11 @@ func tailscaledConfig(stsC *tailscaleSTSConfig, newAuthkey string, oldSecret *co
}
conf.AuthKey = key
}

if stsC.ControlURL != "" {
conf.ServerURL = &stsC.ControlURL
}

capVerConfigs := make(map[tailcfg.CapabilityVersion]ipn.ConfigVAlpha)
capVerConfigs[95] = *conf
// legacy config should not contain NoStatefulFiltering field.
Expand Down

0 comments on commit 7f9120a

Please sign in to comment.