Try to open port before allowing it
parent
b544852ec7
commit
768d5c9d93
|
|
@ -3,6 +3,7 @@ package view
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/derailed/k9s/internal/client"
|
"github.com/derailed/k9s/internal/client"
|
||||||
|
|
@ -157,6 +158,12 @@ func (c *Container) preparePort(pp []string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) portForward(address, lport, cport string) {
|
func (c *Container) portForward(address, lport, cport string) {
|
||||||
|
err := tryListenPort(lport)
|
||||||
|
if err != nil {
|
||||||
|
c.App().Flash().Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
co := c.GetTable().GetSelectedCell(0)
|
co := c.GetTable().GetSelectedCell(0)
|
||||||
pf := dao.NewPortForwarder(c.App().Conn())
|
pf := dao.NewPortForwarder(c.App().Conn())
|
||||||
ports := []string{lport + ":" + cport}
|
ports := []string{lport + ":" + cport}
|
||||||
|
|
@ -187,3 +194,11 @@ func (c *Container) runForward(pf *dao.PortForwarder, f *portforward.PortForward
|
||||||
pf.SetActive(false)
|
pf.SetActive(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tryListenPort(port string) error {
|
||||||
|
server, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return server.Close()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue