1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-06 08:47:04 +00:00
node-feature-discovery/pkg/api/feature/feature.go

68 lines
1.9 KiB
Go
Raw Normal View History

/*
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 feature
// NewDomainFeatures creates a new instance of Features, initializing specified
// features to empty values
func NewDomainFeatures() *DomainFeatures {
return &DomainFeatures{
Keys: make(map[string]KeyFeatureSet),
Values: make(map[string]ValueFeatureSet),
Instances: make(map[string]InstanceFeatureSet)}
}
func NewKeyFeatures(keys ...string) KeyFeatureSet {
e := make(map[string]Nil, len(keys))
for _, k := range keys {
e[k] = Nil{}
}
return KeyFeatureSet{Elements: e}
}
func NewValueFeatures(values map[string]string) ValueFeatureSet {
if values == nil {
values = make(map[string]string)
}
return ValueFeatureSet{Elements: values}
}
func NewInstanceFeatures(instances []InstanceFeature) InstanceFeatureSet {
return InstanceFeatureSet{Elements: instances}
}
func NewInstanceFeature(attrs map[string]string) *InstanceFeature {
if attrs == nil {
attrs = make(map[string]string)
}
return &InstanceFeature{Attributes: attrs}
}
pkg/apis/nfd: add variables to rule spec and support backreferences Support backreferencing of output values from previous rules. Enables complex rule setups where custom features are further combined together to form even more sophisticated higher level labels. The labels created by preceding rules are available as a special 'rule.matched' feature (for matchFeatures to use). If referencing rules accross multiple configs/CRDs care must be taken with the ordering. Processing order of rules in nfd-worker: 1. Static rules 2. Files from /etc/kubernetes/node-feature-discovery/custom.d/ in alphabetical order. Subdirectories are processed by reading their files in alphabetical order. 3. Custom rules from main nfd-worker.conf In nfd-master, NodeFeatureRule objects are processed in alphabetical order (based on their metadata.name). This patch also adds new 'vars' fields to the rule spec. Like 'labels', it is a map of key-value pairs but no labels are generated from these. The values specified in 'vars' are only added for backreferencing into the 'rules.matched' feature. This may by desired in schemes where the output of certain rules is only used as intermediate variables for other rules and no labels out of these are wanted. An example setup: - name: "kernel feature" labels: kernel-feature: matchFeatures: - feature: kernel.version matchExpressions: major: {op: Gt, value: ["4"]} - name: "intermediate var feature" vars: nolabel-feature: "true" matchFeatures: - feature: cpu.cpuid matchExpressions: AVX512F: {op: Exists} - feature: pci.device matchExpressions: vendor: {op: In, value: ["8086"]} device: {op: In, value: ["1234", "1235"]} - name: top-level-feature matchFeatures: - feature: rule.matched matchExpressions: kernel-feature: "true" nolabel-feature: "true"
2021-06-18 18:29:08 +03:00
// InsertFeatureValues inserts new values into a specific feature.
func InsertFeatureValues(f Features, domain, feature string, values map[string]string) {
if _, ok := f[domain]; !ok {
f[domain] = NewDomainFeatures()
}
if _, ok := f[domain].Values[feature]; !ok {
f[domain].Values[feature] = NewValueFeatures(values)
return
}
for k, v := range values {
f[domain].Values[feature].Elements[k] = v
}
}