@@ -43,22 +43,20 @@ import (
43
43
"k8s.io/apimachinery/pkg/api/errors"
44
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
45
45
utilerrors "k8s.io/apimachinery/pkg/util/errors"
46
- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
47
- "k8s.io/apimachinery/pkg/util/version"
48
46
"k8s.io/apimachinery/pkg/util/wait"
49
47
serveroptions "k8s.io/apiserver/pkg/server/options"
50
48
"k8s.io/apiserver/pkg/storage/storagebackend"
51
49
"k8s.io/apiserver/pkg/storageversion"
50
+ "k8s.io/apiserver/pkg/util/compatibility"
52
51
utilfeature "k8s.io/apiserver/pkg/util/feature"
53
52
"k8s.io/client-go/kubernetes"
54
53
restclient "k8s.io/client-go/rest"
55
54
clientgotransport "k8s.io/client-go/transport"
56
55
"k8s.io/client-go/util/cert"
57
56
"k8s.io/client-go/util/keyutil"
58
- "k8s.io/component-base/featuregate "
57
+ basecompatibility "k8s.io/component-base/compatibility "
59
58
featuregatetesting "k8s.io/component-base/featuregate/testing"
60
59
logsapi "k8s.io/component-base/logs/api/v1"
61
- utilversion "k8s.io/component-base/version"
62
60
"k8s.io/klog/v2"
63
61
"k8s.io/kube-aggregator/pkg/apiserver"
64
62
"k8s.io/kubernetes/pkg/features"
@@ -104,11 +102,8 @@ type TestServerInstanceOptions struct {
104
102
// an apiserver version skew scenario where all apiservers use the same proxyCA to verify client connections.
105
103
ProxyCA * ProxyCA
106
104
// Set the BinaryVersion of server effective version.
107
- // If empty, effective version will default to version.DefaultKubeBinaryVersion .
105
+ // If empty, effective version will default to DefaultKubeEffectiveVersion .
108
106
BinaryVersion string
109
- // Set the EmulationVersion of server effective version.
110
- // If empty, emulation version will default to the effective version.
111
- EmulationVersion string
112
107
// Set non-default request timeout in the server.
113
108
RequestTimeout time.Duration
114
109
}
@@ -194,21 +189,20 @@ func StartTestServer(t ktesting.TB, instanceOptions *TestServerInstanceOptions,
194
189
195
190
fs := pflag .NewFlagSet ("test" , pflag .PanicOnError )
196
191
197
- featureGate := utilfeature .DefaultMutableFeatureGate
198
- featureGate .AddMetrics ()
199
- effectiveVersion := utilversion .DefaultKubeEffectiveVersion ()
192
+ featureGate := utilfeature .DefaultMutableFeatureGate .DeepCopy ()
193
+ effectiveVersion := compatibility .DefaultKubeEffectiveVersionForTest ()
200
194
if instanceOptions .BinaryVersion != "" {
201
- effectiveVersion = utilversion . NewEffectiveVersion (instanceOptions .BinaryVersion )
195
+ effectiveVersion = basecompatibility . NewEffectiveVersionFromString (instanceOptions .BinaryVersion , "" , "" )
202
196
}
203
- if instanceOptions .EmulationVersion != "" {
204
- effectiveVersion .SetEmulationVersion (version .MustParse (instanceOptions .EmulationVersion ))
197
+ effectiveVersion .SetEmulationVersion (featureGate .EmulationVersion ())
198
+ componentGlobalsRegistry := basecompatibility .NewComponentGlobalsRegistry ()
199
+ if err := componentGlobalsRegistry .Register (basecompatibility .DefaultKubeComponent , effectiveVersion , featureGate ); err != nil {
200
+ return result , err
205
201
}
206
- // need to call SetFeatureGateEmulationVersionDuringTest to reset the feature gate emulation version at the end of the test.
207
- featuregatetesting .SetFeatureGateEmulationVersionDuringTest (t , featureGate , effectiveVersion .EmulationVersion ())
208
- featuregate .DefaultComponentGlobalsRegistry .Reset ()
209
- utilruntime .Must (featuregate .DefaultComponentGlobalsRegistry .Register (featuregate .DefaultKubeComponent , effectiveVersion , featureGate ))
210
202
211
203
s := options .NewServerRunOptions ()
204
+ // set up new instance of ComponentGlobalsRegistry instead of using the DefaultComponentGlobalsRegistry to avoid contention in parallel tests.
205
+ s .Options .GenericServerRunOptions .ComponentGlobalsRegistry = componentGlobalsRegistry
212
206
if instanceOptions .RequestTimeout > 0 {
213
207
s .GenericServerRunOptions .RequestTimeout = instanceOptions .RequestTimeout
214
208
}
@@ -330,15 +324,6 @@ func StartTestServer(t ktesting.TB, instanceOptions *TestServerInstanceOptions,
330
324
return result , err
331
325
}
332
326
s .Authentication .ClientCert .ClientCA = clientCACertFile
333
- if utilfeature .DefaultFeatureGate .Enabled (features .UnknownVersionInteroperabilityProxy ) {
334
- // TODO: set up a general clean up for testserver
335
- if clientgotransport .DialerStopCh == wait .NeverStop {
336
- ctx , cancel := context .WithTimeout (context .Background (), time .Hour )
337
- t .Cleanup (cancel )
338
- clientgotransport .DialerStopCh = ctx .Done ()
339
- }
340
- s .PeerCAFile = filepath .Join (s .SecureServing .ServerCert .CertDirectory , s .SecureServing .ServerCert .PairName + ".crt" )
341
- }
342
327
}
343
328
344
329
s .SecureServing .ExternalAddress = s .SecureServing .Listener .Addr ().(* net.TCPAddr ).IP // use listener addr although it is a loopback device
@@ -374,8 +359,32 @@ func StartTestServer(t ktesting.TB, instanceOptions *TestServerInstanceOptions,
374
359
s .Authentication .RequestHeader .ExtraHeaderPrefixes = extraHeaders
375
360
}
376
361
377
- if err := featuregate .DefaultComponentGlobalsRegistry .Set (); err != nil {
378
- return result , err
362
+ if err := componentGlobalsRegistry .Set (); err != nil {
363
+ return result , fmt .Errorf ("%w\n If you are using SetFeatureGate*DuringTest, try using --emulated-version and --feature-gates flags instead" , err )
364
+ }
365
+ // If the local ComponentGlobalsRegistry is changed by the flags,
366
+ // we need to copy the new feature values back to the DefaultFeatureGate because most feature checks still use the DefaultFeatureGate.
367
+ // We cannot directly use DefaultFeatureGate in ComponentGlobalsRegistry because the changes done by ComponentGlobalsRegistry.Set() will not be undone at the end of the test.
368
+ if ! featureGate .EmulationVersion ().EqualTo (utilfeature .DefaultMutableFeatureGate .EmulationVersion ()) {
369
+ featuregatetesting .SetFeatureGateEmulationVersionDuringTest (t , utilfeature .DefaultMutableFeatureGate , effectiveVersion .EmulationVersion ())
370
+ }
371
+ for f := range utilfeature .DefaultMutableFeatureGate .GetAll () {
372
+ if featureGate .Enabled (f ) != utilfeature .DefaultFeatureGate .Enabled (f ) {
373
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , f , featureGate .Enabled (f ))
374
+ }
375
+ }
376
+ utilfeature .DefaultMutableFeatureGate .AddMetrics ()
377
+
378
+ if instanceOptions .EnableCertAuth {
379
+ if featureGate .Enabled (features .UnknownVersionInteroperabilityProxy ) {
380
+ // TODO: set up a general clean up for testserver
381
+ if clientgotransport .DialerStopCh == wait .NeverStop {
382
+ ctx , cancel := context .WithTimeout (context .Background (), time .Hour )
383
+ t .Cleanup (cancel )
384
+ clientgotransport .DialerStopCh = ctx .Done ()
385
+ }
386
+ s .PeerCAFile = filepath .Join (s .SecureServing .ServerCert .CertDirectory , s .SecureServing .ServerCert .PairName + ".crt" )
387
+ }
379
388
}
380
389
381
390
saSigningKeyFile , err := os .CreateTemp ("/tmp" , "insecure_test_key" )
0 commit comments