@@ -31,6 +31,7 @@ import (
31
31
"k8s.io/apimachinery/pkg/util/sets"
32
32
utilfeature"k8s.io/apiserver/pkg/util/feature"
33
33
"k8s.io/client-go/kubernetes/fake"
34
+ "k8s.io/client-go/tools/record"
34
35
featuregatetesting"k8s.io/component-base/featuregate/testing"
35
36
"k8s.io/kubernetes/pkg/features"
36
37
"k8s.io/kubernetes/pkg/kubelet/allocation/state"
@@ -185,7 +186,16 @@ func TestUpdatePodFromAllocation(t *testing.T) {
185
186
}
186
187
}
187
188
188
- func TestIsPodResizeInProgress (t * testing.T ) {
189
+ func getEventsFromFakeRecorder (t * testing.T ,am Manager )string {
190
+ select {
191
+ case e := <- am .(* manager ).recorder .(* record.FakeRecorder ).Events :
192
+ return e
193
+ default :
194
+ return ""
195
+ }
196
+ }
197
+
198
+ func TestCheckPodResizeInProgress (t * testing.T ) {
189
199
type testResources struct {
190
200
cpuReq ,cpuLim ,memReq ,memLim int64
191
201
}
@@ -198,72 +208,83 @@ func TestIsPodResizeInProgress(t *testing.T) {
198
208
}
199
209
200
210
tests := []struct {
201
- name string
202
- containers []testContainer
203
- expectHasResize bool
211
+ name string
212
+ containers []testContainer
213
+ oldPodResizeInProgressCondition bool
214
+ expectHasResize bool
215
+ expectPodResizeCompletedMsg string
204
216
}{{
205
217
name :"simple running container" ,
206
218
containers : []testContainer {{
207
219
allocated :testResources {100 ,100 ,100 ,100 },
208
220
actuated :& testResources {100 ,100 ,100 ,100 },
209
221
isRunning :true ,
210
222
}},
211
- expectHasResize :false ,
223
+ oldPodResizeInProgressCondition :true ,
224
+ expectHasResize :false ,
225
+ expectPodResizeCompletedMsg :`Normal ResizeCompleted Pod resize completed: {"containers":[{"name":"c0","resources":{"limits":{"cpu":"100m","memory":"100"},"requests":{"cpu":"100m","memory":"100"}}}]}` ,
212
226
}, {
213
227
name :"simple unstarted container" ,
214
228
containers : []testContainer {{
215
229
allocated :testResources {100 ,100 ,100 ,100 },
216
230
unstarted :true ,
217
231
}},
218
- expectHasResize :false ,
232
+ oldPodResizeInProgressCondition :false ,
233
+ expectHasResize :false ,
219
234
}, {
220
235
name :"simple resized container/cpu req" ,
221
236
containers : []testContainer {{
222
237
allocated :testResources {100 ,200 ,100 ,200 },
223
238
actuated :& testResources {150 ,200 ,100 ,200 },
224
239
isRunning :true ,
225
240
}},
226
- expectHasResize :true ,
241
+ oldPodResizeInProgressCondition :false ,
242
+ expectHasResize :true ,
227
243
}, {
228
244
name :"simple resized container/cpu limit" ,
229
245
containers : []testContainer {{
230
246
allocated :testResources {100 ,200 ,100 ,200 },
231
247
actuated :& testResources {100 ,300 ,100 ,200 },
232
248
isRunning :true ,
233
249
}},
234
- expectHasResize :true ,
250
+ oldPodResizeInProgressCondition :false ,
251
+ expectHasResize :true ,
235
252
}, {
236
253
name :"simple resized container/mem req" ,
237
254
containers : []testContainer {{
238
255
allocated :testResources {100 ,200 ,100 ,200 },
239
256
actuated :& testResources {100 ,200 ,150 ,200 },
240
257
isRunning :true ,
241
258
}},
242
- expectHasResize :true ,
259
+ oldPodResizeInProgressCondition :false ,
260
+ expectHasResize :true ,
243
261
}, {
244
262
name :"simple resized container/cpu+mem req" ,
245
263
containers : []testContainer {{
246
264
allocated :testResources {100 ,200 ,100 ,200 },
247
265
actuated :& testResources {150 ,200 ,150 ,200 },
248
266
isRunning :true ,
249
267
}},
250
- expectHasResize :true ,
268
+ oldPodResizeInProgressCondition :false ,
269
+ expectHasResize :true ,
251
270
}, {
252
271
name :"simple resized container/mem limit" ,
253
272
containers : []testContainer {{
254
273
allocated :testResources {100 ,200 ,100 ,200 },
255
274
actuated :& testResources {100 ,200 ,100 ,300 },
256
275
isRunning :true ,
257
276
}},
258
- expectHasResize :true ,
277
+ oldPodResizeInProgressCondition :false ,
278
+ expectHasResize :true ,
259
279
}, {
260
280
name :"terminated resized container" ,
261
281
containers : []testContainer {{
262
282
allocated :testResources {100 ,200 ,100 ,200 },
263
283
actuated :& testResources {200 ,200 ,100 ,200 },
264
284
isRunning :false ,
265
285
}},
266
- expectHasResize :false ,
286
+ oldPodResizeInProgressCondition :false ,
287
+ expectHasResize :false ,
267
288
}, {
268
289
name :"non-sidecar init container" ,
269
290
containers : []testContainer {{
@@ -275,7 +296,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
275
296
actuated :& testResources {100 ,200 ,100 ,200 },
276
297
isRunning :true ,
277
298
}},
278
- expectHasResize :false ,
299
+ oldPodResizeInProgressCondition :false ,
300
+ expectHasResize :false ,
279
301
}, {
280
302
name :"non-resized sidecar" ,
281
303
containers : []testContainer {{
@@ -288,7 +310,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
288
310
actuated :& testResources {100 ,200 ,100 ,200 },
289
311
isRunning :true ,
290
312
}},
291
- expectHasResize :false ,
313
+ oldPodResizeInProgressCondition :false ,
314
+ expectHasResize :false ,
292
315
}, {
293
316
name :"resized sidecar" ,
294
317
containers : []testContainer {{
@@ -301,7 +324,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
301
324
actuated :& testResources {100 ,200 ,100 ,200 },
302
325
isRunning :true ,
303
326
}},
304
- expectHasResize :true ,
327
+ oldPodResizeInProgressCondition :false ,
328
+ expectHasResize :true ,
305
329
}, {
306
330
name :"several containers and a resize" ,
307
331
containers : []testContainer {{
@@ -320,31 +344,55 @@ func TestIsPodResizeInProgress(t *testing.T) {
320
344
actuated :& testResources {200 ,200 ,100 ,200 },// Resized
321
345
isRunning :true ,
322
346
}},
323
- expectHasResize :true ,
347
+ oldPodResizeInProgressCondition :false ,
348
+ expectHasResize :true ,
349
+ }, {
350
+ name :"several containers" ,
351
+ containers : []testContainer {{
352
+ allocated :testResources {cpuReq :100 ,cpuLim :200 },
353
+ actuated :& testResources {cpuReq :100 ,cpuLim :200 },
354
+ sidecar :true ,
355
+ isRunning :true ,
356
+ }, {
357
+ allocated :testResources {memReq :100 ,memLim :200 },
358
+ actuated :& testResources {memReq :100 ,memLim :200 },
359
+ isRunning :true ,
360
+ }, {
361
+ allocated :testResources {cpuReq :200 ,memReq :100 },
362
+ actuated :& testResources {cpuReq :200 ,memReq :100 },
363
+ isRunning :true ,
364
+ }},
365
+ oldPodResizeInProgressCondition :true ,
366
+ expectHasResize :false ,
367
+ expectPodResizeCompletedMsg :`Normal ResizeCompleted Pod resize completed: {"initContainers":[{"name":"c0","resources":{"limits":{"cpu":"200m"},"requests":{"cpu":"100m"}}}],"containers":[{"name":"c1","resources":{"limits":{"memory":"200"},"requests":{"memory":"100"}}},{"name":"c2","resources":{"requests":{"cpu":"200m","memory":"100"}}}]}` ,
324
368
}, {
325
369
name :"best-effort pod" ,
326
370
containers : []testContainer {{
327
371
allocated :testResources {},
328
372
actuated :& testResources {},
329
373
isRunning :true ,
330
374
}},
331
- expectHasResize :false ,
375
+ oldPodResizeInProgressCondition :true ,
376
+ expectHasResize :false ,
377
+ expectPodResizeCompletedMsg :`Normal ResizeCompleted Pod resize completed: {"containers":[{"name":"c0","resources":{}}]}` ,
332
378
}, {
333
379
name :"burstable pod/not resizing" ,
334
380
containers : []testContainer {{
335
381
allocated :testResources {cpuReq :100 },
336
382
actuated :& testResources {cpuReq :100 },
337
383
isRunning :true ,
338
384
}},
339
- expectHasResize :false ,
385
+ oldPodResizeInProgressCondition :false ,
386
+ expectHasResize :false ,
340
387
}, {
341
388
name :"burstable pod/resized" ,
342
389
containers : []testContainer {{
343
390
allocated :testResources {cpuReq :100 },
344
391
actuated :& testResources {cpuReq :500 },
345
392
isRunning :true ,
346
393
}},
347
- expectHasResize :true ,
394
+ oldPodResizeInProgressCondition :false ,
395
+ expectHasResize :true ,
348
396
}}
349
397
350
398
mkRequirements := func (r testResources ) v1.ResourceRequirements {
@@ -431,8 +479,32 @@ func TestIsPodResizeInProgress(t *testing.T) {
431
479
}
432
480
require .NoError (t ,am .SetAllocatedResources (pod ))
433
481
434
- hasResizedResources := am .(* manager ).isPodResizeInProgress (pod ,podStatus )
435
- require .Equal (t ,test .expectHasResize ,hasResizedResources ,"hasResizedResources" )
482
+ am .(* manager ).recorder = record .NewFakeRecorder (200 )
483
+
484
+ // Set old Pod condition as Inprogress, so that ClearPodResizeInProgressCondition is true and emit resize completed event
485
+ if test .oldPodResizeInProgressCondition {
486
+ am .(* manager ).statusManager .SetPodResizeInProgressCondition (pod .UID ,"" ,"" ,int64 (1 ))
487
+ }
488
+
489
+ am .CheckPodResizeInProgress (pod ,podStatus )
490
+
491
+ // Verify pod resize completed event is emitted
492
+ podResizeCompletionEvent := getEventsFromFakeRecorder (t ,am )
493
+ assert .Equal (t ,test .expectPodResizeCompletedMsg ,podResizeCompletionEvent )
494
+
495
+ if test .expectHasResize {
496
+ // Verify the status manager has the InProgress condition set on it
497
+ gotResizeConditions := am .(* manager ).statusManager .GetPodResizeConditions (pod .UID )
498
+ for _ ,c := range gotResizeConditions {
499
+ require .Equal (t ,v1 .PodResizeInProgress ,c .Type ,"ResizeConditions Type should be PodResizeInProgress" )
500
+ require .Empty (t ,c .Reason ,"ResizeConditions Error" )
501
+ require .Empty (t ,c .Message ,"ResizeConditions Message" )
502
+ }
503
+ }else {
504
+ // Verify pod resize Inprogress condition is cleared
505
+ gotResizeConditions := am .(* manager ).statusManager .GetPodResizeConditions (pod .UID )
506
+ require .Empty (t ,gotResizeConditions ,"ResizeConditions Error" )
507
+ }
436
508
})
437
509
}
438
510
}