@@ -85,7 +85,8 @@ func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta
85
85
86
86
for _ , outcome := range analyzer .Outcomes {
87
87
if outcome .Fail != nil {
88
- isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Fail .When , matchingNodes )
88
+ isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Fail .When , matchingNodes , analyzer .Filters )
89
+
89
90
if err != nil {
90
91
return nil , errors .Wrap (err , "failed to parse when" )
91
92
}
@@ -100,7 +101,7 @@ func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta
100
101
return result , nil
101
102
}
102
103
} else if outcome .Warn != nil {
103
- isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Warn .When , matchingNodes )
104
+ isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Warn .When , matchingNodes , analyzer . Filters )
104
105
if err != nil {
105
106
return nil , errors .Wrap (err , "failed to parse when" )
106
107
}
@@ -116,7 +117,7 @@ func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta
116
117
return result , nil
117
118
}
118
119
} else if outcome .Pass != nil {
119
- isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Pass .When , matchingNodes )
120
+ isWhenMatch , err := compareNodeResourceConditionalToActual (outcome .Pass .When , matchingNodes , analyzer . Filters )
120
121
if err != nil {
121
122
return nil , errors .Wrap (err , "failed to parse when" )
122
123
}
@@ -137,7 +138,7 @@ func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta
137
138
return result , nil
138
139
}
139
140
140
- func compareNodeResourceConditionalToActual (conditional string , matchingNodes []corev1.Node ) (res bool , err error ) {
141
+ func compareNodeResourceConditionalToActual (conditional string , matchingNodes []corev1.Node , filters * troubleshootv1beta2. NodeResourceFilters ) (res bool , err error ) {
141
142
res = false
142
143
err = nil
143
144
@@ -190,18 +191,23 @@ func compareNodeResourceConditionalToActual(conditional string, matchingNodes []
190
191
191
192
function := match [1 ]
192
193
property := match [2 ]
194
+ resourceName := ""
195
+
196
+ if filters != nil {
197
+ resourceName = filters .ResourceName
198
+ }
193
199
194
200
var actualValue interface {}
195
201
196
202
switch function {
197
203
case "count" :
198
204
actualValue = len (matchingNodes )
199
205
case "min" :
200
- actualValue = findMin (matchingNodes , property )
206
+ actualValue = findMin (matchingNodes , property , resourceName )
201
207
case "max" :
202
- actualValue = findMax (matchingNodes , property )
208
+ actualValue = findMax (matchingNodes , property , resourceName )
203
209
case "sum" :
204
- actualValue = findSum (matchingNodes , property )
210
+ actualValue = findSum (matchingNodes , property , resourceName )
205
211
case "nodeCondition" :
206
212
operatorChecker := regexp .MustCompile (`={1,3}` )
207
213
if ! operatorChecker .MatchString (operator ) {
@@ -311,7 +317,7 @@ func compareNodeResourceConditionalToActual(conditional string, matchingNodes []
311
317
return
312
318
}
313
319
314
- func getQuantity (node corev1.Node , property string ) * resource.Quantity {
320
+ func getQuantity (node corev1.Node , property string , resourceName string ) * resource.Quantity {
315
321
switch property {
316
322
case "cpuCapacity" :
317
323
return node .Status .Capacity .Cpu ()
@@ -329,27 +335,39 @@ func getQuantity(node corev1.Node, property string) *resource.Quantity {
329
335
return node .Status .Capacity .StorageEphemeral ()
330
336
case "ephemeralStorageAllocatable" :
331
337
return node .Status .Allocatable .StorageEphemeral ()
338
+ case "resourceCapacity" :
339
+ capacity , ok := node .Status .Capacity [corev1 .ResourceName (resourceName )]
340
+ if ! ok {
341
+ return nil
342
+ }
343
+ return & capacity
344
+ case "resourceAllocatable" :
345
+ allocatable , ok := node .Status .Allocatable [corev1 .ResourceName (resourceName )]
346
+ if ! ok {
347
+ return nil
348
+ }
349
+ return & allocatable
332
350
}
333
351
return nil
334
352
}
335
353
336
- func findSum (nodes []corev1.Node , property string ) * resource.Quantity {
354
+ func findSum (nodes []corev1.Node , property string , resourceName string ) * resource.Quantity {
337
355
sum := resource.Quantity {}
338
356
339
357
for _ , node := range nodes {
340
- if quant := getQuantity (node , property ); quant != nil {
358
+ if quant := getQuantity (node , property , resourceName ); quant != nil {
341
359
sum .Add (* quant )
342
360
}
343
361
}
344
362
345
363
return & sum
346
364
}
347
365
348
- func findMin (nodes []corev1.Node , property string ) * resource.Quantity {
366
+ func findMin (nodes []corev1.Node , property string , resourceName string ) * resource.Quantity {
349
367
var min * resource.Quantity
350
368
351
369
for _ , node := range nodes {
352
- if quant := getQuantity (node , property ); quant != nil {
370
+ if quant := getQuantity (node , property , resourceName ); quant != nil {
353
371
if min == nil {
354
372
min = quant
355
373
} else if quant .Cmp (* min ) == - 1 {
@@ -361,11 +379,11 @@ func findMin(nodes []corev1.Node, property string) *resource.Quantity {
361
379
return min
362
380
}
363
381
364
- func findMax (nodes []corev1.Node , property string ) * resource.Quantity {
382
+ func findMax (nodes []corev1.Node , property string , resourceName string ) * resource.Quantity {
365
383
var max * resource.Quantity
366
384
367
385
for _ , node := range nodes {
368
- if quant := getQuantity (node , property ); quant != nil {
386
+ if quant := getQuantity (node , property , resourceName ); quant != nil {
369
387
if max == nil {
370
388
max = quant
371
389
} else if quant .Cmp (* max ) == 1 {
@@ -382,6 +400,39 @@ func nodeMatchesFilters(node corev1.Node, filters *troubleshootv1beta2.NodeResou
382
400
return true , nil
383
401
}
384
402
403
+ if filters .ResourceName != "" {
404
+ capacity , capacityExists := node .Status .Capacity [corev1 .ResourceName (filters .ResourceName )]
405
+ allocatable , allocatableExists := node .Status .Allocatable [corev1 .ResourceName (filters .ResourceName )]
406
+
407
+ if ! capacityExists && ! allocatableExists {
408
+ return false , nil
409
+ }
410
+
411
+ if filters .ResourceCapacity != "" {
412
+ parsed , err := resource .ParseQuantity (filters .ResourceCapacity )
413
+ if err != nil {
414
+ return false , errors .Wrap (err , "failed to parse resource capacity" )
415
+ }
416
+
417
+ // Compare the capacity value with the parsed value
418
+ if capacity .Cmp (parsed ) == - 1 {
419
+ return false , nil
420
+ }
421
+ }
422
+
423
+ if filters .ResourceAllocatable != "" {
424
+ parsed , err := resource .ParseQuantity (filters .ResourceAllocatable )
425
+ if err != nil {
426
+ return false , errors .Wrap (err , "failed to parse resource allocatable" )
427
+ }
428
+
429
+ // Compare the allocatable value with the parsed value
430
+ if allocatable .Cmp (parsed ) == - 1 {
431
+ return false , nil
432
+ }
433
+ }
434
+ }
435
+
385
436
// all filters must pass for this to pass
386
437
if filters .Selector != nil {
387
438
selector , err := metav1 .LabelSelectorAsSelector (
0 commit comments