Skip to content

Commit 98f9ebd

Browse files
authored
feat: fixed duration policy uses POD scheduled time (#123)
1 parent 9184734 commit 98f9ebd

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ spec:
187187

188188
### [Boost duration] fixed time
189189

190-
Define the fixed amount of time, the resource boost effect will last for it since the POD's creation.
190+
Define the fixed amount of time, the resource boost effect will last for it since the
191+
**POD's schedule time**.
191192

192193
```yaml
193194
spec:

internal/boost/duration/fixed.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,10 @@ func (p *FixedDurationPolicy) Duration() time.Duration {
5252

5353
func (p *FixedDurationPolicy) Valid(pod *v1.Pod) bool {
5454
now := p.timeFunc()
55-
return pod.CreationTimestamp.Add(p.duration).After(now)
55+
for _, condition := range pod.Status.Conditions {
56+
if condition.Type == v1.PodScheduled && condition.Status == v1.ConditionTrue {
57+
return condition.LastTransitionTime.Add(p.duration).After(now)
58+
}
59+
}
60+
return true
5661
}

internal/boost/duration/fixed_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/google/kube-startup-cpu-boost/internal/boost/duration"
2121
. "github.com/onsi/ginkgo/v2"
2222
. "github.com/onsi/gomega"
23+
v1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
)
2526

@@ -39,17 +40,33 @@ var _ = Describe("FixedDurationPolicy", func() {
3940
})
4041

4142
Describe("Validates POD", func() {
43+
When("the POD has no status conditions", func() {
44+
It("returns policy is valid", func() {
45+
pod.Status.Conditions = []v1.PodCondition{}
46+
Expect(policy.Valid(pod)).To(BeTrue())
47+
})
48+
})
4249
When("the life time of a POD exceeds the policy duration", func() {
4350
It("returns policy is not valid", func() {
44-
creationTimesamp := now.Add(-1 * timeDuration).Add(-1 * time.Minute)
45-
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
51+
scheduleTime := now.Add(-1 * timeDuration).Add(-1 * time.Minute)
52+
pod.Status.Conditions = []v1.PodCondition{
53+
{
54+
LastTransitionTime: metav1.NewTime(scheduleTime),
55+
Type: v1.PodScheduled,
56+
Status: v1.ConditionTrue,
57+
}}
4658
Expect(policy.Valid(pod)).To(BeFalse())
4759
})
4860
})
4961
When("the life time of a POD is within policy duration", func() {
5062
It("returns policy is valid", func() {
51-
creationTimesamp := now.Add(-1 * timeDuration).Add(1 * time.Minute)
52-
pod.CreationTimestamp = metav1.NewTime(creationTimesamp)
63+
scheduleTime := now.Add(-1 * timeDuration).Add(1 * time.Minute)
64+
pod.Status.Conditions = []v1.PodCondition{
65+
{
66+
LastTransitionTime: metav1.NewTime(scheduleTime),
67+
Type: v1.PodScheduled,
68+
Status: v1.ConditionTrue,
69+
}}
5370
Expect(policy.Valid(pod)).To(BeTrue())
5471
})
5572
})

internal/boost/manager_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,15 @@ var _ = Describe("Manager", func() {
434434
durationSeconds = 60
435435

436436
pod = podTemplate.DeepCopy()
437-
creationTimestamp := time.Now().
437+
scheduledTimestamp := time.Now().
438438
Add(-1 * time.Duration(durationSeconds) * time.Second).
439439
Add(-1 * time.Minute)
440-
pod.CreationTimestamp = metav1.NewTime(creationTimestamp)
440+
pod.Status.Conditions = []corev1.PodCondition{
441+
{
442+
LastTransitionTime: metav1.NewTime(scheduledTimestamp),
443+
Type: corev1.PodScheduled,
444+
Status: corev1.ConditionTrue,
445+
}}
441446
mockClient = mock.NewMockClient(mockCtrl)
442447
mockReconciler = mock.NewMockReconciler(mockCtrl)
443448

0 commit comments

Comments
 (0)