1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-28 02:37:11 +00:00

nfd-worker: bail out on invalid config file

Changes the behaviour so that if the specified configuration file exists
it must be valid. Error out at startup if the config is invalid.
Similarly, exit with an error at runtime if the config file becomes
invalid. Bailing out, instead of just printing an error, was a
deliberate choice in order to make configuration mistakes evident.

Having no configuration file is tolerated, however. If the specified
configuration file does not exists nfd-worker resorts to default
settings.
This commit is contained in:
Markus Lehtonen 2020-11-30 17:23:28 +02:00
parent 7e88f00e05
commit c2c9dff724
2 changed files with 28 additions and 18 deletions

View file

@ -110,7 +110,7 @@ func TestConfigParse(t *testing.T) {
worker := w.(*nfdWorker)
Convey("and a non-accessible file and some overrides are specified", func() {
overrides := `{"sources": {"cpu": {"cpuid": {"attributeBlacklist": ["foo","bar"]}}}}`
worker.configure("non-existing-file", overrides)
So(worker.configure("non-existing-file", overrides), ShouldBeNil)
Convey("overrides should take effect", func() {
c := worker.getSource("cpu").GetConfig().(*cpu.Config)
@ -132,7 +132,7 @@ func TestConfigParse(t *testing.T) {
So(err, ShouldBeNil)
Convey("and a proper config file is specified", func() {
worker.configure(f.Name(), "")
So(worker.configure(f.Name(), ""), ShouldBeNil)
Convey("specified configuration should take effect", func() {
So(err, ShouldBeNil)
@ -145,7 +145,7 @@ func TestConfigParse(t *testing.T) {
Convey("and a proper config file and overrides are given", func() {
overrides := `{"sources": {"pci": {"deviceClassWhitelist": ["03"]}}}`
worker.configure(f.Name(), overrides)
So(worker.configure(f.Name(), overrides), ShouldBeNil)
Convey("overrides should take precedence over the config file", func() {
So(err, ShouldBeNil)
@ -170,7 +170,7 @@ func TestNewNfdWorker(t *testing.T) {
So(err, ShouldBeNil)
})
worker := w.(*nfdWorker)
worker.configure("", "")
So(worker.configure("", ""), ShouldBeNil)
Convey("all sources should be enabled and the whitelist regexp should be empty", func() {
So(len(worker.enabledSources), ShouldEqual, len(worker.realSources))
So(worker.config.Core.LabelWhiteList, ShouldResemble, emptyRegexp)
@ -184,7 +184,7 @@ func TestNewNfdWorker(t *testing.T) {
So(err, ShouldBeNil)
})
worker := w.(*nfdWorker)
worker.configure("", "")
So(worker.configure("", ""), ShouldBeNil)
Convey("proper sources should be enabled", func() {
So(len(worker.enabledSources), ShouldEqual, 1)
So(worker.enabledSources[0], ShouldHaveSameTypeAs, &fake.Source{})
@ -199,7 +199,7 @@ func TestNewNfdWorker(t *testing.T) {
So(err, ShouldBeNil)
})
worker := w.(*nfdWorker)
worker.configure("", "")
So(worker.configure("", ""), ShouldBeNil)
expectRegexp := regex{*regexp.MustCompile(".*rdt.*")}
Convey("proper labelWhiteList regexp should be produced", func() {
So(worker.config.Core.LabelWhiteList, ShouldResemble, expectRegexp)

View file

@ -218,7 +218,9 @@ func (w *nfdWorker) Run() error {
if err != nil {
return err
}
w.configure(w.configFilePath, w.args.Options)
if err := w.configure(w.configFilePath, w.args.Options); err != nil {
return err
}
// Connect to NFD master
err = w.connect()
@ -277,7 +279,9 @@ func (w *nfdWorker) Run() error {
stderrLogger.Printf("ERROR: config file watcher error: %v", e)
case <-configTrigger:
w.configure(w.configFilePath, w.args.Options)
if err := w.configure(w.configFilePath, w.args.Options); err != nil {
return err
}
// Manage connection to master
if w.config.Core.NoPublish {
w.disconnect()
@ -396,7 +400,7 @@ func (w *nfdWorker) configureCore(c coreConfig) {
}
// Parse configuration options
func (w *nfdWorker) configure(filepath string, overrides string) {
func (w *nfdWorker) configure(filepath string, overrides string) error {
// Create a new default config
c := newDefaultConfig()
allSources := append(w.realSources, w.testSources...)
@ -406,22 +410,26 @@ func (w *nfdWorker) configure(filepath string, overrides string) {
}
// Try to read and parse config file
data, err := ioutil.ReadFile(filepath)
if err != nil {
stderrLogger.Printf("Failed to read config file: %s", err)
} else {
err = yaml.Unmarshal(data, c)
if filepath != "" {
data, err := ioutil.ReadFile(filepath)
if err != nil {
stderrLogger.Printf("Failed to parse config file: %s", err)
if os.IsNotExist(err) {
stderrLogger.Printf("config file %q not found, using defaults", filepath)
} else {
return fmt.Errorf("error reading config file: %s", err)
}
} else {
err = yaml.Unmarshal(data, c)
if err != nil {
return fmt.Errorf("Failed to parse config file: %s", err)
}
stdoutLogger.Printf("Configuration successfully loaded from %q", filepath)
}
}
// Parse config overrides
err = yaml.Unmarshal([]byte(overrides), c)
if err != nil {
stderrLogger.Printf("Failed to parse --options: %s", err)
if err := yaml.Unmarshal([]byte(overrides), c); err != nil {
return fmt.Errorf("Failed to parse --options: %s", err)
}
if w.args.LabelWhiteList != nil {
@ -447,6 +455,8 @@ func (w *nfdWorker) configure(filepath string, overrides string) {
for _, s := range allSources {
s.SetConfig(c.Sources[s.Name()])
}
return nil
}
// createFeatureLabels returns the set of feature labels from the enabled