fix(initialize): also check for unknown coreos keys
This commit is contained in:
parent
e852be65f7
commit
81fe0dc9e0
@ -45,20 +45,38 @@ type CloudConfig struct {
|
|||||||
|
|
||||||
type warner func(format string, v ...interface{})
|
type warner func(format string, v ...interface{})
|
||||||
|
|
||||||
|
// warnOnUnrecognizedKeys parses the contents of a cloud-config file and calls
|
||||||
|
// warn(msg, key) for every unrecognized key (i.e. those not present in CloudConfig)
|
||||||
func warnOnUnrecognizedKeys(contents string, warn warner) {
|
func warnOnUnrecognizedKeys(contents string, warn warner) {
|
||||||
// Generate a map of all understood cloud config options
|
// Generate a map of all understood cloud config options
|
||||||
var cc map[string]interface{}
|
var cc map[string]interface{}
|
||||||
b, _ := goyaml.Marshal(&CloudConfig{})
|
b, _ := goyaml.Marshal(&CloudConfig{})
|
||||||
goyaml.Unmarshal(b, &cc)
|
goyaml.Unmarshal(b, &cc)
|
||||||
|
|
||||||
// Now unmarshal the entire provided contents
|
// Now unmarshal the entire provided contents
|
||||||
var c map[string]interface{}
|
var c map[string]interface{}
|
||||||
goyaml.Unmarshal([]byte(contents), &c)
|
goyaml.Unmarshal([]byte(contents), &c)
|
||||||
|
|
||||||
// Check that every key in the contents exists in the cloud config
|
// Check that every key in the contents exists in the cloud config
|
||||||
for k, _ := range c {
|
for k, _ := range c {
|
||||||
if _, ok := cc[k]; !ok {
|
if _, ok := cc[k]; !ok {
|
||||||
warn("Warning: unrecognized key %q in provided cloud config - ignoring section", k)
|
warn("Warning: unrecognized key %q in provided cloud config - ignoring section", k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finally, check for unrecognized coreos options, if any are set
|
||||||
|
coreos, ok := c["coreos"]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
set := coreos.(map[interface{}]interface{})
|
||||||
|
known := cc["coreos"].(map[interface{}]interface{})
|
||||||
|
for k, _ := range set {
|
||||||
|
key := k.(string)
|
||||||
|
if _, ok := known[key]; !ok {
|
||||||
|
warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCloudConfig instantiates a new CloudConfig from the given contents (a
|
// NewCloudConfig instantiates a new CloudConfig from the given contents (a
|
||||||
|
@ -11,10 +11,12 @@ func TestCloudConfigUnknownKeys(t *testing.T) {
|
|||||||
coreos:
|
coreos:
|
||||||
etcd:
|
etcd:
|
||||||
discovery: "https://discovery.etcd.io/827c73219eeb2fa5530027c37bf18877"
|
discovery: "https://discovery.etcd.io/827c73219eeb2fa5530027c37bf18877"
|
||||||
unknown:
|
coreos_unknown:
|
||||||
|
foo: "bar"
|
||||||
|
section_unknown:
|
||||||
dunno:
|
dunno:
|
||||||
something
|
something
|
||||||
foo:
|
bare_unknown:
|
||||||
bar
|
bar
|
||||||
hostname:
|
hostname:
|
||||||
foo
|
foo
|
||||||
@ -37,11 +39,14 @@ hostname:
|
|||||||
|
|
||||||
warnOnUnrecognizedKeys(contents, catchWarn)
|
warnOnUnrecognizedKeys(contents, catchWarn)
|
||||||
|
|
||||||
if !strings.Contains(warnings, "foo") {
|
if !strings.Contains(warnings, "coreos_unknown") {
|
||||||
t.Errorf("warnings did not catch unrecognized key foo")
|
t.Errorf("warnings did not catch unrecognized coreos option coreos_unknown")
|
||||||
}
|
}
|
||||||
if !strings.Contains(warnings, "unknown") {
|
if !strings.Contains(warnings, "bare_unknown") {
|
||||||
t.Errorf("warnings did not catch unrecognized key unknown")
|
t.Errorf("warnings did not catch unrecognized key bare_unknown")
|
||||||
|
}
|
||||||
|
if !strings.Contains(warnings, "section_unknown") {
|
||||||
|
t.Errorf("warnings did not catch unrecognized key section_unknown")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user