@@ -250,58 +250,94 @@ func TestStatefulPodControlCreatePodFailed(t *testing.T) {
250
250
}
251
251
252
252
func TestStatefulPodControlNoOpUpdate (t * testing.T ) {
253
- recorder := record .NewFakeRecorder (10 )
254
- set := newStatefulSet (3 )
255
- pod := newStatefulSetPod (set , 0 )
256
- fakeClient := & fake.Clientset {}
257
- claims := getPersistentVolumeClaims (set , pod )
258
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
259
- for k := range claims {
260
- claim := claims [k ]
261
- indexer .Add (& claim )
253
+ testFn := func (t * testing.T ) {
254
+ recorder := record .NewFakeRecorder (10 )
255
+ set := newStatefulSet (3 )
256
+ pod := newStatefulSetPod (set , 0 )
257
+ fakeClient := & fake.Clientset {}
258
+ claims := getPersistentVolumeClaims (set , pod )
259
+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
260
+ for k := range claims {
261
+ claim := claims [k ]
262
+ indexer .Add (& claim )
263
+ }
264
+ claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
265
+ control := NewStatefulPodControl (fakeClient , nil , claimLister , nil , recorder )
266
+ fakeClient .AddReactor ("*" , "*" , func (action core.Action ) (bool , runtime.Object , error ) {
267
+ t .Error ("no-op update should not make any client invocation" )
268
+ return true , nil , apierrors .NewInternalError (errors .New ("If we are here we have a problem" ))
269
+ })
270
+ if err := control .UpdateStatefulPod (set , pod ); err != nil {
271
+ t .Errorf ("Error returned on no-op update error: %s" , err )
272
+ }
273
+ events := collectEvents (recorder .Events )
274
+ if eventCount := len (events ); eventCount != 0 {
275
+ t .Errorf ("no-op update: got %d events, but want 0" , eventCount )
276
+ }
262
277
}
263
- claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
264
- control := NewStatefulPodControl (fakeClient , nil , claimLister , nil , recorder )
265
- fakeClient .AddReactor ("*" , "*" , func (action core.Action ) (bool , runtime.Object , error ) {
266
- t .Error ("no-op update should not make any client invocation" )
267
- return true , nil , apierrors .NewInternalError (errors .New ("If we are here we have a problem" ))
278
+
279
+ t .Run ("RecreatePodWhenChangeVCTInStatefulSetGate enabled" , func (t * testing.T ) {
280
+ defer utilfeature .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RecreatePodWhenChangeVCTInStatefulSetGate , true )()
281
+ testFn (t )
268
282
})
269
- if err := control .UpdateStatefulPod (set , pod ); err != nil {
270
- t .Errorf ("Error returned on no-op update error: %s" , err )
271
- }
272
- events := collectEvents (recorder .Events )
273
- if eventCount := len (events ); eventCount != 0 {
274
- t .Errorf ("no-op update: got %d events, but want 0" , eventCount )
275
- }
283
+ t .Run ("RecreatePodWhenChangeVCTInStatefulSetGate disabled" , func (t * testing.T ) {
284
+ defer utilfeature .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RecreatePodWhenChangeVCTInStatefulSetGate , false )()
285
+ testFn (t )
286
+ })
287
+
276
288
}
277
289
278
290
func TestStatefulPodControlUpdatesIdentity (t * testing.T ) {
279
- recorder := record .NewFakeRecorder (10 )
280
- set := newStatefulSet (3 )
281
- pod := newStatefulSetPod (set , 0 )
282
- fakeClient := fake .NewSimpleClientset (pod )
283
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
284
- claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
285
- control := NewStatefulPodControl (fakeClient , nil , claimLister , nil , recorder )
286
- var updated * v1.Pod
287
- fakeClient .PrependReactor ("update" , "pods" , func (action core.Action ) (bool , runtime.Object , error ) {
288
- update := action .(core.UpdateAction )
289
- updated = update .GetObject ().(* v1.Pod )
290
- return true , update .GetObject (), nil
291
- })
292
- pod .Name = "goo-0"
293
- if err := control .UpdateStatefulPod (set , pod ); err != nil {
294
- t .Errorf ("Successful update returned an error: %s" , err )
295
- }
296
- events := collectEvents (recorder .Events )
297
- if eventCount := len (events ); eventCount != 1 {
298
- t .Errorf ("Pod update successful:got %d events,but want 1" , eventCount )
299
- } else if ! strings .Contains (events [0 ], v1 .EventTypeNormal ) {
300
- t .Errorf ("Found unexpected non-normal event %s" , events [0 ])
301
- }
302
- if ! identityMatches (set , updated ) {
303
- t .Error ("Name update failed identity does not match" )
291
+ testFn := func (t * testing.T , expectAnnotationUpdate bool ) {
292
+ recorder := record .NewFakeRecorder (10 )
293
+ set := newStatefulSet (3 )
294
+ pod := newStatefulSetPod (set , 0 )
295
+
296
+ vctAnnotationBefore := pod .Annotations [PodVolumeClaimTemplatesKey ]
297
+
298
+ fakeClient := fake .NewSimpleClientset (pod )
299
+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
300
+ claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
301
+ control := NewStatefulPodControl (fakeClient , nil , claimLister , nil , recorder )
302
+ var updated * v1.Pod
303
+ fakeClient .PrependReactor ("update" , "pods" , func (action core.Action ) (bool , runtime.Object , error ) {
304
+ update := action .(core.UpdateAction )
305
+ updated = update .GetObject ().(* v1.Pod )
306
+ return true , update .GetObject (), nil
307
+ })
308
+ pod .Name = "goo-0"
309
+ set .Spec .VolumeClaimTemplates = append (set .Spec .VolumeClaimTemplates , v1.PersistentVolumeClaim {
310
+ ObjectMeta : metav1.ObjectMeta {
311
+ Name : "test" ,
312
+ },
313
+ })
314
+
315
+ if err := control .UpdateStatefulPod (set , pod ); err != nil {
316
+ t .Errorf ("Successful update returned an error: %s" , err )
317
+ }
318
+ events := collectEvents (recorder .Events )
319
+ if eventCount := len (events ); eventCount != 3 {
320
+ t .Errorf ("Pod update successful:got %d events,but want 3" , eventCount )
321
+ t .Log (events )
322
+ } else if ! strings .Contains (events [0 ], v1 .EventTypeNormal ) {
323
+ t .Errorf ("Found unexpected non-normal event %s" , events [0 ])
324
+ }
325
+ if ! identityMatches (set , updated ) {
326
+ t .Error ("Name update failed identity does not match" )
327
+ }
328
+ if expectAnnotationUpdate != (vctAnnotationBefore != updated .Annotations [PodVolumeClaimTemplatesKey ]) {
329
+ t .Error ("Expected pod-vct-names to be updated" )
330
+ }
304
331
}
332
+
333
+ t .Run ("RecreatePodWhenChangeVCTInStatefulSetGate enabled" , func (t * testing.T ) {
334
+ defer utilfeature .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RecreatePodWhenChangeVCTInStatefulSetGate , true )()
335
+ testFn (t , true )
336
+ })
337
+ t .Run ("RecreatePodWhenChangeVCTInStatefulSetGate disabled" , func (t * testing.T ) {
338
+ defer utilfeature .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RecreatePodWhenChangeVCTInStatefulSetGate , false )()
339
+ testFn (t , false )
340
+ })
305
341
}
306
342
307
343
func TestStatefulPodControlUpdateIdentityFailure (t * testing.T ) {
0 commit comments