Skip to content

Commit 9184734

Browse files
authored
fix: resources are reverted on boosts updated with fixedDuration policy (#120)
1 parent 1622ff6 commit 9184734

File tree

2 files changed

+76
-30
lines changed

2 files changed

+76
-30
lines changed

internal/boost/manager.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ func (m *managerImpl) UpdateRegularCPUBoost(ctx context.Context,
183183
log.V(5).Info("boost not found")
184184
return nil
185185
}
186-
return boost.UpdateFromSpec(ctx, spec)
186+
if err := boost.UpdateFromSpec(ctx, spec); err != nil {
187+
return err
188+
}
189+
m.postProcessNewBoost(ctx, boost)
190+
return nil
187191
}
188192

189193
// GetRegularCPUBoost returns a regular startup cpu boost with a given name and namespace

internal/boost/manager_test.go

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -397,24 +397,57 @@ var _ = Describe("Manager", func() {
397397
mockClient *mock.MockClient
398398
mockReconciler *mock.MockReconciler
399399
c chan time.Time
400+
durationSeconds int64
400401
)
402+
var itShouldRevertBoost = func() {
403+
When("legacy revert mode is not used", func() {
404+
var (
405+
mockSubResourceClient *mock.MockSubResourceClient
406+
)
407+
BeforeEach(func() {
408+
useLegacyRevertMode = false
409+
mockSubResourceClient = mock.NewMockSubResourceClient(mockCtrl)
410+
mockSubResourceClient.EXPECT().Patch(gomock.Any(), gomock.Eq(pod),
411+
gomock.Eq(bpod.NewRevertBootsResourcesPatch())).Return(nil).Times(1)
412+
mockClient.EXPECT().SubResource("resize").
413+
Return(mockSubResourceClient).Times(1)
414+
mockClient.EXPECT().Patch(gomock.Any(), gomock.Eq(pod),
415+
gomock.Eq(bpod.NewRevertBoostLabelsPatch())).Return(nil).Times(1)
416+
})
417+
It("doesn't error", func() {
418+
Expect(err).NotTo(HaveOccurred())
419+
})
420+
})
421+
When("legacy revert mode is used", func() {
422+
BeforeEach(func() {
423+
useLegacyRevertMode = true
424+
mockClient.EXPECT().Update(gomock.Any(), gomock.Eq(pod)).
425+
MinTimes(1).Return(nil)
426+
})
427+
It("doesn't error", func() {
428+
Expect(err).NotTo(HaveOccurred())
429+
})
430+
})
431+
}
401432
BeforeEach(func() {
402433
spec = specTemplate.DeepCopy()
403-
var seconds int64 = 60
404-
spec.Spec.DurationPolicy.Fixed = &autoscaling.FixedDurationPolicy{
405-
Unit: autoscaling.FixedDurationPolicyUnitSec,
406-
Value: seconds,
407-
}
434+
durationSeconds = 60
435+
408436
pod = podTemplate.DeepCopy()
409-
creationTimestamp := time.Now().Add(-1 * time.Duration(seconds) * time.Second).Add(-1 * time.Minute)
437+
creationTimestamp := time.Now().
438+
Add(-1 * time.Duration(durationSeconds) * time.Second).
439+
Add(-1 * time.Minute)
410440
pod.CreationTimestamp = metav1.NewTime(creationTimestamp)
411441
mockClient = mock.NewMockClient(mockCtrl)
412442
mockReconciler = mock.NewMockReconciler(mockCtrl)
413443

414444
c = make(chan time.Time, 1)
415445
mockTicker.EXPECT().Tick().MinTimes(1).Return(c)
416446
mockTicker.EXPECT().Stop().Return()
417-
reconcileReq := reconcile.Request{NamespacedName: types.NamespacedName{Name: spec.Name, Namespace: spec.Namespace}}
447+
reconcileReq := reconcile.Request{
448+
NamespacedName: types.NamespacedName{
449+
Name: spec.Name, Namespace: spec.Namespace,
450+
}}
418451
mockReconciler.EXPECT().Reconcile(gomock.Any(), gomock.Eq(reconcileReq)).Times(1)
419452
})
420453
JustBeforeEach(func() {
@@ -425,36 +458,45 @@ var _ = Describe("Manager", func() {
425458
Expect(err).ShouldNot(HaveOccurred())
426459
err = manager.AddRegularCPUBoost(context.TODO(), boost)
427460
Expect(err).ShouldNot(HaveOccurred())
428-
429-
c <- time.Now()
430-
time.Sleep(500 * time.Millisecond)
431-
cancel()
432-
<-done
433461
})
434-
When("legacy revert mode is not used", func() {
435-
var (
436-
mockSubResourceClient *mock.MockSubResourceClient
437-
)
462+
When("The startup-cpu-boost was created with fixed duration policy", func() {
438463
BeforeEach(func() {
439-
mockSubResourceClient = mock.NewMockSubResourceClient(mockCtrl)
440-
mockSubResourceClient.EXPECT().Patch(gomock.Any(), gomock.Eq(pod),
441-
gomock.Eq(bpod.NewRevertBootsResourcesPatch())).Return(nil).Times(1)
442-
mockClient.EXPECT().SubResource("resize").Return(mockSubResourceClient).Times(1)
443-
mockClient.EXPECT().Patch(gomock.Any(), gomock.Eq(pod),
444-
gomock.Eq(bpod.NewRevertBoostLabelsPatch())).Return(nil).Times(1)
464+
spec.Spec.DurationPolicy.Fixed = &autoscaling.FixedDurationPolicy{
465+
Unit: autoscaling.FixedDurationPolicyUnitSec,
466+
Value: durationSeconds,
467+
}
445468
})
446-
It("doesn't error", func() {
447-
Expect(err).NotTo(HaveOccurred())
469+
JustBeforeEach(func() {
470+
c <- time.Now()
471+
time.Sleep(500 * time.Millisecond)
472+
cancel()
473+
<-done
448474
})
475+
itShouldRevertBoost()
449476
})
450-
When("legacy revert mode is used", func() {
477+
When("The startup-cpu-boost was updated with fixed duration policy", func() {
478+
var updatedSpec *autoscaling.StartupCPUBoost
451479
BeforeEach(func() {
452-
useLegacyRevertMode = true
453-
mockClient.EXPECT().Update(gomock.Any(), gomock.Eq(pod)).MinTimes(1).Return(nil)
480+
spec.Spec.DurationPolicy.PodCondition = &autoscaling.PodConditionDurationPolicy{
481+
Type: corev1.PodReady,
482+
Status: corev1.ConditionTrue,
483+
}
484+
updatedSpec = specTemplate.DeepCopy()
485+
updatedSpec.Spec.DurationPolicy.Fixed = &autoscaling.FixedDurationPolicy{
486+
Unit: autoscaling.FixedDurationPolicyUnitSec,
487+
Value: durationSeconds,
488+
}
454489
})
455-
It("doesn't error", func() {
456-
Expect(err).NotTo(HaveOccurred())
490+
JustBeforeEach(func() {
491+
err = manager.UpdateRegularCPUBoost(ctx, updatedSpec)
492+
Expect(err).ShouldNot(HaveOccurred())
493+
494+
c <- time.Now()
495+
time.Sleep(500 * time.Millisecond)
496+
cancel()
497+
<-done
457498
})
499+
itShouldRevertBoost()
458500
})
459501
})
460502
})

0 commit comments

Comments
 (0)