Skip to content

Commit b2e72e6

Browse files
authored
Merge pull request kubernetes#131462 from BenTheElder/kubectl-proxy-local-skip
kubectl proxy env e2e test, skip when host is localhost
2 parents e2ccbd2 + 540ea59 commit b2e72e6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

test/e2e/kubectl/kubectl.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"net"
2828
"net/http"
2929
"net/http/httptest"
30+
"net/netip"
31+
"net/url"
3032
"os"
3133
"os/exec"
3234
"path"
@@ -251,6 +253,23 @@ func runKubectlRetryOrDie(ns string, args ...string) string {
251253
return output
252254
}
253255

256+
// matches localhost / loopback skip from
257+
// https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config.ProxyFunc
258+
func hostIsLocal(host string) bool {
259+
if host == "localhost" {
260+
return true
261+
}
262+
nip, err := netip.ParseAddr(host)
263+
var ip net.IP
264+
if err == nil {
265+
ip = net.IP(nip.AsSlice())
266+
if ip.IsLoopback() {
267+
return true
268+
}
269+
}
270+
return false
271+
}
272+
254273
var _ = SIGDescribe("Kubectl client", func() {
255274
defer ginkgo.GinkgoRecover()
256275
f := framework.NewDefaultFramework("kubectl")
@@ -467,8 +486,20 @@ var _ = SIGDescribe("Kubectl client", func() {
467486
})
468487

469488
ginkgo.It("should support exec through an HTTP proxy", func(ctx context.Context) {
489+
// testContextHost is a KUBECONFIG URL
470490
testContextHost := getTestContextHost()
471491

492+
// check if testContextHost is on localhost and skip the tests
493+
// proxy env vars are always ignored on localhost and loopback IPs
494+
// https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config.ProxyFunc
495+
// TODO: consider if we can test proxying some other way with local clusters
496+
// https://github.com/kubernetes/kubectl/issues/1655#issuecomment-2829408755
497+
u, err := url.Parse(testContextHost)
498+
framework.ExpectNoError(err, "parsing test context host: %s", testContextHost)
499+
if hostIsLocal(u.Hostname()) {
500+
e2eskipper.Skipf("Test host %q is on localhost and would not be proxied by HTTP_PROXY, skipping test", testContextHost)
501+
}
502+
472503
ginkgo.By("Starting http_proxy")
473504
var proxyLogs bytes.Buffer
474505
testSrv := httptest.NewServer(utilnettesting.NewHTTPProxyHandler(ginkgo.GinkgoTB(), func(req *http.Request) bool {

0 commit comments

Comments
 (0)