mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #35125 from ripcurld0/reload_no_config
Reload daemon even if "/etc/docker/daemon.json" does not exist
This commit is contained in:
commit
04e8d7b8bf
2 changed files with 51 additions and 1 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -249,7 +250,10 @@ func Reload(configFile string, flags *pflag.FlagSet, reload func(*Config)) error
|
||||||
logrus.Infof("Got signal to reload configuration, reloading from: %s", configFile)
|
logrus.Infof("Got signal to reload configuration, reloading from: %s", configFile)
|
||||||
newConfig, err := getConflictFreeConfiguration(configFile, flags)
|
newConfig, err := getConflictFreeConfiguration(configFile, flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if flags.Changed("config-file") || !os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("unable to configure the Docker daemon with file %s: %v", configFile, err)
|
||||||
|
}
|
||||||
|
newConfig = New()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Validate(newConfig); err != nil {
|
if err := Validate(newConfig); err != nil {
|
||||||
|
|
|
@ -389,3 +389,49 @@ func discoveryConfig(backendAddr, advertiseAddr string, opts map[string]string)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestReloadSetConfigFileNotExist tests that when `--config-file` is set
|
||||||
|
// and it doesn't exist the `Reload` function returns an error.
|
||||||
|
func TestReloadSetConfigFileNotExist(t *testing.T) {
|
||||||
|
configFile := "/tmp/blabla/not/exists/config.json"
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.String("config-file", "", "")
|
||||||
|
flags.Set("config-file", configFile)
|
||||||
|
|
||||||
|
err := Reload(configFile, flags, func(c *Config) {})
|
||||||
|
assert.Error(t, err)
|
||||||
|
testutil.ErrorContains(t, err, "unable to configure the Docker daemon with file")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestReloadDefaultConfigNotExist tests that if the default configuration file
|
||||||
|
// doesn't exist the daemon still will be reloaded.
|
||||||
|
func TestReloadDefaultConfigNotExist(t *testing.T) {
|
||||||
|
reloaded := false
|
||||||
|
configFile := "/etc/docker/daemon.json"
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.String("config-file", configFile, "")
|
||||||
|
err := Reload(configFile, flags, func(c *Config) {
|
||||||
|
reloaded = true
|
||||||
|
})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, reloaded)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestReloadBadDefaultConfig tests that when `--config-file` is not set
|
||||||
|
// and the default configuration file exists and is bad return an error
|
||||||
|
func TestReloadBadDefaultConfig(t *testing.T) {
|
||||||
|
f, err := ioutil.TempFile("", "docker-config-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configFile := f.Name()
|
||||||
|
f.Write([]byte(`{wrong: "configuration"}`))
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.String("config-file", configFile, "")
|
||||||
|
err = Reload(configFile, flags, func(c *Config) {})
|
||||||
|
assert.Error(t, err)
|
||||||
|
testutil.ErrorContains(t, err, "unable to configure the Docker daemon with file")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue