Skip to content

Commit 3b9bc57

Browse files
committed
Change ExecuteGroupRule to return detailed result
Modify the ExecuteGroupRule() which is used for NodeFeatureGroups to return detailed match status, similar to the Execute (used for NodeFeatureRules). This prepares for using the simpler GroupRule type in the image compatibility API.
1 parent a7392f1 commit 3b9bc57

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

pkg/apis/nfd/nodefeaturerule/rule.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,37 +149,48 @@ func Execute(r *nfdv1alpha1.Rule, features *nfdv1alpha1.Features, failFast bool)
149149

150150
// ExecuteGroupRule executes the GroupRule against a set of input features, and return true if the
151151
// rule matches.
152-
func ExecuteGroupRule(r *nfdv1alpha1.GroupRule, features *nfdv1alpha1.Features, failFast bool) (bool, error) {
153-
matched := false
154-
if len(r.MatchAny) > 0 {
152+
func ExecuteGroupRule(r *nfdv1alpha1.GroupRule, features *nfdv1alpha1.Features, failFast bool) (MatchStatus, error) {
153+
var (
154+
matchStatus MatchStatus
155+
isMatch bool
156+
)
157+
if n := len(r.MatchAny); n > 0 {
158+
matchStatus.MatchAny = make([]*MatchFeatureStatus, 0, n)
155159
// Logical OR over the matchAny matchers
156160
for _, matcher := range r.MatchAny {
157-
if isMatch, matches, err := evaluateMatchAnyElem(&matcher, features, failFast); err != nil {
158-
return false, err
159-
} else if isMatch {
160-
matched = true
161-
klog.V(4).InfoS("matchAny matched", "ruleName", r.Name, "matchedFeatures", utils.DelayedDumper(matches))
162-
// there's no need to evaluate other matchers in MatchAny
163-
// One match is enough for MatchAny
164-
break
161+
matched, featureStatus, err := evaluateMatchAnyElem(&matcher, features, failFast)
162+
if err != nil {
163+
return matchStatus, err
164+
} else if matched {
165+
isMatch = true
166+
klog.V(4).InfoS("matchAny matched", "ruleName", r.Name, "matchedFeatures", utils.DelayedDumper(featureStatus.MatchedFeatures))
167+
168+
if failFast {
169+
// there's no need to evaluate other matchers in MatchAny
170+
break
171+
}
165172
}
173+
matchStatus.MatchAny = append(matchStatus.MatchAny, featureStatus)
166174
}
167-
if !matched {
168-
return false, nil
175+
if !isMatch && failFast {
176+
return matchStatus, nil
169177
}
170178
}
171179

172180
if len(r.MatchFeatures) > 0 {
173-
if isMatch, _, err := evaluateFeatureMatcher(&r.MatchFeatures, features, failFast); err != nil {
174-
return false, err
181+
var err error
182+
if isMatch, matchStatus.MatchFeatureStatus, err = evaluateFeatureMatcher(&r.MatchFeatures, features, failFast); err != nil {
183+
return matchStatus, err
175184
} else if !isMatch {
176185
klog.V(2).InfoS("rule did not match", "ruleName", r.Name)
177-
return false, nil
186+
return matchStatus, nil
178187
}
179188
}
180189

190+
matchStatus.IsMatch = true
191+
181192
klog.V(2).InfoS("rule matched", "ruleName", r.Name)
182-
return true, nil
193+
return matchStatus, nil
183194
}
184195

185196
func executeLabelsTemplate(r *nfdv1alpha1.Rule, in matchedFeatures, out map[string]string) error {

pkg/nfd-master/nfd-master.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ func (m *nfdMaster) nfdAPIUpdateNodeFeatureGroup(nfdClient nfdclientset.Interfac
742742
continue
743743
}
744744

745-
if match {
746-
klog.ErrorS(err, "failed to evaluate rule", "ruleName", rule.Name, "nodeName", feature.Name)
745+
if match.IsMatch {
747746
system := feature.Spec.Features.Attributes["system.name"]
748747
nodeName := system.Elements["nodename"]
749748
if _, ok := nodeGroupValidator[nodeName]; !ok {

0 commit comments

Comments
 (0)