...
1
16
17 package metrics
18
19 import (
20 "fmt"
21 )
22
23
24
25
26 func GetResourceUtilizationRatio(metrics PodMetricsInfo, requests map[string]int64, targetUtilization int32) (utilizationRatio float64, currentUtilization int32, rawAverageValue int64, err error) {
27 metricsTotal := int64(0)
28 requestsTotal := int64(0)
29 numEntries := 0
30
31 for podName, metric := range metrics {
32 request, hasRequest := requests[podName]
33 if !hasRequest {
34
35 continue
36 }
37
38 metricsTotal += metric.Value
39 requestsTotal += request
40 numEntries++
41 }
42
43
44
45 if requestsTotal == 0 {
46 return 0, 0, 0, fmt.Errorf("no metrics returned matched known pods")
47 }
48
49 currentUtilization = int32((metricsTotal * 100) / requestsTotal)
50
51 return float64(currentUtilization) / float64(targetUtilization), currentUtilization, metricsTotal / int64(numEntries), nil
52 }
53
54
55
56
57 func GetMetricUsageRatio(metrics PodMetricsInfo, targetUsage int64) (usageRatio float64, currentUsage int64) {
58 metricsTotal := int64(0)
59 for _, metric := range metrics {
60 metricsTotal += metric.Value
61 }
62
63 currentUsage = metricsTotal / int64(len(metrics))
64
65 return float64(currentUsage) / float64(targetUsage), currentUsage
66 }
67
View as plain text