diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 03f3f628e7..6325693076 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -688,6 +688,12 @@ func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string) (cfg.Config, er // convert https_proxy to HTTPS_PROXY for linux // 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") { + out.WarningT("Not passing {{.name}}={{.value}} to docker env.", out.V{"name": k, "value": v}) + continue + } + } dockerEnv = append(dockerEnv, fmt.Sprintf("%s=%s", k, v)) } } diff --git a/cmd/minikube/cmd/start_test.go b/cmd/minikube/cmd/start_test.go index 446091c17f..97e203466f 100644 --- a/cmd/minikube/cmd/start_test.go +++ b/cmd/minikube/cmd/start_test.go @@ -17,7 +17,12 @@ limitations under the License. package cmd import ( + "os" "testing" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + "k8s.io/minikube/pkg/minikube/constants" ) func Test_extractVMDriverVersion(t *testing.T) { @@ -43,3 +48,58 @@ func Test_extractVMDriverVersion(t *testing.T) { t.Errorf("Expected version: %s, got: %s", expectedVersion, v) } } + +func TestGenerateCfgFromFlagsHTTPProxyHandling(t *testing.T) { + viper.SetDefault(memory, constants.DefaultMemorySize) + viper.SetDefault(humanReadableDiskSize, constants.DefaultDiskSize) + originalEnv := os.Getenv("HTTP_PROXY") + defer func() { + err := os.Setenv("HTTP_PROXY", originalEnv) + if err != nil { + t.Fatalf("Error reverting env HTTP_PROXY to it's original value. Got err: %s", err) + } + }() + k8sVersion := constants.NewestKubernetesVersion + var tests = []struct { + description string + proxy string + proxyIgnored bool + }{ + + { + description: "http_proxy=127.0.0.1:3128", + proxy: "127.0.0.1:3128", + proxyIgnored: true, + }, + { + description: "http_proxy=localhost:3128", + proxy: "localhost:3128", + proxyIgnored: true, + }, + { + description: "http_proxy=1.2.3.4:3128", + proxy: "1.2.3.4:3128", + }, + { + description: "no http_proxy", + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + cmd := &cobra.Command{} + if err := os.Setenv("HTTP_PROXY", test.proxy); err != nil { + t.Fatalf("Unexpected error setting HTTP_PROXY: %v", err) + } + config, err := generateCfgFromFlags(cmd, k8sVersion) + if err != nil { + t.Fatalf("Got unexpected error %v during config generation", err) + } + // ignored proxy should not be in config + for _, v := range config.MachineConfig.DockerEnv { + if v == test.proxy && test.proxyIgnored { + t.Fatalf("Value %v not expected in dockerEnv but occurred", v) + } + } + }) + } +}