From bb0e51661705059d2835f63d00586504c4aeb045 Mon Sep 17 00:00:00 2001 From: Vishal Jain Date: Tue, 22 Jun 2021 15:32:07 -0700 Subject: [PATCH 1/2] Clean up ssh tunnels during exit. --- pkg/minikube/tunnel/kic/ssh_conn.go | 38 +++++++++++++++++---------- pkg/minikube/tunnel/kic/ssh_tunnel.go | 10 +++++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/pkg/minikube/tunnel/kic/ssh_conn.go b/pkg/minikube/tunnel/kic/ssh_conn.go index b43e47af6a12..991fcaa5b5b8 100644 --- a/pkg/minikube/tunnel/kic/ssh_conn.go +++ b/pkg/minikube/tunnel/kic/ssh_conn.go @@ -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 { @@ -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, } } @@ -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 } @@ -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 { + 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 } diff --git a/pkg/minikube/tunnel/kic/ssh_tunnel.go b/pkg/minikube/tunnel/kic/ssh_tunnel.go index a6bd5f0671f3..9fcd6446a2c4 100644 --- a/pkg/minikube/tunnel/kic/ssh_tunnel.go +++ b/pkg/minikube/tunnel/kic/ssh_tunnel.go @@ -63,6 +63,7 @@ func (t *SSHTunnel) Start() error { if err != nil { klog.Errorf("error cleaning up: %v", err) } + t.stopActiveConnections() return err default: } @@ -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() From ecaa3c1f0302562bc54d47e1aac803c12deaa9ad Mon Sep 17 00:00:00 2001 From: Vishal Jain Date: Sun, 25 Jul 2021 14:45:41 -0700 Subject: [PATCH 2/2] Addressed comment about activeConn. --- pkg/minikube/tunnel/kic/ssh_conn.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/minikube/tunnel/kic/ssh_conn.go b/pkg/minikube/tunnel/kic/ssh_conn.go index 991fcaa5b5b8..35b1abac0233 100644 --- a/pkg/minikube/tunnel/kic/ssh_conn.go +++ b/pkg/minikube/tunnel/kic/ssh_conn.go @@ -157,6 +157,7 @@ func (c *sshConn) startAndWait() error { func (c *sshConn) stop() error { 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() }