Skip to content

Commit 327ba9e

Browse files
authored
Merge pull request #8198 from medyagh/fix_proxy
fix proxy env not being passed to docker engine
2 parents 1eb346d + 5640da0 commit 327ba9e

File tree

6 files changed

+57
-24
lines changed

6 files changed

+57
-24
lines changed

cmd/minikube/cmd/start.go

-19
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import (
5656
"k8s.io/minikube/pkg/minikube/node"
5757
"k8s.io/minikube/pkg/minikube/notify"
5858
"k8s.io/minikube/pkg/minikube/out"
59-
"k8s.io/minikube/pkg/minikube/proxy"
6059
"k8s.io/minikube/pkg/minikube/registry"
6160
"k8s.io/minikube/pkg/minikube/translate"
6261
"k8s.io/minikube/pkg/util"
@@ -909,24 +908,6 @@ func createNode(cc config.ClusterConfig, kubeNodeName string, existing *config.C
909908
return cc, cp, nil
910909
}
911910

912-
// setDockerProxy sets the proxy environment variables in the docker environment.
913-
func setDockerProxy() {
914-
for _, k := range proxy.EnvVars {
915-
if v := os.Getenv(k); v != "" {
916-
// convert https_proxy to HTTPS_PROXY for linux
917-
// TODO (@medyagh): if user has both http_proxy & HTTPS_PROXY set merge them.
918-
k = strings.ToUpper(k)
919-
if k == "HTTP_PROXY" || k == "HTTPS_PROXY" {
920-
if strings.HasPrefix(v, "localhost") || strings.HasPrefix(v, "127.0") {
921-
out.WarningT("Not passing {{.name}}={{.value}} to docker env.", out.V{"name": k, "value": v})
922-
continue
923-
}
924-
}
925-
config.DockerEnv = append(config.DockerEnv, fmt.Sprintf("%s=%s", k, v))
926-
}
927-
}
928-
}
929-
930911
// autoSetDriverOptions sets the options needed for specific driver automatically.
931912
func autoSetDriverOptions(cmd *cobra.Command, drvName string) (err error) {
932913
err = nil

cmd/minikube/cmd/start_flags.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"k8s.io/minikube/pkg/minikube/driver"
3737
"k8s.io/minikube/pkg/minikube/exit"
3838
"k8s.io/minikube/pkg/minikube/out"
39+
"k8s.io/minikube/pkg/minikube/proxy"
3940
pkgutil "k8s.io/minikube/pkg/util"
4041
"k8s.io/minikube/pkg/version"
4142
)
@@ -340,8 +341,8 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
340341

341342
// Feed Docker our host proxy environment by default, so that it can pull images
342343
// doing this for both new config and existing, in case proxy changed since previous start
343-
if _, ok := r.(*cruntime.Docker); ok && !cmd.Flags().Changed("docker-env") {
344-
setDockerProxy()
344+
if _, ok := r.(*cruntime.Docker); ok {
345+
proxy.SetDockerEnv()
345346
}
346347

347348
var kubeNodeName string

pkg/minikube/machine/cluster_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func TestStartHostConfig(t *testing.T) {
290290

291291
for i := range h.HostOptions.EngineOptions.Env {
292292
if h.HostOptions.EngineOptions.Env[i] != cfg.DockerEnv[i] {
293-
t.Fatal("Docker env variables were not set!")
293+
t.Fatalf("Docker env variables were not set! got %+v but want %+v", h.HostOptions.EngineOptions.Env, cfg.DockerEnv)
294294
}
295295
}
296296

pkg/minikube/machine/start.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"k8s.io/minikube/pkg/minikube/driver"
4040
"k8s.io/minikube/pkg/minikube/localpath"
4141
"k8s.io/minikube/pkg/minikube/out"
42+
"k8s.io/minikube/pkg/minikube/proxy"
4243
"k8s.io/minikube/pkg/minikube/registry"
4344
"k8s.io/minikube/pkg/minikube/vmpath"
4445
"k8s.io/minikube/pkg/util/lock"
@@ -89,9 +90,25 @@ func StartHost(api libmachine.API, cfg config.ClusterConfig, n config.Node) (*ho
8990
return h, exists, err
9091
}
9192

93+
// engineOptions returns docker engine options for the dockerd running inside minikube
9294
func engineOptions(cfg config.ClusterConfig) *engine.Options {
95+
// get docker env from user's proxy settings
96+
dockerEnv := proxy.SetDockerEnv()
97+
// get docker env from user specifiec config
98+
dockerEnv = append(dockerEnv, cfg.DockerEnv...)
99+
100+
// remove duplicates
101+
seen := map[string]bool{}
102+
uniqueEnvs := []string{}
103+
for e := range dockerEnv {
104+
if !seen[dockerEnv[e]] {
105+
seen[dockerEnv[e]] = true
106+
uniqueEnvs = append(uniqueEnvs, dockerEnv[e])
107+
}
108+
}
109+
93110
o := engine.Options{
94-
Env: cfg.DockerEnv,
111+
Env: uniqueEnvs,
95112
InsecureRegistry: append([]string{constants.DefaultServiceCIDR}, cfg.InsecureRegistry...),
96113
RegistryMirror: cfg.RegistryMirror,
97114
ArbitraryFlags: cfg.DockerOpt,

pkg/minikube/node/start.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ func validateNetwork(h *host.Host, r command.Runner, imageRepository string) (st
407407
ipExcluded := proxy.IsIPExcluded(ip) // Skip warning if minikube ip is already in NO_PROXY
408408
k = strings.ToUpper(k) // for http_proxy & https_proxy
409409
if (k == "HTTP_PROXY" || k == "HTTPS_PROXY") && !ipExcluded && !warnedOnce {
410-
out.WarningT("You appear to be using a proxy, but your NO_PROXY environment does not include the minikube IP ({{.ip_address}}). Please see {{.documentation_url}} for more details", out.V{"ip_address": ip, "documentation_url": "https://minikube.sigs.k8s.io/docs/handbook/vpn_and_proxy/"})
410+
out.WarningT("You appear to be using a proxy, but your NO_PROXY environment does not include the minikube IP ({{.ip_address}}).", out.V{"ip_address": ip})
411+
out.T(out.Documentation, "Please see {{.documentation_url}} for more details", out.V{"documentation_url": "https://minikube.sigs.k8s.io/docs/handbook/vpn_and_proxy/"})
411412
warnedOnce = true
412413
}
413414
}

pkg/minikube/proxy/proxy.go

+33
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/golang/glog"
2727
"github.com/pkg/errors"
2828
"k8s.io/client-go/rest"
29+
"k8s.io/minikube/pkg/minikube/config"
30+
"k8s.io/minikube/pkg/minikube/out"
2931
)
3032

3133
// EnvVars are variables we plumb through to the underlying container runtime
@@ -149,3 +151,34 @@ func UpdateTransport(cfg *rest.Config) *rest.Config {
149151
}
150152
return cfg
151153
}
154+
155+
// SetDockerEnv sets the proxy environment variables in the docker environment.
156+
func SetDockerEnv() []string {
157+
for _, k := range EnvVars {
158+
if v := os.Getenv(k); v != "" {
159+
// convert https_proxy to HTTPS_PROXY for linux
160+
// TODO (@medyagh): if user has both http_proxy & HTTPS_PROXY set merge them.
161+
k = strings.ToUpper(k)
162+
if k == "HTTP_PROXY" || k == "HTTPS_PROXY" {
163+
if strings.HasPrefix(v, "localhost") || strings.HasPrefix(v, "127.0") {
164+
out.WarningT("Not passing {{.name}}={{.value}} to docker env.", out.V{"name": k, "value": v})
165+
continue
166+
}
167+
}
168+
config.DockerEnv = append(config.DockerEnv, fmt.Sprintf("%s=%s", k, v))
169+
}
170+
}
171+
172+
// remove duplicates
173+
seen := map[string]bool{}
174+
uniqueEnvs := []string{}
175+
for e := range config.DockerEnv {
176+
if !seen[config.DockerEnv[e]] {
177+
seen[config.DockerEnv[e]] = true
178+
uniqueEnvs = append(uniqueEnvs, config.DockerEnv[e])
179+
}
180+
}
181+
config.DockerEnv = uniqueEnvs
182+
183+
return config.DockerEnv
184+
}

0 commit comments

Comments
 (0)