diff --git a/README.md b/README.md index 70c8ba5..6da11f6 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,8 @@ spec: ### [Boost duration] fixed time -Define the fixed amount of time, the resource boost effect will last for it since the POD's creation. +Define the fixed amount of time, the resource boost effect will last for it since the +**POD's schedule time**. ```yaml spec: diff --git a/internal/boost/duration/fixed.go b/internal/boost/duration/fixed.go index 1060174..fdee18f 100644 --- a/internal/boost/duration/fixed.go +++ b/internal/boost/duration/fixed.go @@ -52,5 +52,10 @@ func (p *FixedDurationPolicy) Duration() time.Duration { func (p *FixedDurationPolicy) Valid(pod *v1.Pod) bool { now := p.timeFunc() - return pod.CreationTimestamp.Add(p.duration).After(now) + for _, condition := range pod.Status.Conditions { + if condition.Type == v1.PodScheduled && condition.Status == v1.ConditionTrue { + return condition.LastTransitionTime.Add(p.duration).After(now) + } + } + return true } diff --git a/internal/boost/duration/fixed_test.go b/internal/boost/duration/fixed_test.go index eba7c10..3c34fd1 100644 --- a/internal/boost/duration/fixed_test.go +++ b/internal/boost/duration/fixed_test.go @@ -20,6 +20,7 @@ import ( "github.com/google/kube-startup-cpu-boost/internal/boost/duration" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -39,17 +40,33 @@ var _ = Describe("FixedDurationPolicy", func() { }) Describe("Validates POD", func() { + When("the POD has no status conditions", func() { + It("returns policy is valid", func() { + pod.Status.Conditions = []v1.PodCondition{} + Expect(policy.Valid(pod)).To(BeTrue()) + }) + }) When("the life time of a POD exceeds the policy duration", func() { It("returns policy is not valid", func() { - creationTimesamp := now.Add(-1 * timeDuration).Add(-1 * time.Minute) - pod.CreationTimestamp = metav1.NewTime(creationTimesamp) + scheduleTime := now.Add(-1 * timeDuration).Add(-1 * time.Minute) + pod.Status.Conditions = []v1.PodCondition{ + { + LastTransitionTime: metav1.NewTime(scheduleTime), + Type: v1.PodScheduled, + Status: v1.ConditionTrue, + }} Expect(policy.Valid(pod)).To(BeFalse()) }) }) When("the life time of a POD is within policy duration", func() { It("returns policy is valid", func() { - creationTimesamp := now.Add(-1 * timeDuration).Add(1 * time.Minute) - pod.CreationTimestamp = metav1.NewTime(creationTimesamp) + scheduleTime := now.Add(-1 * timeDuration).Add(1 * time.Minute) + pod.Status.Conditions = []v1.PodCondition{ + { + LastTransitionTime: metav1.NewTime(scheduleTime), + Type: v1.PodScheduled, + Status: v1.ConditionTrue, + }} Expect(policy.Valid(pod)).To(BeTrue()) }) }) diff --git a/internal/boost/manager_test.go b/internal/boost/manager_test.go index 2f55735..b564bfc 100644 --- a/internal/boost/manager_test.go +++ b/internal/boost/manager_test.go @@ -434,10 +434,15 @@ var _ = Describe("Manager", func() { durationSeconds = 60 pod = podTemplate.DeepCopy() - creationTimestamp := time.Now(). + scheduledTimestamp := time.Now(). Add(-1 * time.Duration(durationSeconds) * time.Second). Add(-1 * time.Minute) - pod.CreationTimestamp = metav1.NewTime(creationTimestamp) + pod.Status.Conditions = []corev1.PodCondition{ + { + LastTransitionTime: metav1.NewTime(scheduledTimestamp), + Type: corev1.PodScheduled, + Status: corev1.ConditionTrue, + }} mockClient = mock.NewMockClient(mockCtrl) mockReconciler = mock.NewMockReconciler(mockCtrl)