Skip to content

Commit 25167e1

Browse files
authored
Fix docker-env and add unit and integration tests (#6604)
* Fix docker cert env in docker-env for docker driver * add unit tests * add integration tests for docker-env cmd * lint * fix lint * merge conflict * improve docker-env integraiton test * linting
1 parent ef99687 commit 25167e1

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

Diff for: cmd/minikube/cmd/docker-env.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929

3030
"github.com/docker/machine/libmachine/drivers"
3131
"github.com/docker/machine/libmachine/state"
32-
"github.com/pkg/errors"
3332
"github.com/spf13/cobra"
3433
"github.com/spf13/viper"
3534
"k8s.io/minikube/pkg/drivers/kic/oci"
@@ -182,11 +181,22 @@ var dockerEnvCmd = &cobra.Command{
182181
sh := shell.EnvConfig{
183182
Shell: shell.ForceShell,
184183
}
184+
185+
port := constants.DockerDaemonPort
186+
if driver.IsKIC(host.DriverName) { // for kic we need to find what port docker/podman chose for us
187+
hostIP = oci.DefaultBindIPV4
188+
port, err = oci.HostPortBinding(host.DriverName, profile, port)
189+
if err != nil {
190+
exit.WithCodeT(exit.Failure, "Error getting port binding for '{{.driver_name}} driver: {{.error}}", out.V{"driver_name": host.DriverName, "error": err})
191+
}
192+
}
193+
185194
ec := DockerEnvConfig{
186195
EnvConfig: sh,
187196
profile: profile,
188197
driver: host.DriverName,
189198
hostIP: hostIP,
199+
port: port,
190200
certsDir: localpath.MakeMiniPath("certs"),
191201
noProxy: noProxy,
192202
}
@@ -217,16 +227,14 @@ type DockerEnvConfig struct {
217227
profile string
218228
driver string
219229
hostIP string
230+
port int
220231
certsDir string
221232
noProxy bool
222233
}
223234

224235
// dockerSetScript writes out a shell-compatible 'docker-env' script
225236
func dockerSetScript(ec DockerEnvConfig, w io.Writer) error {
226-
envVars, err := dockerEnvVars(ec)
227-
if err != nil {
228-
return err
229-
}
237+
envVars := dockerEnvVars(ec)
230238
return shell.SetScript(ec.EnvConfig, w, dockerEnvTmpl, dockerShellCfgSet(ec, envVars))
231239
}
232240

@@ -255,22 +263,15 @@ func dockerURL(ip string, port int) string {
255263
}
256264

257265
// dockerEnvVars gets the necessary docker env variables to allow the use of minikube's docker daemon
258-
func dockerEnvVars(ec DockerEnvConfig) (map[string]string, error) {
266+
func dockerEnvVars(ec DockerEnvConfig) map[string]string {
259267
env := map[string]string{
260268
constants.DockerTLSVerifyEnv: "1",
261-
constants.DockerHostEnv: dockerURL(ec.hostIP, constants.DockerDaemonPort),
269+
constants.DockerHostEnv: dockerURL(ec.hostIP, ec.port),
262270
constants.DockerCertPathEnv: ec.certsDir,
263271
constants.MinikubeActiveDockerdEnv: ec.profile,
264272
}
265273

266-
if driver.IsKIC(ec.driver) { // for kic we need to find out what port docker allocated during creation
267-
port, err := oci.HostPortBinding(ec.driver, ec.profile, constants.DockerDaemonPort)
268-
if err != nil {
269-
return nil, errors.Wrapf(err, "get hostbind port for %d", constants.DockerDaemonPort)
270-
}
271-
env[constants.DockerCertPathEnv] = dockerURL(oci.DefaultBindIPV4, port)
272-
}
273-
return env, nil
274+
return env
274275
}
275276

276277
func init() {

Diff for: cmd/minikube/cmd/docker-env_test.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ func TestGenerateDockerScripts(t *testing.T) {
4242
}{
4343
{
4444
"bash",
45-
DockerEnvConfig{profile: "bash", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs"},
45+
DockerEnvConfig{profile: "dockerdrver", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs"},
46+
nil,
47+
`export DOCKER_TLS_VERIFY="1"
48+
export DOCKER_HOST="tcp://127.0.0.1:32842"
49+
export DOCKER_CERT_PATH="/certs"
50+
export MINIKUBE_ACTIVE_DOCKERD="dockerdrver"
51+
52+
# To point your shell to minikube's docker-daemon, run:
53+
# eval $(minikube -p dockerdrver docker-env)
54+
`,
55+
`unset DOCKER_TLS_VERIFY DOCKER_HOST DOCKER_CERT_PATH MINIKUBE_ACTIVE_DOCKERD
56+
`,
57+
},
58+
{
59+
"bash",
60+
DockerEnvConfig{profile: "bash", driver: "kvm2", hostIP: "127.0.0.1", port: 2376, certsDir: "/certs"},
4661
nil,
4762
`export DOCKER_TLS_VERIFY="1"
4863
export DOCKER_HOST="tcp://127.0.0.1:2376"
@@ -57,7 +72,7 @@ export MINIKUBE_ACTIVE_DOCKERD="bash"
5772
},
5873
{
5974
"bash",
60-
DockerEnvConfig{profile: "ipv6", driver: "kvm2", hostIP: "fe80::215:5dff:fe00:a903", certsDir: "/certs"},
75+
DockerEnvConfig{profile: "ipv6", driver: "kvm2", hostIP: "fe80::215:5dff:fe00:a903", port: 2376, certsDir: "/certs"},
6176
nil,
6277
`export DOCKER_TLS_VERIFY="1"
6378
export DOCKER_HOST="tcp://[fe80::215:5dff:fe00:a903]:2376"
@@ -72,7 +87,7 @@ export MINIKUBE_ACTIVE_DOCKERD="ipv6"
7287
},
7388
{
7489
"fish",
75-
DockerEnvConfig{profile: "fish", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs"},
90+
DockerEnvConfig{profile: "fish", driver: "kvm2", hostIP: "127.0.0.1", port: 2376, certsDir: "/certs"},
7691
nil,
7792
`set -gx DOCKER_TLS_VERIFY "1"
7893
set -gx DOCKER_HOST "tcp://127.0.0.1:2376"
@@ -90,7 +105,7 @@ set -e MINIKUBE_ACTIVE_DOCKERD
90105
},
91106
{
92107
"powershell",
93-
DockerEnvConfig{profile: "powershell", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs"},
108+
DockerEnvConfig{profile: "powershell", driver: "hyperv", hostIP: "192.168.0.1", port: 2376, certsDir: "/certs"},
94109
nil,
95110
`$Env:DOCKER_TLS_VERIFY = "1"
96111
$Env:DOCKER_HOST = "tcp://192.168.0.1:2376"
@@ -105,7 +120,7 @@ $Env:MINIKUBE_ACTIVE_DOCKERD = "powershell"
105120
},
106121
{
107122
"cmd",
108-
DockerEnvConfig{profile: "cmd", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs"},
123+
DockerEnvConfig{profile: "cmd", driver: "hyperv", hostIP: "192.168.0.1", port: 2376, certsDir: "/certs"},
109124
nil,
110125
`SET DOCKER_TLS_VERIFY=1
111126
SET DOCKER_HOST=tcp://192.168.0.1:2376
@@ -123,7 +138,7 @@ SET MINIKUBE_ACTIVE_DOCKERD=
123138
},
124139
{
125140
"emacs",
126-
DockerEnvConfig{profile: "emacs", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs"},
141+
DockerEnvConfig{profile: "emacs", driver: "hyperv", hostIP: "192.168.0.1", port: 2376, certsDir: "/certs"},
127142
nil,
128143
`(setenv "DOCKER_TLS_VERIFY" "1")
129144
(setenv "DOCKER_HOST" "tcp://192.168.0.1:2376")
@@ -140,7 +155,7 @@ SET MINIKUBE_ACTIVE_DOCKERD=
140155
},
141156
{
142157
"bash",
143-
DockerEnvConfig{profile: "bash-no-proxy", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs", noProxy: true},
158+
DockerEnvConfig{profile: "bash-no-proxy", driver: "kvm2", hostIP: "127.0.0.1", port: 2376, certsDir: "/certs", noProxy: true},
144159
&FakeNoProxyGetter{"NO_PROXY", "127.0.0.1"},
145160
`export DOCKER_TLS_VERIFY="1"
146161
export DOCKER_HOST="tcp://127.0.0.1:2376"
@@ -157,7 +172,7 @@ export NO_PROXY="127.0.0.1"
157172
},
158173
{
159174
"bash",
160-
DockerEnvConfig{profile: "bash-no-proxy-lower", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs", noProxy: true},
175+
DockerEnvConfig{profile: "bash-no-proxy-lower", driver: "kvm2", hostIP: "127.0.0.1", port: 2376, certsDir: "/certs", noProxy: true},
161176
&FakeNoProxyGetter{"no_proxy", "127.0.0.1"},
162177
`export DOCKER_TLS_VERIFY="1"
163178
export DOCKER_HOST="tcp://127.0.0.1:2376"
@@ -174,7 +189,7 @@ export no_proxy="127.0.0.1"
174189
},
175190
{
176191
"powershell",
177-
DockerEnvConfig{profile: "powershell-no-proxy-idempotent", driver: "hyperv", hostIP: "192.168.0.1", certsDir: "/certs", noProxy: true},
192+
DockerEnvConfig{profile: "powershell-no-proxy-idempotent", driver: "hyperv", hostIP: "192.168.0.1", port: 2376, certsDir: "/certs", noProxy: true},
178193
&FakeNoProxyGetter{"no_proxy", "192.168.0.1"},
179194
`$Env:DOCKER_TLS_VERIFY = "1"
180195
$Env:DOCKER_HOST = "tcp://192.168.0.1:2376"
@@ -190,7 +205,7 @@ $Env:no_proxy = "192.168.0.1"
190205
},
191206
{
192207
"bash",
193-
DockerEnvConfig{profile: "sh-no-proxy-add", driver: "kvm2", hostIP: "127.0.0.1", certsDir: "/certs", noProxy: true},
208+
DockerEnvConfig{profile: "sh-no-proxy-add", driver: "kvm2", hostIP: "127.0.0.1", port: 2376, certsDir: "/certs", noProxy: true},
194209
&FakeNoProxyGetter{"NO_PROXY", "192.168.0.1,10.0.0.4"},
195210
`export DOCKER_TLS_VERIFY="1"
196211
export DOCKER_HOST="tcp://127.0.0.1:2376"

Diff for: test/integration/functional_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestFunctional(t *testing.T) {
104104
{"MySQL", validateMySQL},
105105
{"FileSync", validateFileSync},
106106
{"UpdateContextCmd", validateUpdateContextCmd},
107+
{"DockerEnv", validateDockerEnv},
107108
}
108109
for _, tc := range tests {
109110
tc := tc
@@ -115,6 +116,36 @@ func TestFunctional(t *testing.T) {
115116
})
116117
}
117118

119+
// check functionality of minikube after evaling docker-env
120+
func validateDockerEnv(ctx context.Context, t *testing.T, profile string) {
121+
mctx, cancel := context.WithTimeout(ctx, 13*time.Second)
122+
defer cancel()
123+
// we should be able to get minikube status with a bash which evaled docker-env
124+
c := exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && "+Target()+" status -p "+profile)
125+
rr, err := Run(t, c)
126+
if err != nil {
127+
t.Fatalf("Failed to do minikube status after eval-ing docker-env %s", err)
128+
}
129+
if !strings.Contains(rr.Output(), "Running") {
130+
t.Fatalf("Expected status output to include 'Running' after eval docker-env but got \n%s", rr.Output())
131+
}
132+
133+
mctx, cancel = context.WithTimeout(ctx, 13*time.Second)
134+
defer cancel()
135+
// do a eval $(minikube -p profile docker-env) and check if we are point to docker inside minikube
136+
c = exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && docker images")
137+
rr, err = Run(t, c)
138+
if err != nil {
139+
t.Fatalf("Failed to test eval docker-evn %s", err)
140+
}
141+
142+
expectedImgInside := "gcr.io/k8s-minikube/storage-provisioner"
143+
if !strings.Contains(rr.Output(), expectedImgInside) {
144+
t.Fatalf("Expected 'docker ps' to have %q from docker-daemon inside minikube. the docker ps output is:\n%q\n", expectedImgInside, rr.Output())
145+
}
146+
147+
}
148+
118149
func validateStartWithProxy(ctx context.Context, t *testing.T, profile string) {
119150
srv, err := startHTTPProxy(t)
120151
if err != nil {

0 commit comments

Comments
 (0)