From 4375e08e39e970033c2b974f6026de0911bcfcd0 Mon Sep 17 00:00:00 2001 From: Viktor Oreshkin Date: Mon, 22 Aug 2022 02:27:53 +0300 Subject: [PATCH 1/2] apis/nfd: add more tests for templates test that NodeFeatureRule templates work with empty MatchFeatures, but with MatchAny. this test would fail, higligting an issue which is fixed in next commit. see #864. Signed-off-by: Viktor Oreshkin --- pkg/apis/nfd/v1alpha1/rule_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/apis/nfd/v1alpha1/rule_test.go b/pkg/apis/nfd/v1alpha1/rule_test.go index 144999cdb5..a13760c6ca 100644 --- a/pkg/apis/nfd/v1alpha1/rule_test.go +++ b/pkg/apis/nfd/v1alpha1/rule_test.go @@ -297,6 +297,11 @@ var-2= }, } + // test with empty MatchFeatures, but with MatchAny + r3 := r1.DeepCopy() + r3.MatchAny = []MatchAnyElem{{MatchFeatures: r3.MatchFeatures}} + r3.MatchFeatures = nil + expectedLabels := map[string]string{ "label-1": "label-val-1", "label-2": "", @@ -325,6 +330,11 @@ var-2= assert.Equal(t, expectedLabels, m.Labels, "instances should have matched") assert.Equal(t, expectedVars, m.Vars, "instances should have matched") + m, err = r3.Execute(f) + assert.Nilf(t, err, "unexpected error: %v", err) + assert.Equal(t, expectedLabels, m.Labels, "instances should have matched") + assert.Equal(t, expectedVars, m.Vars, "instances should have matched") + // // Test error cases // From 6fd12a2da781663aa4481cff1094b7da84a7f071 Mon Sep 17 00:00:00 2001 From: Viktor Oreshkin Date: Mon, 22 Aug 2022 02:29:50 +0300 Subject: [PATCH 2/2] apis/nfd: fix templates with MatchAny only Signed-off-by: Viktor Oreshkin --- pkg/apis/nfd/v1alpha1/rule.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/apis/nfd/v1alpha1/rule.go b/pkg/apis/nfd/v1alpha1/rule.go index 267680326f..1237a6f192 100644 --- a/pkg/apis/nfd/v1alpha1/rule.go +++ b/pkg/apis/nfd/v1alpha1/rule.go @@ -50,11 +50,13 @@ func (r *Rule) Execute(features feature.Features) (RuleOutput, error) { matched = true utils.KlogDump(4, "matches for matchAny "+r.Name, " ", matches) - if r.labelsTemplate == nil { - // No templating so we stop here (further matches would just - // produce the same labels) + if r.LabelsTemplate == "" && r.VarsTemplate == "" { + // there's no need to evaluate other matchers in MatchAny + // if there are no templates to be executed on them - so + // short-circuit and stop on first match here break } + if err := r.executeLabelsTemplate(matches, labels); err != nil { return RuleOutput{}, err }