Skip to content

Commit

Permalink
Stop scaledown timer
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Feb 6, 2024
1 parent d4896cf commit 27f9cd6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/deployments/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func getModelsFromAnnotation(ann map[string]string) []string {
}

func (r *Manager) removeDeployment(req ctrl.Request) {
r.getScaler(req.Name).StopScaleDownTimer()
r.scalersMtx.Lock()
delete(r.scalers, req.Name)
r.scalersMtx.Unlock()
Expand Down
78 changes: 78 additions & 0 deletions pkg/deployments/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"context"
"reflect"
"testing"
"time"

"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
Expand Down Expand Up @@ -136,6 +141,79 @@ func TestAddDeployment(t *testing.T) {
}
}

func TestRemoveDeployment(t *testing.T) {
const myDeployment = "myDeployment"
specs := map[string]struct {
setup func(t *testing.T, m *Manager)
assert func(t *testing.T, m *Manager)
}{
"single model deployment": {
setup: func(t *testing.T, m *Manager) {
m.modelToDeployment["model1"] = myDeployment
m.scalers[myDeployment] = &scaler{}
},
assert: func(t *testing.T, m *Manager) {
assert.Len(t, m.modelToDeployment, 0)
assert.Len(t, m.scalers, 0)
},
},
"multi model deployment": {
setup: func(t *testing.T, m *Manager) {
m.modelToDeployment["model1"] = myDeployment
m.modelToDeployment["model2"] = myDeployment
m.modelToDeployment["other"] = "other"
m.scalers[myDeployment] = &scaler{}
m.scalers["other"] = &scaler{}
},
assert: func(t *testing.T, m *Manager) {
assert.Equal(t, map[string]string{"other": "other"}, m.modelToDeployment)
assert.Equal(t, map[string]*scaler{"other": {}}, m.scalers)
},
},
"unknown deployment - ignored": {
setup: func(t *testing.T, m *Manager) {
m.modelToDeployment["other"] = "other"
m.scalers["other"] = &scaler{}
},
assert: func(t *testing.T, m *Manager) {
assert.Equal(t, map[string]string{"other": "other"}, m.modelToDeployment)
assert.Equal(t, map[string]*scaler{"other": {}}, m.scalers)
},
},
"scale down timer stopped": {
setup: func(t *testing.T, m *Manager) {
m.modelToDeployment["model1"] = myDeployment
m.scalers[myDeployment] = &scaler{
scaleDownStarted: true,
scaleDownTimer: time.AfterFunc(100*time.Millisecond, func() {
t.Fatal("scale down timer not stopped")
}),
}
},
assert: func(t *testing.T, m *Manager) {
// wait a bit longer than the timer would need to run
time.Sleep(120 * time.Millisecond)
assert.Len(t, m.modelToDeployment, 0)
assert.Len(t, m.scalers, 0)
},
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
m := &Manager{
scalers: make(map[string]*scaler),
modelToDeployment: make(map[string]string),
}
spec.setup(t, m)
req := reconcile.Request{NamespacedName: types.NamespacedName{Name: myDeployment}}
// when
m.removeDeployment(req)
// then
spec.assert(t, m)
})
}
}

type partialFakeClient struct {
client.Client
subRes client.Object
Expand Down
9 changes: 9 additions & 0 deletions pkg/deployments/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ func (s *scaler) compareScales(current, desired int32) {
}
}

func (s *scaler) StopScaleDownTimer() {
s.mtx.Lock()
defer s.mtx.Unlock()
if s.scaleDownTimer != nil {
s.scaleDownTimer.Stop()
}
s.scaleDownStarted = false
}

type scale struct {
Current, Min, Max int32
}
Expand Down

0 comments on commit 27f9cd6

Please sign in to comment.