cli/command/network: rewrite consolidateIpam to take an option-struct

Introduce a (non-exported) ipamOptions that collects all options for
creating a network.IPAM, so that this utility is more atomic (potentially
even could be moved to a separate package and exported).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-07-04 01:33:35 +02:00
parent 2eb61318b5
commit b3d8809f42
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 40 additions and 34 deletions

View File

@ -29,13 +29,16 @@ type createOptions struct {
ingress bool ingress bool
configOnly bool configOnly bool
configFrom string configFrom string
ipam ipamOptions
}
ipamDriver string type ipamOptions struct {
ipamSubnet []string driver string
ipamIPRange []string subnets []string
ipamGateway []string ipRanges []string
ipamAux opts.MapOpts gateways []string
ipamOpt opts.MapOpts auxAddresses opts.MapOpts
driverOpts opts.MapOpts
} }
func newCreateCommand(dockerCLI command.Cli) *cobra.Command { func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
@ -43,8 +46,10 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
options := createOptions{ options := createOptions{
driverOpts: *opts.NewMapOpts(nil, nil), driverOpts: *opts.NewMapOpts(nil, nil),
labels: opts.NewListOpts(opts.ValidateLabel), labels: opts.NewListOpts(opts.ValidateLabel),
ipamAux: *opts.NewMapOpts(nil, nil), ipam: ipamOptions{
ipamOpt: *opts.NewMapOpts(nil, nil), auxAddresses: *opts.NewMapOpts(nil, nil),
driverOpts: *opts.NewMapOpts(nil, nil),
},
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -80,19 +85,19 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
flags.StringVar(&options.configFrom, "config-from", "", "The network from which to copy the configuration") flags.StringVar(&options.configFrom, "config-from", "", "The network from which to copy the configuration")
flags.SetAnnotation("config-from", "version", []string{"1.30"}) flags.SetAnnotation("config-from", "version", []string{"1.30"})
flags.StringVar(&options.ipamDriver, "ipam-driver", "default", "IP Address Management Driver") flags.StringVar(&options.ipam.driver, "ipam-driver", "default", "IP Address Management Driver")
flags.StringSliceVar(&options.ipamSubnet, "subnet", []string{}, "Subnet in CIDR format that represents a network segment") flags.StringSliceVar(&options.ipam.subnets, "subnet", []string{}, "Subnet in CIDR format that represents a network segment")
flags.StringSliceVar(&options.ipamIPRange, "ip-range", []string{}, "Allocate container ip from a sub-range") flags.StringSliceVar(&options.ipam.ipRanges, "ip-range", []string{}, "Allocate container ip from a sub-range")
flags.StringSliceVar(&options.ipamGateway, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet") flags.StringSliceVar(&options.ipam.gateways, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet")
flags.Var(&options.ipamAux, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver") flags.Var(&options.ipam.auxAddresses, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver")
flags.Var(&options.ipamOpt, "ipam-opt", "Set IPAM driver specific options") flags.Var(&options.ipam.driverOpts, "ipam-opt", "Set IPAM driver specific options")
return cmd return cmd
} }
func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, options createOptions) error { func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, options createOptions) error {
ipamCfg, err := consolidateIpam(options.ipamSubnet, options.ipamIPRange, options.ipamGateway, options.ipamAux.GetAll()) ipamCfg, err := createIPAMConfig(options.ipam)
if err != nil { if err != nil {
return err return err
} }
@ -106,11 +111,7 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
resp, err := apiClient.NetworkCreate(ctx, options.name, network.CreateOptions{ resp, err := apiClient.NetworkCreate(ctx, options.name, network.CreateOptions{
Driver: options.driver, Driver: options.driver,
Options: options.driverOpts.GetAll(), Options: options.driverOpts.GetAll(),
IPAM: &network.IPAM{ IPAM: ipamCfg,
Driver: options.ipamDriver,
Config: ipamCfg,
Options: options.ipamOpt.GetAll(),
},
Internal: options.internal, Internal: options.internal,
EnableIPv6: options.ipv6, EnableIPv6: options.ipv6,
Attachable: options.attachable, Attachable: options.attachable,
@ -130,18 +131,18 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
// Consolidates the ipam configuration as a group from different related configurations // Consolidates the ipam configuration as a group from different related configurations
// user can configure network with multiple non-overlapping subnets and hence it is // user can configure network with multiple non-overlapping subnets and hence it is
// possible to correlate the various related parameters and consolidate them. // possible to correlate the various related parameters and consolidate them.
// consolidateIpam consolidates subnets, ip-ranges, gateways and auxiliary addresses into // createIPAMConfig consolidates subnets, ip-ranges, gateways and auxiliary addresses into
// structured ipam data. // structured ipam data.
// //
//nolint:gocyclo //nolint:gocyclo
func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]string) ([]network.IPAMConfig, error) { func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
if len(subnets) < len(ranges) || len(subnets) < len(gateways) { if len(options.subnets) < len(options.ipRanges) || len(options.subnets) < len(options.gateways) {
return nil, errors.Errorf("every ip-range or gateway must have a corresponding subnet") return nil, errors.Errorf("every ip-range or gateway must have a corresponding subnet")
} }
iData := map[string]*network.IPAMConfig{} iData := map[string]*network.IPAMConfig{}
// Populate non-overlapping subnets into consolidation map // Populate non-overlapping subnets into consolidation map
for _, s := range subnets { for _, s := range options.subnets {
for k := range iData { for k := range iData {
ok1, err := subnetMatches(s, k) ok1, err := subnetMatches(s, k)
if err != nil { if err != nil {
@ -159,9 +160,9 @@ func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]str
} }
// Validate and add valid ip ranges // Validate and add valid ip ranges
for _, r := range ranges { for _, r := range options.ipRanges {
match := false match := false
for _, s := range subnets { for _, s := range options.subnets {
ok, err := subnetMatches(s, r) ok, err := subnetMatches(s, r)
if err != nil { if err != nil {
return nil, err return nil, err
@ -182,9 +183,9 @@ func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]str
} }
// Validate and add valid gateways // Validate and add valid gateways
for _, g := range gateways { for _, g := range options.gateways {
match := false match := false
for _, s := range subnets { for _, s := range options.subnets {
ok, err := subnetMatches(s, g) ok, err := subnetMatches(s, g)
if err != nil { if err != nil {
return nil, err return nil, err
@ -205,9 +206,9 @@ func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]str
} }
// Validate and add aux-addresses // Validate and add aux-addresses
for key, aa := range auxaddrs { for key, aa := range options.auxAddresses.GetAll() {
match := false match := false
for _, s := range subnets { for _, s := range options.subnets {
ok, err := subnetMatches(s, aa) ok, err := subnetMatches(s, aa)
if err != nil { if err != nil {
return nil, err return nil, err
@ -223,11 +224,16 @@ func consolidateIpam(subnets, ranges, gateways []string, auxaddrs map[string]str
} }
} }
idl := []network.IPAMConfig{} idl := make([]network.IPAMConfig, 0, len(iData))
for _, v := range iData { for _, v := range iData {
idl = append(idl, *v) idl = append(idl, *v)
} }
return idl, nil
return &network.IPAM{
Driver: options.driver,
Config: idl,
Options: options.driverOpts.GetAll(),
}, nil
} }
func subnetMatches(subnet, data string) (bool, error) { func subnetMatches(subnet, data string) (bool, error) {