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

Fix a variety of bugs in docker-env output #6540

Merged
merged 2 commits into from
Feb 7, 2020
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
113 changes: 51 additions & 62 deletions cmd/minikube/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,18 @@ var envTmpl = fmt.Sprintf("{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerTLSVerify }}

const (
fishSetPfx = "set -gx "
fishSetSfx = "\";\n"
fishSetSfx = "\"\n"
fishSetDelim = " \""

fishUnsetPfx = "set -e "
fishUnsetSfx = ";\n"
fishUnsetDelim = ""
fishUnsetPfx = "set -e "
fishUnsetSfx = "\n"

psSetPfx = "$Env:"
psSetSfx = "\"\n"
psSetDelim = " = \""

psUnsetPfx = `Remove-Item Env:\\`
psUnsetSfx = "\n"
psUnsetDelim = ""
psUnsetPfx = `Remove-Item Env:\\`
psUnsetSfx = "\n"

cmdSetPfx = "SET "
cmdSetSfx = "\n"
Expand All @@ -85,9 +83,8 @@ const (
bashSetSfx = "\"\n"
bashSetDelim = "=\""

bashUnsetPfx = "unset "
bashUnsetSfx = "\n"
bashUnsetDelim = ""
bashUnsetPfx = "unset "
bashUnsetSfx = "\n"

nonePfx = ""
noneSfx = "\n"
Expand Down Expand Up @@ -124,7 +121,7 @@ type NoProxyGetter interface {
type EnvNoProxyGetter struct{}

func generateUsageHint(profile, sh string) string {
const usgPlz = "Please run command bellow to point your shell to minikube's docker-daemon :"
const usgPlz = "To point your shell to minikube's docker-daemon, run:"
var usgCmd = fmt.Sprintf("minikube -p %s docker-env", profile)
var usageHintMap = map[string]string{
"bash": fmt.Sprintf(`
Expand All @@ -135,18 +132,15 @@ func generateUsageHint(profile, sh string) string {
# %s
# eval (%s)
`, usgPlz, usgCmd),
"powershell": fmt.Sprintf(`
# %s
"powershell": fmt.Sprintf(`# %s
# & %s | Invoke-Expression
`, usgPlz, usgCmd),
"cmd": fmt.Sprintf(`
REM %s
`, usgPlz, usgCmd),
"cmd": fmt.Sprintf(`REM %s
REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i
`, usgPlz, usgCmd),
"emacs": fmt.Sprintf(`
;; %s
`, usgPlz, usgCmd),
"emacs": fmt.Sprintf(`;; %s
;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer))
`, usgPlz, usgCmd),
`, usgPlz, usgCmd),
}

hint, ok := usageHintMap[sh]
Expand Down Expand Up @@ -213,46 +207,6 @@ func shellCfgSet(ec EnvConfig, envMap map[string]string) *ShellConfig {
return s
}

// shellCfgUnset generates context variables for "docker-env -u"
func shellCfgUnset(ec EnvConfig) *ShellConfig {
s := &ShellConfig{
UsageHint: generateUsageHint(ec.profile, ec.shell),
}

if ec.noProxy {
s.NoProxyVar, s.NoProxyValue = defaultNoProxyGetter.GetNoProxyVar()
}

switch ec.shell {
case "fish":
s.Prefix = fishUnsetPfx
s.Suffix = fishUnsetSfx
s.Delimiter = fishUnsetDelim
case "powershell":
s.Prefix = psUnsetPfx
s.Suffix = psUnsetSfx
s.Delimiter = psUnsetDelim
case "cmd":
s.Prefix = cmdUnsetPfx
s.Suffix = cmdUnsetSfx
s.Delimiter = cmdUnsetDelim
case "emacs":
s.Prefix = emacsUnsetPfx
s.Suffix = emacsUnsetSfx
s.Delimiter = emacsUnsetDelim
case "none":
s.Prefix = nonePfx
s.Suffix = noneSfx
s.Delimiter = noneDelim
s.UsageHint = ""
default:
s.Prefix = bashUnsetPfx
s.Suffix = bashUnsetSfx
s.Delimiter = bashUnsetDelim
}
return s
}

// GetNoProxyVar gets the no_proxy var
func (EnvNoProxyGetter) GetNoProxyVar() (string, string) {
// first check for an existing lower case no_proxy var
Expand Down Expand Up @@ -379,8 +333,43 @@ func setScript(ec EnvConfig, w io.Writer) error {

// setScript writes out a shell-compatible 'docker-env unset' script
func unsetScript(ec EnvConfig, w io.Writer) error {
tmpl := template.Must(template.New("envConfig").Parse(envTmpl))
return tmpl.Execute(w, shellCfgUnset(ec))
vars := []string{
constants.DockerTLSVerifyEnv,
constants.DockerHostEnv,
constants.DockerCertPathEnv,
constants.MinikubeActiveDockerdEnv,
}

if ec.noProxy {
k, _ := defaultNoProxyGetter.GetNoProxyVar()
if k != "" {
vars = append(vars, k)
}
}

var sb strings.Builder
switch ec.shell {
case "fish":
for _, v := range vars {
sb.WriteString(fmt.Sprintf("%s%s%s", fishUnsetPfx, v, fishUnsetSfx))
}
case "powershell":
sb.WriteString(fmt.Sprintf("%s%s%s", psUnsetPfx, strings.Join(vars, " Env:\\\\"), psUnsetSfx))
case "cmd":
for _, v := range vars {
sb.WriteString(fmt.Sprintf("%s%s%s%s", cmdUnsetPfx, v, cmdUnsetDelim, cmdUnsetSfx))
}
case "emacs":
for _, v := range vars {
sb.WriteString(fmt.Sprintf("%s%s%s%s", emacsUnsetPfx, v, emacsUnsetDelim, emacsUnsetSfx))
}
case "none":
sb.WriteString(fmt.Sprintf("%s%s%s", nonePfx, strings.Join(vars, " "), noneSfx))
default:
sb.WriteString(fmt.Sprintf("%s%s%s", bashUnsetPfx, strings.Join(vars, " "), bashUnsetSfx))
}
_, err := w.Write([]byte(sb.String()))
return err
}

// dockerURL returns a the docker endpoint URL for an ip/port pair.
Expand Down
137 changes: 39 additions & 98 deletions cmd/minikube/cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,10 @@ export DOCKER_HOST="tcp://127.0.0.1:2376"
export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="bash"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p bash docker-env)
`,
`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p bash docker-env)
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`,
},
{
Expand All @@ -67,36 +61,27 @@ export DOCKER_HOST="tcp://[fe80::215:5dff:fe00:a903]:2376"
export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="ipv6"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p ipv6 docker-env)
`,
`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p ipv6 docker-env)
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
`,
},
{
EnvConfig{profile: "fish", shell: "fish", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs"},
nil,
`set -gx DOCKER_TLS_VERIFY "1";
set -gx DOCKER_HOST "tcp://127.0.0.1:2376";
set -gx DOCKER_CERT_PATH "/certs";
set -gx MINIKUBE_ACTIVE_DOCKERD "fish";
`set -gx DOCKER_TLS_VERIFY "1"
set -gx DOCKER_HOST "tcp://127.0.0.1:2376"
set -gx DOCKER_CERT_PATH "/certs"
set -gx MINIKUBE_ACTIVE_DOCKERD "fish"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval (minikube -p fish docker-env)
`,
`set -e DOCKER_TLS_VERIFY;
set -e DOCKER_HOST;
set -e DOCKER_CERT_PATH;
set -e MINIKUBE_ACTIVE_DOCKERD;

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval (minikube -p fish docker-env)
`set -e DOCKER_TLS_VERIFY
set -e DOCKER_HOST
set -e DOCKER_CERT_PATH
set -e MINIKUBE_ACTIVE_DOCKERD
`,
},
{
Expand All @@ -106,19 +91,12 @@ set -e MINIKUBE_ACTIVE_DOCKERD;
$Env:DOCKER_HOST = "tcp://192.168.0.1:2376"
$Env:DOCKER_CERT_PATH = "/certs"
$Env:MINIKUBE_ACTIVE_DOCKERD = "powershell"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# & minikube -p powershell docker-env | Invoke-Expression
`,

`Remove-Item Env:\\DOCKER_TLS_VERIFY
Remove-Item Env:\\DOCKER_HOST
Remove-Item Env:\\DOCKER_CERT_PATH
Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
`,

# Please run command bellow to point your shell to minikube's docker-daemon :
# & minikube -p powershell docker-env | Invoke-Expression
`,
`Remove-Item Env:\\DOCKER_TLS_VERIFY Env:\\DOCKER_HOST Env:\\DOCKER_CERT_PATH Env:\\MINIKUBE_ACTIVE_DOCKERD
`,
},
{
EnvConfig{profile: "cmd", shell: "cmd", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs"},
Expand All @@ -127,19 +105,15 @@ Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
SET DOCKER_HOST=tcp://192.168.0.1:2376
SET DOCKER_CERT_PATH=/certs
SET MINIKUBE_ACTIVE_DOCKERD=cmd

REM Please run command bellow to point your shell to minikube's docker-daemon :
REM To point your shell to minikube's docker-daemon, run:
REM @FOR /f "tokens=*" %i IN ('minikube -p cmd docker-env') DO @%i
`,
`,

`SET DOCKER_TLS_VERIFY=
SET DOCKER_HOST=
SET DOCKER_CERT_PATH=
SET MINIKUBE_ACTIVE_DOCKERD=

REM Please run command bellow to point your shell to minikube's docker-daemon :
REM @FOR /f "tokens=*" %i IN ('minikube -p cmd docker-env') DO @%i
`,
`,
},
{
EnvConfig{profile: "emacs", shell: "emacs", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs"},
Expand All @@ -148,18 +122,14 @@ REM @FOR /f "tokens=*" %i IN ('minikube -p cmd docker-env') DO @%i
(setenv "DOCKER_HOST" "tcp://192.168.0.1:2376")
(setenv "DOCKER_CERT_PATH" "/certs")
(setenv "MINIKUBE_ACTIVE_DOCKERD" "emacs")

;; Please run command bellow to point your shell to minikube's docker-daemon :
;; To point your shell to minikube's docker-daemon, run:
;; (with-temp-buffer (shell-command "minikube -p emacs docker-env" (current-buffer)) (eval-buffer))
`,
`,
`(setenv "DOCKER_TLS_VERIFY" nil)
(setenv "DOCKER_HOST" nil)
(setenv "DOCKER_CERT_PATH" nil)
(setenv "MINIKUBE_ACTIVE_DOCKERD" nil)

;; Please run command bellow to point your shell to minikube's docker-daemon :
;; (with-temp-buffer (shell-command "minikube -p emacs docker-env" (current-buffer)) (eval-buffer))
`,
`,
},
{
EnvConfig{profile: "bash-no-proxy", shell: "bash", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs", noProxy: true},
Expand All @@ -170,18 +140,11 @@ export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="bash-no-proxy"
export NO_PROXY="127.0.0.1"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p bash-no-proxy docker-env)
`,

`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD
unset NO_PROXY127.0.0.1

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p bash-no-proxy docker-env)
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD NO_PROXY
`,
},
{
Expand All @@ -193,41 +156,26 @@ export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="bash-no-proxy-lower"
export no_proxy="127.0.0.1"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p bash-no-proxy-lower docker-env)
`,

`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD
unset no_proxy127.0.0.1

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p bash-no-proxy-lower docker-env)
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD no_proxy
`,
},
{
EnvConfig{profile: "bash-no-proxy-idempotent", shell: "bash", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs", noProxy: true},
&FakeNoProxyGetter{"no_proxy", "127.0.0.1"},
`export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://127.0.0.1:2376"
export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="bash-no-proxy-idempotent"
export no_proxy="127.0.0.1"

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p bash-no-proxy-idempotent docker-env)
EnvConfig{profile: "powershell-no-proxy-idempotent", shell: "powershell", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs", noProxy: true},
&FakeNoProxyGetter{"no_proxy", "192.168.0.1"},
`$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.0.1:2376"
$Env:DOCKER_CERT_PATH = "/certs"
$Env:MINIKUBE_ACTIVE_DOCKERD = "powershell-no-proxy-idempotent"
$Env:no_proxy = "192.168.0.1"
# To point your shell to minikube's docker-daemon, run:
# & minikube -p powershell-no-proxy-idempotent docker-env | Invoke-Expression
`,

`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD
unset no_proxy127.0.0.1

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p bash-no-proxy-idempotent docker-env)
`Remove-Item Env:\\DOCKER_TLS_VERIFY Env:\\DOCKER_HOST Env:\\DOCKER_CERT_PATH Env:\\MINIKUBE_ACTIVE_DOCKERD Env:\\no_proxy
`,
},
{
Expand All @@ -239,18 +187,11 @@ export DOCKER_CERT_PATH="/certs"
export MINIKUBE_ACTIVE_DOCKERD="sh-no-proxy-add"
export NO_PROXY="192.168.0.1,10.0.0.4,127.0.0.1"

# Please run command bellow to point your shell to minikube's docker-daemon :
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p sh-no-proxy-add docker-env)
`,

`unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset MINIKUBE_ACTIVE_DOCKERD
unset NO_PROXY192.168.0.1,10.0.0.4

# Please run command bellow to point your shell to minikube's docker-daemon :
# eval $(minikube -p sh-no-proxy-add docker-env)
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD NO_PROXY
`,
},
}
Expand Down