@@ -17,13 +17,16 @@ limitations under the License.
17
17
package statefulset
18
18
19
19
import (
20
+ "bytes"
21
+ "encoding/json"
20
22
"sort"
21
23
"testing"
22
24
23
25
apps "k8s.io/api/apps/v1"
24
26
"k8s.io/api/core/v1"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
28
"k8s.io/apimachinery/pkg/runtime"
29
+ "k8s.io/apimachinery/pkg/types"
27
30
"k8s.io/apimachinery/pkg/util/sets"
28
31
"k8s.io/client-go/informers"
29
32
"k8s.io/client-go/kubernetes/fake"
@@ -33,6 +36,8 @@ import (
33
36
"k8s.io/kubernetes/pkg/controller/history"
34
37
)
35
38
39
+ var parentKind = apps .SchemeGroupVersion .WithKind ("StatefulSet" )
40
+
36
41
func alwaysReady () bool { return true }
37
42
38
43
func TestStatefulSetControllerCreates (t * testing.T ) {
@@ -534,6 +539,48 @@ func TestGetPodsForStatefulSetAdopt(t *testing.T) {
534
539
}
535
540
}
536
541
542
+ func TestAdoptOrphanRevisions (t * testing.T ) {
543
+ ss1 := newStatefulSetWithLabels (3 , "ss1" , types .UID ("ss1" ), map [string ]string {"foo" : "bar" })
544
+ ss1 .Status .CollisionCount = new (int32 )
545
+ ss1Rev1 , err := history .NewControllerRevision (ss1 , parentKind , ss1 .Spec .Template .Labels , rawTemplate (& ss1 .Spec .Template ), 1 , ss1 .Status .CollisionCount )
546
+ if err != nil {
547
+ t .Fatal (err )
548
+ }
549
+ ss1Rev1 .Namespace = ss1 .Namespace
550
+ ss1 .Spec .Template .Annotations = make (map [string ]string )
551
+ ss1 .Spec .Template .Annotations ["ss1" ] = "ss1"
552
+ ss1Rev2 , err := history .NewControllerRevision (ss1 , parentKind , ss1 .Spec .Template .Labels , rawTemplate (& ss1 .Spec .Template ), 2 , ss1 .Status .CollisionCount )
553
+ if err != nil {
554
+ t .Fatal (err )
555
+ }
556
+ ss1Rev2 .Namespace = ss1 .Namespace
557
+ ss1Rev2 .OwnerReferences = []metav1.OwnerReference {}
558
+
559
+ ssc , spc := newFakeStatefulSetController (ss1 , ss1Rev1 , ss1Rev2 )
560
+
561
+ spc .revisionsIndexer .Add (ss1Rev1 )
562
+ spc .revisionsIndexer .Add (ss1Rev2 )
563
+
564
+ err = ssc .adoptOrphanRevisions (ss1 )
565
+ if err != nil {
566
+ t .Errorf ("adoptOrphanRevisions() error: %v" , err )
567
+ }
568
+
569
+ if revisions , err := ssc .control .ListRevisions (ss1 ); err != nil {
570
+ t .Errorf ("ListRevisions() error: %v" , err )
571
+ } else {
572
+ var adopted bool
573
+ for i := range revisions {
574
+ if revisions [i ].Name == ss1Rev2 .Name && metav1 .GetControllerOf (revisions [i ]) != nil {
575
+ adopted = true
576
+ }
577
+ }
578
+ if ! adopted {
579
+ t .Error ("adoptOrphanRevisions() not adopt orphan revisions" )
580
+ }
581
+ }
582
+ }
583
+
537
584
func TestGetPodsForStatefulSetRelease (t * testing.T ) {
538
585
set := newStatefulSet (3 )
539
586
ssc , spc := newFakeStatefulSetController (set )
@@ -575,7 +622,7 @@ func TestGetPodsForStatefulSetRelease(t *testing.T) {
575
622
func newFakeStatefulSetController (initialObjects ... runtime.Object ) (* StatefulSetController , * fakeStatefulPodControl ) {
576
623
client := fake .NewSimpleClientset (initialObjects ... )
577
624
informerFactory := informers .NewSharedInformerFactory (client , controller .NoResyncPeriodFunc ())
578
- fpc := newFakeStatefulPodControl (informerFactory .Core ().V1 ().Pods (), informerFactory .Apps ().V1 ().StatefulSets ())
625
+ fpc := newFakeStatefulPodControl (informerFactory .Core ().V1 ().Pods (), informerFactory .Apps ().V1 ().StatefulSets (), informerFactory . Apps (). V1 (). ControllerRevisions () )
579
626
ssu := newFakeStatefulSetStatusUpdater (informerFactory .Apps ().V1 ().StatefulSets ())
580
627
ssc := NewStatefulSetController (
581
628
informerFactory .Core ().V1 ().Pods (),
@@ -710,3 +757,12 @@ func scaleDownStatefulSetController(set *apps.StatefulSet, ssc *StatefulSetContr
710
757
}
711
758
return assertMonotonicInvariants (set , spc )
712
759
}
760
+
761
+ func rawTemplate (template * v1.PodTemplateSpec ) runtime.RawExtension {
762
+ buf := new (bytes.Buffer )
763
+ enc := json .NewEncoder (buf )
764
+ if err := enc .Encode (template ); err != nil {
765
+ panic (err )
766
+ }
767
+ return runtime.RawExtension {Raw : buf .Bytes ()}
768
+ }
0 commit comments