@@ -39,6 +39,7 @@ import (
39
39
"k8s.io/apimachinery/pkg/types"
40
40
utilerrors "k8s.io/apimachinery/pkg/util/errors"
41
41
"k8s.io/apimachinery/pkg/util/intstr"
42
+ utilversion "k8s.io/apimachinery/pkg/util/version"
42
43
utilfeature "k8s.io/apiserver/pkg/util/feature"
43
44
"k8s.io/client-go/informers"
44
45
appsinformers "k8s.io/client-go/informers/apps/v1"
@@ -165,7 +166,8 @@ func TestStatefulSetControl(t *testing.T) {
165
166
fn func (* testing.T , * apps.StatefulSet , invariantFunc )
166
167
obj func () * apps.StatefulSet
167
168
}{
168
- {CreatesPods , simpleSetFn },
169
+ {CreatesPodsWithPodIndexLabelFeature , simpleSetFn },
170
+ {CreatesPodsWithoutPodIndexLabelFeature , simpleSetFn },
169
171
{ScalesUp , simpleSetFn },
170
172
{ScalesDown , simpleSetFn },
171
173
{ReplacesPods , largeSetFn },
@@ -208,7 +210,20 @@ func TestStatefulSetControl(t *testing.T) {
208
210
}
209
211
}
210
212
211
- func CreatesPods (t * testing.T , set * apps.StatefulSet , invariants invariantFunc ) {
213
+ func CreatesPodsWithPodIndexLabelFeature (t * testing.T , set * apps.StatefulSet , invariants invariantFunc ) {
214
+ createPods (t , set , invariants , true )
215
+ }
216
+
217
+ func CreatesPodsWithoutPodIndexLabelFeature (t * testing.T , set * apps.StatefulSet , invariants invariantFunc ) {
218
+ createPods (t , set , invariants , false )
219
+ }
220
+
221
+ func createPods (t * testing.T , set * apps.StatefulSet , invariants invariantFunc , isPodIndexLabelEnabled bool ) {
222
+ if ! isPodIndexLabelEnabled {
223
+ // TODO: this will be removed in 1.35
224
+ featuregatetesting .SetFeatureGateEmulationVersionDuringTest (t , utilfeature .DefaultFeatureGate , utilversion .MustParse ("1.31" ))
225
+ }
226
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .PodIndexLabel , isPodIndexLabelEnabled )
212
227
client := fake .NewSimpleClientset (set )
213
228
om , _ , ssc := setupController (client )
214
229
@@ -229,20 +244,21 @@ func CreatesPods(t *testing.T, set *apps.StatefulSet, invariants invariantFunc)
229
244
if set .Status .UpdatedReplicas != 3 {
230
245
t .Error ("Failed to set UpdatedReplicas correctly" )
231
246
}
247
+
232
248
// Check all pods have correct pod index label.
233
- if utilfeature . DefaultFeatureGate . Enabled ( features . PodIndexLabel ) {
234
- selector , err := metav1 . LabelSelectorAsSelector ( set . Spec . Selector )
235
- if err != nil {
236
- t . Error ( err )
237
- }
238
- pods , err := om . podsLister . Pods ( set . Namespace ). List ( selector )
239
- if err != nil {
240
- t . Error ( err )
241
- }
242
- if len ( pods ) != 3 {
243
- t . Errorf ( "Expected 3 pods, got %d" , len ( pods ))
244
- }
245
- for _ , pod := range pods {
249
+ selector , err := metav1 . LabelSelectorAsSelector ( set . Spec . Selector )
250
+ if err != nil {
251
+ t . Error ( err )
252
+ }
253
+ pods , err := om . podsLister . Pods ( set . Namespace ). List ( selector )
254
+ if err != nil {
255
+ t . Error ( err )
256
+ }
257
+ if len ( pods ) != 3 {
258
+ t . Errorf ( "Expected 3 pods, got %d" , len ( pods ))
259
+ }
260
+ for _ , pod := range pods {
261
+ if isPodIndexLabelEnabled {
246
262
podIndexFromLabel , exists := pod .Labels [apps .PodIndexLabel ]
247
263
if ! exists {
248
264
t .Errorf ("Missing pod index label: %s" , apps .PodIndexLabel )
@@ -252,6 +268,12 @@ func CreatesPods(t *testing.T, set *apps.StatefulSet, invariants invariantFunc)
252
268
if podIndexFromLabel != podIndexFromName {
253
269
t .Errorf ("Pod index label value (%s) does not match pod index in pod name (%s)" , podIndexFromLabel , podIndexFromName )
254
270
}
271
+ } else {
272
+ _ , exists := pod .Labels [apps .PodIndexLabel ]
273
+ if exists {
274
+ t .Errorf ("Pod index label should not exist when feature gate is disabled: %s" , apps .PodIndexLabel )
275
+ continue
276
+ }
255
277
}
256
278
}
257
279
}
0 commit comments