Reload daemon even if "/etc/docker/daemon.json" does not exist
Sebastiaan van Stijn authored on 2017/10/25 04:23:49... | ... |
@@ -7,6 +7,7 @@ import ( |
7 | 7 |
"fmt" |
8 | 8 |
"io" |
9 | 9 |
"io/ioutil" |
10 |
+ "os" |
|
10 | 11 |
"reflect" |
11 | 12 |
"runtime" |
12 | 13 |
"strings" |
... | ... |
@@ -249,7 +250,10 @@ func Reload(configFile string, flags *pflag.FlagSet, reload func(*Config)) error |
249 | 249 |
logrus.Infof("Got signal to reload configuration, reloading from: %s", configFile) |
250 | 250 |
newConfig, err := getConflictFreeConfiguration(configFile, flags) |
251 | 251 |
if err != nil { |
252 |
- return err |
|
252 |
+ if flags.Changed("config-file") || !os.IsNotExist(err) { |
|
253 |
+ return fmt.Errorf("unable to configure the Docker daemon with file %s: %v", configFile, err) |
|
254 |
+ } |
|
255 |
+ newConfig = New() |
|
253 | 256 |
} |
254 | 257 |
|
255 | 258 |
if err := Validate(newConfig); err != nil { |
... | ... |
@@ -389,3 +389,49 @@ func discoveryConfig(backendAddr, advertiseAddr string, opts map[string]string) |
389 | 389 |
}, |
390 | 390 |
} |
391 | 391 |
} |
392 |
+ |
|
393 |
+// TestReloadSetConfigFileNotExist tests that when `--config-file` is set |
|
394 |
+// and it doesn't exist the `Reload` function returns an error. |
|
395 |
+func TestReloadSetConfigFileNotExist(t *testing.T) { |
|
396 |
+ configFile := "/tmp/blabla/not/exists/config.json" |
|
397 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError) |
|
398 |
+ flags.String("config-file", "", "") |
|
399 |
+ flags.Set("config-file", configFile) |
|
400 |
+ |
|
401 |
+ err := Reload(configFile, flags, func(c *Config) {}) |
|
402 |
+ assert.Error(t, err) |
|
403 |
+ testutil.ErrorContains(t, err, "unable to configure the Docker daemon with file") |
|
404 |
+} |
|
405 |
+ |
|
406 |
+// TestReloadDefaultConfigNotExist tests that if the default configuration file |
|
407 |
+// doesn't exist the daemon still will be reloaded. |
|
408 |
+func TestReloadDefaultConfigNotExist(t *testing.T) { |
|
409 |
+ reloaded := false |
|
410 |
+ configFile := "/etc/docker/daemon.json" |
|
411 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError) |
|
412 |
+ flags.String("config-file", configFile, "") |
|
413 |
+ err := Reload(configFile, flags, func(c *Config) { |
|
414 |
+ reloaded = true |
|
415 |
+ }) |
|
416 |
+ assert.Nil(t, err) |
|
417 |
+ assert.True(t, reloaded) |
|
418 |
+} |
|
419 |
+ |
|
420 |
+// TestReloadBadDefaultConfig tests that when `--config-file` is not set |
|
421 |
+// and the default configuration file exists and is bad return an error |
|
422 |
+func TestReloadBadDefaultConfig(t *testing.T) { |
|
423 |
+ f, err := ioutil.TempFile("", "docker-config-") |
|
424 |
+ if err != nil { |
|
425 |
+ t.Fatal(err) |
|
426 |
+ } |
|
427 |
+ |
|
428 |
+ configFile := f.Name() |
|
429 |
+ f.Write([]byte(`{wrong: "configuration"}`)) |
|
430 |
+ f.Close() |
|
431 |
+ |
|
432 |
+ flags := pflag.NewFlagSet("test", pflag.ContinueOnError) |
|
433 |
+ flags.String("config-file", configFile, "") |
|
434 |
+ err = Reload(configFile, flags, func(c *Config) {}) |
|
435 |
+ assert.Error(t, err) |
|
436 |
+ testutil.ErrorContains(t, err, "unable to configure the Docker daemon with file") |
|
437 |
+} |