mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #40711 from wangyumu/fix-merge-flags-address-pools
merge default address pool flags with daemon config
This commit is contained in:
commit
a770dc191e
2 changed files with 48 additions and 5 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/daemon/discovery"
|
"github.com/docker/docker/daemon/discovery"
|
||||||
"github.com/docker/docker/opts"
|
"github.com/docker/docker/opts"
|
||||||
|
"github.com/docker/libnetwork/ipamutils"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
|
@ -153,6 +154,48 @@ func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for #40711
|
||||||
|
func TestDaemonConfigurationMergeDefaultAddressPools(t *testing.T) {
|
||||||
|
emptyConfigFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
|
||||||
|
defer emptyConfigFile.Remove()
|
||||||
|
configFile := fs.NewFile(t, "config", fs.WithContent(`{"default-address-pools":[{"base": "10.123.0.0/16", "size": 24 }]}`))
|
||||||
|
defer configFile.Remove()
|
||||||
|
|
||||||
|
expected := []*ipamutils.NetworkToSplit{{Base: "10.123.0.0/16", Size: 24}}
|
||||||
|
|
||||||
|
t.Run("empty config file", func(t *testing.T) {
|
||||||
|
var conf = Config{}
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
||||||
|
flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
||||||
|
|
||||||
|
config, err := MergeDaemonConfigurations(&conf, flags, emptyConfigFile.Path())
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("config file", func(t *testing.T) {
|
||||||
|
var conf = Config{}
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
||||||
|
|
||||||
|
config, err := MergeDaemonConfigurations(&conf, flags, configFile.Path())
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("with conflicting options", func(t *testing.T) {
|
||||||
|
var conf = Config{}
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
||||||
|
flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
||||||
|
|
||||||
|
_, err := MergeDaemonConfigurations(&conf, flags, configFile.Path())
|
||||||
|
assert.ErrorContains(t, err, "the following directives are specified both as a flag and in the configuration file")
|
||||||
|
assert.ErrorContains(t, err, "default-address-pools")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
|
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
|
||||||
config := map[string]interface{}{"tls-verify": "true"}
|
config := map[string]interface{}{"tls-verify": "true"}
|
||||||
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
|
|
@ -12,12 +12,12 @@ import (
|
||||||
|
|
||||||
// PoolsOpt is a Value type for parsing the default address pools definitions
|
// PoolsOpt is a Value type for parsing the default address pools definitions
|
||||||
type PoolsOpt struct {
|
type PoolsOpt struct {
|
||||||
values []*types.NetworkToSplit
|
Values []*types.NetworkToSplit
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON fills values structure info from JSON input
|
// UnmarshalJSON fills values structure info from JSON input
|
||||||
func (p *PoolsOpt) UnmarshalJSON(raw []byte) error {
|
func (p *PoolsOpt) UnmarshalJSON(raw []byte) error {
|
||||||
return json.Unmarshal(raw, &(p.values))
|
return json.Unmarshal(raw, &(p.Values))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set predefined pools
|
// Set predefined pools
|
||||||
|
@ -53,7 +53,7 @@ func (p *PoolsOpt) Set(value string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p.values = append(p.values, &poolsDef)
|
p.Values = append(p.Values, &poolsDef)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func (p *PoolsOpt) Type() string {
|
||||||
// String returns a string repr of this option
|
// String returns a string repr of this option
|
||||||
func (p *PoolsOpt) String() string {
|
func (p *PoolsOpt) String() string {
|
||||||
var pools []string
|
var pools []string
|
||||||
for _, pool := range p.values {
|
for _, pool := range p.Values {
|
||||||
repr := fmt.Sprintf("%s %d", pool.Base, pool.Size)
|
repr := fmt.Sprintf("%s %d", pool.Base, pool.Size)
|
||||||
pools = append(pools, repr)
|
pools = append(pools, repr)
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ func (p *PoolsOpt) String() string {
|
||||||
|
|
||||||
// Value returns the mounts
|
// Value returns the mounts
|
||||||
func (p *PoolsOpt) Value() []*types.NetworkToSplit {
|
func (p *PoolsOpt) Value() []*types.NetworkToSplit {
|
||||||
return p.values
|
return p.Values
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the flag name of this option
|
// Name returns the flag name of this option
|
||||||
|
|
Loading…
Add table
Reference in a new issue