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

Add logic for finding next available port for gRPC if provided one is in use #1742

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func AddRunDeployFlags(cmd *cobra.Command) {

func AddRunDevFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&opts.EnableRPC, "enable-rpc", false, "Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)")
cmd.Flags().StringVar(&opts.RPCPort, "rpc-port", ":50051", "tcp port to expose event API")
cmd.Flags().StringVar(&opts.RPCPort, "rpc-port", constants.DefaultRPCPort, "tcp port to expose event API")
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&opts.Notification, "toot", false, "Emit a terminal beep after the deploy is complete")
cmd.Flags().StringArrayVarP(&opts.Profiles, "profile", "p", nil, "Activate profiles by name")
Expand Down
2 changes: 2 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
SkaffoldPluginValue = "1337"
SkaffoldPluginName = "SKAFFOLD_PLUGIN_NAME"
DockerBuilderPluginName = "docker"

DefaultRPCPort = ":50051"
)

var (
Expand Down
41 changes: 41 additions & 0 deletions pkg/skaffold/event/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ package event

import (
"context"
"fmt"
"math/rand"
"net"
"strconv"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/proto"

empty "github.com/golang/protobuf/ptypes/empty"
Expand Down Expand Up @@ -66,10 +71,12 @@ func newStatusServer(port string) (func() error, error) {
if port == "" {
return func() error { return nil }, nil
}
port = getAvailablePort(port)
l, err := net.Listen("tcp", port)
if err != nil {
return func() error { return nil }, errors.Wrap(err, "creating listener")
}
logrus.Infof("starting gRPC server on port %s", port)

s := grpc.NewServer()
proto.RegisterSkaffoldServiceServer(s, &server{})
Expand All @@ -84,3 +91,37 @@ func newStatusServer(port string) (func() error, error) {
return l.Close()
}, nil
}

// getOpenPort tests the provided port for availability,
// and if it's already in use, finds another open port.
func getAvailablePort(port string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be able to reuse some of the port-forwarding functions here --
getAvailablePort
portAvailable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. I did some refactoring that will have a slight functional change on the way we select ports when forwarding, so gonna close this PR and open that one first, then reopen this on top of those changes.

ln, err := net.Listen("tcp", port)
if err != nil {
// if user provided non-default port, warn them that it is unavailable
if port != constants.DefaultRPCPort {
logrus.Warnf("provided port %s unavailable: finding another available port", port)
}
} else {
ln.Close()
return port
}
for {
var portNum int
logrus.Debugf("port %s already in use: attempting to find an available one", port)
port = strings.Replace(port, ":", "", -1)
portNum, err = strconv.Atoi(port)
if err == nil {
portNum++
}
if err != nil || portNum > 65535 {
portNum = rand.Intn(64511) + 1024 // range [1024, 65535]
}
port = fmt.Sprintf(":%d", portNum)
ln, err := net.Listen("tcp", port)
if err == nil {
ln.Close()
logrus.Debugf("found open port: %s", port)
return port
}
}
}