Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore localhost proxy started with scheme. #8885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions cmd/minikube/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package cmd

import (
"os"
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/spf13/viper"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/proxy"
)

func TestGetKubernetesVersion(t *testing.T) {
Expand Down Expand Up @@ -167,6 +169,20 @@ func TestGenerateCfgFromFlagsHTTPProxyHandling(t *testing.T) {
proxy: "localhost:3128",
proxyIgnored: true,
},
{
description: "http_proxy=http://localhost:3128",
proxy: "http://localhost:3128",
proxyIgnored: true,
},
{
description: "http_proxy=http://127.0.0.1:3128",
proxy: "http://127.0.0.1:3128",
proxyIgnored: true,
},
{
description: "http_proxy=http://1.2.127.0:3128",
proxy: "http://1.2.127.0:3128",
},
{
description: "http_proxy=1.2.3.4:3128",
proxy: "1.2.3.4:3128",
Expand All @@ -181,14 +197,42 @@ func TestGenerateCfgFromFlagsHTTPProxyHandling(t *testing.T) {
if err := os.Setenv("HTTP_PROXY", test.proxy); err != nil {
t.Fatalf("Unexpected error setting HTTP_PROXY: %v", err)
}

cfg.DockerEnv = []string{} // clear docker env to avoid pollution
proxy.SetDockerEnv()
config, _, err := generateClusterConfig(cmd, nil, k8sVersion, "none")
if err != nil {
t.Fatalf("Got unexpected error %v during config generation", err)
}
// ignored proxy should not be in config
for _, v := range config.DockerEnv {
if v == test.proxy && test.proxyIgnored {
t.Fatalf("Value %v not expected in dockerEnv but occurred", v)
envPrefix := "HTTP_PROXY="
proxyEnv := envPrefix + test.proxy
if test.proxy == "" {
// If test.proxy is not set, ensure HTTP_PROXY is empty
for _, v := range config.DockerEnv {
if strings.HasPrefix(v, envPrefix) && len(v) > len(envPrefix) {
t.Fatalf("HTTP_PROXY should be empty but got %s", v)
}
}
} else {
if test.proxyIgnored {
// ignored proxy should not in config
for _, v := range config.DockerEnv {
if v == proxyEnv {
t.Fatalf("Value %v not expected in dockerEnv but occurred", test.proxy)
}
}
} else {
// proxy must in config
found := false
for _, v := range config.DockerEnv {
if v == proxyEnv {
found = true
break
}
}
if !found {
t.Fatalf("Value %s expected in dockerEnv but not occurred", test.proxy)
}
}
}
})
Expand Down
17 changes: 16 additions & 1 deletion pkg/minikube/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -160,7 +161,21 @@ func SetDockerEnv() []string {
// TODO (@medyagh): if user has both http_proxy & HTTPS_PROXY set merge them.
k = strings.ToUpper(k)
if k == "HTTP_PROXY" || k == "HTTPS_PROXY" {
if strings.HasPrefix(v, "localhost") || strings.HasPrefix(v, "127.0") {
isLocalProxy := func(url string) bool {
return strings.HasPrefix(url, "localhost") || strings.HasPrefix(url, "127.0")
}

normalizedURL := v
if !strings.Contains(v, "://") {
normalizedURL = "http://" + v // by default, assumes the url is HTTP scheme
}
u, err := url.Parse(normalizedURL)
if err != nil {
out.WarningT("Error parsing {{.name}}={{.value}}, {{.err}}", out.V{"name": k, "value": v, "err": err})
continue
}

if isLocalProxy(u.Host) {
out.WarningT("Not passing {{.name}}={{.value}} to docker env.", out.V{"name": k, "value": v})
continue
}
Expand Down