diff --git a/pkg/minikube/tunnel/kic/ssh_conn.go b/pkg/minikube/tunnel/kic/ssh_conn.go index b43e47af6a12..35b1abac0233 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,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 } 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()