Skip to content

Commit fa84d3f

Browse files
authored
Merge pull request #180 from MikaelSmith/fix-allocatable-sum
Fix sum(memoryAllocatable) and sum(podAllocatable)
2 parents f377b7c + 8f8b8b2 commit fa84d3f

File tree

2 files changed

+331
-830
lines changed

2 files changed

+331
-830
lines changed

pkg/analyze/node_resources.go

Lines changed: 41 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,12 @@ func compareNodeResourceConditionalToActual(conditional string, matchingNodes []
139139
switch function {
140140
case "count":
141141
actualValue = len(matchingNodes)
142-
break
143142
case "min":
144-
av, err := findMin(matchingNodes, property)
145-
if err != nil {
146-
return false, errors.Wrap(err, "failed to find min")
147-
}
148-
actualValue = av
143+
actualValue = findMin(matchingNodes, property)
149144
case "max":
150-
av, err := findMax(matchingNodes, property)
151-
if err != nil {
152-
return false, errors.Wrap(err, "failed to find max")
153-
}
154-
actualValue = av
145+
actualValue = findMax(matchingNodes, property)
155146
case "sum":
156-
sum, err := findSum(matchingNodes, property)
157-
if err != nil {
158-
return false, errors.Wrap(err, "failed to find sum")
159-
}
160-
actualValue = sum
147+
actualValue = findSum(matchingNodes, property)
161148
}
162149

163150
switch operator {
@@ -230,223 +217,70 @@ func compareNodeResourceConditionalToActual(conditional string, matchingNodes []
230217
return false, errors.New("unexpected conditional in nodeResources")
231218
}
232219

233-
func findSum(nodes []corev1.Node, property string) (*resource.Quantity, error) {
220+
func getQuantity(node corev1.Node, property string) *resource.Quantity {
221+
switch property {
222+
case "cpuCapacity":
223+
return node.Status.Capacity.Cpu()
224+
case "cpuAllocatable":
225+
return node.Status.Allocatable.Cpu()
226+
case "memoryCapacity":
227+
return node.Status.Capacity.Memory()
228+
case "memoryAllocatable":
229+
return node.Status.Allocatable.Memory()
230+
case "podCapacity":
231+
return node.Status.Capacity.Pods()
232+
case "podAllocatable":
233+
return node.Status.Allocatable.Pods()
234+
case "ephemeralStorageCapacity":
235+
return node.Status.Capacity.StorageEphemeral()
236+
case "ephemeralStorageAllocatable":
237+
return node.Status.Allocatable.StorageEphemeral()
238+
}
239+
return nil
240+
}
241+
242+
func findSum(nodes []corev1.Node, property string) *resource.Quantity {
234243
sum := resource.Quantity{}
235244

236245
for _, node := range nodes {
237-
switch property {
238-
case "cpuCapacity":
239-
if node.Status.Capacity.Cpu() != nil {
240-
sum.Add(*node.Status.Capacity.Cpu())
241-
}
242-
break
243-
case "cpuAllocatable":
244-
if node.Status.Allocatable.Cpu() != nil {
245-
sum.Add(*node.Status.Allocatable.Cpu())
246-
}
247-
break
248-
case "memoryCapacity":
249-
if node.Status.Capacity.Memory() != nil {
250-
sum.Add(*node.Status.Capacity.Memory())
251-
}
252-
break
253-
case "memoryAllocatable":
254-
if node.Status.Allocatable.Memory() != nil {
255-
sum.Add(*node.Status.Allocatable.Cpu())
256-
}
257-
break
258-
case "podCapacity":
259-
if node.Status.Capacity.Pods() != nil {
260-
sum.Add(*node.Status.Capacity.Pods())
261-
}
262-
break
263-
case "podAllocatable":
264-
if node.Status.Allocatable.Pods() != nil {
265-
sum.Add(*node.Status.Allocatable.Cpu())
266-
}
267-
break
268-
case "ephemeralStorageCapacity":
269-
if node.Status.Capacity.StorageEphemeral() != nil {
270-
sum.Add(*node.Status.Capacity.StorageEphemeral())
271-
}
272-
break
273-
case "ephemeralStorageAllocatable":
274-
if node.Status.Allocatable.StorageEphemeral() != nil {
275-
sum.Add(*node.Status.Allocatable.StorageEphemeral())
276-
}
277-
break
246+
if quant := getQuantity(node, property); quant != nil {
247+
sum.Add(*quant)
278248
}
279249
}
280250

281-
return &sum, nil
251+
return &sum
282252
}
283253

284-
func findMin(nodes []corev1.Node, property string) (*resource.Quantity, error) {
254+
func findMin(nodes []corev1.Node, property string) *resource.Quantity {
285255
var min *resource.Quantity
286256

287257
for _, node := range nodes {
288-
switch property {
289-
case "cpuCapacity":
290-
if min == nil {
291-
min = node.Status.Capacity.Cpu()
292-
} else {
293-
if node.Status.Capacity.Cpu().Cmp(*min) == -1 {
294-
min = node.Status.Capacity.Cpu()
295-
}
296-
}
297-
break
298-
case "cpuAllocatable":
299-
if min == nil {
300-
min = node.Status.Allocatable.Cpu()
301-
} else {
302-
if node.Status.Allocatable.Cpu().Cmp(*min) == -1 {
303-
min = node.Status.Allocatable.Cpu()
304-
}
305-
}
306-
break
307-
case "memoryCapacity":
258+
if quant := getQuantity(node, property); quant != nil {
308259
if min == nil {
309-
min = node.Status.Capacity.Memory()
310-
} else {
311-
if node.Status.Capacity.Memory().Cmp(*min) == -1 {
312-
min = node.Status.Capacity.Memory()
313-
}
260+
min = quant
261+
} else if quant.Cmp(*min) == -1 {
262+
min = quant
314263
}
315-
break
316-
case "memoryAllocatable":
317-
if min == nil {
318-
min = node.Status.Allocatable.Memory()
319-
} else {
320-
if node.Status.Allocatable.Memory().Cmp(*min) == -1 {
321-
min = node.Status.Allocatable.Memory()
322-
}
323-
}
324-
break
325-
case "podCapacity":
326-
if min == nil {
327-
min = node.Status.Capacity.Pods()
328-
} else {
329-
if node.Status.Capacity.Pods().Cmp(*min) == -1 {
330-
min = node.Status.Capacity.Pods()
331-
}
332-
}
333-
break
334-
case "podAllocatable":
335-
if min == nil {
336-
min = node.Status.Allocatable.Pods()
337-
} else {
338-
if node.Status.Allocatable.Pods().Cmp(*min) == -1 {
339-
min = node.Status.Allocatable.Pods()
340-
}
341-
}
342-
break
343-
case "ephemeralStorageCapacity":
344-
if min == nil {
345-
min = node.Status.Capacity.StorageEphemeral()
346-
} else {
347-
if node.Status.Capacity.StorageEphemeral().Cmp(*min) == -1 {
348-
min = node.Status.Capacity.StorageEphemeral()
349-
}
350-
}
351-
break
352-
case "ephemeralStorageAllocatable":
353-
if min == nil {
354-
min = node.Status.Allocatable.StorageEphemeral()
355-
} else {
356-
if node.Status.Allocatable.StorageEphemeral().Cmp(*min) == -1 {
357-
min = node.Status.Allocatable.StorageEphemeral()
358-
}
359-
}
360-
break
361-
362264
}
363265
}
364266

365-
return min, nil
267+
return min
366268
}
367269

368-
func findMax(nodes []corev1.Node, property string) (*resource.Quantity, error) {
270+
func findMax(nodes []corev1.Node, property string) *resource.Quantity {
369271
var max *resource.Quantity
370272

371273
for _, node := range nodes {
372-
switch property {
373-
case "cpuCapacity":
374-
if max == nil {
375-
max = node.Status.Capacity.Cpu()
376-
} else {
377-
if node.Status.Capacity.Cpu().Cmp(*max) == 1 {
378-
max = node.Status.Capacity.Cpu()
379-
}
380-
}
381-
break
382-
case "cpuAllocatable":
383-
if max == nil {
384-
max = node.Status.Allocatable.Cpu()
385-
} else {
386-
if node.Status.Allocatable.Cpu().Cmp(*max) == 1 {
387-
max = node.Status.Allocatable.Cpu()
388-
}
389-
}
390-
break
391-
case "memoryCapacity":
274+
if quant := getQuantity(node, property); quant != nil {
392275
if max == nil {
393-
max = node.Status.Capacity.Memory()
394-
} else {
395-
if node.Status.Capacity.Memory().Cmp(*max) == 1 {
396-
max = node.Status.Capacity.Memory()
397-
}
276+
max = quant
277+
} else if quant.Cmp(*max) == 1 {
278+
max = quant
398279
}
399-
break
400-
case "memoryAllocatable":
401-
if max == nil {
402-
max = node.Status.Allocatable.Memory()
403-
} else {
404-
if node.Status.Allocatable.Memory().Cmp(*max) == 1 {
405-
max = node.Status.Allocatable.Memory()
406-
}
407-
}
408-
break
409-
case "podCapacity":
410-
if max == nil {
411-
max = node.Status.Capacity.Pods()
412-
} else {
413-
if node.Status.Capacity.Pods().Cmp(*max) == 1 {
414-
max = node.Status.Capacity.Pods()
415-
}
416-
}
417-
break
418-
case "podAllocatable":
419-
if max == nil {
420-
max = node.Status.Allocatable.Pods()
421-
} else {
422-
if node.Status.Allocatable.Pods().Cmp(*max) == 1 {
423-
max = node.Status.Allocatable.Pods()
424-
}
425-
}
426-
break
427-
case "ephemeralStorageCapacity":
428-
if max == nil {
429-
max = node.Status.Capacity.StorageEphemeral()
430-
} else {
431-
if node.Status.Capacity.StorageEphemeral().Cmp(*max) == 1 {
432-
max = node.Status.Capacity.StorageEphemeral()
433-
}
434-
}
435-
break
436-
case "ephemeralStorageAllocatable":
437-
if max == nil {
438-
max = node.Status.Allocatable.StorageEphemeral()
439-
} else {
440-
if node.Status.Allocatable.StorageEphemeral().Cmp(*max) == 1 {
441-
max = node.Status.Allocatable.StorageEphemeral()
442-
}
443-
}
444-
break
445-
446280
}
447281
}
448282

449-
return max, nil
283+
return max
450284
}
451285

452286
func nodeMatchesFilters(node corev1.Node, filters *troubleshootv1beta1.NodeResourceFilters) (bool, error) {

0 commit comments

Comments
 (0)