Skip to content

Commit

Permalink
rpk: generate pool of available ports for container start command
Browse files Browse the repository at this point in the history
  • Loading branch information
r-vasquez committed May 4, 2022
1 parent 63a7e65 commit ec75490
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
66 changes: 27 additions & 39 deletions src/go/rpk/pkg/cli/cmd/container/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,21 @@ func startCluster(
return err
}

// Start a seed node.
seedID := uint(0)
seedKafkaPort, err := vnet.GetFreePort()
if err != nil {
return err
}
seedProxyPort, err := vnet.GetFreePort()
if err != nil {
return err
}
seedSchemaRegPort, err := vnet.GetFreePort()
if err != nil {
return err
}
seedRPCPort, err := vnet.GetFreePort()
if err != nil {
return err
}
seedMetricsPort, err := vnet.GetFreePort()
reqPorts := n * 5 // we need 5 ports per node
ports, err := vnet.GetFreePortPool(int(reqPorts))
if err != nil {
return err
}

// Start a seed node.
seedID := uint(0)
seedKafkaPort := ports[0]
seedProxyPort := ports[1]
seedSchemaRegPort := ports[2]
seedRPCPort := ports[3]
seedMetricsPort := ports[4]
ports = ports[5:]

seedState, err := common.CreateNode(
c,
seedID,
Expand Down Expand Up @@ -236,26 +229,21 @@ func startCluster(
for nodeID := uint(1); nodeID < n; nodeID++ {
id := nodeID
grp.Go(func() error {
kafkaPort, err := vnet.GetFreePort()
if err != nil {
return err
}
proxyPort, err := vnet.GetFreePort()
if err != nil {
return err
}
schemaRegPort, err := vnet.GetFreePort()
if err != nil {
return err
}
rpcPort, err := vnet.GetFreePort()
if err != nil {
return err
}
metricsPort, err := vnet.GetFreePort()
if err != nil {
return err
}
kafkaPort := ports[0]
ports = ports[1:]

proxyPort := ports[0]
ports = ports[1:]

schemaRegPort := ports[0]
ports = ports[1:]

rpcPort := ports[0]
ports = ports[1:]

metricsPort := ports[0]
ports = ports[1:]

args := []string{
"--seeds",
net.JoinHostPort(
Expand Down
9 changes: 6 additions & 3 deletions src/go/rpk/pkg/cli/cmd/container/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ Please check your internet connection and try again.`,
expectedErrMsg: "Network create go boom",
},
{
name: "it should fail if inspecting the network fails",
name: "it should fail if inspecting the network fails",
nodes: 1,
client: func(_ *testing.T) (common.Client, error) {
return &common.MockClient{
MockNetworkInspect: func(
Expand All @@ -147,7 +148,8 @@ Please check your internet connection and try again.`,
expectedErrMsg: "Can't inspect the network",
},
{
name: "it should fail if the network config is corrupted",
name: "it should fail if the network config is corrupted",
nodes: 1,
client: func(_ *testing.T) (common.Client, error) {
return &common.MockClient{
MockNetworkInspect: func(
Expand Down Expand Up @@ -211,7 +213,8 @@ Please check your internet connection and try again.`,
expectedErrMsg: "Can't inspect",
},
{
name: "it should fail if creating the container fails",
name: "it should fail if creating the container fails",
nodes: 1,
client: func(_ *testing.T) (common.Client, error) {
return &common.MockClient{
// NetworkInspect succeeds returning the
Expand Down
14 changes: 13 additions & 1 deletion src/go/rpk/pkg/net/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetInterfacesByIps(addresses ...string) ([]string, error) {
return utils.GetKeys(nics), nil
}

func GetFreePort() (uint, error) {
func getFreePort() (uint, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
Expand All @@ -58,3 +58,15 @@ func GetFreePort() (uint, error) {
defer l.Close()
return uint(l.Addr().(*net.TCPAddr).Port), nil
}

func GetFreePortPool(n int) ([]uint, error) {
var ports []uint
for i := 0; i < n; i++ {
p, err := getFreePort()
if err != nil {
return nil, err
}
ports = append(ports, p)
}
return ports, nil
}

0 comments on commit ec75490

Please sign in to comment.