1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-05 08:17:04 +00:00

nfd-worker: stop masking crashes in feature discovery

The code should be stable enough. If there are fatal bugs causing the
discovery to panic/segfault that should be made visible instead of
semi-siently hiding it. Also, this caused one (negative) test case to
fail undetected which is now fixed.
This commit is contained in:
Markus Lehtonen 2021-03-01 09:14:19 +02:00
parent e370429970
commit 5e6f0779e9
3 changed files with 4 additions and 72 deletions

View file

@ -17,7 +17,6 @@ limitations under the License.
package nfdworker
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -36,7 +35,6 @@ import (
"sigs.k8s.io/node-feature-discovery/source/cpu"
"sigs.k8s.io/node-feature-discovery/source/fake"
"sigs.k8s.io/node-feature-discovery/source/kernel"
"sigs.k8s.io/node-feature-discovery/source/panic_fake"
"sigs.k8s.io/node-feature-discovery/source/pci"
)
@ -358,12 +356,12 @@ func TestNewNfdWorker(t *testing.T) {
func TestCreateFeatureLabels(t *testing.T) {
Convey("When creating feature labels from the configured sources", t, func() {
fakeFeatureSource := source.FeatureSource(new(fake.Source))
fakeFeatureSource.SetConfig(fakeFeatureSource.NewConfig())
sources := []source.FeatureSource{fakeFeatureSource}
Convey("When fake feature source is configured", func() {
emptyLabelWL := regexp.MustCompile("")
fakeFeatureSource := source.FeatureSource(new(fake.Source))
fakeFeatureSource.SetConfig(fakeFeatureSource.NewConfig())
sources := []source.FeatureSource{}
sources = append(sources, fakeFeatureSource)
labels := createFeatureLabels(sources, *emptyLabelWL)
Convey("Proper fake labels are returned", func() {
@ -374,9 +372,6 @@ func TestCreateFeatureLabels(t *testing.T) {
})
})
Convey("When fake feature source is configured with a whitelist that doesn't match", func() {
fakeFeatureSource := source.FeatureSource(new(fake.Source))
sources := []source.FeatureSource{}
sources = append(sources, fakeFeatureSource)
labels := createFeatureLabels(sources, *regexp.MustCompile(".*rdt.*"))
Convey("fake labels are not returned", func() {
@ -389,21 +384,6 @@ func TestCreateFeatureLabels(t *testing.T) {
})
}
func TestGetFeatureLabels(t *testing.T) {
Convey("When I get feature labels and panic occurs during discovery of a feature source", t, func() {
fakePanicFeatureSource := source.FeatureSource(new(panicfake.Source))
returnedLabels, err := getFeatureLabels(fakePanicFeatureSource, *regexp.MustCompile(""))
Convey("No label is returned", func() {
So(len(returnedLabels), ShouldEqual, 0)
})
Convey("Error is produced and panic error is returned", func() {
So(err, ShouldResemble, fmt.Errorf("fake panic error"))
})
})
}
func TestAdvertiseFeatureLabels(t *testing.T) {
Convey("When advertising labels", t, func() {
mockClient := &labeler.MockLabelerClient{}

View file

@ -48,7 +48,6 @@ import (
"sigs.k8s.io/node-feature-discovery/source/local"
"sigs.k8s.io/node-feature-discovery/source/memory"
"sigs.k8s.io/node-feature-discovery/source/network"
"sigs.k8s.io/node-feature-discovery/source/panic_fake"
"sigs.k8s.io/node-feature-discovery/source/pci"
"sigs.k8s.io/node-feature-discovery/source/storage"
"sigs.k8s.io/node-feature-discovery/source/system"
@ -146,7 +145,6 @@ func NewNfdWorker(args *Args) (NfdWorker, error) {
},
testSources: []source.FeatureSource{
&fake.Source{},
&panicfake.Source{},
},
stop: make(chan struct{}, 1),
}
@ -527,13 +525,6 @@ func createFeatureLabels(sources []source.FeatureSource, labelWhiteList regexp.R
// getFeatureLabels returns node labels for features discovered by the
// supplied source.
func getFeatureLabels(source source.FeatureSource, labelWhiteList regexp.Regexp) (labels Labels, err error) {
defer func() {
if r := recover(); r != nil {
klog.Errorf("panic occurred during discovery of source [%s]: %v", source.Name(), r)
err = fmt.Errorf("%v", r)
}
}()
labels = Labels{}
features, err := source.Discover()
if err != nil {

View file

@ -1,39 +0,0 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package panicfake
import "sigs.k8s.io/node-feature-discovery/source"
// Source implements FeatureSource.
type Source struct{}
// Name returns an identifier string for this feature source.
func (s Source) Name() string { return "panic_fake" }
// NewConfig method of the FeatureSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
func (s *Source) SetConfig(source.Config) {}
// Discover calls panic().
func (s Source) Discover() (source.Features, error) {
panic("fake panic error")
}