Skip to content

Commit efdfd54

Browse files
authored
cmd/k8s-operator: avoid port collision with metrics endpoint (tailscale#14185)
When the operator enables metrics on a proxy, it uses the port 9001, and in the near future it will start using 9002 for the debug endpoint as well. Make sure we don't choose ports from a range that includes 9001 so that we never clash. Setting TS_SOCKS5_SERVER, TS_HEALTHCHECK_ADDR_PORT, TS_OUTBOUND_HTTP_PROXY_LISTEN, and PORT could also open arbitrary ports, so we will need to document that users should not choose ports from the 10000-11000 range for those settings. Updates tailscale#13406 Signed-off-by: Tom Proctor <[email protected]>
1 parent 9f9063e commit efdfd54

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

cmd/k8s-operator/egress-services.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ const (
5151
labelSvcType = "tailscale.com/svc-type" // ingress or egress
5252
typeEgress = "egress"
5353
// maxPorts is the maximum number of ports that can be exposed on a
54-
// container. In practice this will be ports in range [3000 - 4000). The
54+
// container. In practice this will be ports in range [10000 - 11000). The
5555
// high range should make it easier to distinguish container ports from
5656
// the tailnet target ports for debugging purposes (i.e when reading
57-
// netfilter rules). The limit of 10000 is somewhat arbitrary, the
57+
// netfilter rules). The limit of 1000 is somewhat arbitrary, the
5858
// assumption is that this would not be hit in practice.
59-
maxPorts = 10000
59+
maxPorts = 1000
6060

6161
indexEgressProxyGroup = ".metadata.annotations.egress-proxy-group"
6262
)
@@ -254,7 +254,7 @@ func (esr *egressSvcsReconciler) provision(ctx context.Context, proxyGroupName s
254254
if !found {
255255
// Calculate a free port to expose on container and add
256256
// a new PortMap to the ClusterIP Service.
257-
if usedPorts.Len() == maxPorts {
257+
if usedPorts.Len() >= maxPorts {
258258
// TODO(irbekrm): refactor to avoid extra reconciles here. Low priority as in practice,
259259
// the limit should not be hit.
260260
return nil, false, fmt.Errorf("unable to allocate additional ports on ProxyGroup %s, %d ports already used. Create another ProxyGroup or open an issue if you believe this is unexpected.", proxyGroupName, maxPorts)
@@ -548,13 +548,13 @@ func svcNameBase(s string) string {
548548
}
549549
}
550550

551-
// unusedPort returns a port in range [3000 - 4000). The caller must ensure that
552-
// usedPorts does not contain all ports in range [3000 - 4000).
551+
// unusedPort returns a port in range [10000 - 11000). The caller must ensure that
552+
// usedPorts does not contain all ports in range [10000 - 11000).
553553
func unusedPort(usedPorts sets.Set[int32]) int32 {
554554
foundFreePort := false
555555
var suggestPort int32
556556
for !foundFreePort {
557-
suggestPort = rand.Int32N(maxPorts) + 3000
557+
suggestPort = rand.Int32N(maxPorts) + 10000
558558
if !usedPorts.Has(suggestPort) {
559559
foundFreePort = true
560560
}

0 commit comments

Comments
 (0)