Browse code

Make TLSOptions and LogConfig embedded structs.

That way the configuration file becomes flag, without extra keys.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2016/01/23 03:14:48
Showing 4 changed files
... ...
@@ -56,7 +56,6 @@ type CommonConfig struct {
56 56
 	GraphDriver          string              `json:"storage-driver,omitempty"`
57 57
 	GraphOptions         []string            `json:"storage-opts,omitempty"`
58 58
 	Labels               []string            `json:"labels,omitempty"`
59
-	LogConfig            LogConfig           `json:"log-config,omitempty"`
60 59
 	Mtu                  int                 `json:"mtu,omitempty"`
61 60
 	Pidfile              string              `json:"pidfile,omitempty"`
62 61
 	Root                 string              `json:"graph,omitempty"`
... ...
@@ -76,12 +75,16 @@ type CommonConfig struct {
76 76
 	// reachable by other hosts.
77 77
 	ClusterAdvertise string `json:"cluster-advertise,omitempty"`
78 78
 
79
-	Debug      bool             `json:"debug,omitempty"`
80
-	Hosts      []string         `json:"hosts,omitempty"`
81
-	LogLevel   string           `json:"log-level,omitempty"`
82
-	TLS        bool             `json:"tls,omitempty"`
83
-	TLSVerify  bool             `json:"tlsverify,omitempty"`
84
-	TLSOptions CommonTLSOptions `json:"tls-opts,omitempty"`
79
+	Debug     bool     `json:"debug,omitempty"`
80
+	Hosts     []string `json:"hosts,omitempty"`
81
+	LogLevel  string   `json:"log-level,omitempty"`
82
+	TLS       bool     `json:"tls,omitempty"`
83
+	TLSVerify bool     `json:"tlsverify,omitempty"`
84
+
85
+	// Embedded structs that allow config
86
+	// deserialization without the full struct.
87
+	CommonTLSOptions
88
+	LogConfig
85 89
 
86 90
 	reloadLock sync.Mutex
87 91
 	valuesSet  map[string]interface{}
... ...
@@ -227,7 +230,8 @@ func findConfigurationConflicts(config map[string]interface{}, flags *flag.FlagS
227 227
 		}
228 228
 	}
229 229
 
230
-	// 2. Discard keys that might have a given name, like `labels`.
230
+	// 2. Discard values that implement NamedOption.
231
+	// Their configuration name differs from their flag name, like `labels` and `label`.
231 232
 	unknownNamedConflicts := func(f *flag.Flag) {
232 233
 		if namedOption, ok := f.Value.(opts.NamedOption); ok {
233 234
 			if _, valid := unknownKeys[namedOption.Name()]; valid {
... ...
@@ -204,9 +204,9 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
204 204
 	defaultHost := opts.DefaultHost
205 205
 	if cli.Config.TLS {
206 206
 		tlsOptions := tlsconfig.Options{
207
-			CAFile:   cli.Config.TLSOptions.CAFile,
208
-			CertFile: cli.Config.TLSOptions.CertFile,
209
-			KeyFile:  cli.Config.TLSOptions.KeyFile,
207
+			CAFile:   cli.Config.CommonTLSOptions.CAFile,
208
+			CertFile: cli.Config.CommonTLSOptions.CertFile,
209
+			KeyFile:  cli.Config.CommonTLSOptions.KeyFile,
210 210
 		}
211 211
 
212 212
 		if cli.Config.TLSVerify {
... ...
@@ -338,12 +338,12 @@ func loadDaemonCliConfig(config *daemon.Config, daemonFlags *flag.FlagSet, commo
338 338
 	config.LogLevel = commonConfig.LogLevel
339 339
 	config.TLS = commonConfig.TLS
340 340
 	config.TLSVerify = commonConfig.TLSVerify
341
-	config.TLSOptions = daemon.CommonTLSOptions{}
341
+	config.CommonTLSOptions = daemon.CommonTLSOptions{}
342 342
 
343 343
 	if commonConfig.TLSOptions != nil {
344
-		config.TLSOptions.CAFile = commonConfig.TLSOptions.CAFile
345
-		config.TLSOptions.CertFile = commonConfig.TLSOptions.CertFile
346
-		config.TLSOptions.KeyFile = commonConfig.TLSOptions.KeyFile
344
+		config.CommonTLSOptions.CAFile = commonConfig.TLSOptions.CAFile
345
+		config.CommonTLSOptions.CertFile = commonConfig.TLSOptions.CertFile
346
+		config.CommonTLSOptions.KeyFile = commonConfig.TLSOptions.KeyFile
347 347
 	}
348 348
 
349 349
 	if configFile != "" {
... ...
@@ -51,8 +51,8 @@ func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
51 51
 	if loadedConfig == nil {
52 52
 		t.Fatalf("expected configuration %v, got nil", c)
53 53
 	}
54
-	if loadedConfig.TLSOptions.CAFile != "/tmp/ca.pem" {
55
-		t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.TLSOptions.CAFile, loadedConfig)
54
+	if loadedConfig.CommonTLSOptions.CAFile != "/tmp/ca.pem" {
55
+		t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.CommonTLSOptions.CAFile, loadedConfig)
56 56
 	}
57 57
 }
58 58
 
... ...
@@ -217,9 +217,12 @@ func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
217 217
 	}
218 218
 }
219 219
 
220
-func TestLoadDaemonCliConfigWithTLSOptions(t *testing.T) {
220
+func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
221 221
 	c := &daemon.Config{}
222 222
 	common := &cli.CommonFlags{}
223
+	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
224
+	flags.String([]string{"-tlscacert"}, "", "")
225
+	flags.String([]string{"-log-driver"}, "", "")
223 226
 
224 227
 	f, err := ioutil.TempFile("", "docker-config-")
225 228
 	if err != nil {
... ...
@@ -227,11 +230,9 @@ func TestLoadDaemonCliConfigWithTLSOptions(t *testing.T) {
227 227
 	}
228 228
 
229 229
 	configFile := f.Name()
230
-	f.Write([]byte(`{"tls-opts": {"tlscacert": "/etc/certs/ca.pem"}}`))
230
+	f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`))
231 231
 	f.Close()
232 232
 
233
-	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
234
-	flags.String([]string{"-tlscacert"}, "", "")
235 233
 	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
236 234
 	if err != nil {
237 235
 		t.Fatal(err)
... ...
@@ -239,7 +240,10 @@ func TestLoadDaemonCliConfigWithTLSOptions(t *testing.T) {
239 239
 	if loadedConfig == nil {
240 240
 		t.Fatalf("expected configuration %v, got nil", c)
241 241
 	}
242
-	if loadedConfig.TLSOptions.CAFile != "/etc/certs/ca.pem" {
243
-		t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.TLSOptions.CAFile)
242
+	if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
243
+		t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
244
+	}
245
+	if loadedConfig.LogConfig.Type != "syslog" {
246
+		t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
244 247
 	}
245 248
 }
... ...
@@ -838,10 +838,8 @@ This is a full example of the allowed configuration options in the file:
838 838
 	"storage-driver": "",
839 839
 	"storage-opts": "",
840 840
 	"labels": [],
841
-	"log-config": {
842
-		"log-driver": "",
843
-		"log-opts": []
844
-	},
841
+	"log-driver": "",
842
+	"log-opts": [],
845 843
 	"mtu": 0,
846 844
 	"pidfile": "",
847 845
 	"graph": "",
... ...
@@ -853,11 +851,9 @@ This is a full example of the allowed configuration options in the file:
853 853
 	"log-level": "",
854 854
 	"tls": true,
855 855
 	"tlsverify": true,
856
-	"tls-opts": {
857
-		"tlscacert": "",
858
-		"tlscert": "",
859
-		"tlskey": ""
860
-	},
856
+	"tlscacert": "",
857
+	"tlscert": "",
858
+	"tlskey": "",
861 859
 	"api-cors-headers": "",
862 860
 	"selinux-enabled": false,
863 861
 	"userns-remap": "",