merge default address pool flags with daemon config#40711
merge default address pool flags with daemon config#40711cpuguy83 merged 1 commit intomoby:masterfrom
Conversation
9309c5f to
bd3e4f9
Compare
Signed-off-by: Wang Yumu <37442693@qq.com>
|
@thaJeztah PTAL, thanks. |
Ah, hm, good call Do you think it would be possible to have a test for this? With #40388 merged, I expect it to be easier to call the API, and verify that the excepted settings were loaded 🤔 wdyt? There are some existing tests in moby/daemon/config/config_test.go Line 41 in 3af8d48 |
bd3e4f9 to
1239ba5
Compare
|
@thaJeztah good idea, I added a new test: |
thaJeztah
left a comment
There was a problem hiding this comment.
Thanks! Left some suggestions inline 🤗
daemon/config/config_test.go
Outdated
There was a problem hiding this comment.
Probably good to check that the value is what we expected. Something like;
| if config.DefaultAddressPools.Value() == nil || len(config.DefaultAddressPools.Value()) == 0 { | |
| t.Fatalf("expect result not empty, but got %v", config.DefaultAddressPools.Value()) | |
| } | |
| expected := []*ipamutils.NetworkToSplit{{Base: "10.123.0.0/16", Size: 24}} | |
| assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected) |
There was a problem hiding this comment.
I had the branch checked out locally, so perhaps easier to copy/paste here what I wrote up quickly with my suggestions;
// 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")
})
}Let me know if you think that looks good (and feel free to copy to your PR)
There was a problem hiding this comment.
It looks wonderful, really appreciate your work, I will copy it. 👍
8102107 to
c8008bf
Compare
thaJeztah
left a comment
There was a problem hiding this comment.
LGTM if green; thanks so much!
- What I did
Fixes #40388
this PR will fix the bug that cause
dockerd ignores the --default-address-pool option- How I did it
We use
mergo.Mergeto merge the options from command-line and daemon.json, butMergo won't merge unexported (private) fields(see https://github.com/imdario/mergo#usage), that's whydefault-address-poolwere ignored.So I just change
valuestoValuesof PoolsOpt struct.- How to verify it
Steps to verify:
it successfully allocate from the default pool set on command-line