Skip to content

Commit 3c4e689

Browse files
committed
Fixes after code review.
1 parent 7d9c533 commit 3c4e689

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

vertical-pod-autoscaler/pkg/updater/logic/updater.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ func (u *updater) RunOnce(ctx context.Context) {
189189
defer vpasWithEvictablePodsCounter.Observe()
190190
defer vpasWithEvictedPodsCounter.Observe()
191191

192+
// NOTE: this loop assumes that controlledPods are filtered
193+
// to contain only Pods controlled by a VPA in auto or recreate mode
192194
for vpa, livePods := range controlledPods {
193195
vpaSize := len(livePods)
194196
controlledPodsCounter.Add(vpaSize, vpaSize)

vertical-pod-autoscaler/pkg/utils/metrics/updater/updater.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package updater
1919

2020
import (
21+
"math"
2122
"strconv"
2223

2324
"github.com/prometheus/client_golang/prometheus"
@@ -27,14 +28,14 @@ import (
2728
const (
2829
metricsNamespace = metrics.TopMetricsNamespace + "updater"
2930

31+
// The metrics will distinguish VPA sizes up to 2^maxVpaSizeLog (~1M)
32+
// Anything above that size will be reported in the top bucket.
3033
maxVpaSizeLog = 20
31-
// maxVpaSize = 2 ^ maxVpaSizeLog
32-
maxVpaSize = 1024 * 1024
3334
)
3435

3536
// SizeBasedGauge is a wrapper for incrementally recording values indexed by log2(VPA size)
3637
type SizeBasedGauge struct {
37-
values map[int]int
38+
values [maxVpaSizeLog]int
3839
gauge *prometheus.GaugeVec
3940
}
4041

@@ -95,17 +96,10 @@ func NewExecutionTimer() *metrics.ExecutionTimer {
9596

9697
// newSizeBasedGauge provides a wrapper for counting items in a loop
9798
func newSizeBasedGauge(gauge *prometheus.GaugeVec) *SizeBasedGauge {
98-
obj := SizeBasedGauge{
99-
values: make(map[int]int),
99+
return &SizeBasedGauge{
100+
values: [maxVpaSizeLog]int{},
100101
gauge: gauge,
101102
}
102-
103-
// initialize with empty data so we can clean stale gauge values in Observe
104-
for i := 0; i <= maxVpaSizeLog; i++ {
105-
obj.values[i] = 0
106-
}
107-
108-
return &obj
109103
}
110104

111105
// NewControlledPodsCounter returns a wrapper for counting Pods controlled by Updater
@@ -129,16 +123,15 @@ func NewVpasWithEvictedPodsCounter() *SizeBasedGauge {
129123
}
130124

131125
func getVpaSizeLog2(vpaSize int) int {
132-
if vpaSize >= maxVpaSize {
133-
return maxVpaSizeLog
126+
if vpaSize == 0 {
127+
return 0
134128
}
135129

136-
log2 := 0
137-
for vpaSize > 1 {
138-
log2++
139-
vpaSize >>= 1
130+
ret := int(math.Log2(float64(vpaSize)))
131+
if ret > maxVpaSizeLog {
132+
return maxVpaSizeLog
140133
}
141-
return log2
134+
return ret
142135
}
143136

144137
// AddEvictedPod increases the counter of pods evicted by Updater, by given VPA size

0 commit comments

Comments
 (0)