Skip to content

Commit

Permalink
Gangplank: ensure that the local port is available for SSH
Browse files Browse the repository at this point in the history
This builds on PR coreos#2378 which maps the remote port to the local port.
However, if the local port is in use, then Gangplank will fail to
launch.

Signed-off-by: Ben Howard <ben.howard@redhat.com>
  • Loading branch information
Ben Howard committed Aug 20, 2021
1 parent fa1fade commit 6572809
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions gangplank/internal/ocp/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,28 @@ func (m *minioServer) startMinioAndForwardOverSSH(ctx context.Context, termCh te
// dynamically chosen based on port availabilty on the remote. If
// we don't do this then multiple concurrent gangplank runs will fail
// because they'll try to use the same port.
remoteConn, err := client.Listen("tcp4", "127.0.0.1:")
if err != nil {
err = fmt.Errorf("%w: failed to open remote port over ssh for proxy", err)
return err
}
remoteSSHport, err := strconv.Atoi(strings.Split(remoteConn.Addr().String(), ":")[1])
if err != nil {
err = fmt.Errorf("%w: failed to parse remote ssh port from connection", err)
return err
var remoteConn net.Listener
var remoteSSHport int
for {
remoteConn, err = client.Listen("tcp4", "127.0.0.1:")
if err != nil {
err = fmt.Errorf("%w: failed to open remote port over ssh for proxy", err)
return err
}
remoteSSHport, err := strconv.Atoi(strings.Split(remoteConn.Addr().String(), ":")[1])
if err != nil {
err = fmt.Errorf("%w: failed to parse remote ssh port from connection", err)
return err
}
log.Infof("The SSH forwarding chose port %d on the remote host", remoteSSHport)

if getPortOrNext(remoteSSHport) == remoteSSHport {
break
}

log.Infof("Local Port %d is not available, selecting another port", remoteSSHport)
remoteConn.Close()
}
log.Infof("The SSH forwarding chose port %v on the remote host", remoteSSHport)
// Update m.Port in the minioServer definition so the miniocfg
// that gets passed to the remote specifies the correct port for
// the local connection there.
Expand Down

0 comments on commit 6572809

Please sign in to comment.