Skip to content

Commit ae624d9

Browse files
feat: use automated ssh-agent in docker-env
1 parent 3d1b440 commit ae624d9

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

cmd/minikube/cmd/docker-env.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,6 @@ docker-cli install instructions: https://minikube.sigs.k8s.io/docs/tutorials/doc
298298

299299
cname := ClusterFlagValue()
300300

301-
// start the ssh-agent
302-
// this must be done before the cluster config is loaded
303-
// otherwise we won't be able to get SSH_AUTH_SOCK and SSH_AGENT_PID from cluster config.
304-
if err := sshagent.Start(cname); err != nil {
305-
exit.Message(reason.SshAgentStart, err.Error())
306-
}
307-
308301
co := mustload.Running(cname)
309302

310303
driverName := co.CP.Host.DriverName
@@ -336,11 +329,19 @@ docker-cli install instructions: https://minikube.sigs.k8s.io/docs/tutorials/doc
336329
// user also need to execute ssh-agent bash and minikube ssh-host --append-known before this
337330
// so remind them to do so
338331
out.WarningT("Please ensure you have executed 'ssh-agent bash' and 'minikube ssh-host --append-known' in this shell before using docker-env on containerd. Ignore this message if you have done it")
339-
}
340332

341-
// set the ssh-agent envs for current process
342-
os.Setenv("SSH_AUTH_SOCK", co.Config.SSHAuthSock)
343-
os.Setenv("SSH_AGENT_PID", strconv.Itoa(co.Config.SSHAgentPID))
333+
// start the ssh-agent
334+
if err := sshagent.Start(cname); err != nil {
335+
exit.Message(reason.SSHAgentStart, err.Error())
336+
}
337+
// cluster config must be reloaded
338+
// otherwise we won't be able to get SSH_AUTH_SOCK and SSH_AGENT_PID from cluster config.
339+
co = mustload.Running(cname)
340+
341+
// set the ssh-agent envs for current process
342+
os.Setenv("SSH_AUTH_SOCK", co.Config.SSHAuthSock)
343+
os.Setenv("SSH_AGENT_PID", strconv.Itoa(co.Config.SSHAgentPID))
344+
}
344345

345346
r := co.CP.Runner
346347

pkg/minikube/reason/reason.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ var (
429429
// minikube failed to cache images for the current container runtime
430430
RuntimeCache = Kind{ID: "RUNTIME_CACHE", ExitCode: ExRuntimeError}
431431
// minikube failed to start an ssh-agent when executing docker-env
432-
SshAgentStart = Kind{ID: "SSH_AGENT_START", ExitCode: ExRuntimeError}
432+
SSHAgentStart = Kind{ID: "SSH_AGENT_START", ExitCode: ExRuntimeError}
433433

434434
// service check timed out while starting minikube dashboard
435435
SvcCheckTimeout = Kind{ID: "SVC_CHECK_TIMEOUT", ExitCode: ExSvcTimeout}

test/integration/docker_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestDockerEnvContainerd(t *testing.T) {
194194
output := result.Output()
195195
groups := regexp.MustCompile(`DOCKER_HOST="(\S*)"`).FindStringSubmatch(output)
196196
if len(groups) < 2 {
197-
t.Errorf("failed to acquire SSH_AUTH_SOCK, output is %s", output)
197+
t.Errorf("DOCKER_HOST doesn't match expected format, output is %s", output)
198198
}
199199
dockerHost := groups[1]
200200
segments := strings.Split(dockerHost, ":")
@@ -211,7 +211,7 @@ func TestDockerEnvContainerd(t *testing.T) {
211211
// get SSH_AGENT_PID
212212
groups = regexp.MustCompile(`SSH_AGENT_PID=(\S*)`).FindStringSubmatch(output)
213213
if len(groups) < 2 {
214-
t.Errorf("failed to acquire SSH_AUTH_SOCK, output is %s", output)
214+
t.Errorf("failed to acquire SSH_AUTH_PID, output is %s", output)
215215
}
216216
sshAgentPid := groups[1]
217217

0 commit comments

Comments
 (0)