Skip to content

Commit

Permalink
nfd-worker: add core.rawFeatureSources config option
Browse files Browse the repository at this point in the history
Add a configuration option for controlling the enabled raw feature
sources. This is useful e.g. in testing and development, plus it also
allows fully shutting down discovery of features that are not needed in
a deployment.  Supplements core.sources which controls the enabled label
sources.
  • Loading branch information
marquiz committed Sep 21, 2021
1 parent e56e2ea commit c36f86e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# noPublish: false
# sleepInterval: 60s
# sources: [all]
# rawFeatureSources: [all]
# klog:
# addDirHeader: false
# alsologtostderr: false
Expand Down
1 change: 1 addition & 0 deletions deployment/helm/node-feature-discovery/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ worker:
# noPublish: false
# sleepInterval: 60s
# sources: [all]
# rawFeatureSources: [all]
# klog:
# addDirHeader: false
# alsologtostderr: false
Expand Down
24 changes: 22 additions & 2 deletions docs/advanced/worker-configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ core:
### core.sources
`core.sources` specifies the list of enabled feature sources. A special value
`all` enables all feature sources.
`core.sources` specifies the list of enabled feature label sources. A special
value `all` enables all sources. This configuration option affects the feature
labels that are being generated but not the discovery of raw features that are
available for custom labels.

Note: Overridden by the deprecated `--sources` command line flag (if
specified).
Expand All @@ -66,6 +68,24 @@ core:
- custom
```

### core.rawFeatureSources

`core.rawFeatureSources` specifies the list of enabled raw feature sources. A
special value `all` enables all sources. The option allows disabling the raw
feature detection of sources so that neither standard feature labels are
generated nor the raw features are available for custom rule processing.

Default: `[all]`

Example:

```yaml
core:
rawFeatureSources:
- cpu
- local
```

### core.labelWhiteList

`core.labelWhiteList` specifies a regular expression for filtering feature
Expand Down
10 changes: 8 additions & 2 deletions pkg/nfd-client/worker/nfd-worker-internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,26 @@ func TestCreateFeatureLabels(t *testing.T) {

func TestAdvertiseFeatureLabels(t *testing.T) {
Convey("When advertising labels", t, func() {
w, err := NewNfdWorker(&Args{})
So(err, ShouldBeNil)
worker := w.(*nfdWorker)

mockClient := &labeler.MockLabelerClient{}
worker.client = mockClient

labels := map[string]string{"feature-1": "value-1"}

Convey("Correct labeling request is sent", func() {
mockClient.On("SetLabels", mock.AnythingOfType("*context.timerCtx"), mock.AnythingOfType("*labeler.SetLabelsRequest")).Return(&labeler.SetLabelsReply{}, nil)
err := advertiseFeatureLabels(mockClient, labels)
err := worker.advertiseFeatureLabels(labels)
Convey("There should be no error", func() {
So(err, ShouldBeNil)
})
})
Convey("Labeling request fails", func() {
mockErr := errors.New("mock-error")
mockClient.On("SetLabels", mock.AnythingOfType("*context.timerCtx"), mock.AnythingOfType("*labeler.SetLabelsRequest")).Return(&labeler.SetLabelsReply{}, mockErr)
err := advertiseFeatureLabels(mockClient, labels)
err := worker.advertiseFeatureLabels(labels)
Convey("An error should be returned", func() {
So(err, ShouldEqual, mockErr)
})
Expand Down
64 changes: 48 additions & 16 deletions pkg/nfd-client/worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ type NFDConfig struct {
}

type coreConfig struct {
Klog map[string]string
LabelWhiteList utils.RegexpVal
NoPublish bool
Sources []string
SleepInterval duration
Klog map[string]string
LabelWhiteList utils.RegexpVal
NoPublish bool
RawFeatureSources []string
Sources []string
SleepInterval duration
}

type sourcesConfig map[string]source.Config
Expand Down Expand Up @@ -103,6 +104,7 @@ type nfdWorker struct {
configFilePath string
config *NFDConfig
stop chan struct{} // channel for signaling stop
featureSources []source.FeatureSource
labelSources []source.LabelSource
}

Expand Down Expand Up @@ -135,10 +137,11 @@ func NewNfdWorker(args *Args) (nfdclient.NfdClient, error) {
func newDefaultConfig() *NFDConfig {
return &NFDConfig{
Core: coreConfig{
LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")},
SleepInterval: duration{60 * time.Second},
Sources: []string{"all"},
Klog: make(map[string]string),
LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")},
SleepInterval: duration{60 * time.Second},
RawFeatureSources: []string{"all"},
Sources: []string{"all"},
Klog: make(map[string]string),
},
}
}
Expand Down Expand Up @@ -176,10 +179,10 @@ func (w *nfdWorker) Run() error {
select {
case <-labelTrigger:
// Run feature discovery
for n, s := range source.GetAllFeatureSources() {
klog.V(2).Infof("running discovery for %q source", n)
for _, s := range w.featureSources {
klog.V(2).Infof("running discovery for %q source", s.Name())
if err := s.Discover(); err != nil {
klog.Errorf("feature discovery of %q source failed: %v", n, err)
klog.Errorf("feature discovery of %q source failed: %v", s.Name(), err)
}
}

Expand All @@ -188,7 +191,7 @@ func (w *nfdWorker) Run() error {

// Update the node with the feature labels.
if w.client != nil {
err := advertiseFeatureLabels(w.client, labels)
err := w.advertiseFeatureLabels(labels)
if err != nil {
return fmt.Errorf("failed to advertise labels: %s", err.Error())
}
Expand Down Expand Up @@ -291,6 +294,29 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
}
}

// Determine enabled feature sources
featureSources := make(map[string]source.FeatureSource)
for _, name := range c.RawFeatureSources {
if name == "all" {
for n, s := range source.GetAllFeatureSources() {
if ts, ok := s.(source.TestSource); !ok || !ts.IsTestSource() {
featureSources[n] = s
}
}
} else {
if s := source.GetFeatureSource(name); s != nil {
featureSources[name] = s
} else {
klog.Warningf("skipping unknown feature source %q specified in core.rawFeatureSources", name)
}
}
}

w.featureSources = make([]source.FeatureSource, 0, len(featureSources))
for _, s := range featureSources {
w.featureSources = append(w.featureSources, s)
}

// Determine enabled label sources
labelSources := make(map[string]source.LabelSource)
for _, name := range c.Sources {
Expand Down Expand Up @@ -323,7 +349,13 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
})

if klog.V(1).Enabled() {
n := make([]string, len(w.labelSources))
n := make([]string, len(w.featureSources))
for i, s := range w.featureSources {
n[i] = s.Name()
}
klog.Infof("enabled raw feature sources: %s", strings.Join(n, ", "))

n = make([]string, len(w.labelSources))
for i, s := range w.labelSources {
n[i] = s.Name()
}
Expand Down Expand Up @@ -481,7 +513,7 @@ func getFeatureLabels(source source.LabelSource, labelWhiteList regexp.Regexp) (

// advertiseFeatureLabels advertises the feature labels to a Kubernetes node
// via the NFD server.
func advertiseFeatureLabels(client pb.LabelerClient, labels Labels) error {
func (w *nfdWorker) advertiseFeatureLabels(labels Labels) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand All @@ -490,7 +522,7 @@ func advertiseFeatureLabels(client pb.LabelerClient, labels Labels) error {
labelReq := pb.SetLabelsRequest{Labels: labels,
NfdVersion: version.Get(),
NodeName: nfdclient.NodeName()}
_, err := client.SetLabels(ctx, &labelReq)
_, err := w.client.SetLabels(ctx, &labelReq)
if err != nil {
klog.Errorf("failed to set node labels: %v", err)
return err
Expand Down

0 comments on commit c36f86e

Please sign in to comment.