Files
David Bond 87edd6b488 cmd/k8s-operator,k8s-operator: truncate long default egress proxy hostnames (#22081)
An egress Service without the tailscale.com/hostname annotation gets the
default proxy hostname <namespace>-<name>. When that hostname exceeds 63
bytes, the operator rejects the Service. This includes ProxyGroup egress
Services, which do not use the hostname.

For egress Services, shorten a default that is too long to a 54-char
prefix and an 8-char hash. Defaults that fit do not change, so no
existing proxy is renamed. Explicit annotations and ingress hostnames
keep the current validation.

Rename TruncateLabelValue to TruncateWithHash, as it now also truncates
hostnames.

Fixes #22080

Change-Id: Ib1589a5e9c750cd37d0641829a84fabd0c4352a7

Signed-off-by: David Bond <[email protected]>
2026-11-18 13:17:36 +00:00

116 lines
2.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !plan9
package kube
import (
"strings"
"testing"
"tailscale.com/util/dnsname"
)
func TestTruncateWithHash(t *testing.T) {
tests := []struct {
name string
input string
want string // empty means expect input unchanged
}{
{
name: "short-value-unchanged",
input: "my-service",
},
{
name: "exactly-63-chars-unchanged",
input: strings.Repeat("a", 63),
},
{
name: "64-chars-gets-truncated",
input: strings.Repeat("a", 64),
},
{
name: "very-long-value-gets-truncated",
input: "tailscale-nginx-clickhouse-o11y-server-https-with-extra-long-suffix-that-exceeds-limit",
},
{
name: "253-chars-max-k8s-resource-name",
input: strings.Repeat("x", 253),
},
{
name: "empty-string-unchanged",
input: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TruncateWithHash(tt.input)
if len(got) > 63 {
t.Errorf("TruncateWithHash(%q) = %q (len %d), exceeds 63 chars", tt.input, got, len(got))
}
if len(tt.input) <= 63 && got != tt.input {
t.Errorf("TruncateWithHash(%q) = %q, want unchanged input", tt.input, got)
}
if len(tt.input) > 63 && got == tt.input {
t.Errorf("TruncateWithHash(%q) was not truncated", tt.input)
}
})
}
}
func TestTruncateWithHashDeterministic(t *testing.T) {
input := strings.Repeat("a", 100)
first := TruncateWithHash(input)
for range 10 {
got := TruncateWithHash(input)
if got != first {
t.Fatalf("non-deterministic: got %q, want %q", got, first)
}
}
}
func TestTruncateWithHashUniqueness(t *testing.T) {
// Two inputs sharing a long prefix but differing at the end should produce different outputs.
a := strings.Repeat("a", 100) + "-one"
b := strings.Repeat("a", 100) + "-two"
if TruncateWithHash(a) == TruncateWithHash(b) {
t.Errorf("collision: %q and %q produce the same truncated label", a, b)
}
}
func TestTruncateWithHashDNSLabel(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "short-hostname-unchanged",
input: "default-my-service",
want: "default-my-service",
},
{
name: "exactly-63-chars-unchanged",
input: strings.Repeat("a", 63),
want: strings.Repeat("a", 63),
},
{
name: "long-hostname-truncated",
input: "tailscale-external-services-harry1027133059-trefknee01-bee-oqggo-kinfe-0-g",
want: "tailscale-external-services-harry1027133059-trefknee01-0908ae76",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TruncateWithHash(tt.input)
if got != tt.want {
t.Errorf("TruncateWithHash(%q) = %q, want %q", tt.input, got, tt.want)
}
if err := dnsname.ValidLabel(got); err != nil {
t.Errorf("TruncateWithHash(%q) = %q is not a valid DNS label: %v", tt.input, got, err)
}
})
}
}