1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-05 16:27:05 +00:00
node-feature-discovery/source/custom/custom.go
Markus Lehtonen 8b4314bbbb source/custom: expression based label rules
Implement a framework for more flexible rule configuration and matching,
mimicking the MatchExpressions pattern from K8s nodeselector.

The basic building block is MatchExpression which contains an operator
and a list of values. The operator specifies that "function" that is
applied when evaluating a given input agains the list of values.
Available operators are:

- MatchIn
- MatchNotIn
- MatchInRegexp
- MatchExists
- MatchDoesNotExist
- MatchGt
- MatchLt
- MatchIsTrue
- MatchIsFalse

Another building block of the framework is MatchExpressionSet which is a
map of string-MatchExpression pairs. It is a helper for specifying
multiple expressions that can be matched against a set of set of
features.

This patch converts all existing custom rules to utilize the new
expression-based framework.
2021-11-11 19:59:34 +02:00

156 lines
4.2 KiB
Go

/*
Copyright 2020-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 custom
import (
"reflect"
"k8s.io/klog/v2"
"sigs.k8s.io/node-feature-discovery/pkg/utils"
"sigs.k8s.io/node-feature-discovery/source"
"sigs.k8s.io/node-feature-discovery/source/custom/rules"
)
const Name = "custom"
// Custom Features Configurations
type MatchRule struct {
PciID *rules.PciIDRule `json:"pciId,omitempty"`
UsbID *rules.UsbIDRule `json:"usbId,omitempty"`
LoadedKMod *rules.LoadedKModRule `json:"loadedKMod,omitempty"`
CpuID *rules.CpuIDRule `json:"cpuId,omitempty"`
Kconfig *rules.KconfigRule `json:"kConfig,omitempty"`
Nodename *rules.NodenameRule `json:"nodename,omitempty"`
}
type FeatureSpec struct {
Name string `json:"name"`
Value *string `json:"value,omitempty"`
MatchOn []MatchRule `json:"matchOn"`
}
type config []FeatureSpec
// newDefaultConfig returns a new config with pre-populated defaults
func newDefaultConfig() *config {
return &config{}
}
// customSource implements the LabelSource and ConfigurableSource interfaces.
type customSource struct {
config *config
}
type legacyRule interface {
Match() (bool, error)
}
// Singleton source instance
var (
src = customSource{config: newDefaultConfig()}
_ source.LabelSource = &src
_ source.ConfigurableSource = &src
)
// Name returns the name of the feature source
func (s *customSource) Name() string { return Name }
// NewConfig method of the LabelSource interface
func (s *customSource) NewConfig() source.Config { return newDefaultConfig() }
// GetConfig method of the LabelSource interface
func (s *customSource) GetConfig() source.Config { return s.config }
// SetConfig method of the LabelSource interface
func (s *customSource) SetConfig(conf source.Config) {
switch v := conf.(type) {
case *config:
s.config = v
default:
klog.Fatalf("invalid config type: %T", conf)
}
}
// Priority method of the LabelSource interface
func (s *customSource) Priority() int { return 10 }
// GetLabels method of the LabelSource interface
func (s *customSource) GetLabels() (source.FeatureLabels, error) {
features := source.FeatureLabels{}
allFeatureConfig := append(getStaticFeatureConfig(), *s.config...)
allFeatureConfig = append(allFeatureConfig, getDirectoryFeatureConfig()...)
utils.KlogDump(2, "custom features configuration:", " ", allFeatureConfig)
// Iterate over features
for _, customFeature := range allFeatureConfig {
featureExist, err := s.discoverFeature(customFeature)
if err != nil {
klog.Errorf("failed to discover feature: %q: %s", customFeature.Name, err.Error())
continue
}
if featureExist {
var value interface{} = true
if customFeature.Value != nil {
value = *customFeature.Value
}
features[customFeature.Name] = value
}
}
return features, nil
}
// Process a single feature by Matching on the defined rules.
// A feature is present if all defined Rules in a MatchRule return a match.
func (s *customSource) discoverFeature(feature FeatureSpec) (bool, error) {
for _, matchRules := range feature.MatchOn {
allRules := []legacyRule{
matchRules.PciID,
matchRules.UsbID,
matchRules.LoadedKMod,
matchRules.CpuID,
matchRules.Kconfig,
matchRules.Nodename,
}
// return true, nil if all rules match
matchRules := func(rules []legacyRule) (bool, error) {
for _, rule := range rules {
if reflect.ValueOf(rule).IsNil() {
continue
}
if match, err := rule.Match(); err != nil {
return false, err
} else if !match {
return false, nil
}
}
return true, nil
}
if match, err := matchRules(allRules); err != nil {
return false, err
} else if match {
return true, nil
}
}
return false, nil
}
func init() {
source.Register(&src)
}