Skip to content

Commit

Permalink
Introduce a constant for 127.0.0.1
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Mar 22, 2019
1 parent 0e36e4d commit 6cd6fe7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions pkg/skaffold/event/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
gw "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
empty "github.com/golang/protobuf/ptypes/empty"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"

gw "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"
)

type server struct{}
Expand Down Expand Up @@ -93,7 +92,7 @@ func newStatusServer(originalRPCPort, originalHTTPPort int) (func() error, error
}

func newGRPCServer(port int) (func() error, error) {
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", util.Loopback, port))
if err != nil {
return func() error { return nil }, errors.Wrap(err, "creating listener")
}
Expand All @@ -116,12 +115,12 @@ func newGRPCServer(port int) (func() error, error) {
func newHTTPServer(port, proxyPort int) (func() error, error) {
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := gw.RegisterSkaffoldServiceHandlerFromEndpoint(context.Background(), mux, fmt.Sprintf("127.0.0.1:%d", proxyPort), opts)
err := gw.RegisterSkaffoldServiceHandlerFromEndpoint(context.Background(), mux, fmt.Sprintf("%s:%d", util.Loopback, proxyPort), opts)
if err != nil {
return func() error { return nil }, err
}

l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", util.Loopback, port))
if err != nil {
return func() error { return nil }, errors.Wrap(err, "creating listener")
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/skaffold/util/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/sirupsen/logrus"
)

// Loopback network address. Skaffold should not bind to 0.0.0.0
// unless we really want to expose something to the network.
const Loopback = "127.0.0.1"

// First, check if the provided port is available. If so, use it.
// If not, check if any of the next 10 subsequent ports are available.
// If not, check if any of ports 4503-4533 are available.
Expand All @@ -50,7 +54,7 @@ func GetAvailablePort(port int, forwardedPorts *sync.Map) int {
}
}

l, err := net.Listen("tcp", "127.0.0.1:0")
l, err := net.Listen("tcp", fmt.Sprintf("%s:0", Loopback))
if err != nil {
return -1
}
Expand All @@ -68,7 +72,7 @@ func getPortIfAvailable(p int, forwardedPorts *sync.Map) bool {
return false
}

l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", p))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", Loopback, p))
if err != nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/util/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestGetAvailablePort(t *testing.T) {
go func() {
port := GetAvailablePort(4503, &ports)

l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", Loopback, port))
if err != nil {
atomic.AddInt32(&errors, 1)
} else {
Expand Down

0 comments on commit 6cd6fe7

Please sign in to comment.