From b79ab250466b6b61eb64a7d75ab9c8248e1368f3 Mon Sep 17 00:00:00 2001 From: Maximilian Rink Date: Mon, 13 Jul 2026 09:53:14 +0200 Subject: [PATCH] Fix CSI controller URL for IPv6 service hosts Build the CSI controller REST endpoint with net.JoinHostPort so IPv6 service hosts from Kubernetes environment variables are bracketed correctly while preserving IPv4 and DNS host behavior. Add unit coverage for IPv6, IPv4, and DNS controller hosts. Fixes #1164 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Maximilian Rink --- main.go | 3 ++- main_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index def5d36bc..d8c8701a5 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "fmt" "math" "math/rand" + "net" "os" "os/signal" "path/filepath" @@ -348,7 +349,7 @@ func controllerRestURL() string { if hostname == "" { hostname = config.ServerCertName } - return "https://" + hostname + ":" + port + return "https://" + net.JoinHostPort(hostname, port) } func main() { diff --git a/main_test.go b/main_test.go index 0610a43ef..121e29c00 100644 --- a/main_test.go +++ b/main_test.go @@ -31,6 +31,46 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestControllerRestURL(t *testing.T) { + testCases := []struct { + name string + host string + port string + expected string + }{ + { + name: "IPv6 address", + host: "2001:db8::1", + port: "34571", + expected: "https://[2001:db8::1]:34571", + }, + { + name: "IPv4 address", + host: "192.0.2.10", + port: "34571", + expected: "https://192.0.2.10:34571", + }, + { + name: "DNS name", + host: "trident-csi", + port: "34571", + expected: "https://trident-csi:34571", + }, + { + name: "defaults", + expected: "https://trident-csi:34571", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("TRIDENT_CSI_SERVICE_HOST", tc.host) + t.Setenv("TRIDENT_CSI_SERVICE_PORT", tc.port) + assert.Equal(t, tc.expected, controllerRestURL()) + }) + } +} + func TestMain_processCommandLineArgs_QPS(t *testing.T) { type parameters struct { k8sApiQPS float64