Signed-off-by: Wang Yumu <37442693@qq.com>
| ... | ... |
@@ -8,6 +8,7 @@ import ( |
| 8 | 8 |
|
| 9 | 9 |
"github.com/docker/docker/daemon/discovery" |
| 10 | 10 |
"github.com/docker/docker/opts" |
| 11 |
+ "github.com/docker/libnetwork/ipamutils" |
|
| 11 | 12 |
"github.com/spf13/pflag" |
| 12 | 13 |
"gotest.tools/v3/assert" |
| 13 | 14 |
is "gotest.tools/v3/assert/cmp" |
| ... | ... |
@@ -153,6 +154,48 @@ func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
|
| 153 | 153 |
} |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 |
+// Test for #40711 |
|
| 157 |
+func TestDaemonConfigurationMergeDefaultAddressPools(t *testing.T) {
|
|
| 158 |
+ emptyConfigFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
|
|
| 159 |
+ defer emptyConfigFile.Remove() |
|
| 160 |
+ configFile := fs.NewFile(t, "config", fs.WithContent(`{"default-address-pools":[{"base": "10.123.0.0/16", "size": 24 }]}`))
|
|
| 161 |
+ defer configFile.Remove() |
|
| 162 |
+ |
|
| 163 |
+ expected := []*ipamutils.NetworkToSplit{{Base: "10.123.0.0/16", Size: 24}}
|
|
| 164 |
+ |
|
| 165 |
+ t.Run("empty config file", func(t *testing.T) {
|
|
| 166 |
+ var conf = Config{}
|
|
| 167 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
| 168 |
+ flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "") |
|
| 169 |
+ flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
|
| 170 |
+ |
|
| 171 |
+ config, err := MergeDaemonConfigurations(&conf, flags, emptyConfigFile.Path()) |
|
| 172 |
+ assert.NilError(t, err) |
|
| 173 |
+ assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected) |
|
| 174 |
+ }) |
|
| 175 |
+ |
|
| 176 |
+ t.Run("config file", func(t *testing.T) {
|
|
| 177 |
+ var conf = Config{}
|
|
| 178 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
| 179 |
+ flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "") |
|
| 180 |
+ |
|
| 181 |
+ config, err := MergeDaemonConfigurations(&conf, flags, configFile.Path()) |
|
| 182 |
+ assert.NilError(t, err) |
|
| 183 |
+ assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected) |
|
| 184 |
+ }) |
|
| 185 |
+ |
|
| 186 |
+ t.Run("with conflicting options", func(t *testing.T) {
|
|
| 187 |
+ var conf = Config{}
|
|
| 188 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
| 189 |
+ flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "") |
|
| 190 |
+ flags.Set("default-address-pool", "base=10.123.0.0/16,size=24")
|
|
| 191 |
+ |
|
| 192 |
+ _, err := MergeDaemonConfigurations(&conf, flags, configFile.Path()) |
|
| 193 |
+ assert.ErrorContains(t, err, "the following directives are specified both as a flag and in the configuration file") |
|
| 194 |
+ assert.ErrorContains(t, err, "default-address-pools") |
|
| 195 |
+ }) |
|
| 196 |
+} |
|
| 197 |
+ |
|
| 156 | 198 |
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
|
| 157 | 199 |
config := map[string]interface{}{"tls-verify": "true"}
|
| 158 | 200 |
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
| ... | ... |
@@ -12,12 +12,12 @@ import ( |
| 12 | 12 |
|
| 13 | 13 |
// PoolsOpt is a Value type for parsing the default address pools definitions |
| 14 | 14 |
type PoolsOpt struct {
|
| 15 |
- values []*types.NetworkToSplit |
|
| 15 |
+ Values []*types.NetworkToSplit |
|
| 16 | 16 |
} |
| 17 | 17 |
|
| 18 | 18 |
// UnmarshalJSON fills values structure info from JSON input |
| 19 | 19 |
func (p *PoolsOpt) UnmarshalJSON(raw []byte) error {
|
| 20 |
- return json.Unmarshal(raw, &(p.values)) |
|
| 20 |
+ return json.Unmarshal(raw, &(p.Values)) |
|
| 21 | 21 |
} |
| 22 | 22 |
|
| 23 | 23 |
// Set predefined pools |
| ... | ... |
@@ -53,7 +53,7 @@ func (p *PoolsOpt) Set(value string) error {
|
| 53 | 53 |
} |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 |
- p.values = append(p.values, &poolsDef) |
|
| 56 |
+ p.Values = append(p.Values, &poolsDef) |
|
| 57 | 57 |
|
| 58 | 58 |
return nil |
| 59 | 59 |
} |
| ... | ... |
@@ -66,7 +66,7 @@ func (p *PoolsOpt) Type() string {
|
| 66 | 66 |
// String returns a string repr of this option |
| 67 | 67 |
func (p *PoolsOpt) String() string {
|
| 68 | 68 |
var pools []string |
| 69 |
- for _, pool := range p.values {
|
|
| 69 |
+ for _, pool := range p.Values {
|
|
| 70 | 70 |
repr := fmt.Sprintf("%s %d", pool.Base, pool.Size)
|
| 71 | 71 |
pools = append(pools, repr) |
| 72 | 72 |
} |
| ... | ... |
@@ -75,7 +75,7 @@ func (p *PoolsOpt) String() string {
|
| 75 | 75 |
|
| 76 | 76 |
// Value returns the mounts |
| 77 | 77 |
func (p *PoolsOpt) Value() []*types.NetworkToSplit {
|
| 78 |
- return p.values |
|
| 78 |
+ return p.Values |
|
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
// Name returns the flag name of this option |