Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/metrics"
)

type metricObserver interface {
ObserveMaxNodeSkipEvalDurationSeconds(duration time.Duration)
}

// MaxNodeSkipEvalTime is a time tracker for the biggest evaluation time of a node during ScaleDown
type MaxNodeSkipEvalTime struct {
// lastEvalTime is the time of previous currentlyUnneededNodeNames parsing
lastEvalTime time.Time
// nodeNamesWithTimeStamps is maps of nodeNames with their time of last successful evaluation
nodeNamesWithTimeStamps map[string]time.Time

metrics metricObserver
}

// NewMaxNodeSkipEvalTime returns LongestNodeScaleDownEvalTime with lastEvalTime set to currentTime
func NewMaxNodeSkipEvalTime(currentTime time.Time) *MaxNodeSkipEvalTime {
return &MaxNodeSkipEvalTime{lastEvalTime: currentTime}
return &MaxNodeSkipEvalTime{lastEvalTime: currentTime, metrics: metrics.DefaultMetrics}
}

// Retrieves the time of the last evaluation of a node.
Expand Down Expand Up @@ -65,6 +71,6 @@ func (l *MaxNodeSkipEvalTime) Update(nodeNames []string, currentTime time.Time)
l.lastEvalTime = currentTime
minimumTime := l.getMin()
longestDuration := currentTime.Sub(minimumTime)
metrics.ObserveMaxNodeSkipEvalDurationSeconds(longestDuration)
l.metrics.ObserveMaxNodeSkipEvalDurationSeconds(longestDuration)
return longestDuration
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type mockMetrics struct {
mock.Mock
}

func (m *mockMetrics) ObserveMaxNodeSkipEvalDurationSeconds(duration time.Duration) {
m.Called(duration)
}

func newMaxNodeSkipEvalTime(currentTime time.Time, metrics metricObserver) *MaxNodeSkipEvalTime {
return &MaxNodeSkipEvalTime{lastEvalTime: currentTime, metrics: metrics}
}

func TestMaxNodeSkipEvalTime(t *testing.T) {
type testCase struct {
name string
Expand Down Expand Up @@ -66,11 +79,15 @@ func TestMaxNodeSkipEvalTime(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
timestamp := start
maxNodeSkipEvalTime := NewMaxNodeSkipEvalTime(start)
mockMetrics := &mockMetrics{}
maxNodeSkipEvalTime := newMaxNodeSkipEvalTime(start, mockMetrics)
mockMetrics.On("ObserveMaxNodeSkipEvalDurationSeconds", mock.Anything).Return()

for i := 0; i < len(tc.unprocessedNodes); i++ {
timestamp = timestamp.Add(1 * time.Second)
assert.Equal(t, time.Duration(tc.wantMaxSkipEvalTimeSeconds[i])*time.Second, maxNodeSkipEvalTime.Update(tc.unprocessedNodes[i], timestamp))
assert.Equal(t, len(tc.unprocessedNodes[i]), len(maxNodeSkipEvalTime.nodeNamesWithTimeStamps))
mockMetrics.AssertCalled(t, "ObserveMaxNodeSkipEvalDurationSeconds", time.Duration(tc.wantMaxSkipEvalTimeSeconds[i])*time.Second)
}
})
}
Expand Down
Loading