@@ -13,8 +13,10 @@ import (
13
13
batchv1 "k8s.io/api/batch/v1"
14
14
corev1 "k8s.io/api/core/v1"
15
15
rbacv1 "k8s.io/api/rbac/v1"
16
+ storagev1 "k8s.io/api/storage/v1"
16
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18
"k8s.io/apimachinery/pkg/runtime"
19
+ "k8s.io/utils/pointer"
18
20
ctrl "sigs.k8s.io/controller-runtime"
19
21
"sigs.k8s.io/controller-runtime/pkg/client"
20
22
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -32,7 +34,20 @@ func TestInplaceController(t *testing.T) {
32
34
33
35
t .Run ("InplaceE2E" , func (t * testing.T ) {
34
36
35
- t .Run ("Don't scale down correct StatfulSets" , func (t * testing.T ) {
37
+ sc := & storagev1.StorageClass {
38
+ ObjectMeta : metav1.ObjectMeta {
39
+ Name : "mysc" ,
40
+ Annotations : map [string ]string {
41
+ "storageclass.kubernetes.io/is-default-class" : "true" ,
42
+ },
43
+ },
44
+ Provisioner : "mysc" ,
45
+ AllowVolumeExpansion : pointer .Bool (true ),
46
+ }
47
+
48
+ require .NoError (t , c .Create (ctx , sc ))
49
+
50
+ t .Run ("Don't touch correct PVCs" , func (t * testing.T ) {
36
51
t .Parallel ()
37
52
ctx := context .Background ()
38
53
ns := "e2e1"
@@ -42,21 +57,66 @@ func TestInplaceController(t *testing.T) {
42
57
Name : ns ,
43
58
},
44
59
}))
60
+ pvcSize := "2G"
61
+ sts := newTestStatefulSet (ns , "test" , 1 , pvcSize )
62
+ sts .Labels = map [string ]string {
63
+ testLabelName : "true" ,
64
+ }
65
+
66
+ pvc := applyResizablePVC (ctx , "data-test-0" , ns , pvcSize , sts , c , require )
67
+
68
+ require .NoError (c .Create (ctx , sts ))
69
+
70
+ consistently (t , func () bool {
71
+ return pvcEqualSize (ctx , c , pvc , pvcSize )
72
+ }, duration , interval , "PVCs equal size" )
73
+
74
+ })
75
+ t .Run ("Ignore STS without the label" , func (t * testing.T ) {
76
+ t .Parallel ()
77
+ ctx := context .Background ()
78
+ ns := "e2e2"
79
+ require := require .New (t )
80
+ require .NoError (c .Create (ctx , & corev1.Namespace {
81
+ ObjectMeta : metav1.ObjectMeta {
82
+ Name : ns ,
83
+ },
84
+ }))
45
85
sts := newTestStatefulSet (ns , "test" , 1 , "2G" )
46
- // sts.Labels[testLabelName] = "true"
47
- require .NoError (c .Create (ctx , newSource (ns , "data-test-0" , "2G" ,
48
- func (pvc * corev1.PersistentVolumeClaim ) * corev1.PersistentVolumeClaim {
49
- pvc .Labels = sts .Spec .Selector .MatchLabels
50
- return pvc
51
- })))
86
+
87
+ pvc := applyResizablePVC (ctx , "data-test-0" , ns , "1G" , sts , c , require )
88
+
52
89
require .NoError (c .Create (ctx , sts ))
53
90
54
91
consistently (t , func () bool {
55
- return stsExists (ctx , c , sts )
56
- }, duration , interval , "Sts exists" )
92
+ return pvcEqualSize (ctx , c , pvc , "1G" )
93
+ }, duration , interval , "PVCs equal size" )
94
+ })
95
+ t .Run ("Change PVCs if they not match" , func (t * testing.T ) {
96
+ t .Parallel ()
97
+ ctx := context .Background ()
98
+ ns := "e2e3"
99
+ require := require .New (t )
100
+ require .NoError (c .Create (ctx , & corev1.Namespace {
101
+ ObjectMeta : metav1.ObjectMeta {
102
+ Name : ns ,
103
+ },
104
+ }))
105
+ sts := newTestStatefulSet (ns , "test" , 1 , "2G" )
106
+ sts .Labels = map [string ]string {
107
+ testLabelName : "true" ,
108
+ }
57
109
110
+ pvc := applyResizablePVC (ctx , "data-test-0" , ns , "1G" , sts , c , require )
111
+
112
+ require .NoError (c .Create (ctx , sts ))
113
+
114
+ consistently (t , func () bool {
115
+ return pvcEqualSize (ctx , c , pvc , "2G" )
116
+ }, duration , interval , "PVCs equal size" )
58
117
})
59
118
})
119
+
60
120
}
61
121
62
122
// startInplaceTestReconciler sets up a separate test env and starts the controller
@@ -72,6 +132,7 @@ func startInplaceTestReconciler(t *testing.T, ctx context.Context, crname string
72
132
req .NoError (corev1 .AddToScheme (s ))
73
133
req .NoError (batchv1 .AddToScheme (s ))
74
134
req .NoError (rbacv1 .AddToScheme (s ))
135
+ req .NoError (storagev1 .AddToScheme (s ))
75
136
76
137
mgr , err := ctrl .NewManager (conf , ctrl.Options {
77
138
Scheme : s ,
@@ -90,3 +151,19 @@ func startInplaceTestReconciler(t *testing.T, ctx context.Context, crname string
90
151
91
152
return mgr .GetClient (), testEnv .Stop
92
153
}
154
+
155
+ func applyResizablePVC (ctx context.Context , name , ns , size string , sts * appsv1.StatefulSet , c client.Client , require * require.Assertions ) * corev1.PersistentVolumeClaim {
156
+ pvc := newSource (ns , name , size ,
157
+ func (pvc * corev1.PersistentVolumeClaim ) * corev1.PersistentVolumeClaim {
158
+ pvc .Labels = sts .Spec .Selector .MatchLabels
159
+ return pvc
160
+ })
161
+
162
+ pvc .Spec .StorageClassName = pointer .String ("mysc" )
163
+ require .NoError (c .Create (ctx , pvc ))
164
+
165
+ // we need to set the PVC to bound in order for the resize to work
166
+ pvc .Status .Phase = corev1 .ClaimBound
167
+ require .NoError (c .Status ().Update (ctx , pvc ))
168
+ return pvc
169
+ }
0 commit comments