@@ -147,50 +147,71 @@ func Execute(r *nfdv1alpha1.Rule, features *nfdv1alpha1.Features, failFast bool)
147
147
return ret , nil
148
148
}
149
149
150
+ // GroupRuleOutput contains the output of group rule execution.
151
+ type GroupRuleOutput struct {
152
+ Vars map [string ]string
153
+ MatchStatus * MatchStatus
154
+ }
155
+
150
156
// ExecuteGroupRule executes the GroupRule against a set of input features, and return true if the
151
157
// rule matches.
152
- func ExecuteGroupRule (r * nfdv1alpha1.GroupRule , features * nfdv1alpha1.Features , failFast bool ) (MatchStatus , error ) {
158
+ func ExecuteGroupRule (r * nfdv1alpha1.GroupRule , features * nfdv1alpha1.Features , failFast bool ) (GroupRuleOutput , error ) {
153
159
var (
154
160
matchStatus MatchStatus
155
161
isMatch bool
156
162
)
163
+ vars := make (map [string ]string )
164
+
157
165
if n := len (r .MatchAny ); n > 0 {
158
166
matchStatus .MatchAny = make ([]* MatchFeatureStatus , 0 , n )
159
167
// Logical OR over the matchAny matchers
160
168
for _ , matcher := range r .MatchAny {
161
169
matched , featureStatus , err := evaluateMatchAnyElem (& matcher , features , failFast )
162
170
if err != nil {
163
- return matchStatus , err
171
+ return GroupRuleOutput {} , err
164
172
} else if matched {
165
173
isMatch = true
166
174
klog .V (4 ).InfoS ("matchAny matched" , "ruleName" , r .Name , "matchedFeatures" , utils .DelayedDumper (featureStatus .MatchedFeatures ))
167
175
168
- if failFast {
176
+ if r . VarsTemplate == "" && failFast {
169
177
// there's no need to evaluate other matchers in MatchAny
178
+ // if there are no templates to be executed on them
170
179
break
171
180
}
181
+
182
+ if err := executeTemplate (r .VarsTemplate , featureStatus .MatchedFeatures , vars ); err != nil {
183
+ return GroupRuleOutput {}, err
184
+ }
172
185
}
173
186
matchStatus .MatchAny = append (matchStatus .MatchAny , featureStatus )
174
187
}
175
188
if ! isMatch && failFast {
176
- return matchStatus , nil
189
+ return GroupRuleOutput { MatchStatus : & matchStatus } , nil
177
190
}
178
191
}
179
192
180
193
if len (r .MatchFeatures ) > 0 {
181
194
var err error
182
195
if isMatch , matchStatus .MatchFeatureStatus , err = evaluateFeatureMatcher (& r .MatchFeatures , features , failFast ); err != nil {
183
- return matchStatus , err
196
+ return GroupRuleOutput {} , err
184
197
} else if ! isMatch {
185
198
klog .V (2 ).InfoS ("rule did not match" , "ruleName" , r .Name )
186
- return matchStatus , nil
199
+ return GroupRuleOutput {MatchStatus : & matchStatus }, nil
200
+ }
201
+ if err := executeTemplate (r .VarsTemplate , matchStatus .MatchedFeatures , vars ); err != nil {
202
+ return GroupRuleOutput {}, err
187
203
}
188
204
}
189
205
206
+ maps .Copy (vars , r .Vars )
190
207
matchStatus .IsMatch = true
191
208
192
- klog .V (2 ).InfoS ("rule matched" , "ruleName" , r .Name )
193
- return matchStatus , nil
209
+ ret := GroupRuleOutput {
210
+ Vars : vars ,
211
+ MatchStatus : & matchStatus ,
212
+ }
213
+ klog .V (2 ).InfoS ("rule matched" , "ruleName" , r .Name , "ruleOutput" , utils .DelayedDumper (ret ))
214
+ return ret , nil
194
215
}
195
216
196
217
func executeTemplate (tmpl string , in matchedFeatures , out map [string ]string ) error {
0 commit comments