Skip to content

Commit

Permalink
Make it possible to override config options from command line
Browse files Browse the repository at this point in the history
Implement new '--options' command line flag that can be used to specify
config options from command line. Options specified via this command
line flag will override those read from the config file. The same format
as in the config file must be used, that is, the flag value must be
valid YAML or JSON.
  • Loading branch information
marquiz committed Oct 10, 2018
1 parent 9171517 commit 244e049
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ node-feature-discovery.
Usage:
node-feature-discovery [--no-publish] [--sources=<sources>] [--label-whitelist=<pattern>]
[--oneshot | --sleep-interval=<seconds>] [--config=<path>]
[--options=<config>]
node-feature-discovery -h | --help
node-feature-discovery --version
Expand All @@ -47,6 +48,11 @@ node-feature-discovery.
--version Output version and exit.
--config=<path> Config file to use.
[Default: /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf]
--options=<config> Specify config options from command line. Config
options are specified in the same format as in the
config file (i.e. json or yaml). These options
will override settings read from the config file.
[Default: ]
--sources=<sources> Comma separated list of feature sources.
[Default: cpuid,iommu,memory,network,pstate,rdt,selinux,storage]
--no-publish Do not publish discovered features to the
Expand Down Expand Up @@ -233,7 +239,7 @@ For example, if some node is tainted NoSchedule or fails to start a job for some

[![asciicast](https://asciinema.org/a/11wir751y89617oemwnsgli4a.png)](https://asciinema.org/a/11wir751y89617oemwnsgli4a)

### Configuration file
### Configuration options

NFD supports a configuration file. The default location is
`/etc/kubernetes/node-feature-discovery/node-feature-discovery.conf`, but,
Expand Down Expand Up @@ -267,9 +273,16 @@ different config for different nodes would be required, for example.

The (empty-by-default)
[example config](https://github.com/kubernetes-incubator/node-feature-discovery/blob/master/node-feature-discovery.conf.example)
is used as a config in the NFD Docker image. Thus, this can be used as default
is used as a config in the NFD Docker image. Thus, this can be used as a default
configuration in custom-built images.

Configuration options can also be specified via the `--options` command line
flag, in which case no mounts need to be used. The same format as in the config
file must be used, i.e. JSON (or YAML).

Configuration options specified from the command line will override those read
from the config file.

## Building from source

Download the source code.
Expand Down
22 changes: 18 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Args struct {
labelWhiteList string
configFile string
noPublish bool
options string
oneshot bool
sleepInterval time.Duration
sources []string
Expand All @@ -102,8 +103,8 @@ func main() {
// Parse command-line arguments.
args := argsParse(nil)

// Read the config file
err := configParse(args.configFile)
// Parse config
err := configParse(args.configFile, args.options)
if err != nil {
stderrLogger.Print(err)
}
Expand Down Expand Up @@ -147,6 +148,7 @@ func argsParse(argv []string) (args Args) {
Usage:
%s [--no-publish] [--sources=<sources>] [--label-whitelist=<pattern>]
[--oneshot | --sleep-interval=<seconds>] [--config=<path>]
[--options=<config>]
%s -h | --help
%s --version
Expand All @@ -155,6 +157,11 @@ func argsParse(argv []string) (args Args) {
--version Output version and exit.
--config=<path> Config file to use.
[Default: /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf]
--options=<config> Specify config options from command line. Config
options are specified in the same format as in the
config file (i.e. json or yaml). These options
will override settings read from the config file.
[Default: ]
--sources=<sources> Comma separated list of feature sources.
[Default: cpuid,iommu,memory,network,pstate,rdt,selinux,storage]
--no-publish Do not publish discovered features to the
Expand All @@ -178,6 +185,7 @@ func argsParse(argv []string) (args Args) {
var err error
args.configFile = arguments["--config"].(string)
args.noPublish = arguments["--no-publish"].(bool)
args.options = arguments["--options"].(string)
args.sources = strings.Split(arguments["--sources"].(string), ",")
args.labelWhiteList = arguments["--label-whitelist"].(string)
args.oneshot = arguments["--oneshot"].(bool)
Expand All @@ -195,8 +203,8 @@ func argsParse(argv []string) (args Args) {
return args
}

// Parse configuration file
func configParse(filepath string) error {
// Parse configuration options
func configParse(filepath string, overrides string) error {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return fmt.Errorf("Failed to read config file: %s", err)
Expand All @@ -208,6 +216,12 @@ func configParse(filepath string) error {
return fmt.Errorf("Failed to parse config file: %s", err)
}

// Parse config overrides
err = yaml.Unmarshal([]byte(overrides), &config)
if err != nil {
return fmt.Errorf("Failed to parse --options: %s", err)
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestArgsParse(t *testing.T) {
func TestConfigParse(t *testing.T) {
Convey("When parsing configuration file", t, func() {
Convey("When non-accessible file is given", func() {
err := configParse("non-existing-file")
err := configParse("non-existing-file", "")

Convey("Should return error", func() {
So(err, ShouldNotBeNil)
Expand All @@ -193,7 +193,7 @@ func TestConfigParse(t *testing.T) {
f.Close()

Convey("When proper config file is given", func() {
err := configParse(f.Name())
err := configParse(f.Name(), "")

Convey("Should return error", func() {
So(err, ShouldBeNil)
Expand Down

0 comments on commit 244e049

Please sign in to comment.