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

rpk: generate pool of available ports for container start command #4565

Merged
merged 1 commit into from
May 5, 2022
Merged
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
60 changes: 21 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,22 @@ 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.
var (
seedID uint
seedKafkaPort = ports[0]
seedProxyPort = ports[1]
seedSchemaRegPort = ports[2]
seedRPCPort = ports[3]
seedMetricsPort = ports[4]
)

seedState, err := common.CreateNode(
c,
seedID,
Expand Down Expand Up @@ -236,26 +230,14 @@ 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
}
var (
kafkaPort = ports[0+5*id]
proxyPort = ports[1+5*id]
schemaRegPort = ports[2+5*id]
rpcPort = ports[3+5*id]
metricsPort = ports[4+5*id]
)

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
}