Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fix for RPC port detection #5715

Merged
2 changes: 1 addition & 1 deletion pkg/skaffold/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func errorHandler(ctx context.Context, _ *runtime.ServeMux, marshaler runtime.Ma

func listenOnAvailablePort(preferredPort int, usedPorts *util.PortSet) (net.Listener, int, error) {
for try := 1; ; try++ {
port := util.GetAvailablePort(preferredPort, usedPorts)
port := util.GetAvailablePortWithAddress(util.Loopback, preferredPort, usedPorts)

l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", util.Loopback, port))
if err != nil {
Expand Down
27 changes: 20 additions & 7 deletions pkg/skaffold/util/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
// unless we really want to expose something to the network.
const Loopback = "127.0.0.1"

// Network address which represent any address. This is the default that
// we should use when checking if port is free.
const Any = ""

type PortSet struct {
ports map[int]bool
lock sync.Mutex
Expand Down Expand Up @@ -95,29 +99,34 @@ func (f *PortSet) List() []int {
//
// See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
func GetAvailablePort(port int, usedPorts *PortSet) int {
// We map this to allow
return GetAvailablePortWithAddress(Any, port, usedPorts)
}

func GetAvailablePortWithAddress(address string, port int, usedPorts *PortSet) int {
if port > 0 {
dat-boris marked this conversation as resolved.
Show resolved Hide resolved
if getPortIfAvailable(port, usedPorts) {
if getPortIfAvailable(address, port, usedPorts) {
return port
}

// try the next 10 ports after the provided one
for i := 0; i < 10; i++ {
port++
if getPortIfAvailable(port, usedPorts) {
if getPortIfAvailable(address, port, usedPorts) {
logrus.Debugf("found open port: %d", port)
return port
}
}
}

for port = 4503; port <= 4533; port++ {
if getPortIfAvailable(port, usedPorts) {
if getPortIfAvailable(address, port, usedPorts) {
logrus.Debugf("found open port: %d", port)
return port
}
}

l, err := net.Listen("tcp", ":0")
l, err := net.Listen("tcp", fmt.Sprintf("%s:0", address))
if err != nil {
return -1
}
Expand All @@ -129,16 +138,20 @@ func GetAvailablePort(port int, usedPorts *PortSet) int {
return p
}

func getPortIfAvailable(p int, usedPorts *PortSet) bool {
func getPortIfAvailable(address string, p int, usedPorts *PortSet) bool {
if alreadySet := usedPorts.LoadOrSet(p); alreadySet {
return false
}

return IsPortFree(p)
return IsPortFreeWithAddress(address, p)
}

func IsPortFree(p int) bool {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", p))
return IsPortFreeWithAddress(Any, p)
}

func IsPortFreeWithAddress(address string, p int) bool {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, p))
if err != nil {
return false
}
dat-boris marked this conversation as resolved.
Show resolved Hide resolved
Expand Down