1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-07 01:07:05 +00:00
node-feature-discovery/pkg/apis/nfd/v1alpha1/rule_test.go
Markus Lehtonen c8d73666d6 pkg/apis/nfd: support label name templating
Support templating of label names in feature rules. It is available both
in NodeFeatureRule CRs and in custom rule configuration of nfd-worker.

This patch adds a new 'labelsTemplate' field to the rule spec, making it
possible to dynamically generate multiple labels per rule based on the
matched features. The feature relies on the golang "text/template"
package.  When expanded, the template must contain labels in a raw
<key>[=<value>] format (where 'value' defaults to "true"), separated by
newlines i.e.:

  - name: <rule-name>
    labelsTemplate: |
      <label-1>[=<value-1>]
      <label-2>[=<value-2>]
      ...

All the matched features of 'matchFeatures' directives are available for
templating engine in a nested data structure that can be described in
yaml as:

.
  <domain-1>:
      <key-feature-1>:
        - Name: <matched-key>
        - ...

      <value-feature-1:
        - Name: <matched-key>
          Value: <matched-value>
        - ...

      <instance-feature-1>:
        - <attribute-1-name>: <attribute-1-value>
          <attribute-2-name>: <attribute-2-value>
          ...
        - ...

  <domain-2>:
     ...

That is, the per-feature data available for matching depends on the type
of feature that was matched:

- "key features": only 'Name' is available
- "value features": 'Name' and 'Value' can be used
- "instance features": all attributes of the matched instance are
   available

NOTE: In case of matchAny is specified, the template is executed
separately against each individual matchFeatures matcher and the
eventual set of labels is a superset of all these expansions.  Consider
the following:

  - name: <name>
    labelsTemplate: <template>
    matchAny:
      - matchFeatures: <matcher#1>
      - matchFeatures: <matcher#2>
    matchFeatures: <matcher#3>

In the example above (assuming the overall result is a match) the
template would be executed on matcher#1 and/or matcher#2 (depending on
whether both or only one of them match), and finally on matcher#3, and
all the labels from these separate expansions would be created (i.e. the
end result would be a union of all the individual expansions).

NOTE 2: The 'labels' field has priority over 'labelsTemplate', i.e.
labels specified in the 'labels' field will override any labels
originating from the 'labelsTemplate' field.

A special case of an empty match expression set matches everything (i.e.
matches/returns all existing keys/values). This makes it simpler to
write templates that run over all values. Also, makes it possible to
later implement support for templates that run over all _keys_ of a
feature.

Some example configurations:

  - name: "my-pci-template-features"
    labelsTemplate: |
      {{ range .pci.device }}intel-{{ .class }}-{{ .device }}=present
      {{ end }}
    matchFeatures:
      - feature: pci.device
        matchExpressions:
          class: {op: InRegexp, value: ["^06"]}
          vendor: ["8086"]

  - name: "my-system-template-features"
    labelsTemplate: |
      {{ range .system.osrelease }}system-{{ .Name }}={{ .Value }}
      {{ end }}
    matchFeatures:
      - feature: system.osRelease
        matchExpressions:
          ID: {op: Exists}
          VERSION_ID.major: {op: Exists}

Imaginative template pipelines are possible, of course, but care must be
taken in order to produce understandable and maintainable rule sets.
2021-11-23 21:03:22 +02:00

298 lines
8.7 KiB
Go

/*
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 v1alpha1
import (
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
)
func TestRule(t *testing.T) {
f := map[string]*feature.DomainFeatures{}
r1 := Rule{Labels: map[string]string{"label-1": "", "label-2": "true"}}
r2 := Rule{
Labels: map[string]string{"label-1": "label-val-1"},
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchExists)},
},
},
},
}
// Test totally empty features
m, err := r1.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r1.Labels, m, "empty matcher should have matched empty features")
_, err = r2.Execute(f)
assert.Error(t, err, "matching agains a missing domain should have returned an error")
// Test empty domain
d := feature.NewDomainFeatures()
f["domain-1"] = d
m, err = r1.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r1.Labels, m, "empty matcher should have matched empty features")
_, err = r2.Execute(f)
assert.Error(t, err, "matching agains a missing feature type should have returned an error")
// Test empty feature sets
d.Keys["kf-1"] = feature.NewKeyFeatures()
d.Values["vf-1"] = feature.NewValueFeatures(nil)
d.Instances["if-1"] = feature.NewInstanceFeatures(nil)
m, err = r1.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r1.Labels, m, "empty matcher should have matched empty features")
m, err = r2.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "unexpected match")
// Test non-empty feature sets
d.Keys["kf-1"].Elements["key-x"] = feature.Nil{}
d.Values["vf-1"].Elements["key-1"] = "val-x"
d.Instances["if-1"] = feature.NewInstanceFeatures([]feature.InstanceFeature{
*feature.NewInstanceFeature(map[string]string{"attr-1": "val-x"})})
m, err = r1.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r1.Labels, m, "empty matcher should have matched empty features")
// Match "key" features
m, err = r2.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "keys should not have matched")
d.Keys["kf-1"].Elements["key-1"] = feature.Nil{}
m, err = r2.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r2.Labels, m, "keys should have matched")
// Match "value" features
r3 := Rule{
Labels: map[string]string{"label-3": "label-val-3", "empty": ""},
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchIn, "val-1")},
},
},
},
}
m, err = r3.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "values should not have matched")
d.Values["vf-1"].Elements["key-1"] = "val-1"
m, err = r3.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r3.Labels, m, "values should have matched")
// Match "instance" features
r4 := Rule{
Labels: map[string]string{"label-4": "label-val-4"},
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"attr-1": MustCreateMatchExpression(MatchIn, "val-1")},
},
},
},
}
m, err = r4.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "instances should not have matched")
d.Instances["if-1"].Elements[0].Attributes["attr-1"] = "val-1"
m, err = r4.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r4.Labels, m, "instances should have matched")
// Test multiple feature matchers
r5 := Rule{
Labels: map[string]string{"label-5": "label-val-5"},
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.vf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchIn, "val-x")},
},
},
FeatureMatcherTerm{
Feature: "domain-1.if-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"attr-1": MustCreateMatchExpression(MatchIn, "val-1")},
},
},
},
}
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "instances should not have matched")
r5.MatchFeatures[0].MatchExpressions.Expressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m, "instances should have matched")
// Test MatchAny
r5.MatchAny = []MatchAnyElem{
MatchAnyElem{
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-na": MustCreateMatchExpression(MatchExists)},
},
},
},
},
}
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Nil(t, m, "instances should not have matched")
r5.MatchAny = append(r5.MatchAny,
MatchAnyElem{
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain-1.kf-1",
MatchExpressions: MatchExpressionSet{
Expressions: Expressions{"key-1": MustCreateMatchExpression(MatchExists)},
},
},
},
})
r5.MatchFeatures[0].MatchExpressions.Expressions["key-1"] = MustCreateMatchExpression(MatchIn, "val-1")
m, err = r5.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, r5.Labels, m, "instances should have matched")
}
func TestTemplating(t *testing.T) {
f := map[string]*feature.DomainFeatures{
"domain_1": &feature.DomainFeatures{
Keys: map[string]feature.KeyFeatureSet{
"kf_1": feature.KeyFeatureSet{
Elements: map[string]feature.Nil{
"key-a": feature.Nil{},
"key-b": feature.Nil{},
"key-c": feature.Nil{},
},
},
},
Values: map[string]feature.ValueFeatureSet{
"vf_1": feature.ValueFeatureSet{
Elements: map[string]string{
"key-1": "val-1",
"keu-2": "val-2",
"key-3": "val-3",
},
},
},
Instances: map[string]feature.InstanceFeatureSet{
"if_1": feature.InstanceFeatureSet{
Elements: []feature.InstanceFeature{
feature.InstanceFeature{
Attributes: map[string]string{
"attr-1": "1",
"attr-2": "val-2",
},
},
feature.InstanceFeature{
Attributes: map[string]string{
"attr-1": "10",
"attr-2": "val-20",
},
},
feature.InstanceFeature{
Attributes: map[string]string{
"attr-1": "100",
"attr-2": "val-200",
},
},
},
},
},
},
}
r1 := Rule{
Labels: map[string]string{"label-1": "label-val-1"},
LabelsTemplate: `
{{range .domain_1.kf_1}}kf-{{.Name}}=present
{{end}}
{{range .domain_1.vf_1}}vf-{{.Name}}=vf-{{.Value}}
{{end}}
{{range .domain_1.if_1}}if-{{index . "attr-1"}}_{{index . "attr-2"}}=present
{{end}}`,
MatchFeatures: FeatureMatcher{
FeatureMatcherTerm{
Feature: "domain_1.kf_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
"key-a": MustCreateMatchExpression(MatchExists),
"key-c": MustCreateMatchExpression(MatchExists),
"foo": MustCreateMatchExpression(MatchDoesNotExist),
},
},
},
FeatureMatcherTerm{
Feature: "domain_1.vf_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
"key-1": MustCreateMatchExpression(MatchIn, "val-1", "val-2"),
"bar": MustCreateMatchExpression(MatchDoesNotExist),
},
},
},
FeatureMatcherTerm{
Feature: "domain_1.if_1",
MatchExpressions: MatchExpressionSet{Expressions: Expressions{
"attr-1": MustCreateMatchExpression(MatchLt, "100"),
},
},
},
},
}
expectedLabels := map[string]string{
"label-1": "label-val-1",
// From kf_1 template
"kf-key-a": "present",
"kf-key-c": "present",
"kf-foo": "present",
// From vf_1 template
"vf-key-1": "vf-val-1",
"vf-bar": "vf-",
// From if_1 template
"if-1_val-2": "present",
"if-10_val-20": "present",
}
m, err := r1.Execute(f)
assert.Nilf(t, err, "unexpected error: %v", err)
assert.Equal(t, expectedLabels, m, "instances should have matched")
}