1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-15 17:50:49 +00:00

Make it possible to override config options from command line

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.
This commit is contained in:
Markus Lehtonen 2018-09-21 11:26:57 +03:00
parent 917151728a
commit 244e049729
3 changed files with 35 additions and 8 deletions

View file

@ -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
@ -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
@ -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,
@ -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.

22
main.go
View file

@ -88,6 +88,7 @@ type Args struct {
labelWhiteList string
configFile string
noPublish bool
options string
oneshot bool
sleepInterval time.Duration
sources []string
@ -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)
}
@ -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
@ -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
@ -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)
@ -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)
@ -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
}

View file

@ -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)
@ -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)