1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

Add config file support

Support yaml/json based config file for nfd. This commit does not add
any actual consumers for the config file, yet.

By default, nfd tries to read
/etc/kubernetes/node-feature-discovery/node-feature-discovery.conf.
This can be changed by specifying the --config command line flag.
This commit is contained in:
Markus Lehtonen 2018-07-06 14:11:07 +03:00
parent 4fa7ae38e3
commit 917151728a
5 changed files with 108 additions and 2 deletions

View file

@ -26,6 +26,7 @@ RUN glide install --strip-vendor
RUN go install \
-ldflags "-s -w -X main.version=$NFD_VERSION" \
github.com/kubernetes-incubator/node-feature-discovery
RUN install -D -m644 node-feature-discovery.conf.example /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf
RUN go test .
@ -35,6 +36,7 @@ FROM debian:stretch-slim
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --from=builder /usr/local/lib /usr/local/lib
COPY --from=builder /etc/kubernetes/node-feature-discovery /etc/kubernetes/node-feature-discovery
RUN ldconfig
COPY --from=builder /go/bin/node-feature-discovery /usr/bin/node-feature-discovery

View file

@ -38,13 +38,15 @@ node-feature-discovery.
Usage:
node-feature-discovery [--no-publish] [--sources=<sources>] [--label-whitelist=<pattern>]
[--oneshot | --sleep-interval=<seconds>]
[--oneshot | --sleep-interval=<seconds>] [--config=<path>]
node-feature-discovery -h | --help
node-feature-discovery --version
Options:
-h --help Show this screen.
--version Output version and exit.
--config=<path> Config file to use.
[Default: /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf]
--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
@ -231,6 +233,43 @@ 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
NFD supports a configuration file. The default location is
`/etc/kubernetes/node-feature-discovery/node-feature-discovery.conf`, but,
this can be changed by specifying the`--config` command line flag. The file is
read inside the Docker image, and thus, Volumes and VolumeMounts are needed to
make your configuration available for NFD. The preferred method is to use a
ConfigMap.
For example, create a config map using the example config as a template:
```
cp node-feature-discovery.conf.example node-feature-discovery.conf
vim node-feature-discovery.conf # edit the configuration
kubectl create configmap node-feature-discovery-config --from-file=node-feature-discovery.conf
```
Then, configure Volumes and VolumeMounts in the Pod spec (just the relevant
snippets shown below):
```
...
containers:
volumeMounts:
- name: node-feature-discovery-config
mountPath: "/etc/kubernetes/node-feature-discovery/"
...
volumes:
- name: node-feature-discovery-config
configMap:
name: node-feature-discovery-config
...
```
You could also use other types of volumes, of course. That is, hostPath if
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
configuration in custom-built images.
## Building from source
Download the source code.

38
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
@ -9,6 +10,7 @@ import (
"time"
"github.com/docopt/docopt-go"
"github.com/ghodss/yaml"
"github.com/kubernetes-incubator/node-feature-discovery/source"
"github.com/kubernetes-incubator/node-feature-discovery/source/cpuid"
"github.com/kubernetes-incubator/node-feature-discovery/source/fake"
@ -48,6 +50,14 @@ var (
stderrLogger = log.New(os.Stderr, "", log.LstdFlags)
)
// Global config
type NFDConfig struct {
Sources struct {
} `json:"sources,omitempty"`
}
var config = NFDConfig{}
// Labels are a Kubernetes representation of discovered features.
type Labels map[string]string
@ -76,6 +86,7 @@ type APIHelpers interface {
// Command line arguments
type Args struct {
labelWhiteList string
configFile string
noPublish bool
oneshot bool
sleepInterval time.Duration
@ -91,6 +102,12 @@ func main() {
// Parse command-line arguments.
args := argsParse(nil)
// Read the config file
err := configParse(args.configFile)
if err != nil {
stderrLogger.Print(err)
}
// Configure the parameters for feature discovery.
enabledSources, labelWhiteList, err := configureParameters(args.sources, args.labelWhiteList)
if err != nil {
@ -129,13 +146,15 @@ func argsParse(argv []string) (args Args) {
Usage:
%s [--no-publish] [--sources=<sources>] [--label-whitelist=<pattern>]
[--oneshot | --sleep-interval=<seconds>]
[--oneshot | --sleep-interval=<seconds>] [--config=<path>]
%s -h | --help
%s --version
Options:
-h --help Show this screen.
--version Output version and exit.
--config=<path> Config file to use.
[Default: /etc/kubernetes/node-feature-discovery/node-feature-discovery.conf]
--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
@ -157,6 +176,7 @@ func argsParse(argv []string) (args Args) {
// Parse argument values as usable types.
var err error
args.configFile = arguments["--config"].(string)
args.noPublish = arguments["--no-publish"].(bool)
args.sources = strings.Split(arguments["--sources"].(string), ",")
args.labelWhiteList = arguments["--label-whitelist"].(string)
@ -175,6 +195,22 @@ func argsParse(argv []string) (args Args) {
return args
}
// Parse configuration file
func configParse(filepath string) error {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return fmt.Errorf("Failed to read config file: %s", err)
}
// Read config file
err = yaml.Unmarshal(data, &config)
if err != nil {
return fmt.Errorf("Failed to parse config file: %s", err)
}
return nil
}
// configureParameters returns all the variables required to perform feature
// discovery based on command line arguments.
func configureParameters(sourcesWhiteList []string, labelWhiteListStr string) (enabledSources []source.FeatureSource, labelWhiteList *regexp.Regexp, err error) {

View file

@ -2,6 +2,8 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"
"time"
@ -174,6 +176,32 @@ 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")
Convey("Should return error", func() {
So(err, ShouldNotBeNil)
})
})
// Create a temporary config file
f, err := ioutil.TempFile("", "nfd-test-")
defer os.Remove(f.Name())
So(err, ShouldBeNil)
f.WriteString(`sources:`)
f.Close()
Convey("When proper config file is given", func() {
err := configParse(f.Name())
Convey("Should return error", func() {
So(err, ShouldBeNil)
})
})
})
}
func TestConfigureParameters(t *testing.T) {
Convey("When configuring parameters for node feature discovery", t, func() {

View file

@ -0,0 +1 @@
#sources: