From 540ea5949f3a25bbb241550754d1d52a89e3011a Mon Sep 17 00:00:00 2001 From: Benjamin Elder Date: Thu, 24 Apr 2025 22:33:29 -0700 Subject: [PATCH] skip kubectl proxy env tests when the host is localhost/loopback and would not be proxied --- test/e2e/kubectl/kubectl.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/e2e/kubectl/kubectl.go b/test/e2e/kubectl/kubectl.go index 504f1d42d43..1e6a5077dde 100644 --- a/test/e2e/kubectl/kubectl.go +++ b/test/e2e/kubectl/kubectl.go @@ -27,6 +27,8 @@ import ( "net" "net/http" "net/http/httptest" + "net/netip" + "net/url" "os" "os/exec" "path" @@ -251,6 +253,23 @@ func runKubectlRetryOrDie(ns string, args ...string) string { return output } +// matches localhost / loopback skip from +// https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config.ProxyFunc +func hostIsLocal(host string) bool { + if host == "localhost" { + return true + } + nip, err := netip.ParseAddr(host) + var ip net.IP + if err == nil { + ip = net.IP(nip.AsSlice()) + if ip.IsLoopback() { + return true + } + } + return false +} + var _ = SIGDescribe("Kubectl client", func() { defer ginkgo.GinkgoRecover() f := framework.NewDefaultFramework("kubectl") @@ -467,8 +486,20 @@ var _ = SIGDescribe("Kubectl client", func() { }) ginkgo.It("should support exec through an HTTP proxy", func(ctx context.Context) { + // testContextHost is a KUBECONFIG URL testContextHost := getTestContextHost() + // check if testContextHost is on localhost and skip the tests + // proxy env vars are always ignored on localhost and loopback IPs + // https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config.ProxyFunc + // TODO: consider if we can test proxying some other way with local clusters + // https://github.com/kubernetes/kubectl/issues/1655#issuecomment-2829408755 + u, err := url.Parse(testContextHost) + framework.ExpectNoError(err, "parsing test context host: %s", testContextHost) + if hostIsLocal(u.Hostname()) { + e2eskipper.Skipf("Test host %q is on localhost and would not be proxied by HTTP_PROXY, skipping test", testContextHost) + } + ginkgo.By("Starting http_proxy") var proxyLogs bytes.Buffer testSrv := httptest.NewServer(utilnettesting.NewHTTPProxyHandler(ginkgo.GinkgoTB(), func(req *http.Request) bool {