1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-17 13:58:21 +00:00

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.
This commit is contained in:
Markus Lehtonen 2025-02-28 15:11:01 +02:00
parent a7392f106c
commit 3b9bc575bc
2 changed files with 29 additions and 19 deletions

View file

@ -149,37 +149,48 @@ func Execute(r *nfdv1alpha1.Rule, features *nfdv1alpha1.Features, failFast bool)
// ExecuteGroupRule executes the GroupRule against a set of input features, and return true if the // ExecuteGroupRule executes the GroupRule against a set of input features, and return true if the
// rule matches. // rule matches.
func ExecuteGroupRule(r *nfdv1alpha1.GroupRule, features *nfdv1alpha1.Features, failFast bool) (bool, error) { func ExecuteGroupRule(r *nfdv1alpha1.GroupRule, features *nfdv1alpha1.Features, failFast bool) (MatchStatus, error) {
matched := false var (
if len(r.MatchAny) > 0 { matchStatus MatchStatus
isMatch bool
)
if n := len(r.MatchAny); n > 0 {
matchStatus.MatchAny = make([]*MatchFeatureStatus, 0, n)
// Logical OR over the matchAny matchers // Logical OR over the matchAny matchers
for _, matcher := range r.MatchAny { for _, matcher := range r.MatchAny {
if isMatch, matches, err := evaluateMatchAnyElem(&matcher, features, failFast); err != nil { matched, featureStatus, err := evaluateMatchAnyElem(&matcher, features, failFast)
return false, err if err != nil {
} else if isMatch { return matchStatus, err
matched = true } else if matched {
klog.V(4).InfoS("matchAny matched", "ruleName", r.Name, "matchedFeatures", utils.DelayedDumper(matches)) isMatch = true
// there's no need to evaluate other matchers in MatchAny klog.V(4).InfoS("matchAny matched", "ruleName", r.Name, "matchedFeatures", utils.DelayedDumper(featureStatus.MatchedFeatures))
// One match is enough for MatchAny
break if failFast {
// there's no need to evaluate other matchers in MatchAny
break
}
} }
matchStatus.MatchAny = append(matchStatus.MatchAny, featureStatus)
} }
if !matched { if !isMatch && failFast {
return false, nil return matchStatus, nil
} }
} }
if len(r.MatchFeatures) > 0 { if len(r.MatchFeatures) > 0 {
if isMatch, _, err := evaluateFeatureMatcher(&r.MatchFeatures, features, failFast); err != nil { var err error
return false, err if isMatch, matchStatus.MatchFeatureStatus, err = evaluateFeatureMatcher(&r.MatchFeatures, features, failFast); err != nil {
return matchStatus, err
} else if !isMatch { } else if !isMatch {
klog.V(2).InfoS("rule did not match", "ruleName", r.Name) klog.V(2).InfoS("rule did not match", "ruleName", r.Name)
return false, nil return matchStatus, nil
} }
} }
matchStatus.IsMatch = true
klog.V(2).InfoS("rule matched", "ruleName", r.Name) klog.V(2).InfoS("rule matched", "ruleName", r.Name)
return true, nil return matchStatus, nil
} }
func executeLabelsTemplate(r *nfdv1alpha1.Rule, in matchedFeatures, out map[string]string) error { func executeLabelsTemplate(r *nfdv1alpha1.Rule, in matchedFeatures, out map[string]string) error {

View file

@ -742,8 +742,7 @@ func (m *nfdMaster) nfdAPIUpdateNodeFeatureGroup(nfdClient nfdclientset.Interfac
continue continue
} }
if match { if match.IsMatch {
klog.ErrorS(err, "failed to evaluate rule", "ruleName", rule.Name, "nodeName", feature.Name)
system := feature.Spec.Features.Attributes["system.name"] system := feature.Spec.Features.Attributes["system.name"]
nodeName := system.Elements["nodename"] nodeName := system.Elements["nodename"]
if _, ok := nodeGroupValidator[nodeName]; !ok { if _, ok := nodeGroupValidator[nodeName]; !ok {