Skip to content

Commit

Permalink
Merge pull request #11745 from vishjain/user/vishal/ssh-tunnel/master
Browse files Browse the repository at this point in the history
Clean up ssh tunnels during exit.
  • Loading branch information
medyagh authored Aug 13, 2021
2 parents 0cd6ce8 + ecaa3c1 commit d7c3ade
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
39 changes: 25 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,22 @@ 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 {
c.activeConn = false
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

0 comments on commit d7c3ade

Please sign in to comment.