| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package storage |
| 18 |
|
| 19 |
import ( |
| 20 |
"mime" |
| 21 |
"reflect" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
|
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
"k8s.io/apimachinery/pkg/runtime" |
| 27 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 28 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 29 |
apimachineryversion "k8s.io/apimachinery/pkg/util/version" |
| 30 |
"k8s.io/apiserver/pkg/apis/example" |
| 31 |
exampleinstall "k8s.io/apiserver/pkg/apis/example/install" |
| 32 |
examplev1 "k8s.io/apiserver/pkg/apis/example/v1" |
| 33 |
"k8s.io/apiserver/pkg/apis/example2" |
| 34 |
example2install "k8s.io/apiserver/pkg/apis/example2/install" |
| 35 |
"k8s.io/apiserver/pkg/storage/storagebackend" |
| 36 |
basecompatibility "k8s.io/component-base/compatibility" |
| 37 |
) |
| 38 |
|
| 39 |
var ( |
| 40 |
v1GroupVersion = schema.GroupVersion{Group: "", Version: "v1"} |
| 41 |
|
| 42 |
scheme = runtime.NewScheme() |
| 43 |
codecs = serializer.NewCodecFactory(scheme) |
| 44 |
) |
| 45 |
|
| 46 |
func init() { |
| 47 |
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion) |
| 48 |
scheme.AddUnversionedTypes(v1GroupVersion, |
| 49 |
&metav1.Status{}, |
| 50 |
&metav1.APIVersions{}, |
| 51 |
&metav1.APIGroupList{}, |
| 52 |
&metav1.APIGroup{}, |
| 53 |
&metav1.APIResourceList{}, |
| 54 |
) |
| 55 |
|
| 56 |
exampleinstall.Install(scheme) |
| 57 |
example2install.Install(scheme) |
| 58 |
} |
| 59 |
|
| 60 |
type fakeNegotiater struct { |
| 61 |
serializer, streamSerializer runtime.Serializer |
| 62 |
framer runtime.Framer |
| 63 |
types, streamTypes []string |
| 64 |
} |
| 65 |
|
| 66 |
func (n *fakeNegotiater) SupportedMediaTypes() []runtime.SerializerInfo { |
| 67 |
var out []runtime.SerializerInfo |
| 68 |
for _, s := range n.types { |
| 69 |
mediaType, _, err := mime.ParseMediaType(s) |
| 70 |
if err != nil { |
| 71 |
panic(err) |
| 72 |
} |
| 73 |
parts := strings.SplitN(mediaType, "/", 2) |
| 74 |
if len(parts) == 1 { |
| 75 |
// this is an error on the server side |
| 76 |
parts = append(parts, "") |
| 77 |
} |
| 78 |
|
| 79 |
info := runtime.SerializerInfo{ |
| 80 |
Serializer: n.serializer, |
| 81 |
MediaType: s, |
| 82 |
MediaTypeType: parts[0], |
| 83 |
MediaTypeSubType: parts[1], |
| 84 |
EncodesAsText: true, |
| 85 |
} |
| 86 |
|
| 87 |
for _, t := range n.streamTypes { |
| 88 |
if t == s { |
| 89 |
info.StreamSerializer = &runtime.StreamSerializerInfo{ |
| 90 |
EncodesAsText: true, |
| 91 |
Framer: n.framer, |
| 92 |
Serializer: n.streamSerializer, |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
out = append(out, info) |
| 97 |
} |
| 98 |
return out |
| 99 |
} |
| 100 |
|
| 101 |
func (n *fakeNegotiater) UniversalDeserializer() runtime.Decoder { |
| 102 |
return n.serializer |
| 103 |
} |
| 104 |
|
| 105 |
func (n *fakeNegotiater) EncoderForVersion(serializer runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { |
| 106 |
return n.serializer |
| 107 |
} |
| 108 |
|
| 109 |
func (n *fakeNegotiater) DecoderToVersion(serializer runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder { |
| 110 |
return n.serializer |
| 111 |
} |
| 112 |
|
| 113 |
type newStorageCodecFn func(opts StorageCodecConfig) (codec runtime.Codec, encodeVersioner runtime.GroupVersioner, err error) |
| 114 |
type newStorageCodecRecorder struct { |
| 115 |
opt StorageCodecConfig |
| 116 |
} |
| 117 |
|
| 118 |
func (c *newStorageCodecRecorder) Decorators(f newStorageCodecFn) newStorageCodecFn { |
| 119 |
return func(opts StorageCodecConfig) (codec runtime.Codec, encodeVersioner runtime.GroupVersioner, err error) { |
| 120 |
c.opt = opts |
| 121 |
return f(opts) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestConfigurableStorageFactory(t *testing.T) { |
| 126 |
var emptyResource schema.GroupResource |
| 127 |
|
| 128 |
testCases := []struct { |
| 129 |
name string |
| 130 |
enabledResources []schema.GroupVersionResource |
| 131 |
overrideResource schema.GroupResource |
| 132 |
cohabitatingResources []schema.GroupResource |
| 133 |
resourceForConfig schema.GroupResource |
| 134 |
exampleForConfig runtime.Object |
| 135 |
wantStorageVersion schema.GroupVersion |
| 136 |
}{ |
| 137 |
{ |
| 138 |
name: "config resource is primary cohabitating resources", |
| 139 |
enabledResources: []schema.GroupVersionResource{ |
| 140 |
example.Resource("replicasets").WithVersion("v1"), |
| 141 |
example2.Resource("replicasets").WithVersion("v1"), |
| 142 |
}, |
| 143 |
cohabitatingResources: []schema.GroupResource{example.Resource("replicasets"), example2.Resource("replicasets")}, |
| 144 |
resourceForConfig: example.Resource("replicasets"), |
| 145 |
exampleForConfig: &example.ReplicaSet{}, |
| 146 |
wantStorageVersion: schema.GroupVersion{Group: example.GroupName, Version: "v1"}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "config resource is secondary cohabitating resources", |
| 150 |
enabledResources: []schema.GroupVersionResource{ |
| 151 |
example.Resource("replicasets").WithVersion("v1"), |
| 152 |
example2.Resource("replicasets").WithVersion("v1"), |
| 153 |
}, |
| 154 |
cohabitatingResources: []schema.GroupResource{example.Resource("replicasets"), example2.Resource("replicasets")}, |
| 155 |
resourceForConfig: example2.Resource("replicasets"), |
| 156 |
exampleForConfig: &example.ReplicaSet{}, |
| 157 |
wantStorageVersion: schema.GroupVersion{Group: example.GroupName, Version: "v1"}, |
| 158 |
}, |
| 159 |
{ |
| 160 |
name: "config resource is primary cohabitating resources and not enabled", |
| 161 |
enabledResources: []schema.GroupVersionResource{ |
| 162 |
// example.Resource("replicasets").WithVersion("v1"), // <- disabled |
| 163 |
example2.Resource("replicasets").WithVersion("v1"), |
| 164 |
}, |
| 165 |
cohabitatingResources: []schema.GroupResource{example.Resource("replicasets"), example2.Resource("replicasets")}, |
| 166 |
resourceForConfig: example.Resource("replicasets"), |
| 167 |
exampleForConfig: &example.ReplicaSet{}, |
| 168 |
wantStorageVersion: schema.GroupVersion{Group: example2.GroupName, Version: "v1"}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "config resource is secondary cohabitating resources and not enabled", |
| 172 |
enabledResources: []schema.GroupVersionResource{ |
| 173 |
example.Resource("replicasets").WithVersion("v1"), |
| 174 |
// example2.Resource("replicasets").WithVersion("v1"), // <- disabled |
| 175 |
}, |
| 176 |
cohabitatingResources: []schema.GroupResource{example.Resource("replicasets"), example2.Resource("replicasets")}, |
| 177 |
resourceForConfig: example2.Resource("replicasets"), |
| 178 |
exampleForConfig: &example.ReplicaSet{}, |
| 179 |
wantStorageVersion: schema.GroupVersion{Group: example.GroupName, Version: "v1"}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "override config for one resource of group", |
| 183 |
enabledResources: []schema.GroupVersionResource{ |
| 184 |
example.Resource("replicasets").WithVersion("v1"), |
| 185 |
example2.Resource("replicasets").WithVersion("v1"), |
| 186 |
}, |
| 187 |
overrideResource: example.Resource("replicasets"), |
| 188 |
resourceForConfig: example.Resource("replicasets"), |
| 189 |
exampleForConfig: &example.ReplicaSet{}, |
| 190 |
wantStorageVersion: schema.GroupVersion{Group: example.GroupName, Version: "v1"}, |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "override config for all resource of group", |
| 194 |
enabledResources: []schema.GroupVersionResource{ |
| 195 |
example.Resource("replicasets").WithVersion("v1"), |
| 196 |
example2.Resource("replicasets").WithVersion("v1"), |
| 197 |
}, |
| 198 |
cohabitatingResources: []schema.GroupResource{example.Resource("replicasets"), example2.Resource("replicasets")}, |
| 199 |
overrideResource: example.Resource("*"), |
| 200 |
resourceForConfig: example.Resource("replicasets"), |
| 201 |
exampleForConfig: &example.ReplicaSet{}, |
| 202 |
wantStorageVersion: schema.GroupVersion{Group: example.GroupName, Version: "v1"}, |
| 203 |
}, |
| 204 |
} |
| 205 |
|
| 206 |
for _, test := range testCases { |
| 207 |
t.Run(test.name, func(t *testing.T) { |
| 208 |
ns := &fakeNegotiater{types: []string{"test/test"}} |
| 209 |
resourceConfig := NewResourceConfig() |
| 210 |
resourceConfig.EnableResources(test.enabledResources...) |
| 211 |
|
| 212 |
f := NewDefaultStorageFactory(storagebackend.Config{}, "test/test", ns, NewDefaultResourceEncodingConfig(scheme), resourceConfig, nil) |
| 213 |
|
| 214 |
encoderChainCalled := false |
| 215 |
recorder := newStorageCodecRecorder{} |
| 216 |
|
| 217 |
f.newStorageCodecFn = recorder.Decorators(f.newStorageCodecFn) |
| 218 |
f.AddCohabitatingResources(test.cohabitatingResources...) |
| 219 |
|
| 220 |
if test.overrideResource != emptyResource { |
| 221 |
testEncoderChain := func(e runtime.Encoder) runtime.Encoder { |
| 222 |
encoderChainCalled = true |
| 223 |
return e |
| 224 |
} |
| 225 |
|
| 226 |
f.SetEtcdLocation(test.overrideResource, []string{"/server2"}) |
| 227 |
f.AddSerializationChains(testEncoderChain, nil, test.overrideResource) |
| 228 |
f.SetEtcdPrefix(test.overrideResource, "/prefix_for_test") |
| 229 |
} |
| 230 |
|
| 231 |
config, err := f.NewConfig(test.resourceForConfig, test.exampleForConfig) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
|
| 236 |
// check override resources config |
| 237 |
if test.overrideResource != emptyResource { |
| 238 |
if config.Prefix != "/prefix_for_test" || !reflect.DeepEqual(config.Transport.ServerList, []string{"/server2"}) { |
| 239 |
t.Errorf("unexpected config %#v", config) |
| 240 |
} |
| 241 |
if !encoderChainCalled { |
| 242 |
t.Errorf("expected encoder chain to be called") |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// check cohabitating resources config |
| 247 |
if recorder.opt.StorageVersion != test.wantStorageVersion { |
| 248 |
t.Errorf("unexpected encoding version %#v, but expected %#v", recorder.opt.StorageVersion, test.wantStorageVersion) |
| 249 |
} |
| 250 |
}) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
func TestUpdateEtcdOverrides(t *testing.T) { |
| 255 |
exampleinstall.Install(scheme) |
| 256 |
|
| 257 |
testCases := []struct { |
| 258 |
resource schema.GroupResource |
| 259 |
servers []string |
| 260 |
}{ |
| 261 |
{ |
| 262 |
resource: schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 263 |
servers: []string{"http://127.0.0.1:10000"}, |
| 264 |
}, |
| 265 |
{ |
| 266 |
resource: schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 267 |
servers: []string{"http://127.0.0.1:10000", "http://127.0.0.1:20000"}, |
| 268 |
}, |
| 269 |
{ |
| 270 |
resource: schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 271 |
servers: []string{"http://127.0.0.1:10000"}, |
| 272 |
}, |
| 273 |
} |
| 274 |
|
| 275 |
defaultEtcdLocation := []string{"http://127.0.0.1"} |
| 276 |
for i, test := range testCases { |
| 277 |
defaultConfig := storagebackend.Config{ |
| 278 |
Prefix: "/registry", |
| 279 |
Transport: storagebackend.TransportConfig{ |
| 280 |
ServerList: defaultEtcdLocation, |
| 281 |
}, |
| 282 |
} |
| 283 |
storageFactory := NewDefaultStorageFactory(defaultConfig, "", codecs, NewDefaultResourceEncodingConfig(scheme), NewResourceConfig(), nil) |
| 284 |
storageFactory.SetEtcdLocation(test.resource, test.servers) |
| 285 |
|
| 286 |
var err error |
| 287 |
config, err := storageFactory.NewConfig(test.resource, nil) |
| 288 |
if err != nil { |
| 289 |
t.Errorf("%d: unexpected error %v", i, err) |
| 290 |
continue |
| 291 |
} |
| 292 |
if !reflect.DeepEqual(config.Transport.ServerList, test.servers) { |
| 293 |
t.Errorf("%d: expected %v, got %v", i, test.servers, config.Transport.ServerList) |
| 294 |
continue |
| 295 |
} |
| 296 |
|
| 297 |
config, err = storageFactory.NewConfig(schema.GroupResource{Group: examplev1.GroupName, Resource: "unlikely"}, nil) |
| 298 |
if err != nil { |
| 299 |
t.Errorf("%d: unexpected error %v", i, err) |
| 300 |
continue |
| 301 |
} |
| 302 |
if !reflect.DeepEqual(config.Transport.ServerList, defaultEtcdLocation) { |
| 303 |
t.Errorf("%d: expected %v, got %v", i, defaultEtcdLocation, config.Transport.ServerList) |
| 304 |
continue |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestConfigs(t *testing.T) { |
| 311 |
exampleinstall.Install(scheme) |
| 312 |
defaultEtcdLocations := []string{"http://127.0.0.1", "http://127.0.0.2"} |
| 313 |
|
| 314 |
testCases := []struct { |
| 315 |
resource *schema.GroupResource |
| 316 |
servers []string |
| 317 |
wantConfigs []storagebackend.Config |
| 318 |
}{ |
| 319 |
{ |
| 320 |
wantConfigs: []storagebackend.Config{ |
| 321 |
{Transport: storagebackend.TransportConfig{ServerList: defaultEtcdLocations}, Prefix: "/registry"}, |
| 322 |
}, |
| 323 |
}, |
| 324 |
{ |
| 325 |
resource: &schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 326 |
servers: []string{}, |
| 327 |
wantConfigs: []storagebackend.Config{ |
| 328 |
{Transport: storagebackend.TransportConfig{ServerList: defaultEtcdLocations}, Prefix: "/registry"}, |
| 329 |
}, |
| 330 |
}, |
| 331 |
{ |
| 332 |
resource: &schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 333 |
servers: []string{"http://127.0.0.1:10000"}, |
| 334 |
wantConfigs: []storagebackend.Config{ |
| 335 |
{Transport: storagebackend.TransportConfig{ServerList: defaultEtcdLocations}, Prefix: "/registry"}, |
| 336 |
{Transport: storagebackend.TransportConfig{ServerList: []string{"http://127.0.0.1:10000"}}, Prefix: "/registry"}, |
| 337 |
}, |
| 338 |
}, |
| 339 |
{ |
| 340 |
resource: &schema.GroupResource{Group: example.GroupName, Resource: "resource"}, |
| 341 |
servers: []string{"http://127.0.0.1:10000", "https://127.0.0.1", "http://127.0.0.2"}, |
| 342 |
wantConfigs: []storagebackend.Config{ |
| 343 |
{Transport: storagebackend.TransportConfig{ServerList: defaultEtcdLocations}, Prefix: "/registry"}, |
| 344 |
{Transport: storagebackend.TransportConfig{ServerList: []string{"http://127.0.0.1:10000", "https://127.0.0.1", "http://127.0.0.2"}}, Prefix: "/registry"}, |
| 345 |
}, |
| 346 |
}, |
| 347 |
} |
| 348 |
|
| 349 |
for i, test := range testCases { |
| 350 |
defaultConfig := storagebackend.Config{ |
| 351 |
Prefix: "/registry", |
| 352 |
Transport: storagebackend.TransportConfig{ |
| 353 |
ServerList: defaultEtcdLocations, |
| 354 |
}, |
| 355 |
} |
| 356 |
storageFactory := NewDefaultStorageFactory(defaultConfig, "", codecs, NewDefaultResourceEncodingConfig(scheme), NewResourceConfig(), nil) |
| 357 |
if test.resource != nil { |
| 358 |
storageFactory.SetEtcdLocation(*test.resource, test.servers) |
| 359 |
} |
| 360 |
|
| 361 |
got := storageFactory.Configs() |
| 362 |
if !reflect.DeepEqual(test.wantConfigs, got) { |
| 363 |
t.Errorf("%d: expected %v, got %v", i, test.wantConfigs, got) |
| 364 |
continue |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
var introducedLifecycles = map[reflect.Type]*apimachineryversion.Version{} |
| 370 |
var removedLifecycles = map[reflect.Type]*apimachineryversion.Version{} |
| 371 |
|
| 372 |
type fakeLifecycler[T, V any] struct { |
| 373 |
metav1.TypeMeta |
| 374 |
metav1.ObjectMeta |
| 375 |
} |
| 376 |
|
| 377 |
type removedLifecycler[T, V any] struct { |
| 378 |
fakeLifecycler[T, V] |
| 379 |
} |
| 380 |
|
| 381 |
func (f *fakeLifecycler[T, V]) GetObjectKind() schema.ObjectKind { return f } |
| 382 |
func (f *fakeLifecycler[T, V]) DeepCopyObject() runtime.Object { return f } |
| 383 |
func (f *fakeLifecycler[T, V]) APILifecycleIntroduced() (major, minor int) { |
| 384 |
if introduced, ok := introducedLifecycles[reflect.TypeOf(f)]; ok { |
| 385 |
return int(introduced.Major()), int(introduced.Minor()) |
| 386 |
} |
| 387 |
panic("no lifecycle version set") |
| 388 |
} |
| 389 |
func (f *removedLifecycler[T, V]) APILifecycleRemoved() (major, minor int) { |
| 390 |
if removed, ok := removedLifecycles[reflect.TypeOf(f)]; ok { |
| 391 |
return int(removed.Major()), int(removed.Minor()) |
| 392 |
} |
| 393 |
panic("no lifecycle version set") |
| 394 |
} |
| 395 |
|
| 396 |
func registerFakeLifecycle[T, V any](sch *runtime.Scheme, group, introduced, removed string) { |
| 397 |
f := fakeLifecycler[T, V]{} |
| 398 |
|
| 399 |
introducedLifecycles[reflect.TypeOf(&f)] = apimachineryversion.MustParseSemantic(introduced) |
| 400 |
|
| 401 |
var res runtime.Object |
| 402 |
if removed != "" { |
| 403 |
removedLifecycles[reflect.TypeOf(&f)] = apimachineryversion.MustParseSemantic(removed) |
| 404 |
res = &removedLifecycler[T, V]{fakeLifecycler: f} |
| 405 |
} else { |
| 406 |
res = &f |
| 407 |
} |
| 408 |
|
| 409 |
var v V |
| 410 |
var t T |
| 411 |
sch.AddKnownTypeWithName( |
| 412 |
schema.GroupVersionKind{ |
| 413 |
Group: group, |
| 414 |
Version: strings.ToLower(reflect.TypeOf(v).Name()), |
| 415 |
Kind: reflect.TypeOf(t).Name(), |
| 416 |
}, |
| 417 |
res, |
| 418 |
) |
| 419 |
|
| 420 |
// Also ensure internal version is registered |
| 421 |
// If it is registertd multiple times, it will ignore subsequent registrations |
| 422 |
internalInstance := &fakeLifecycler[T, struct{}]{} |
| 423 |
sch.AddKnownTypeWithName( |
| 424 |
schema.GroupVersionKind{ |
| 425 |
Group: group, |
| 426 |
Version: runtime.APIVersionInternal, |
| 427 |
Kind: reflect.TypeOf(t).Name(), |
| 428 |
}, |
| 429 |
internalInstance, |
| 430 |
) |
| 431 |
} |
| 432 |
|
| 433 |
func TestStorageFactoryCompatibilityVersion(t *testing.T) { |
| 434 |
// Creates a scheme with stub types for unit test |
| 435 |
sch := runtime.NewScheme() |
| 436 |
codecs := serializer.NewCodecFactory(sch) |
| 437 |
|
| 438 |
type Internal = struct{} |
| 439 |
type V1beta1 struct{} |
| 440 |
type V1beta2 struct{} |
| 441 |
type V1beta3 struct{} |
| 442 |
type V1 struct{} |
| 443 |
|
| 444 |
type Pod struct{} |
| 445 |
type FlowSchema struct{} |
| 446 |
type ValidatingAdmisisonPolicy struct{} |
| 447 |
type CronJob struct{} |
| 448 |
|
| 449 |
// Order dictates priority order |
| 450 |
registerFakeLifecycle[FlowSchema, V1](sch, "flowcontrol.apiserver.k8s.io", "1.29.0", "") |
| 451 |
registerFakeLifecycle[FlowSchema, V1beta3](sch, "flowcontrol.apiserver.k8s.io", "1.26.0", "1.32.0") |
| 452 |
registerFakeLifecycle[FlowSchema, V1beta2](sch, "flowcontrol.apiserver.k8s.io", "1.23.0", "1.29.0") |
| 453 |
registerFakeLifecycle[FlowSchema, V1beta1](sch, "flowcontrol.apiserver.k8s.io", "1.20.0", "1.26.0") |
| 454 |
registerFakeLifecycle[CronJob, V1](sch, "batch", "1.21.0", "") |
| 455 |
registerFakeLifecycle[CronJob, V1beta1](sch, "batch", "1.8.0", "1.21.0") |
| 456 |
registerFakeLifecycle[ValidatingAdmisisonPolicy, V1](sch, "admissionregistration.k8s.io", "1.30.0", "") |
| 457 |
registerFakeLifecycle[ValidatingAdmisisonPolicy, V1beta1](sch, "admissionregistration.k8s.io", "1.28.0", "1.34.0") |
| 458 |
registerFakeLifecycle[Pod, V1](sch, "", "1.31.0", "") |
| 459 |
|
| 460 |
// FlowSchema |
| 461 |
// - v1beta1: 1.20.0 - 1.23.0 |
| 462 |
// - v1beta2: 1.23.0 - 1.26.0 |
| 463 |
// - v1beta3: 1.26.0 - 1.30.0 |
| 464 |
// - v1: 1.29.0+ |
| 465 |
// CronJob |
| 466 |
// - v1beta1: 1.8.0 - 1.21.0 |
| 467 |
// - v1: 1.21.0+ |
| 468 |
// ValidatingAdmissionPolicy |
| 469 |
// - v1beta1: 1.28.0 - 1.31.0 |
| 470 |
// - v1: 1.30.0+ |
| 471 |
|
| 472 |
testcases := []struct { |
| 473 |
effectiveVersion string |
| 474 |
example runtime.Object |
| 475 |
expectedVersion schema.GroupVersion |
| 476 |
}{ |
| 477 |
{ |
| 478 |
// Basic case. Beta version for long time |
| 479 |
effectiveVersion: "1.14.0", |
| 480 |
example: &fakeLifecycler[CronJob, Internal]{}, |
| 481 |
expectedVersion: schema.GroupVersion{Group: "batch", Version: "v1beta1"}, |
| 482 |
}, |
| 483 |
{ |
| 484 |
// Basic case. Beta version for long time |
| 485 |
effectiveVersion: "1.20.0", |
| 486 |
example: &fakeLifecycler[CronJob, Internal]{}, |
| 487 |
expectedVersion: schema.GroupVersion{Group: "batch", Version: "v1beta1"}, |
| 488 |
}, |
| 489 |
{ |
| 490 |
// Basic case. GA version for long time |
| 491 |
effectiveVersion: "1.28.0", |
| 492 |
example: &fakeLifecycler[CronJob, Internal]{}, |
| 493 |
expectedVersion: schema.GroupVersion{Group: "batch", Version: "v1"}, |
| 494 |
}, |
| 495 |
{ |
| 496 |
// Basic core/v1 |
| 497 |
effectiveVersion: "1.31.0", |
| 498 |
example: &fakeLifecycler[Pod, Internal]{}, |
| 499 |
expectedVersion: schema.GroupVersion{Group: "", Version: "v1"}, |
| 500 |
}, |
| 501 |
{ |
| 502 |
// Corner case: 1.1.0 has no flowcontrol. Options are to error |
| 503 |
// out or to use the latest version. This test assumes the latter. |
| 504 |
effectiveVersion: "1.1.0", |
| 505 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 506 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1"}, |
| 507 |
}, |
| 508 |
{ |
| 509 |
effectiveVersion: "1.21.0", |
| 510 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 511 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta1"}, |
| 512 |
}, |
| 513 |
{ |
| 514 |
// v2Beta1 introduced this version, but minCompatibility should |
| 515 |
// force v1beta1 |
| 516 |
effectiveVersion: "1.23.0", |
| 517 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 518 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta1"}, |
| 519 |
}, |
| 520 |
{ |
| 521 |
effectiveVersion: "1.24.0", |
| 522 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 523 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta2"}, |
| 524 |
}, |
| 525 |
{ |
| 526 |
effectiveVersion: "1.26.0", |
| 527 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 528 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta2"}, |
| 529 |
}, |
| 530 |
{ |
| 531 |
effectiveVersion: "1.27.0", |
| 532 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 533 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta3"}, |
| 534 |
}, |
| 535 |
{ |
| 536 |
// GA API introduced 1.29 but must keep storing in v1beta3 for downgrades |
| 537 |
effectiveVersion: "1.29.0", |
| 538 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 539 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta3"}, |
| 540 |
}, |
| 541 |
{ |
| 542 |
// Version after GA api is introduced |
| 543 |
effectiveVersion: "1.30.0", |
| 544 |
example: &fakeLifecycler[FlowSchema, Internal]{}, |
| 545 |
expectedVersion: schema.GroupVersion{Group: "flowcontrol.apiserver.k8s.io", Version: "v1"}, |
| 546 |
}, |
| 547 |
{ |
| 548 |
effectiveVersion: "1.30.0", |
| 549 |
example: &fakeLifecycler[ValidatingAdmisisonPolicy, Internal]{}, |
| 550 |
expectedVersion: schema.GroupVersion{Group: "admissionregistration.k8s.io", Version: "v1beta1"}, |
| 551 |
}, |
| 552 |
{ |
| 553 |
effectiveVersion: "1.31.0", |
| 554 |
example: &fakeLifecycler[ValidatingAdmisisonPolicy, Internal]{}, |
| 555 |
expectedVersion: schema.GroupVersion{Group: "admissionregistration.k8s.io", Version: "v1"}, |
| 556 |
}, |
| 557 |
{ |
| 558 |
effectiveVersion: "1.29.0", |
| 559 |
example: &fakeLifecycler[ValidatingAdmisisonPolicy, Internal]{}, |
| 560 |
expectedVersion: schema.GroupVersion{Group: "admissionregistration.k8s.io", Version: "v1beta1"}, |
| 561 |
}, |
| 562 |
} |
| 563 |
|
| 564 |
for _, tc := range testcases { |
| 565 |
gvks, _, err := sch.ObjectKinds(tc.example) |
| 566 |
if err != nil { |
| 567 |
t.Fatalf("unexpected error: %v", err) |
| 568 |
} |
| 569 |
|
| 570 |
gvk := gvks[0] |
| 571 |
t.Run(gvk.GroupKind().String()+"@"+tc.effectiveVersion, func(t *testing.T) { |
| 572 |
config := NewDefaultResourceEncodingConfigForEffectiveVersion(sch, basecompatibility.NewEffectiveVersionFromString(tc.effectiveVersion, "", "")) |
| 573 |
f := NewDefaultStorageFactory( |
| 574 |
storagebackend.Config{}, |
| 575 |
"", |
| 576 |
codecs, |
| 577 |
config, |
| 578 |
NewResourceConfig(), |
| 579 |
nil) |
| 580 |
|
| 581 |
cfg, err := f.NewConfig(schema.GroupResource{ |
| 582 |
Group: gvk.Group, |
| 583 |
Resource: gvk.Kind, // doesnt really matter here |
| 584 |
}, tc.example) |
| 585 |
if err != nil { |
| 586 |
t.Fatalf("unexpected error: %v", err) |
| 587 |
} |
| 588 |
|
| 589 |
gvks, _, err := sch.ObjectKinds(tc.example) |
| 590 |
if err != nil { |
| 591 |
t.Fatalf("unexpected error: %v", err) |
| 592 |
} |
| 593 |
expectEncodeVersioner := runtime.NewMultiGroupVersioner(tc.expectedVersion, |
| 594 |
schema.GroupKind{ |
| 595 |
Group: gvks[0].Group, |
| 596 |
}, schema.GroupKind{ |
| 597 |
Group: gvks[0].Group, |
| 598 |
}) |
| 599 |
if cfg.EncodeVersioner.Identifier() != expectEncodeVersioner.Identifier() { |
| 600 |
t.Errorf("expected %v, got %v", expectEncodeVersioner, cfg.EncodeVersioner) |
| 601 |
} |
| 602 |
}) |
| 603 |
} |
| 604 |
} |