Skip to content

Commit

Permalink
rpk container: add subnet/gateway flags
Browse files Browse the repository at this point in the history
Let the user select their network configuration.

Fixes #9008
  • Loading branch information
r-vasquez committed Apr 24, 2024
1 parent 7b83528 commit b124534
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/go/rpk/pkg/cli/container/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ func GetState(c Client, nodeID uint) (*NodeState, error) {
}, nil
}

// Creates a network for the cluster's containers and returns its ID. If it
// exists already, it returns the existing network's ID.
func CreateNetwork(c Client) (string, error) {
// CreateNetwork Creates a network for the cluster's containers and returns its
// ID. If it exists already, it returns the existing network's ID.
func CreateNetwork(c Client, subnet, gateway string) (string, error) {
ctx, _ := DefaultCtx()

args := filters.NewArgs()
Expand All @@ -212,16 +212,16 @@ func CreateNetwork(c Client) (string, error) {
}
}

fmt.Printf("Creating network %s\n", redpandaNetwork)
fmt.Printf("Creating network %q\n", redpandaNetwork)
resp, err := c.NetworkCreate(
ctx, redpandaNetwork, types.NetworkCreate{
Driver: "bridge",
IPAM: &network.IPAM{
Driver: "default",
Config: []network.IPAMConfig{
{
Subnet: "172.24.1.0/24",
Gateway: "172.24.1.1",
Subnet: subnet,
Gateway: gateway,
},
},
},
Expand Down
14 changes: 12 additions & 2 deletions src/go/rpk/pkg/cli/container/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func newStartCommand(fs afero.Fs, p *config.Params) *cobra.Command {
rPorts []string
srPorts []string
anyPort bool

subnet string
gateway string
)
command := &cobra.Command{
Use: "start",
Expand All @@ -95,6 +98,9 @@ Each flag accepts a comma-separated list of ports for your listeners. Use the
Optionally, specify a container image; the default image is
redpandadata/redpanda:latest.
In case of IP address pool conflict, you may specify a custom subnet and gateway
using the '--subnet' and '--gateway' flags respectively.
`,
Example: `
Start a 3-broker cluster:
Expand Down Expand Up @@ -130,7 +136,7 @@ Start a 3-broker cluster, selecting every admin API port:
out.MaybeDie(err, "unable to parse container ports: %v", err)

configKvs := collectFlags(os.Args, "--set")
isRestarted, err := startCluster(c, nodes, checkBrokers, retries, image, pull, cPorts, configKvs)
isRestarted, err := startCluster(c, nodes, checkBrokers, retries, image, pull, cPorts, configKvs, subnet, gateway)
if err != nil {
if errors.As(err, &portInUseErr{}) {
out.Die("unable to start cluster: %v\nYou may select different ports to start the cluster using our listener flags. Check '--help' text for more information", err)
Expand Down Expand Up @@ -182,6 +188,9 @@ You can retry profile creation by running:
// opt-in for 'any' in all listeners
command.Flags().BoolVar(&anyPort, flagAnyPort, false, "Opt in for any (random) ports in all listeners")

command.Flags().StringVar(&subnet, "subnet", "172.24.1.0/24", "Subnet to create the cluster network on")
command.Flags().StringVar(&gateway, "gateway", "172.24.1.1", "Gateway IP address for the subnet. Must be in the subnet address range")

command.MarkFlagsMutuallyExclusive(flagAnyPort, flagKafkaPorts)
command.MarkFlagsMutuallyExclusive(flagAnyPort, flagAdminPorts)
command.MarkFlagsMutuallyExclusive(flagAnyPort, flagSRPorts)
Expand Down Expand Up @@ -212,6 +221,7 @@ func startCluster(
pull bool,
clusterPorts clusterPorts,
extraArgs []string,
subnet, gateway string,
) (isRestarted bool, rerr error) {
// Check if cluster exists and start it again.
restarted, err := restartCluster(c, check, retries)
Expand Down Expand Up @@ -251,7 +261,7 @@ func startCluster(
}

// Create the docker network if it doesn't exist already
netID, err := common.CreateNetwork(c)
netID, err := common.CreateNetwork(c, subnet, gateway)
if err != nil {
return false, err
}
Expand Down

0 comments on commit b124534

Please sign in to comment.