From 7ce5547382e25708b8abd7d2e059fc03575893d2 Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Thu, 12 Nov 2026 18:16:31 +0000 Subject: [PATCH] cmd/k8s-proxy: listen on the PORT that the operator sets For kube-apiserver ProxyGroups with static endpoints, the operator sets PORT on the k8s-proxy container to the targetPort of the NodePort Services behind the advertised endpoints. k8s-proxy never read PORT, so tsnet listened on a random port and traffic to the static endpoints was forwarded to a port with nothing listening. Peers stayed on DERP. This fixes this by parsing PORT and setting it as tsnet.Server.Port. Thanks to @icheishvili (#18841) and @rquitales (#21716), whose changes this is based on. Updates #21714 Signed-off-by: chaosinthecrd --- cmd/k8s-proxy/k8s-proxy.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/k8s-proxy/k8s-proxy.go b/cmd/k8s-proxy/k8s-proxy.go index b2cdb7c8a..d82c9badc 100644 --- a/cmd/k8s-proxy/k8s-proxy.go +++ b/cmd/k8s-proxy/k8s-proxy.go @@ -80,6 +80,7 @@ func run(logger *zap.SugaredLogger) error { configPath = os.Getenv("TS_K8S_PROXY_CONFIG") podUID = os.Getenv("POD_UID") podIP = os.Getenv("POD_IP") + port = os.Getenv("PORT") ) if configPath == "" { return errors.New("TS_K8S_PROXY_CONFIG unset") @@ -205,11 +206,18 @@ func run(logger *zap.SugaredLogger) error { authKey = *cfg.Parsed.AuthKey } + // PORT is the static endpoint NodePort targetPort; WireGuard must listen on it. + tsPort, err := parsePort(port) + if err != nil { + return err + } + ts := &tsnet.Server{ Logf: logger.Named("tsnet").Debugf, UserLogf: logger.Named("tsnet").Infof, Store: st, AuthKey: authKey, + Port: tsPort, } if cfg.Parsed.ServerURL != nil { @@ -448,6 +456,19 @@ func getLocalAddrPort(addr string, port uint16) string { return net.JoinHostPort(addr, strconv.FormatUint(uint64(port), 10)) } +// parsePort parses the PORT env var. It returns 0 if PORT is unset, so tsnet +// picks a random port. +func parsePort(p string) (uint16, error) { + if p == "" { + return 0, nil + } + port, err := strconv.ParseUint(p, 10, 16) + if err != nil { + return 0, fmt.Errorf("failed to parse PORT %q: %w", p, err) + } + return uint16(port), nil +} + func getStateStore(path *string, logger *zap.SugaredLogger) (ipn.StateStore, error) { p := "mem:" if path != nil {