Specify a default port for the Linkerd dashboard (#2486)

The CLI now specifies a default port, 50750, for the Linkerd dashboard. 
If that port is not available, it resumes the original behavior of binding to a
free ephemeral port.
This commit is contained in:
Carol A. Scott 2019-03-12 15:58:13 -07:00 committed by GitHub
parent e9287cc8c8
commit 92b47ed29b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 1 deletions

View File

@ -20,6 +20,8 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
const defaultPort = 50750
// PortForward provides a port-forward connection into a Kubernetes cluster.
type PortForward struct {
method string
@ -180,12 +182,22 @@ func (pf *PortForward) URLFor(path string) string {
return fmt.Sprintf("http://127.0.0.1:%d%s", pf.localPort, path)
}
// getLocalPort binds to a free ephemeral port and returns the port number.
// getLocalPort is used by dashboard.go to select a port for the dashboard.
// It first checks the availability of the default port, defined in addr.
// If that port is taken, it binds to a free ephemeral port and returns the
// port number.
func getLocalPort() (int, error) {
defaultAddr := fmt.Sprintf("127.0.0.1:%d", defaultPort)
if ln, err := net.Listen("tcp", defaultAddr); err == nil {
ln.Close()
return defaultPort, nil
}
ln, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer ln.Close()
// get port