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

source/local: implement FeatureSource

Separate feature discovery (i.e. running hooks and reading feature
files) and creation of feature labels in the local source.

Also, add minimalist unit test.
This commit is contained in:
Markus Lehtonen 2021-03-03 22:03:11 +02:00
parent e225f4aad0
commit a91f3325ba
2 changed files with 78 additions and 11 deletions

View file

@ -27,23 +27,30 @@ import (
"k8s.io/klog/v2"
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
"sigs.k8s.io/node-feature-discovery/pkg/utils"
"sigs.k8s.io/node-feature-discovery/source"
)
const Name = "local"
const LabelFeature = "label"
// Config
var (
featureFilesDir = "/etc/kubernetes/node-feature-discovery/features.d/"
hookDir = "/etc/kubernetes/node-feature-discovery/source.d/"
)
// localSource implements the LabelSource interface.
type localSource struct{}
// localSource implements the FeatureSource and LabelSource interfaces.
type localSource struct {
features *feature.DomainFeatures
}
// Singleton source instance
var (
src localSource
_ source.FeatureSource = &src
_ source.LabelSource = &src
)
@ -55,6 +62,19 @@ func (s *localSource) Priority() int { return 20 }
// GetLabels method of the LabelSource interface
func (s *localSource) GetLabels() (source.FeatureLabels, error) {
labels := make(source.FeatureLabels)
features := s.GetFeatures()
for k, v := range features.Values[LabelFeature].Elements {
labels[k] = v
}
return labels, nil
}
// Discover method of the FeatureSource interface
func (s *localSource) Discover() error {
s.features = feature.NewDomainFeatures()
featuresFromHooks, err := getFeaturesFromHooks()
if err != nil {
klog.Error(err)
@ -68,17 +88,28 @@ func (s *localSource) GetLabels() (source.FeatureLabels, error) {
// Merge features from hooks and files
for k, v := range featuresFromHooks {
if old, ok := featuresFromFiles[k]; ok {
klog.Warningf("overriding label '%s': value changed from '%s' to '%s'",
klog.Warningf("overriding '%s': value changed from '%s' to '%s'",
k, old, v)
}
featuresFromFiles[k] = v
}
s.features.Values[LabelFeature] = feature.NewValueFeatures(featuresFromFiles)
return featuresFromFiles, nil
utils.KlogDump(3, "discovered local features:", " ", s.features)
return nil
}
func parseFeatures(lines [][]byte, prefix string) source.FeatureLabels {
features := source.FeatureLabels{}
// GetFeatures method of the FeatureSource Interface
func (s *localSource) GetFeatures() *feature.DomainFeatures {
if s.features == nil {
s.features = feature.NewDomainFeatures()
}
return s.features
}
func parseFeatures(lines [][]byte, prefix string) map[string]string {
features := make(map[string]string)
for _, line := range lines {
if len(line) > 0 {
@ -109,8 +140,8 @@ func parseFeatures(lines [][]byte, prefix string) source.FeatureLabels {
}
// Run all hooks and get features
func getFeaturesFromHooks() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
func getFeaturesFromHooks() (map[string]string, error) {
features := make(map[string]string)
files, err := ioutil.ReadDir(hookDir)
if err != nil {
@ -184,8 +215,8 @@ func runHook(file string) ([][]byte, error) {
}
// Read all files to get features
func getFeaturesFromFiles() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
func getFeaturesFromFiles() (map[string]string, error) {
features := make(map[string]string)
files, err := ioutil.ReadDir(featureFilesDir)
if err != nil {

View file

@ -0,0 +1,36 @@
/*
Copyright 2021 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 local
import (
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
)
func TestLocalSource(t *testing.T) {
assert.Equal(t, src.Name(), Name)
// Check that GetLabels works with empty features
src.features = feature.NewDomainFeatures()
l, err := src.GetLabels()
assert.Nil(t, err, err)
assert.Empty(t, l)
}