1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-14 20:56:42 +00:00

source: rename FeatureSource to LabelSource

Prepare for separating feature detection from label creation.
This commit is contained in:
Markus Lehtonen 2021-03-01 07:45:32 +02:00
parent 795a7e543e
commit befa7e9796
17 changed files with 124 additions and 123 deletions

View file

@ -99,7 +99,7 @@ templates:
@rm nfd-worker.conf.tmp
mock:
mockery --name=FeatureSource --dir=source --inpkg --note="Re-generate by running 'make mock'"
mockery --name=LabelSource --dir=source --inpkg --note="Re-generate by running 'make mock'"
mockery --name=APIHelpers --dir=pkg/apihelper --inpkg --note="Re-generate by running 'make mock'"
mockery --name=LabelerClient --dir=pkg/labeler --inpkg --note="Re-generate by running 'make mock'"

View file

@ -38,26 +38,26 @@ import (
"sigs.k8s.io/node-feature-discovery/source/pci"
)
const fakeFeatureSourceName string = "testSource"
const fakeLabelSourceName string = "testSource"
func TestDiscoveryWithMockSources(t *testing.T) {
Convey("When I discover features from fake source and update the node using fake client", t, func() {
mockFeatureSource := new(source.MockFeatureSource)
mockLabelSource := new(source.MockLabelSource)
allFeatureNames := []string{"testfeature1", "testfeature2", "test.ns/test", "test.ns/foo", "/no-ns-label", "invalid/test/feature"}
whiteListFeatureNames := []string{"testfeature1", "testfeature2", "test.ns/test"}
fakeFeatures, _ := makeFakeFeatures(allFeatureNames)
_, fakeFeatureLabels := makeFakeFeatures(whiteListFeatureNames)
fakeFeatureSource := source.FeatureSource(mockFeatureSource)
fakeLabelSource := source.LabelSource(mockLabelSource)
labelWhiteList := utils.RegexpVal{Regexp: *regexp.MustCompile("^test")}
Convey("When I successfully get the labels from the mock source", func() {
mockFeatureSource.On("Name").Return(fakeFeatureSourceName)
mockFeatureSource.On("Discover").Return(fakeFeatures, nil)
mockLabelSource.On("Name").Return(fakeLabelSourceName)
mockLabelSource.On("Discover").Return(fakeFeatures, nil)
returnedLabels, err := getFeatureLabels(fakeFeatureSource, labelWhiteList.Regexp)
returnedLabels, err := getFeatureLabels(fakeLabelSource, labelWhiteList.Regexp)
Convey("Proper label is returned", func() {
So(returnedLabels, ShouldResemble, fakeFeatureLabels)
})
@ -68,9 +68,9 @@ func TestDiscoveryWithMockSources(t *testing.T) {
Convey("When I fail to get the labels from the mock source", func() {
expectedError := errors.New("fake error")
mockFeatureSource.On("Discover").Return(nil, expectedError)
mockLabelSource.On("Discover").Return(nil, expectedError)
returnedLabels, err := getFeatureLabels(fakeFeatureSource, labelWhiteList.Regexp)
returnedLabels, err := getFeatureLabels(fakeLabelSource, labelWhiteList.Regexp)
Convey("No label is returned", func() {
So(returnedLabels, ShouldBeNil)
})
@ -81,12 +81,12 @@ func TestDiscoveryWithMockSources(t *testing.T) {
})
}
func makeFakeFeatures(names []string) (source.Features, Labels) {
features := source.Features{}
func makeFakeFeatures(names []string) (source.FeatureLabels, Labels) {
features := source.FeatureLabels{}
labels := Labels{}
for _, f := range names {
features[f] = true
labelName := fakeFeatureSourceName + "-" + f
labelName := fakeLabelSourceName + "-" + f
if strings.IndexByte(f, '/') >= 0 {
labelName = f
}
@ -96,7 +96,7 @@ func makeFakeFeatures(names []string) (source.Features, Labels) {
return features, labels
}
func (w *nfdWorker) getSource(name string) source.FeatureSource {
func (w *nfdWorker) getSource(name string) source.LabelSource {
for _, s := range w.realSources {
if s.Name() == name {
return s
@ -356,9 +356,9 @@ 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}
fakeLabelSource := source.LabelSource(new(fake.Source))
fakeLabelSource.SetConfig(fakeLabelSource.NewConfig())
sources := []source.LabelSource{fakeLabelSource}
Convey("When fake feature source is configured", func() {
emptyLabelWL := regexp.MustCompile("")

View file

@ -99,10 +99,10 @@ type nfdWorker struct {
client pb.LabelerClient
configFilePath string
config *NFDConfig
realSources []source.FeatureSource
realSources []source.LabelSource
stop chan struct{} // channel for signaling stop
testSources []source.FeatureSource
enabledSources []source.FeatureSource
testSources []source.LabelSource
enabledSources []source.LabelSource
}
type duration struct {
@ -121,7 +121,7 @@ func NewNfdWorker(args *Args) (nfdclient.NfdClient, error) {
args: *args,
config: &NFDConfig{},
realSources: []source.FeatureSource{
realSources: []source.LabelSource{
&cpu.Source{},
&iommu.Source{},
&kernel.Source{},
@ -136,7 +136,7 @@ func NewNfdWorker(args *Args) (nfdclient.NfdClient, error) {
// labels from other sources
&local.Source{},
},
testSources: []source.FeatureSource{
testSources: []source.LabelSource{
&fake.Source{},
},
stop: make(chan struct{}, 1),
@ -311,7 +311,7 @@ func (w *nfdWorker) configureCore(c coreConfig) error {
sourceList[strings.TrimSpace(s)] = struct{}{}
}
w.enabledSources = []source.FeatureSource{}
w.enabledSources = []source.LabelSource{}
for _, s := range w.realSources {
if _, enabled := sourceList[s.Name()]; all || enabled {
w.enabledSources = append(w.enabledSources, s)
@ -400,7 +400,7 @@ func (w *nfdWorker) configure(filepath string, overrides string) error {
// createFeatureLabels returns the set of feature labels from the enabled
// sources and the whitelist argument.
func createFeatureLabels(sources []source.FeatureSource, labelWhiteList regexp.Regexp) (labels Labels) {
func createFeatureLabels(sources []source.LabelSource, labelWhiteList regexp.Regexp) (labels Labels) {
labels = Labels{}
// Do feature discovery from all configured sources.
@ -423,7 +423,7 @@ 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) {
func getFeatureLabels(source source.LabelSource, labelWhiteList regexp.Regexp) (labels Labels, err error) {
labels = Labels{}
features, err := source.Discover()
if err != nil {

View file

@ -78,7 +78,7 @@ type keyFilter struct {
whitelist bool
}
// Implement FeatureSource interface
// Source implements LabelSource.
type Source struct {
config *Config
cpuidFilter *keyFilter
@ -86,13 +86,13 @@ type Source struct {
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
@ -103,8 +103,8 @@ func (s *Source) SetConfig(conf source.Config) {
}
}
func (s *Source) Discover() (source.Features, error) {
features := source.Features{}
func (s *Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Check if hyper-threading seems to be enabled
found, err := haveThreadSiblings()

View file

@ -51,7 +51,7 @@ func newDefaultConfig() *config {
return &config{}
}
// Source implements FeatureSource Interface
// Source implements LabelSource.
type Source struct {
config *config
}
@ -59,13 +59,13 @@ type Source struct {
// Name returns the name of the feature source
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *config:
@ -76,8 +76,8 @@ func (s *Source) SetConfig(conf source.Config) {
}
// Discover features
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
allFeatureConfig := append(getStaticFeatureConfig(), *s.config...)
allFeatureConfig = append(allFeatureConfig, getDirectoryFeatureConfig()...)
utils.KlogDump(2, "custom features configuration:", " ", allFeatureConfig)

View file

@ -40,7 +40,7 @@ func newDefaultConfig() *Config {
}
}
// Source implements FeatureSource.
// Source implements LabelSource.
type Source struct {
config *Config
}
@ -48,13 +48,13 @@ type Source struct {
// Name returns an identifier string for this feature source.
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
@ -64,13 +64,13 @@ func (s *Source) SetConfig(conf source.Config) {
}
}
// Configure method of the FeatureSource interface
// Configure method of the LabelSource interface
func (s Source) Configure([]byte) error { return nil }
// Discover returns feature names for some fake features.
func (s Source) Discover() (source.Features, error) {
func (s Source) Discover() (source.FeatureLabels, error) {
// Adding three fake features.
features := make(source.Features, len(s.config.Labels))
features := make(source.FeatureLabels, len(s.config.Labels))
for k, v := range s.config.Labels {
features[k] = v
}

View file

@ -25,23 +25,22 @@ import (
const Name = "iommu"
// Implement FeatureSource interface
// Source implements FeatureSource interface
// Source implements LabelSource.
type Source struct{}
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Check if any iommu devices are available
devices, err := ioutil.ReadDir(source.SysfsDir.Path("class/iommu/"))

View file

@ -47,20 +47,20 @@ func newDefaultConfig() *Config {
}
}
// Implement FeatureSource interface
// Source implements LabelSource.
type Source struct {
config *Config
}
func (s *Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
@ -70,8 +70,8 @@ func (s *Source) SetConfig(conf source.Config) {
}
}
func (s *Source) Discover() (source.Features, error) {
features := source.Features{}
func (s *Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Read kernel version
version, err := parseVersion()

View file

@ -38,23 +38,23 @@ var (
hookDir = "/etc/kubernetes/node-feature-discovery/source.d/"
)
// Source implements FeatureSource interface
// Source implements LabelSource.
type Source struct{}
// Name returns the name of the feature source
// Name method of the LabelSource interface
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
// Discover returns features from hooks and files
func (s Source) Discover() (source.Features, error) {
// Discover method of the LabelSource interface
func (s Source) Discover() (source.FeatureLabels, error) {
featuresFromHooks, err := getFeaturesFromHooks()
if err != nil {
klog.Error(err)
@ -77,8 +77,8 @@ func (s Source) Discover() (source.Features, error) {
return featuresFromFiles, nil
}
func parseFeatures(lines [][]byte, prefix string) source.Features {
features := source.Features{}
func parseFeatures(lines [][]byte, prefix string) source.FeatureLabels {
features := source.FeatureLabels{}
for _, line := range lines {
if len(line) > 0 {
@ -109,8 +109,8 @@ func parseFeatures(lines [][]byte, prefix string) source.Features {
}
// Run all hooks and get features
func getFeaturesFromHooks() (source.Features, error) {
features := source.Features{}
func getFeaturesFromHooks() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
files, err := ioutil.ReadDir(hookDir)
if err != nil {
@ -184,8 +184,8 @@ func runHook(file string) ([][]byte, error) {
}
// Read all files to get features
func getFeaturesFromFiles() (source.Features, error) {
features := source.Features{}
func getFeaturesFromFiles() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
files, err := ioutil.ReadDir(featureFilesDir)
if err != nil {

View file

@ -28,24 +28,24 @@ import (
const Name = "memory"
// Source implements FeatureSource.
// Source implements LabelSource.
type Source struct{}
// Name returns an identifier string for this feature source.
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
// Discover returns feature names for memory: numa if more than one memory node is present.
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Detect NUMA
numa, err := isNuma()

View file

@ -6,21 +6,21 @@ package source
import mock "github.com/stretchr/testify/mock"
// MockFeatureSource is an autogenerated mock type for the FeatureSource type
type MockFeatureSource struct {
// MockLabelSource is an autogenerated mock type for the LabelSource type
type MockLabelSource struct {
mock.Mock
}
// Discover provides a mock function with given fields:
func (_m *MockFeatureSource) Discover() (Features, error) {
func (_m *MockLabelSource) Discover() (FeatureLabels, error) {
ret := _m.Called()
var r0 Features
if rf, ok := ret.Get(0).(func() Features); ok {
var r0 FeatureLabels
if rf, ok := ret.Get(0).(func() FeatureLabels); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(Features)
r0 = ret.Get(0).(FeatureLabels)
}
}
@ -35,7 +35,7 @@ func (_m *MockFeatureSource) Discover() (Features, error) {
}
// GetConfig provides a mock function with given fields:
func (_m *MockFeatureSource) GetConfig() Config {
func (_m *MockLabelSource) GetConfig() Config {
ret := _m.Called()
var r0 Config
@ -51,7 +51,7 @@ func (_m *MockFeatureSource) GetConfig() Config {
}
// Name provides a mock function with given fields:
func (_m *MockFeatureSource) Name() string {
func (_m *MockLabelSource) Name() string {
ret := _m.Called()
var r0 string
@ -65,7 +65,7 @@ func (_m *MockFeatureSource) Name() string {
}
// NewConfig provides a mock function with given fields:
func (_m *MockFeatureSource) NewConfig() Config {
func (_m *MockLabelSource) NewConfig() Config {
ret := _m.Called()
var r0 Config
@ -81,6 +81,6 @@ func (_m *MockFeatureSource) NewConfig() Config {
}
// SetConfig provides a mock function with given fields: _a0
func (_m *MockFeatureSource) SetConfig(_a0 Config) {
func (_m *MockLabelSource) SetConfig(_a0 Config) {
_m.Called(_a0)
}

View file

@ -41,24 +41,24 @@ const (
const sysfsBaseDir = "class/net"
// Source implements FeatureSource.
// Source implements LabelSource.
type Source struct{}
// Name returns an identifier string for this feature source.
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
// Discover returns feature names sriov-configured and sriov if SR-IOV capable NICs are present and/or SR-IOV virtual functions are configured on the node
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
netInterfaces, err := ioutil.ReadDir(source.SysfsDir.Path(sysfsBaseDir))
if err != nil {

View file

@ -41,7 +41,7 @@ func newDefaultConfig() *Config {
}
}
// Source implements FeatureSource interface
// Source implements LabelSource.
type Source struct {
config *Config
}
@ -49,13 +49,13 @@ type Source struct {
// Name returns the name of the feature source
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
@ -66,8 +66,8 @@ func (s *Source) SetConfig(conf source.Config) {
}
// Discover features
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Construct a device label format, a sorted list of valid attributes
deviceLabelFields := []string{}

View file

@ -16,18 +16,19 @@ limitations under the License.
package source
// Value of a feature
type FeatureValue interface{}
// FeatureLabelValue represents the value of one feature label
type FeatureLabelValue interface{}
type Features map[string]FeatureValue
// FeatureLabels is a collection of feature labels
type FeatureLabels map[string]FeatureLabelValue
// FeatureSource represents a source of a discovered node feature.
type FeatureSource interface {
// Name returns a friendly name for this source of node feature.
// LabelSource represents a source of node feature labels
type LabelSource interface {
// Name returns a friendly name for this source
Name() string
// Discover returns discovered features for this node.
Discover() (Features, error)
// Discover returns discovered feature labels
Discover() (FeatureLabels, error)
// NewConfig returns a new default config of the source
NewConfig() Config
@ -39,5 +40,6 @@ type FeatureSource interface {
SetConfig(Config)
}
// Config is the generic interface for source configuration data
type Config interface {
}

View file

@ -25,24 +25,24 @@ import (
const Name = "storage"
// Source implements FeatureSource.
// Source implements LabelSource.
type Source struct{}
// Name returns an identifier string for this feature source.
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
// Discover returns feature names for storage: nonrotationaldisk if any SSD drive present.
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Check if there is any non-rotational block devices attached to the node
blockdevices, err := ioutil.ReadDir(source.SysfsDir.Path("block"))

View file

@ -34,22 +34,22 @@ var osReleaseFields = [...]string{
const Name = "system"
// Implement FeatureSource interface
// Source implements LabelSource.
type Source struct{}
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return nil }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return nil }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(source.Config) {}
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
release, err := parseOSRelease()
if err != nil {

View file

@ -44,7 +44,7 @@ func newDefaultConfig() *Config {
}
}
// Source implements FeatureSource interface
// Source implements LabelSource.
type Source struct {
config *Config
}
@ -52,13 +52,13 @@ type Source struct {
// Name returns the name of the feature source
func (s Source) Name() string { return Name }
// NewConfig method of the FeatureSource interface
// NewConfig method of the LabelSource interface
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the FeatureSource interface
// GetConfig method of the LabelSource interface
func (s *Source) GetConfig() source.Config { return s.config }
// SetConfig method of the FeatureSource interface
// SetConfig method of the LabelSource interface
func (s *Source) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *Config:
@ -69,8 +69,8 @@ func (s *Source) SetConfig(conf source.Config) {
}
// Discover features
func (s Source) Discover() (source.Features, error) {
features := source.Features{}
func (s Source) Discover() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
// Construct a device label format, a sorted list of valid attributes
deviceLabelFields := []string{}