Debian Codegraph @

pkg/server/deleted_kinds_test.go

raw
1 /*
2 Copyright 2020 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 server
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apimachinery/pkg/runtime/schema"
26 "k8s.io/apimachinery/pkg/util/dump"
27 "k8s.io/apimachinery/pkg/util/sets"
28 apimachineryversion "k8s.io/apimachinery/pkg/util/version"
29 "k8s.io/apiserver/pkg/registry/rest"
30 "k8s.io/apiserver/pkg/server/resourceconfig"
31 serverstorage "k8s.io/apiserver/pkg/server/storage"
32
33 "github.com/stretchr/testify/require"
34 )
35
36 func Test_newResourceExpirationEvaluator(t *testing.T) {
37 tests := []struct {
38 name string
39 currentVersion string
40 expected resourceExpirationEvaluator
41 expectedErr string
42 }{
43 {
44 name: "beta",
45 currentVersion: "v1.20.0-beta.0.62+a5d22854a2ac21",
46 expected: resourceExpirationEvaluator{currentVersion: apimachineryversion.MajorMinor(1, 20)},
47 },
48 {
49 name: "alpha .0",
50 currentVersion: "v1.20.0-alpha.0.62+a5d22854a2ac21",
51 expected: resourceExpirationEvaluator{currentVersion: apimachineryversion.MajorMinor(1, 20), isAlpha: true, isAlphaZero: true},
52 },
53 {
54 name: "alpha not .0",
55 currentVersion: "v1.20.0-alpha.1.62+a5d22854a2ac21",
56 expected: resourceExpirationEvaluator{currentVersion: apimachineryversion.MajorMinor(1, 20), isAlpha: true, isAlphaZero: false},
57 },
58
59 {
60 name: "maintenance",
61 currentVersion: "v1.20.1",
62 expected: resourceExpirationEvaluator{currentVersion: apimachineryversion.MajorMinor(1, 20)},
63 },
64 {
65 name: "no v prefix",
66 currentVersion: "1.20.1",
67 expected: resourceExpirationEvaluator{currentVersion: apimachineryversion.MajorMinor(1, 20)},
68 },
69 }
70 for _, tt := range tests {
71 t.Run(tt.name, func(t *testing.T) {
72 actual, actualErr := NewResourceExpirationEvaluator(apimachineryversion.MustParse(tt.currentVersion))
73
74 checkErr(t, actualErr, tt.expectedErr)
75 if actualErr != nil {
76 return
77 }
78
79 actual.(*resourceExpirationEvaluator).strictRemovedHandlingInAlpha = false
80 if !reflect.DeepEqual(tt.expected, *actual.(*resourceExpirationEvaluator)) {
81 t.Fatal(actual)
82 }
83 })
84 }
85 }
86
87 type defaultObj struct {
88 }
89
90 func (r *defaultObj) GetObjectKind() schema.ObjectKind {
91 panic("don't do this")
92 }
93 func (r *defaultObj) DeepCopyObject() runtime.Object {
94 panic("don't do this either")
95 }
96
97 type removedInObj struct {
98 major, minor int
99 }
100
101 func (r *removedInObj) GetObjectKind() schema.ObjectKind {
102 panic("don't do this")
103 }
104 func (r *removedInObj) DeepCopyObject() runtime.Object {
105 panic("don't do this either")
106 }
107 func (r *removedInObj) APILifecycleRemoved() (major, minor int) {
108 return r.major, r.minor
109 }
110
111 type IntroducedInObj struct {
112 major, minor int
113 }
114
115 func (r *IntroducedInObj) GetObjectKind() schema.ObjectKind {
116 panic("don't do this")
117 }
118 func (r *IntroducedInObj) DeepCopyObject() runtime.Object {
119 panic("don't do this either")
120 }
121 func (r *IntroducedInObj) APILifecycleIntroduced() (major, minor int) {
122 return r.major, r.minor
123 }
124
125 type introducedAndRemovedInObj struct {
126 majorIntroduced, minorIntroduced int
127 majorRemoved, minorRemoved int
128 }
129
130 func (r *introducedAndRemovedInObj) GetObjectKind() schema.ObjectKind {
131 panic("don't do this")
132 }
133 func (r *introducedAndRemovedInObj) DeepCopyObject() runtime.Object {
134 panic("don't do this either")
135 }
136 func (r *introducedAndRemovedInObj) APILifecycleIntroduced() (major, minor int) {
137 return r.majorIntroduced, r.minorIntroduced
138 }
139 func (r *introducedAndRemovedInObj) APILifecycleRemoved() (major, minor int) {
140 return r.majorRemoved, r.minorRemoved
141 }
142
143 func storageRemovedIn(major, minor int) *introducedAndRemovedInStorage {
144 return &introducedAndRemovedInStorage{majorRemoved: major, minorRemoved: minor}
145 }
146
147 func storageNeverRemoved() *introducedAndRemovedInStorage {
148 return &introducedAndRemovedInStorage{}
149 }
150
151 func storageIntroducedIn(major, minor int) *introducedAndRemovedInStorage {
152 return &introducedAndRemovedInStorage{majorIntroduced: major, minorIntroduced: minor}
153 }
154
155 func storageIntroducedAndRemovedIn(majorIntroduced, minorIntroduced, majorRemoved, minorRemoved int) *introducedAndRemovedInStorage {
156 return &introducedAndRemovedInStorage{majorIntroduced: majorIntroduced, minorIntroduced: minorIntroduced, majorRemoved: majorRemoved, minorRemoved: minorRemoved}
157 }
158
159 type introducedAndRemovedInStorage struct {
160 majorIntroduced, minorIntroduced int
161 majorRemoved, minorRemoved int
162 }
163
164 func (r *introducedAndRemovedInStorage) New() runtime.Object {
165 if r.majorIntroduced == 0 && r.minorIntroduced == 0 && r.majorRemoved == 0 && r.minorRemoved == 0 {
166 return &defaultObj{}
167 }
168 if r.majorIntroduced == 0 && r.minorIntroduced == 0 {
169 return &removedInObj{major: r.majorRemoved, minor: r.minorRemoved}
170 }
171 if r.majorRemoved == 0 && r.minorRemoved == 0 {
172 return &IntroducedInObj{major: r.majorIntroduced, minor: r.minorIntroduced}
173 }
174 return &introducedAndRemovedInObj{majorIntroduced: r.majorIntroduced, minorIntroduced: r.minorIntroduced, majorRemoved: r.majorRemoved, minorRemoved: r.minorRemoved}
175 }
176
177 func (r *introducedAndRemovedInStorage) Destroy() {}
178
179 func Test_resourceExpirationEvaluator_isNotRemoved(t *testing.T) {
180 tests := []struct {
181 name string
182 resourceExpirationEvaluator resourceExpirationEvaluator
183 restStorage rest.Storage
184 expected bool
185 }{
186 {
187 name: "removed-in-curr",
188 resourceExpirationEvaluator: resourceExpirationEvaluator{
189 currentVersion: apimachineryversion.MajorMinor(1, 20),
190 },
191 restStorage: storageRemovedIn(1, 20),
192 expected: false,
193 },
194 {
195 name: "removed-in-curr-but-deferred",
196 resourceExpirationEvaluator: resourceExpirationEvaluator{
197 currentVersion: apimachineryversion.MajorMinor(1, 20),
198 serveRemovedAPIsOneMoreRelease: true,
199 },
200 restStorage: storageRemovedIn(1, 20),
201 expected: true,
202 },
203 {
204 name: "removed-in-curr-but-alpha",
205 resourceExpirationEvaluator: resourceExpirationEvaluator{
206 currentVersion: apimachineryversion.MajorMinor(1, 20),
207 isAlpha: true,
208 },
209 restStorage: storageRemovedIn(1, 20),
210 expected: true,
211 },
212 {
213 name: "removed-in-curr-but-alpha-but-strict",
214 resourceExpirationEvaluator: resourceExpirationEvaluator{
215 currentVersion: apimachineryversion.MajorMinor(1, 20),
216 isAlpha: true,
217 strictRemovedHandlingInAlpha: true,
218 },
219 restStorage: storageRemovedIn(1, 20),
220 expected: false,
221 },
222 {
223 name: "removed-in-curr-but-alpha-but-strict-and-alpha-zero",
224 resourceExpirationEvaluator: resourceExpirationEvaluator{
225 currentVersion: apimachineryversion.MajorMinor(1, 20),
226 isAlpha: true,
227 isAlphaZero: true,
228 strictRemovedHandlingInAlpha: true,
229 },
230 restStorage: storageRemovedIn(1, 20),
231 expected: true,
232 },
233 {
234 name: "removed-in-prev-deferral-does-not-help",
235 resourceExpirationEvaluator: resourceExpirationEvaluator{
236 currentVersion: apimachineryversion.MajorMinor(1, 21),
237 serveRemovedAPIsOneMoreRelease: true,
238 },
239 restStorage: storageRemovedIn(1, 20),
240 expected: false,
241 },
242 {
243 name: "removed-in-prev-major",
244 resourceExpirationEvaluator: resourceExpirationEvaluator{
245 currentVersion: apimachineryversion.MajorMinor(2, 20),
246 serveRemovedAPIsOneMoreRelease: true,
247 },
248 restStorage: storageRemovedIn(1, 20),
249 expected: false,
250 },
251 {
252 name: "removed-in-future",
253 resourceExpirationEvaluator: resourceExpirationEvaluator{
254 currentVersion: apimachineryversion.MajorMinor(1, 20),
255 },
256 restStorage: storageRemovedIn(1, 21),
257 expected: true,
258 },
259 {
260 name: "never-removed",
261 resourceExpirationEvaluator: resourceExpirationEvaluator{
262 currentVersion: apimachineryversion.MajorMinor(1, 20),
263 },
264 restStorage: storageNeverRemoved(),
265 expected: true,
266 },
267 }
268 for _, tt := range tests {
269 t.Run(tt.name, func(t *testing.T) {
270 gv := schema.GroupVersion{Group: "mygroup", Version: "myversion"}
271 convertor := &dummyConvertor{prioritizedVersions: []schema.GroupVersion{gv}}
272 if actual := tt.resourceExpirationEvaluator.isNotRemoved(gv, convertor, tt.restStorage); actual != tt.expected {
273 t.Errorf("isRemoved() = %v, want %v", actual, tt.expected)
274 }
275 if !reflect.DeepEqual(convertor.called, gv) {
276 t.Errorf("expected converter to be called with %#v, got %#v", gv, convertor.called)
277 }
278 })
279 }
280 }
281
282 type dummyConvertor struct {
283 called runtime.GroupVersioner
284 prioritizedVersions []schema.GroupVersion
285 }
286
287 func (d *dummyConvertor) ConvertToVersion(in runtime.Object, gv runtime.GroupVersioner) (runtime.Object, error) {
288 d.called = gv
289 return in, nil
290 }
291
292 func (d *dummyConvertor) PrioritizedVersionsForGroup(group string) []schema.GroupVersion {
293 return d.prioritizedVersions
294 }
295
296 func (d *dummyConvertor) IsGroupRegistered(group string) bool {
297 return true
298 }
299
300 func (d *dummyConvertor) IsVersionRegistered(v schema.GroupVersion) bool {
301 return true
302 }
303
304 func (d *dummyConvertor) PrioritizedVersionsAllGroups() []schema.GroupVersion {
305 return d.prioritizedVersions
306 }
307
308 func checkErr(t *testing.T, actual error, expected string) {
309 t.Helper()
310 switch {
311 case len(expected) == 0 && actual == nil:
312 case len(expected) == 0 && actual != nil:
313 t.Fatal(actual)
314 case len(expected) != 0 && actual == nil:
315 t.Fatalf("missing %q, <nil>", expected)
316 case len(expected) != 0 && actual != nil && !strings.Contains(actual.Error(), expected):
317 t.Fatalf("missing %q, %v", expected, actual)
318 }
319 }
320
321 func Test_removeDeletedKinds(t *testing.T) {
322 groupName := "group.name"
323 tests := []struct {
324 name string
325 resourceExpirationEvaluator resourceExpirationEvaluator
326 versionedResourcesStorageMap map[string]map[string]rest.Storage
327 expectedStorage map[string]map[string]rest.Storage
328 }{
329 {
330 name: "remove-one-of-two",
331 resourceExpirationEvaluator: resourceExpirationEvaluator{
332 currentVersion: apimachineryversion.MajorMinor(1, 20),
333 },
334 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
335 "v1": {
336 "twenty": storageRemovedIn(1, 20),
337 "twentyone": storageRemovedIn(1, 21),
338 },
339 },
340 expectedStorage: map[string]map[string]rest.Storage{
341 "v1": {
342 "twentyone": storageRemovedIn(1, 21),
343 },
344 },
345 },
346 {
347 name: "remove-nested-not-expired",
348 resourceExpirationEvaluator: resourceExpirationEvaluator{
349 currentVersion: apimachineryversion.MajorMinor(1, 20),
350 },
351 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
352 "v1": {
353 "twenty": storageRemovedIn(1, 20),
354 "twenty/scale": storageRemovedIn(1, 21),
355 "twentyone": storageRemovedIn(1, 21),
356 },
357 },
358 expectedStorage: map[string]map[string]rest.Storage{
359 "v1": {
360 "twentyone": storageRemovedIn(1, 21),
361 },
362 },
363 },
364 {
365 name: "remove-all-of-version",
366 resourceExpirationEvaluator: resourceExpirationEvaluator{
367 currentVersion: apimachineryversion.MajorMinor(1, 20),
368 },
369 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
370 "v1": {
371 "twenty": storageRemovedIn(1, 20),
372 },
373 "v2": {
374 "twenty": storageRemovedIn(1, 20),
375 "twentyone": storageRemovedIn(1, 21),
376 },
377 },
378 expectedStorage: map[string]map[string]rest.Storage{
379 "v2": {
380 "twentyone": storageRemovedIn(1, 21),
381 },
382 },
383 },
384 }
385 for _, tt := range tests {
386 t.Run(tt.name, func(t *testing.T) {
387 convertor := &dummyConvertor{prioritizedVersions: []schema.GroupVersion{
388 {Group: groupName, Version: "v2"}, {Group: groupName, Version: "v1"}}}
389 tt.resourceExpirationEvaluator.removeDeletedKinds(groupName, convertor, tt.versionedResourcesStorageMap)
390 if !reflect.DeepEqual(tt.expectedStorage, tt.versionedResourcesStorageMap) {
391 t.Fatal(dump.Pretty(tt.versionedResourcesStorageMap))
392 }
393 })
394 }
395 }
396
397 func Test_removeUnIntroducedKinds(t *testing.T) {
398 groupName := "group.name"
399 resource1 := "resource1"
400 resource2 := "resource2"
401 tests := []struct {
402 name string
403 resourceExpirationEvaluator resourceExpirationEvaluator
404 runtimeConfig map[string]string
405 expectErr bool
406 versionedResourcesStorageMap map[string]map[string]rest.Storage
407 expectedStorage map[string]map[string]rest.Storage
408 }{
409 {
410 name: "remove-future-version",
411 resourceExpirationEvaluator: resourceExpirationEvaluator{
412 currentVersion: apimachineryversion.MajorMinor(1, 21),
413 },
414 runtimeConfig: map[string]string{
415 "api/beta": "true",
416 groupName + "/v2beta1": "true",
417 },
418 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
419 "v1": {
420 resource1: storageIntroducedIn(1, 18),
421 },
422 "v2alpha1": {
423 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
424 },
425 "v2beta1": {
426 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
427 },
428 "v2beta2": {
429 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
430 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
431 },
432 "v2": {
433 resource1: storageIntroducedIn(1, 23),
434 resource2: storageIntroducedIn(1, 23),
435 },
436 },
437 expectedStorage: map[string]map[string]rest.Storage{
438 "v1": {
439 resource1: storageIntroducedIn(1, 18),
440 },
441 "v2alpha1": {
442 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
443 },
444 "v2beta1": {
445 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
446 },
447 },
448 },
449 {
450 name: "missing-introduced-version",
451 resourceExpirationEvaluator: resourceExpirationEvaluator{
452 currentVersion: apimachineryversion.MajorMinor(1, 20),
453 },
454 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
455 "v1": {
456 resource1: storageRemovedIn(1, 23),
457 },
458 "v2": {
459 resource1: storageIntroducedIn(1, 23),
460 resource2: storageIntroducedIn(1, 23),
461 },
462 },
463 expectedStorage: map[string]map[string]rest.Storage{
464 "v1": {
465 resource1: storageRemovedIn(1, 23),
466 },
467 },
468 },
469 {
470 name: "emulation-forward-compatible-ga-api",
471 resourceExpirationEvaluator: resourceExpirationEvaluator{
472 currentVersion: apimachineryversion.MajorMinor(1, 19),
473 emulationForwardCompatible: true,
474 },
475 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
476 "v1": {
477 resource1: storageIntroducedIn(1, 18),
478 },
479 "v2alpha1": {
480 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
481 },
482 "v2beta1": {
483 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
484 },
485 "v2beta2": {
486 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
487 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
488 },
489 "v2": {
490 resource1: storageIntroducedIn(1, 23),
491 resource2: storageIntroducedIn(1, 23),
492 },
493 },
494 expectedStorage: map[string]map[string]rest.Storage{
495 "v1": {
496 resource1: storageIntroducedIn(1, 18),
497 },
498 "v2": {
499 resource1: storageIntroducedIn(1, 23),
500 },
501 },
502 },
503 {
504 name: "emulation-forward-compatible-alpha-api",
505 resourceExpirationEvaluator: resourceExpirationEvaluator{
506 currentVersion: apimachineryversion.MajorMinor(1, 20),
507 emulationForwardCompatible: true,
508 },
509 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
510 "v1": {
511 resource1: storageIntroducedIn(1, 18),
512 },
513 "v2alpha1": {
514 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
515 },
516 "v2beta1": {
517 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
518 },
519 "v2beta2": {
520 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
521 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
522 },
523 "v2": {
524 resource1: storageIntroducedIn(1, 23),
525 resource2: storageIntroducedIn(1, 23),
526 },
527 },
528 expectedStorage: map[string]map[string]rest.Storage{
529 "v1": {
530 resource1: storageIntroducedIn(1, 18),
531 },
532 "v2alpha1": {
533 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
534 },
535 "v2": {
536 resource1: storageIntroducedIn(1, 23),
537 },
538 },
539 },
540 {
541 name: "emulation-forward-compatible-beta1-api",
542 resourceExpirationEvaluator: resourceExpirationEvaluator{
543 currentVersion: apimachineryversion.MajorMinor(1, 21),
544 emulationForwardCompatible: true,
545 },
546 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
547 "v1": {
548 resource1: storageIntroducedIn(1, 20),
549 },
550 "v2beta1": {
551 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
552 },
553 "v2beta2": {
554 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
555 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
556 },
557 "v2": {
558 resource1: storageIntroducedIn(1, 23),
559 resource2: storageIntroducedIn(1, 23),
560 },
561 },
562 expectedStorage: map[string]map[string]rest.Storage{
563 "v1": {
564 resource1: storageIntroducedIn(1, 20),
565 },
566 "v2beta1": {
567 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
568 },
569 "v2beta2": {
570 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
571 },
572 "v2": {
573 resource1: storageIntroducedIn(1, 23),
574 },
575 },
576 },
577 {
578 name: "emulation-forward-compatible-new-resource",
579 resourceExpirationEvaluator: resourceExpirationEvaluator{
580 currentVersion: apimachineryversion.MajorMinor(1, 22),
581 emulationForwardCompatible: true,
582 },
583 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
584 "v1": {
585 resource1: storageIntroducedIn(1, 20),
586 },
587 "v2beta1": {
588 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
589 },
590 "v2beta2": {
591 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
592 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
593 },
594 "v2": {
595 resource1: storageIntroducedIn(1, 23),
596 resource2: storageIntroducedIn(1, 23),
597 },
598 },
599 expectedStorage: map[string]map[string]rest.Storage{
600 "v1": {
601 resource1: storageIntroducedIn(1, 20),
602 },
603 "v2beta1": {
604 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
605 },
606 "v2beta2": {
607 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
608 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
609 },
610 "v2": {
611 resource1: storageIntroducedIn(1, 23),
612 resource2: storageIntroducedIn(1, 23),
613 },
614 },
615 },
616 {
617 name: "runtime-config-enable-future-version-err",
618 resourceExpirationEvaluator: resourceExpirationEvaluator{
619 currentVersion: apimachineryversion.MajorMinor(1, 20),
620 },
621 runtimeConfig: map[string]string{
622 groupName + "/v2beta2": "true",
623 },
624 expectErr: true,
625 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
626 "v1": {
627 resource1: storageIntroducedIn(1, 20),
628 },
629 "v2beta1": {
630 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
631 },
632 "v2beta2": {
633 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
634 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
635 },
636 "v2": {
637 resource1: storageIntroducedIn(1, 23),
638 resource2: storageIntroducedIn(1, 23),
639 },
640 },
641 },
642 {
643 name: "runtime-config-enable-future-resource-err",
644 resourceExpirationEvaluator: resourceExpirationEvaluator{
645 currentVersion: apimachineryversion.MajorMinor(1, 20),
646 },
647 runtimeConfig: map[string]string{
648 groupName + "/v2beta2/resource2": "true",
649 },
650 expectErr: true,
651 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
652 "v1": {
653 resource1: storageIntroducedIn(1, 20),
654 },
655 "v2beta1": {
656 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
657 },
658 "v2beta2": {
659 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
660 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
661 },
662 "v2": {
663 resource1: storageIntroducedIn(1, 23),
664 resource2: storageIntroducedIn(1, 23),
665 },
666 },
667 },
668 {
669 name: "runtime-config-emulation-forward-compatible-beta2-api",
670 resourceExpirationEvaluator: resourceExpirationEvaluator{
671 currentVersion: apimachineryversion.MajorMinor(1, 20),
672 runtimeConfigEmulationForwardCompatible: true,
673 },
674 runtimeConfig: map[string]string{
675 groupName + "/v2beta2": "true",
676 },
677 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
678 "v1": {
679 resource1: storageIntroducedIn(1, 20),
680 },
681 "v2beta1": {
682 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
683 },
684 "v2beta2": {
685 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
686 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
687 },
688 "v2": {
689 resource1: storageIntroducedIn(1, 23),
690 resource2: storageIntroducedIn(1, 23),
691 },
692 },
693 expectedStorage: map[string]map[string]rest.Storage{
694 "v1": {
695 resource1: storageIntroducedIn(1, 20),
696 },
697 "v2beta2": {
698 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
699 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
700 },
701 },
702 },
703 {
704 name: "runtime-config-emulation-forward-compatible-beta2-resource",
705 resourceExpirationEvaluator: resourceExpirationEvaluator{
706 currentVersion: apimachineryversion.MajorMinor(1, 20),
707 runtimeConfigEmulationForwardCompatible: true,
708 },
709 runtimeConfig: map[string]string{
710 groupName + "/v2beta2/resource2": "true",
711 },
712 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
713 "v1": {
714 resource1: storageIntroducedIn(1, 20),
715 },
716 "v2beta1": {
717 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
718 },
719 "v2beta2": {
720 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
721 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
722 },
723 "v2": {
724 resource1: storageIntroducedIn(1, 23),
725 resource2: storageIntroducedIn(1, 23),
726 },
727 },
728 expectedStorage: map[string]map[string]rest.Storage{
729 "v1": {
730 resource1: storageIntroducedIn(1, 20),
731 },
732 "v2beta2": {
733 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
734 },
735 },
736 },
737 {
738 name: "emulation-forward-compatible-runtime-config-beta2-api",
739 resourceExpirationEvaluator: resourceExpirationEvaluator{
740 currentVersion: apimachineryversion.MajorMinor(1, 20),
741 emulationForwardCompatible: true,
742 },
743 runtimeConfig: map[string]string{
744 groupName + "/v2beta2": "true",
745 },
746 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
747 "v1": {
748 resource1: storageIntroducedIn(1, 20),
749 },
750 "v2beta1": {
751 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
752 },
753 "v2beta2": {
754 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
755 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
756 },
757 "v2": {
758 resource1: storageIntroducedIn(1, 23),
759 resource2: storageIntroducedIn(1, 23),
760 },
761 },
762 expectErr: true,
763 },
764 {
765 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-beta2-api",
766 resourceExpirationEvaluator: resourceExpirationEvaluator{
767 currentVersion: apimachineryversion.MajorMinor(1, 20),
768 emulationForwardCompatible: true,
769 runtimeConfigEmulationForwardCompatible: true,
770 },
771 runtimeConfig: map[string]string{
772 groupName + "/v2beta2": "true",
773 },
774 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
775 "v1": {
776 resource1: storageIntroducedIn(1, 20),
777 },
778 "v2beta1": {
779 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
780 },
781 "v2beta2": {
782 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
783 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
784 },
785 "v2": {
786 resource1: storageIntroducedIn(1, 23),
787 resource2: storageIntroducedIn(1, 23),
788 },
789 },
790 expectedStorage: map[string]map[string]rest.Storage{
791 "v1": {
792 resource1: storageIntroducedIn(1, 20),
793 },
794 "v2beta2": {
795 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
796 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
797 },
798 "v2": {
799 resource1: storageIntroducedIn(1, 23),
800 resource2: storageIntroducedIn(1, 23),
801 },
802 },
803 },
804 {
805 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-alpha-api",
806 resourceExpirationEvaluator: resourceExpirationEvaluator{
807 currentVersion: apimachineryversion.MajorMinor(1, 19),
808 runtimeConfigEmulationForwardCompatible: true,
809 emulationForwardCompatible: true,
810 },
811 runtimeConfig: map[string]string{
812 groupName + "/v2alpha1": "true",
813 },
814 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
815 "v1": {
816 resource1: storageIntroducedIn(1, 19),
817 },
818 "v2alpha1": {
819 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
820 resource2: storageIntroducedAndRemovedIn(1, 20, 1, 21),
821 },
822 "v2beta1": {
823 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
824 },
825 "v2beta2": {
826 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
827 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
828 },
829 "v2": {
830 resource1: storageIntroducedIn(1, 23),
831 resource2: storageIntroducedIn(1, 23),
832 },
833 },
834 expectedStorage: map[string]map[string]rest.Storage{
835 "v1": {
836 resource1: storageIntroducedIn(1, 19),
837 },
838 "v2alpha1": {
839 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
840 resource2: storageIntroducedAndRemovedIn(1, 20, 1, 21),
841 },
842 "v2": {
843 resource1: storageIntroducedIn(1, 23),
844 },
845 },
846 },
847 {
848 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-alpha-resource",
849 resourceExpirationEvaluator: resourceExpirationEvaluator{
850 currentVersion: apimachineryversion.MajorMinor(1, 19),
851 runtimeConfigEmulationForwardCompatible: true,
852 emulationForwardCompatible: true,
853 },
854 runtimeConfig: map[string]string{
855 groupName + "/v2alpha1/resource2": "true",
856 },
857 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
858 "v1": {
859 resource1: storageIntroducedIn(1, 19),
860 },
861 "v2alpha1": {
862 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
863 resource2: storageIntroducedAndRemovedIn(1, 20, 1, 21),
864 },
865 "v2beta1": {
866 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
867 },
868 "v2beta2": {
869 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
870 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
871 },
872 "v2": {
873 resource1: storageIntroducedIn(1, 23),
874 resource2: storageIntroducedIn(1, 23),
875 },
876 },
877 expectedStorage: map[string]map[string]rest.Storage{
878 "v1": {
879 resource1: storageIntroducedIn(1, 19),
880 },
881 "v2alpha1": {
882 resource2: storageIntroducedAndRemovedIn(1, 20, 1, 21),
883 },
884 "v2": {
885 resource1: storageIntroducedIn(1, 23),
886 },
887 },
888 },
889 {
890 name: "emulation-forward-compatible-beta1-api",
891 resourceExpirationEvaluator: resourceExpirationEvaluator{
892 currentVersion: apimachineryversion.MajorMinor(1, 21),
893 emulationForwardCompatible: true,
894 },
895 runtimeConfig: map[string]string{
896 groupName + "/v2beta1": "true",
897 },
898 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
899 "v1": {
900 resource1: storageIntroducedIn(1, 20),
901 },
902 "v2beta1": {
903 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
904 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
905 },
906 "v2beta2": {
907 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
908 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
909 },
910 "v2": {
911 resource1: storageIntroducedIn(1, 23),
912 resource2: storageIntroducedIn(1, 23),
913 },
914 },
915 expectedStorage: map[string]map[string]rest.Storage{
916 "v1": {
917 resource1: storageIntroducedIn(1, 20),
918 },
919 "v2beta1": {
920 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
921 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
922 },
923 "v2beta2": {
924 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
925 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
926 },
927 "v2": {
928 resource1: storageIntroducedIn(1, 23),
929 resource2: storageIntroducedIn(1, 23),
930 },
931 },
932 },
933 {
934 name: "emulation-forward-compatible-beta1-resource",
935 resourceExpirationEvaluator: resourceExpirationEvaluator{
936 currentVersion: apimachineryversion.MajorMinor(1, 21),
937 emulationForwardCompatible: true,
938 },
939 runtimeConfig: map[string]string{
940 groupName + "/v2beta1/resource2": "true",
941 },
942 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
943 "v1": {
944 resource1: storageIntroducedIn(1, 20),
945 },
946 "v2beta1": {
947 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
948 },
949 "v2beta2": {
950 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
951 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
952 },
953 "v2": {
954 resource1: storageIntroducedIn(1, 23),
955 resource2: storageIntroducedIn(1, 23),
956 },
957 },
958 expectedStorage: map[string]map[string]rest.Storage{
959 "v1": {
960 resource1: storageIntroducedIn(1, 20),
961 },
962 "v2beta1": {
963 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
964 },
965 "v2beta2": {
966 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
967 },
968 "v2": {
969 resource1: storageIntroducedIn(1, 23),
970 resource2: storageIntroducedIn(1, 23),
971 },
972 },
973 },
974 {
975 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-beta1-api",
976 resourceExpirationEvaluator: resourceExpirationEvaluator{
977 currentVersion: apimachineryversion.MajorMinor(1, 20),
978 emulationForwardCompatible: true,
979 runtimeConfigEmulationForwardCompatible: true,
980 },
981 runtimeConfig: map[string]string{
982 groupName + "/v2beta1": "true",
983 },
984 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
985 "v1": {
986 resource1: storageIntroducedIn(1, 20),
987 },
988 "v2beta1": {
989 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
990 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
991 },
992 "v2beta2": {
993 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
994 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
995 },
996 "v2": {
997 resource1: storageIntroducedIn(1, 23),
998 resource2: storageIntroducedIn(1, 23),
999 },
1000 },
1001 expectedStorage: map[string]map[string]rest.Storage{
1002 "v1": {
1003 resource1: storageIntroducedIn(1, 20),
1004 },
1005 "v2beta1": {
1006 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1007 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1008 },
1009 "v2beta2": {
1010 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1011 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1012 },
1013 "v2": {
1014 resource1: storageIntroducedIn(1, 23),
1015 resource2: storageIntroducedIn(1, 23),
1016 },
1017 },
1018 },
1019 {
1020 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-beta1-resource",
1021 resourceExpirationEvaluator: resourceExpirationEvaluator{
1022 currentVersion: apimachineryversion.MajorMinor(1, 20),
1023 emulationForwardCompatible: true,
1024 runtimeConfigEmulationForwardCompatible: true,
1025 },
1026 runtimeConfig: map[string]string{
1027 groupName + "/v2beta1/resource2": "true",
1028 },
1029 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1030 "v1": {
1031 resource1: storageIntroducedIn(1, 20),
1032 },
1033 "v2beta1": {
1034 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1035 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1036 },
1037 "v2beta2": {
1038 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1039 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1040 },
1041 "v2": {
1042 resource1: storageIntroducedIn(1, 23),
1043 resource2: storageIntroducedIn(1, 23),
1044 },
1045 },
1046 expectedStorage: map[string]map[string]rest.Storage{
1047 "v1": {
1048 resource1: storageIntroducedIn(1, 20),
1049 },
1050 "v2beta1": {
1051 resource2: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1052 },
1053 "v2beta2": {
1054 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1055 },
1056 "v2": {
1057 resource1: storageIntroducedIn(1, 23),
1058 resource2: storageIntroducedIn(1, 23),
1059 },
1060 },
1061 },
1062 }
1063 for _, tt := range tests {
1064 t.Run(tt.name, func(t *testing.T) {
1065 resourceConfig := serverstorage.NewResourceConfig()
1066 convertor := &dummyConvertor{prioritizedVersions: []schema.GroupVersion{
1067 {Group: groupName, Version: "v2"}, {Group: groupName, Version: "v1"},
1068 {Group: groupName, Version: "v2beta1"},
1069 {Group: groupName, Version: "v2beta2"},
1070 {Group: groupName, Version: "v2alpha1"}}}
1071 resourceConfig.EnableVersions(convertor.PrioritizedVersionsForGroup(groupName)...)
1072 resourceConfig, err := resourceconfig.MergeAPIResourceConfigs(resourceConfig, tt.runtimeConfig, convertor)
1073 require.NoError(t, err)
1074 err = tt.resourceExpirationEvaluator.removeUnintroducedKinds(groupName, convertor, tt.versionedResourcesStorageMap, resourceConfig)
1075 if tt.expectErr {
1076 require.Error(t, err)
1077 return
1078 }
1079 require.NoError(t, err)
1080 if !reflect.DeepEqual(tt.expectedStorage, tt.versionedResourcesStorageMap) {
1081 t.Fatal(dump.Pretty(tt.versionedResourcesStorageMap))
1082 }
1083 })
1084 }
1085 }
1086
1087 func Test_RemoveUnavailableKinds(t *testing.T) {
1088 groupName := "group.name"
1089 resource1 := "resource1"
1090 resource2 := "resource2"
1091 tests := []struct {
1092 name string
1093 resourceExpirationEvaluator resourceExpirationEvaluator
1094 runtimeConfig map[string]string
1095 expectErr bool
1096 versionedResourcesStorageMap map[string]map[string]rest.Storage
1097 expectedStorage map[string]map[string]rest.Storage
1098 }{
1099 {
1100 name: "remove-future-version",
1101 resourceExpirationEvaluator: resourceExpirationEvaluator{
1102 currentVersion: apimachineryversion.MajorMinor(1, 21),
1103 },
1104 runtimeConfig: map[string]string{
1105 "api/beta": "true",
1106 groupName + "/v2beta1": "true",
1107 },
1108 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1109 "v1": {
1110 resource1: storageIntroducedIn(1, 18),
1111 },
1112 "v2alpha1": {
1113 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
1114 },
1115 "v2beta1": {
1116 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1117 },
1118 "v2beta2": {
1119 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1120 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1121 },
1122 "v2": {
1123 resource1: storageIntroducedIn(1, 23),
1124 resource2: storageIntroducedIn(1, 23),
1125 },
1126 },
1127 expectedStorage: map[string]map[string]rest.Storage{
1128 "v1": {
1129 resource1: storageIntroducedIn(1, 18),
1130 },
1131 "v2beta1": {
1132 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1133 },
1134 },
1135 },
1136 {
1137 name: "missing-introduced-version",
1138 resourceExpirationEvaluator: resourceExpirationEvaluator{
1139 currentVersion: apimachineryversion.MajorMinor(1, 20),
1140 },
1141 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1142 "v1": {
1143 resource1: storageRemovedIn(1, 23),
1144 },
1145 "v2": {
1146 resource1: storageIntroducedIn(1, 23),
1147 resource2: storageIntroducedIn(1, 23),
1148 },
1149 },
1150 expectedStorage: map[string]map[string]rest.Storage{
1151 "v1": {
1152 resource1: storageRemovedIn(1, 23),
1153 },
1154 },
1155 },
1156 {
1157 name: "emulation-forward-compatible-ga-api",
1158 resourceExpirationEvaluator: resourceExpirationEvaluator{
1159 currentVersion: apimachineryversion.MajorMinor(1, 19),
1160 emulationForwardCompatible: true,
1161 },
1162 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1163 "v1": {
1164 resource1: storageIntroducedIn(1, 18),
1165 },
1166 "v2alpha1": {
1167 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
1168 },
1169 "v2beta1": {
1170 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1171 },
1172 "v2beta2": {
1173 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1174 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1175 },
1176 "v2": {
1177 resource1: storageIntroducedIn(1, 23),
1178 resource2: storageIntroducedIn(1, 23),
1179 },
1180 },
1181 expectedStorage: map[string]map[string]rest.Storage{
1182 "v1": {
1183 resource1: storageIntroducedIn(1, 18),
1184 },
1185 "v2": {
1186 resource1: storageIntroducedIn(1, 23),
1187 },
1188 },
1189 },
1190 {
1191 name: "emulation-forward-compatible-alpha-api",
1192 resourceExpirationEvaluator: resourceExpirationEvaluator{
1193 currentVersion: apimachineryversion.MajorMinor(1, 20),
1194 emulationForwardCompatible: true,
1195 },
1196 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1197 "v1": {
1198 resource1: storageIntroducedIn(1, 18),
1199 },
1200 "v2alpha1": {
1201 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
1202 },
1203 "v2beta1": {
1204 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1205 },
1206 "v2beta2": {
1207 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1208 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1209 },
1210 "v2": {
1211 resource1: storageIntroducedIn(1, 23),
1212 resource2: storageIntroducedIn(1, 23),
1213 },
1214 },
1215 expectedStorage: map[string]map[string]rest.Storage{
1216 "v1": {
1217 resource1: storageIntroducedIn(1, 18),
1218 },
1219 "v2alpha1": {
1220 resource1: storageIntroducedAndRemovedIn(1, 20, 1, 21),
1221 },
1222 "v2": {
1223 resource1: storageIntroducedIn(1, 23),
1224 },
1225 },
1226 },
1227 {
1228 name: "emulation-forward-compatible-beta1-api",
1229 resourceExpirationEvaluator: resourceExpirationEvaluator{
1230 currentVersion: apimachineryversion.MajorMinor(1, 21),
1231 emulationForwardCompatible: true,
1232 },
1233 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1234 "v1": {
1235 resource1: storageIntroducedIn(1, 20),
1236 },
1237 "v2beta1": {
1238 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1239 },
1240 "v2beta2": {
1241 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1242 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1243 },
1244 "v2": {
1245 resource1: storageIntroducedIn(1, 23),
1246 resource2: storageIntroducedIn(1, 23),
1247 },
1248 },
1249 expectedStorage: map[string]map[string]rest.Storage{
1250 "v1": {
1251 resource1: storageIntroducedIn(1, 20),
1252 },
1253 "v2beta1": {
1254 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1255 },
1256 "v2beta2": {
1257 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1258 },
1259 "v2": {
1260 resource1: storageIntroducedIn(1, 23),
1261 },
1262 },
1263 },
1264 {
1265 name: "emulation-forward-compatible-new-resource",
1266 resourceExpirationEvaluator: resourceExpirationEvaluator{
1267 currentVersion: apimachineryversion.MajorMinor(1, 22),
1268 emulationForwardCompatible: true,
1269 },
1270 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1271 "v1": {
1272 resource1: storageIntroducedIn(1, 20),
1273 },
1274 "v2beta1": {
1275 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1276 },
1277 "v2beta2": {
1278 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1279 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1280 },
1281 "v2": {
1282 resource1: storageIntroducedIn(1, 23),
1283 resource2: storageIntroducedIn(1, 23),
1284 },
1285 },
1286 expectedStorage: map[string]map[string]rest.Storage{
1287 "v1": {
1288 resource1: storageIntroducedIn(1, 20),
1289 },
1290 "v2beta2": {
1291 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1292 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1293 },
1294 "v2": {
1295 resource1: storageIntroducedIn(1, 23),
1296 resource2: storageIntroducedIn(1, 23),
1297 },
1298 },
1299 },
1300 {
1301 name: "runtime-config-enable-future-version-err",
1302 resourceExpirationEvaluator: resourceExpirationEvaluator{
1303 currentVersion: apimachineryversion.MajorMinor(1, 20),
1304 },
1305 runtimeConfig: map[string]string{
1306 groupName + "/v2beta2": "true",
1307 },
1308 expectErr: true,
1309 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1310 "v1": {
1311 resource1: storageIntroducedIn(1, 20),
1312 },
1313 "v2beta1": {
1314 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1315 },
1316 "v2beta2": {
1317 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1318 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1319 },
1320 "v2": {
1321 resource1: storageIntroducedIn(1, 23),
1322 resource2: storageIntroducedIn(1, 23),
1323 },
1324 },
1325 },
1326 {
1327 name: "runtime-config-enable-future-resource-err",
1328 resourceExpirationEvaluator: resourceExpirationEvaluator{
1329 currentVersion: apimachineryversion.MajorMinor(1, 20),
1330 },
1331 runtimeConfig: map[string]string{
1332 groupName + "/v2beta2/resource2": "true",
1333 },
1334 expectErr: true,
1335 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1336 "v1": {
1337 resource1: storageIntroducedIn(1, 20),
1338 },
1339 "v2beta1": {
1340 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1341 },
1342 "v2beta2": {
1343 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1344 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1345 },
1346 "v2": {
1347 resource1: storageIntroducedIn(1, 23),
1348 resource2: storageIntroducedIn(1, 23),
1349 },
1350 },
1351 },
1352 {
1353 name: "runtime-config-emulation-forward-compatible-beta2-api",
1354 resourceExpirationEvaluator: resourceExpirationEvaluator{
1355 currentVersion: apimachineryversion.MajorMinor(1, 20),
1356 runtimeConfigEmulationForwardCompatible: true,
1357 },
1358 runtimeConfig: map[string]string{
1359 groupName + "/v2beta2": "true",
1360 },
1361 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1362 "v1": {
1363 resource1: storageIntroducedIn(1, 20),
1364 },
1365 "v2beta1": {
1366 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1367 },
1368 "v2beta2": {
1369 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1370 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1371 },
1372 "v2": {
1373 resource1: storageIntroducedIn(1, 23),
1374 resource2: storageIntroducedIn(1, 23),
1375 },
1376 },
1377 expectedStorage: map[string]map[string]rest.Storage{
1378 "v1": {
1379 resource1: storageIntroducedIn(1, 20),
1380 },
1381 "v2beta2": {
1382 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1383 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1384 },
1385 },
1386 },
1387 {
1388 name: "runtime-config-emulation-forward-compatible-beta2-resource",
1389 resourceExpirationEvaluator: resourceExpirationEvaluator{
1390 currentVersion: apimachineryversion.MajorMinor(1, 20),
1391 runtimeConfigEmulationForwardCompatible: true,
1392 },
1393 runtimeConfig: map[string]string{
1394 groupName + "/v2beta2/resource2": "true",
1395 },
1396 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1397 "v1": {
1398 resource1: storageIntroducedIn(1, 20),
1399 },
1400 "v2beta1": {
1401 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1402 },
1403 "v2beta2": {
1404 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1405 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1406 },
1407 "v2": {
1408 resource1: storageIntroducedIn(1, 23),
1409 resource2: storageIntroducedIn(1, 23),
1410 },
1411 },
1412 expectedStorage: map[string]map[string]rest.Storage{
1413 "v1": {
1414 resource1: storageIntroducedIn(1, 20),
1415 },
1416 "v2beta2": {
1417 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1418 },
1419 },
1420 },
1421 {
1422 name: "emulation-forward-compatible-runtime-config-beta2-api",
1423 resourceExpirationEvaluator: resourceExpirationEvaluator{
1424 currentVersion: apimachineryversion.MajorMinor(1, 20),
1425 emulationForwardCompatible: true,
1426 },
1427 runtimeConfig: map[string]string{
1428 groupName + "/v2beta2": "true",
1429 },
1430 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1431 "v1": {
1432 resource1: storageIntroducedIn(1, 20),
1433 },
1434 "v2beta1": {
1435 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1436 },
1437 "v2beta2": {
1438 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1439 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1440 },
1441 "v2": {
1442 resource1: storageIntroducedIn(1, 23),
1443 resource2: storageIntroducedIn(1, 23),
1444 },
1445 },
1446 expectErr: true,
1447 },
1448 {
1449 name: "both-runtime-config-and-emulation-forward-compatible-runtime-config-beta2-api",
1450 resourceExpirationEvaluator: resourceExpirationEvaluator{
1451 currentVersion: apimachineryversion.MajorMinor(1, 20),
1452 emulationForwardCompatible: true,
1453 runtimeConfigEmulationForwardCompatible: true,
1454 },
1455 runtimeConfig: map[string]string{
1456 groupName + "/v2beta2": "true",
1457 },
1458 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1459 "v1": {
1460 resource1: storageIntroducedIn(1, 20),
1461 },
1462 "v2beta1": {
1463 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1464 },
1465 "v2beta2": {
1466 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1467 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1468 },
1469 "v2": {
1470 resource1: storageIntroducedIn(1, 23),
1471 resource2: storageIntroducedIn(1, 23),
1472 },
1473 },
1474 expectedStorage: map[string]map[string]rest.Storage{
1475 "v1": {
1476 resource1: storageIntroducedIn(1, 20),
1477 },
1478 "v2beta2": {
1479 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1480 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1481 },
1482 "v2": {
1483 resource1: storageIntroducedIn(1, 23),
1484 resource2: storageIntroducedIn(1, 23),
1485 },
1486 },
1487 },
1488 {
1489 name: "emulation-forward-compatible-runtime-config-beta2-api-resource1-ok",
1490 resourceExpirationEvaluator: resourceExpirationEvaluator{
1491 currentVersion: apimachineryversion.MajorMinor(1, 21),
1492 emulationForwardCompatible: true,
1493 },
1494 runtimeConfig: map[string]string{
1495 groupName + "/v2beta2": "true",
1496 },
1497 versionedResourcesStorageMap: map[string]map[string]rest.Storage{
1498 "v1": {
1499 resource1: storageIntroducedIn(1, 20),
1500 },
1501 "v2beta1": {
1502 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1503 },
1504 "v2beta2": {
1505 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1506 resource2: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1507 },
1508 "v2": {
1509 resource1: storageIntroducedIn(1, 23),
1510 resource2: storageIntroducedIn(1, 23),
1511 },
1512 },
1513 expectedStorage: map[string]map[string]rest.Storage{
1514 "v1": {
1515 resource1: storageIntroducedIn(1, 20),
1516 },
1517 "v2beta1": {
1518 resource1: storageIntroducedAndRemovedIn(1, 21, 1, 22),
1519 },
1520 "v2beta2": {
1521 resource1: storageIntroducedAndRemovedIn(1, 22, 1, 23),
1522 },
1523 "v2": {
1524 resource1: storageIntroducedIn(1, 23),
1525 },
1526 },
1527 },
1528 }
1529 for _, tt := range tests {
1530 t.Run(tt.name, func(t *testing.T) {
1531 resourceConfig := serverstorage.NewResourceConfig()
1532 convertor := &dummyConvertor{prioritizedVersions: []schema.GroupVersion{
1533 {Group: groupName, Version: "v2"}, {Group: groupName, Version: "v1"},
1534 {Group: groupName, Version: "v2beta2"}, {Group: groupName, Version: "v2beta1"},
1535 {Group: groupName, Version: "v2alpha1"}}}
1536 resourceConfig.EnableVersions(convertor.PrioritizedVersionsForGroup(groupName)...)
1537 resourceConfig, err := resourceconfig.MergeAPIResourceConfigs(resourceConfig, tt.runtimeConfig, convertor)
1538 require.NoError(t, err)
1539 err = tt.resourceExpirationEvaluator.RemoveUnavailableKinds(groupName, convertor, tt.versionedResourcesStorageMap, resourceConfig)
1540 if tt.expectErr {
1541 require.Error(t, err)
1542 return
1543 }
1544 require.NoError(t, err)
1545 if !reflect.DeepEqual(tt.expectedStorage, tt.versionedResourcesStorageMap) {
1546 t.Fatal(dump.Pretty(tt.versionedResourcesStorageMap))
1547 }
1548 })
1549 }
1550 }
1551
1552 func Test_shouldRemoveResource(t *testing.T) {
1553 tests := []struct {
1554 name string
1555 resourcesToRemove sets.String
1556 resourceName string
1557 want bool
1558 }{
1559 {
1560 name: "prefix-matches",
1561 resourcesToRemove: sets.NewString("foo"),
1562 resourceName: "foo/scale",
1563 want: true,
1564 },
1565 {
1566 name: "exact-matches",
1567 resourcesToRemove: sets.NewString("foo"),
1568 resourceName: "foo",
1569 want: true,
1570 },
1571 {
1572 name: "no-match",
1573 resourcesToRemove: sets.NewString("foo"),
1574 resourceName: "bar",
1575 want: false,
1576 },
1577 }
1578 for _, tt := range tests {
1579 t.Run(tt.name, func(t *testing.T) {
1580 if actual := shouldRemoveResourceAndSubresources(tt.resourcesToRemove, tt.resourceName); actual != tt.want {
1581 t.Errorf("shouldRemoveResourceAndSubresources() = %v, want %v", actual, tt.want)
1582 }
1583 })
1584 }
1585 }