@@ -41,6 +41,7 @@ import (
41
41
_ "k8s.io/kubernetes/pkg/apis/apps/install"
42
42
_ "k8s.io/kubernetes/pkg/apis/core/install"
43
43
"k8s.io/kubernetes/pkg/features"
44
+ "k8s.io/utils/ptr"
44
45
)
45
46
46
47
func TestStatefulPodControlCreatesPods (t * testing.T ) {
@@ -502,7 +503,7 @@ func TestStatefulPodControlDeleteFailure(t *testing.T) {
502
503
}
503
504
504
505
func TestStatefulPodControlClaimsMatchDeletionPolcy (t * testing.T ) {
505
- // The claimOwnerMatchesSetAndPod is tested exhaustively in stateful_set_utils_test; this
506
+ // The isClaimOwnerUpToDate is tested exhaustively in stateful_set_utils_test; this
506
507
// test is for the wiring to the method tested there.
507
508
_ , ctx := ktesting .NewTestContext (t )
508
509
fakeClient := & fake.Clientset {}
@@ -542,38 +543,64 @@ func TestStatefulPodControlUpdatePodClaimForRetentionPolicy(t *testing.T) {
542
543
testFn := func (t * testing.T ) {
543
544
_ , ctx := ktesting .NewTestContext (t )
544
545
featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .StatefulSetAutoDeletePVC , true )
545
- fakeClient := & fake.Clientset {}
546
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
547
- claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
548
- fakeClient .AddReactor ("update" , "persistentvolumeclaims" , func (action core.Action ) (bool , runtime.Object , error ) {
549
- update := action .(core.UpdateAction )
550
- indexer .Update (update .GetObject ())
551
- return true , update .GetObject (), nil
552
- })
553
- set := newStatefulSet (3 )
554
- set .GetObjectMeta ().SetUID ("set-123" )
555
- pod := newStatefulSetPod (set , 0 )
556
- claims := getPersistentVolumeClaims (set , pod )
557
- for k := range claims {
558
- claim := claims [k ]
559
- indexer .Add (& claim )
560
- }
561
- control := NewStatefulPodControl (fakeClient , nil , claimLister , & noopRecorder {})
562
- set .Spec .PersistentVolumeClaimRetentionPolicy = & apps.StatefulSetPersistentVolumeClaimRetentionPolicy {
563
- WhenDeleted : apps .DeletePersistentVolumeClaimRetentionPolicyType ,
564
- WhenScaled : apps .RetainPersistentVolumeClaimRetentionPolicyType ,
565
- }
566
- if err := control .UpdatePodClaimForRetentionPolicy (ctx , set , pod ); err != nil {
567
- t .Errorf ("Unexpected error for UpdatePodClaimForRetentionPolicy (retain): %v" , err )
546
+
547
+ testCases := []struct {
548
+ name string
549
+ ownerRef []metav1.OwnerReference
550
+ expectRef bool
551
+ }{
552
+ {
553
+ name : "bare PVC" ,
554
+ expectRef : true ,
555
+ },
556
+ {
557
+ name : "PVC already controller" ,
558
+ ownerRef : []metav1.OwnerReference {{Controller : ptr .To (true ), Name : "foobar" }},
559
+ expectRef : false ,
560
+ },
568
561
}
569
- expectRef := utilfeature .DefaultFeatureGate .Enabled (features .StatefulSetAutoDeletePVC )
570
- for k := range claims {
571
- claim , err := claimLister .PersistentVolumeClaims (claims [k ].Namespace ).Get (claims [k ].Name )
572
- if err != nil {
573
- t .Errorf ("Unexpected error getting Claim %s/%s: %v" , claim .Namespace , claim .Name , err )
562
+
563
+ for _ , tc := range testCases {
564
+ fakeClient := & fake.Clientset {}
565
+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
566
+ claimLister := corelisters .NewPersistentVolumeClaimLister (indexer )
567
+ fakeClient .AddReactor ("update" , "persistentvolumeclaims" , func (action core.Action ) (bool , runtime.Object , error ) {
568
+ update := action .(core.UpdateAction )
569
+ if err := indexer .Update (update .GetObject ()); err != nil {
570
+ t .Fatalf ("could not update index: %v" , err )
571
+ }
572
+ return true , update .GetObject (), nil
573
+ })
574
+ set := newStatefulSet (3 )
575
+ set .GetObjectMeta ().SetUID ("set-123" )
576
+ pod0 := newStatefulSetPod (set , 0 )
577
+ claims0 := getPersistentVolumeClaims (set , pod0 )
578
+ for k := range claims0 {
579
+ claim := claims0 [k ]
580
+ if tc .ownerRef != nil {
581
+ claim .SetOwnerReferences (tc .ownerRef )
582
+ }
583
+ if err := indexer .Add (& claim ); err != nil {
584
+ t .Errorf ("Could not add claim %s: %v" , k , err )
585
+ }
586
+ }
587
+ control := NewStatefulPodControl (fakeClient , nil , claimLister , & noopRecorder {})
588
+ set .Spec .PersistentVolumeClaimRetentionPolicy = & apps.StatefulSetPersistentVolumeClaimRetentionPolicy {
589
+ WhenDeleted : apps .DeletePersistentVolumeClaimRetentionPolicyType ,
590
+ WhenScaled : apps .RetainPersistentVolumeClaimRetentionPolicyType ,
591
+ }
592
+ if err := control .UpdatePodClaimForRetentionPolicy (ctx , set , pod0 ); err != nil {
593
+ t .Errorf ("Unexpected error for UpdatePodClaimForRetentionPolicy (retain), pod0: %v" , err )
574
594
}
575
- if hasOwnerRef (claim , set ) != expectRef {
576
- t .Errorf ("Claim %s/%s bad set owner ref" , claim .Namespace , claim .Name )
595
+ expectRef := tc .expectRef && utilfeature .DefaultFeatureGate .Enabled (features .StatefulSetAutoDeletePVC )
596
+ for k := range claims0 {
597
+ claim , err := claimLister .PersistentVolumeClaims (claims0 [k ].Namespace ).Get (claims0 [k ].Name )
598
+ if err != nil {
599
+ t .Errorf ("Unexpected error getting Claim %s/%s: %v" , claim .Namespace , claim .Name , err )
600
+ }
601
+ if hasOwnerRef (claim , set ) != expectRef {
602
+ t .Errorf ("%s: Claim %s/%s bad set owner ref" , tc .name , claim .Namespace , claim .Name )
603
+ }
577
604
}
578
605
}
579
606
}
@@ -663,12 +690,22 @@ func TestPodClaimIsStale(t *testing.T) {
663
690
claimIndexer .Add (& claim )
664
691
case stale :
665
692
claim .SetOwnerReferences ([]metav1.OwnerReference {
666
- {Name : "set-3" , UID : types .UID ("stale" )},
693
+ {
694
+ Name : "set-3" ,
695
+ UID : types .UID ("stale" ),
696
+ APIVersion : "v1" ,
697
+ Kind : "Pod" ,
698
+ },
667
699
})
668
700
claimIndexer .Add (& claim )
669
701
case withRef :
670
702
claim .SetOwnerReferences ([]metav1.OwnerReference {
671
- {Name : "set-3" , UID : types .UID ("123" )},
703
+ {
704
+ Name : "set-3" ,
705
+ UID : types .UID ("123" ),
706
+ APIVersion : "v1" ,
707
+ Kind : "Pod" ,
708
+ },
672
709
})
673
710
claimIndexer .Add (& claim )
674
711
}
@@ -710,7 +747,8 @@ func TestStatefulPodControlRetainDeletionPolicyUpdate(t *testing.T) {
710
747
}
711
748
for k := range claims {
712
749
claim := claims [k ]
713
- setOwnerRef (& claim , set , & set .TypeMeta ) // This ownerRef should be removed in the update.
750
+ // This ownerRef should be removed in the update.
751
+ claim .SetOwnerReferences (addControllerRef (claim .GetOwnerReferences (), set , controllerKind ))
714
752
claimIndexer .Add (& claim )
715
753
}
716
754
control := NewStatefulPodControl (fakeClient , podLister , claimLister , recorder )
0 commit comments