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

Clean up ssh tunnels during exit. #11745

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 24 additions & 14 deletions pkg/minikube/tunnel/kic/ssh_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import (
)

type sshConn struct {
name string
service string
cmd *exec.Cmd
ports []int
name string
service string
cmd *exec.Cmd
ports []int
activeConn bool
}

func createSSHConn(name, sshPort, sshKey string, svc *v1.Service) *sshConn {
Expand Down Expand Up @@ -87,9 +88,10 @@ func createSSHConn(name, sshPort, sshKey string, svc *v1.Service) *sshConn {
cmd := exec.Command(command, sshArgs...)

return &sshConn{
name: name,
service: svc.Name,
cmd: cmd,
name: name,
service: svc.Name,
cmd: cmd,
activeConn: false,
}
}

Expand Down Expand Up @@ -127,10 +129,11 @@ func createSSHConnWithRandomPorts(name, sshPort, sshKey string, svc *v1.Service)
cmd := exec.Command("ssh", sshArgs...)

return &sshConn{
name: name,
service: svc.Name,
cmd: cmd,
ports: usedPorts,
name: name,
service: svc.Name,
cmd: cmd,
ports: usedPorts,
activeConn: false,
}, nil
}

Expand All @@ -142,14 +145,21 @@ func (c *sshConn) startAndWait() error {
return err
}

c.activeConn = true
// we ignore wait error because the process will be killed
_ = c.cmd.Wait()

// Wait is finished for connection, mark false.
c.activeConn = false

return nil
}

func (c *sshConn) stop() error {
out.Step(style.Stopping, "Stopping tunnel for service {{.service}}.", out.V{"service": c.service})

return c.cmd.Process.Kill()
if c.activeConn {
vishjain marked this conversation as resolved.
Show resolved Hide resolved
out.Step(style.Stopping, "Stopping tunnel for service {{.service}}.", out.V{"service": c.service})
return c.cmd.Process.Kill()
}
out.Step(style.Stopping, "Stopped tunnel for service {{.service}}.", out.V{"service": c.service})
return nil
}
10 changes: 10 additions & 0 deletions pkg/minikube/tunnel/kic/ssh_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (t *SSHTunnel) Start() error {
if err != nil {
klog.Errorf("error cleaning up: %v", err)
}
t.stopActiveConnections()
return err
default:
}
Expand Down Expand Up @@ -120,6 +121,15 @@ func (t *SSHTunnel) startConnection(svc v1.Service) {
}
}

func (t *SSHTunnel) stopActiveConnections() {
for _, conn := range t.conns {
err := conn.stop()
if err != nil {
klog.Errorf("error stopping ssh tunnel: %v", err)
}
}
}

func (t *SSHTunnel) stopMarkedConnections() {
for _, sshConn := range t.connsToStop {
err := sshConn.stop()
Expand Down