2016-12-13 23:04:59 +00:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
types "github.com/docker/docker/libnetwork/ipamutils"
|
2016-12-13 23:04:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PoolsOpt is a Value type for parsing the default address pools definitions
|
|
|
|
type PoolsOpt struct {
|
2020-07-21 00:54:51 +00:00
|
|
|
Values []*types.NetworkToSplit
|
2016-12-13 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON fills values structure info from JSON input
|
|
|
|
func (p *PoolsOpt) UnmarshalJSON(raw []byte) error {
|
2020-07-21 00:54:51 +00:00
|
|
|
return json.Unmarshal(raw, &(p.Values))
|
2016-12-13 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set predefined pools
|
|
|
|
func (p *PoolsOpt) Set(value string) error {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(value))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
poolsDef := types.NetworkToSplit{}
|
|
|
|
|
|
|
|
for _, field := range fields {
|
2022-11-01 10:13:45 +00:00
|
|
|
// TODO(thaJeztah): this should not be case-insensitive.
|
|
|
|
key, val, ok := strings.Cut(strings.ToLower(field), "=")
|
|
|
|
if !ok {
|
2016-12-13 23:04:59 +00:00
|
|
|
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "base":
|
2022-11-01 10:13:45 +00:00
|
|
|
poolsDef.Base = val
|
2016-12-13 23:04:59 +00:00
|
|
|
case "size":
|
2022-11-01 10:13:45 +00:00
|
|
|
size, err := strconv.Atoi(val)
|
2016-12-13 23:04:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid size value: %q (must be integer): %v", value, err)
|
|
|
|
}
|
|
|
|
poolsDef.Size = size
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 00:54:51 +00:00
|
|
|
p.Values = append(p.Values, &poolsDef)
|
2016-12-13 23:04:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (p *PoolsOpt) Type() string {
|
|
|
|
return "pool-options"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (p *PoolsOpt) String() string {
|
2018-05-19 11:38:54 +00:00
|
|
|
var pools []string
|
2020-07-21 00:54:51 +00:00
|
|
|
for _, pool := range p.Values {
|
2016-12-13 23:04:59 +00:00
|
|
|
repr := fmt.Sprintf("%s %d", pool.Base, pool.Size)
|
|
|
|
pools = append(pools, repr)
|
|
|
|
}
|
|
|
|
return strings.Join(pools, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the mounts
|
|
|
|
func (p *PoolsOpt) Value() []*types.NetworkToSplit {
|
2020-07-21 00:54:51 +00:00
|
|
|
return p.Values
|
2016-12-13 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the flag name of this option
|
|
|
|
func (p *PoolsOpt) Name() string {
|
|
|
|
return "default-address-pools"
|
|
|
|
}
|