@@ -38,13 +38,11 @@ import (
38
38
. "sigs.k8s.io/karpenter/pkg/utils/testing"
39
39
)
40
40
41
- var (
42
- ctx context.Context
43
- podEventsController * podevents.Controller
44
- env * test.Environment
45
- fakeClock * clock.FakeClock
46
- cp * fake.CloudProvider
47
- )
41
+ var ctx context.Context
42
+ var podEventsController * podevents.Controller
43
+ var env * test.Environment
44
+ var fakeClock * clock.FakeClock
45
+ var cp * fake.CloudProvider
48
46
49
47
func TestAPIs (t * testing.T ) {
50
48
ctx = TestContextWithLogger (t )
@@ -77,7 +75,6 @@ var _ = AfterEach(func() {
77
75
cp .Reset ()
78
76
ExpectCleanedUp (ctx , env .Client )
79
77
})
80
-
81
78
var _ = Describe ("PodEvents" , func () {
82
79
var nodePool * v1.NodePool
83
80
var nodeClaim * v1.NodeClaim
@@ -101,45 +98,109 @@ var _ = Describe("PodEvents", func() {
101
98
NodeName : node .Name ,
102
99
})
103
100
})
104
- It ("should set the nodeclaim lastPodEvent based on PodScheduled condition " , func () {
105
- scheduledTime := fakeClock .Now ().Truncate (time .Second )
101
+ It ("should set the nodeclaim lastPodEvent" , func () {
102
+ timeToCheck := fakeClock .Now ().Truncate (time .Second )
106
103
pod .Status .Conditions = []corev1.PodCondition {{
107
104
Type : corev1 .PodScheduled ,
108
105
Status : corev1 .ConditionTrue ,
109
- LastTransitionTime : metav1.Time {Time : scheduledTime },
106
+ LastTransitionTime : metav1.Time {Time : timeToCheck },
110
107
}}
108
+ ExpectApplied (ctx , env .Client , nodePool , nodeClaim , node , pod )
109
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
110
+
111
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
112
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
113
+ })
114
+ It ("should not set the nodeclaim lastPodEvent when the node does not exist" , func () {
115
+ ExpectApplied (ctx , env .Client , nodePool , nodeClaim , pod )
116
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
117
+
118
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
119
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeZero ())
120
+ })
121
+ It ("should not set the nodeclaim lastPodEvent when pod has no PodScheduled condition" , func () {
111
122
ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
112
123
ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
113
124
114
125
nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
115
- Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (scheduledTime ))
126
+ Expect (nodeClaim .Status .LastPodEventTime .Time .IsZero ()).To (BeTrue ())
127
+ })
128
+ It ("should not set the nodeclaim lastPodEvent when PodScheduled condition is not True" , func () {
129
+ pod .Status .Conditions = []corev1.PodCondition {{
130
+ Type : corev1 .PodScheduled ,
131
+ Status : corev1 .ConditionFalse , // PodScheduled false
132
+ LastTransitionTime : metav1.Time {Time : fakeClock .Now ()},
133
+ }}
134
+ ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
135
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
116
136
117
- // Update the PodScheduled condition's lastTransitionTime
118
- newScheduledTime := fakeClock .Now ().Add (time .Minute ).Truncate (time .Second )
137
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
138
+ Expect (nodeClaim .Status .LastPodEventTime .Time .IsZero ()).To (BeTrue ())
139
+ })
140
+ It ("should succeed when the nodeclaim lastPodEvent when the nodeclaim does not exist" , func () {
141
+ ExpectApplied (ctx , env .Client , nodePool , node , pod )
142
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
143
+ })
144
+ It ("should set the nodeclaim lastPodEvent when it's been set before" , func () {
145
+ nodeClaim .Status .LastPodEventTime .Time = fakeClock .Now ().Add (- 5 * time .Minute )
146
+ timeToCheck := fakeClock .Now ().Truncate (time .Second )
119
147
pod .Status .Conditions = []corev1.PodCondition {{
120
148
Type : corev1 .PodScheduled ,
121
149
Status : corev1 .ConditionTrue ,
122
- LastTransitionTime : metav1.Time {Time : newScheduledTime },
150
+ LastTransitionTime : metav1.Time {Time : timeToCheck },
123
151
}}
124
- ExpectApplied (ctx , env .Client , pod )
152
+ ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
125
153
ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
126
154
127
155
nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
128
- Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (newScheduledTime ))
156
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
129
157
})
130
- It ("should not set the nodeclaim lastPodEvent when the node does not exist" , func () {
131
- ExpectApplied (ctx , env .Client , nodePool , nodeClaim , pod )
158
+ It ("should only set the nodeclaim lastPodEventTime when the event time is after than" , func () {
159
+ timeToCheck := fakeClock .Now ().Truncate (time .Second )
160
+ nodeClaim .Status .LastPodEventTime .Time = timeToCheck
161
+ scheduledTimeBeforeLastPodEventTime := timeToCheck .Add (- 5 * time .Minute )
162
+ pod .Status .Conditions = []corev1.PodCondition {{
163
+ Type : corev1 .PodScheduled ,
164
+ Status : corev1 .ConditionTrue ,
165
+ LastTransitionTime : metav1.Time {Time : scheduledTimeBeforeLastPodEventTime },
166
+ }}
167
+ ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
132
168
ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
133
169
134
170
nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
135
- Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeZero ( ))
171
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo ( timeToCheck ))
136
172
})
137
- It ("should succeed when the nodeclaim lastPodEvent when the nodeclaim does not exist" , func () {
138
- ExpectApplied (ctx , env .Client , nodePool , node , pod )
173
+ It ("should only set the nodeclaim lastPodEvent within the dedupe timeframe" , func () {
174
+ timeToCheck := fakeClock .Now ().Truncate (time .Second )
175
+ pod .Status .Conditions = []corev1.PodCondition {{
176
+ Type : corev1 .PodScheduled ,
177
+ Status : corev1 .ConditionTrue ,
178
+ LastTransitionTime : metav1.Time {Time : timeToCheck },
179
+ }}
180
+ ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
181
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
182
+
183
+ // Expect that the lastPodEventTime is set
184
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
185
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
186
+
187
+ // step through half of the dedupe timeout, and re-reconcile,
188
+ // expecting the status to not change
189
+ fakeClock .Step (5 * time .Second )
139
190
ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
191
+
192
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
193
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
194
+
195
+ // step through rest of the dedupe timeout, and re-reconcile,
196
+ // expecting the status to not change since the podscheduled condition has not changed or terminal or terminating
197
+ fakeClock .Step (5 * time .Second )
198
+ ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
199
+
200
+ nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
201
+ Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
140
202
})
141
203
It ("should set the nodeclaim lastPodEvent when pod becomes terminal" , func () {
142
- // First set up a regular pod
143
204
scheduledTime := fakeClock .Now ().Truncate (time .Second )
144
205
pod .Status .Conditions = []corev1.PodCondition {{
145
206
Type : corev1 .PodScheduled ,
@@ -172,26 +233,6 @@ var _ = Describe("PodEvents", func() {
172
233
nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
173
234
Expect (nodeClaim .Status .LastPodEventTime .Time ).To (BeEquivalentTo (timeToCheck ))
174
235
})
175
- It ("should not update lastPodEvent when pod has no PodScheduled condition" , func () {
176
- // Pod with no conditions
177
- ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
178
- ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
179
-
180
- nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
181
- Expect (nodeClaim .Status .LastPodEventTime .Time .IsZero ()).To (BeTrue ())
182
- })
183
- It ("should not update lastPodEvent when PodScheduled condition is not True" , func () {
184
- pod .Status .Conditions = []corev1.PodCondition {{
185
- Type : corev1 .PodScheduled ,
186
- Status : corev1 .ConditionFalse ,
187
- LastTransitionTime : metav1.Time {Time : fakeClock .Now ()},
188
- }}
189
- ExpectApplied (ctx , env .Client , nodePool , node , nodeClaim , pod )
190
- ExpectObjectReconciled (ctx , env .Client , podEventsController , pod )
191
-
192
- nodeClaim = ExpectExists (ctx , env .Client , nodeClaim )
193
- Expect (nodeClaim .Status .LastPodEventTime .Time .IsZero ()).To (BeTrue ())
194
- })
195
236
It ("should set the nodeclaim lastPodEvent when pod is already in a terminal state" , func () {
196
237
// Setup time
197
238
timeToCheck := fakeClock .Now ().Truncate (time .Second )
0 commit comments