|
| 1 | +/* |
| 2 | +Copyright 2024 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 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + runtimetesting "k8s.io/apimachinery/pkg/runtime/testing" |
| 25 | + "k8s.io/apimachinery/pkg/test" |
| 26 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 27 | + utilversion "k8s.io/apimachinery/pkg/util/version" |
| 28 | + "k8s.io/component-base/version" |
| 29 | +) |
| 30 | + |
| 31 | +func TestEmulatedStorageVersion(t *testing.T) { |
| 32 | + cases := []struct { |
| 33 | + name string |
| 34 | + scheme *runtime.Scheme |
| 35 | + binaryVersion schema.GroupVersion |
| 36 | + effectiveVersion version.EffectiveVersion |
| 37 | + want schema.GroupVersion |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "pick compatible", |
| 41 | + scheme: AlphaBetaScheme(utilversion.MustParse("1.31"), utilversion.MustParse("1.32")), |
| 42 | + binaryVersion: v1beta1, |
| 43 | + effectiveVersion: version.NewEffectiveVersion("1.32"), |
| 44 | + want: v1alpha1, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "alpha has been replaced, pick binary version", |
| 48 | + scheme: AlphaReplacedBetaScheme(utilversion.MustParse("1.31"), utilversion.MustParse("1.32")), |
| 49 | + binaryVersion: v1beta1, |
| 50 | + effectiveVersion: version.NewEffectiveVersion("1.32"), |
| 51 | + want: v1beta1, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tc := range cases { |
| 56 | + test.TestScheme() |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + found, err := emulatedStorageVersion(tc.binaryVersion, &CronJob{}, tc.effectiveVersion, tc.scheme) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("unexpected error: %v", err) |
| 61 | + } |
| 62 | + if found != tc.want { |
| 63 | + t.Errorf("got %v; want %v", found, tc.want) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +var internalGV = schema.GroupVersion{Group: "workload.example.com", Version: runtime.APIVersionInternal} |
| 70 | +var v1alpha1 = schema.GroupVersion{Group: "workload.example.com", Version: "v1alpha1"} |
| 71 | +var v1beta1 = schema.GroupVersion{Group: "workload.example.com", Version: "v1beta1"} |
| 72 | + |
| 73 | +type CronJobWithReplacement struct { |
| 74 | + introduced *utilversion.Version |
| 75 | + A string `json:"A,omitempty"` |
| 76 | + B int `json:"B,omitempty"` |
| 77 | +} |
| 78 | + |
| 79 | +func (*CronJobWithReplacement) GetObjectKind() schema.ObjectKind { panic("not implemented") } |
| 80 | +func (*CronJobWithReplacement) DeepCopyObject() runtime.Object { |
| 81 | + panic("not implemented") |
| 82 | +} |
| 83 | + |
| 84 | +func (in *CronJobWithReplacement) APILifecycleIntroduced() (major, minor int) { |
| 85 | + if in.introduced == nil { |
| 86 | + return 0, 0 |
| 87 | + } |
| 88 | + return int(in.introduced.Major()), int(in.introduced.Minor()) |
| 89 | +} |
| 90 | + |
| 91 | +func (in *CronJobWithReplacement) APILifecycleReplacement() schema.GroupVersionKind { |
| 92 | + return schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"} |
| 93 | +} |
| 94 | + |
| 95 | +type CronJob struct { |
| 96 | + introduced *utilversion.Version |
| 97 | + A string `json:"A,omitempty"` |
| 98 | + B int `json:"B,omitempty"` |
| 99 | +} |
| 100 | + |
| 101 | +func (*CronJob) GetObjectKind() schema.ObjectKind { panic("not implemented") } |
| 102 | +func (*CronJob) DeepCopyObject() runtime.Object { |
| 103 | + panic("not implemented") |
| 104 | +} |
| 105 | + |
| 106 | +func (in *CronJob) APILifecycleIntroduced() (major, minor int) { |
| 107 | + if in.introduced == nil { |
| 108 | + return 0, 0 |
| 109 | + } |
| 110 | + return int(in.introduced.Major()), int(in.introduced.Minor()) |
| 111 | +} |
| 112 | + |
| 113 | +func AlphaBetaScheme(alphaVersion, betaVersion *utilversion.Version) *runtime.Scheme { |
| 114 | + s := runtime.NewScheme() |
| 115 | + s.AddKnownTypes(internalGV, &CronJob{}) |
| 116 | + s.AddKnownTypes(v1alpha1, &CronJob{introduced: alphaVersion}) |
| 117 | + s.AddKnownTypes(v1beta1, &CronJob{introduced: betaVersion}) |
| 118 | + s.AddKnownTypeWithName(internalGV.WithKind("CronJob"), &CronJob{}) |
| 119 | + s.AddKnownTypeWithName(v1alpha1.WithKind("CronJob"), &CronJob{introduced: alphaVersion}) |
| 120 | + s.AddKnownTypeWithName(v1beta1.WithKind("CronJob"), &CronJob{introduced: betaVersion}) |
| 121 | + utilruntime.Must(runtimetesting.RegisterConversions(s)) |
| 122 | + return s |
| 123 | +} |
| 124 | + |
| 125 | +func AlphaReplacedBetaScheme(alphaVersion, betaVersion *utilversion.Version) *runtime.Scheme { |
| 126 | + s := runtime.NewScheme() |
| 127 | + s.AddKnownTypes(internalGV, &CronJob{}) |
| 128 | + s.AddKnownTypes(v1alpha1, &CronJobWithReplacement{introduced: alphaVersion}) |
| 129 | + s.AddKnownTypes(v1beta1, &CronJob{introduced: betaVersion}) |
| 130 | + s.AddKnownTypeWithName(internalGV.WithKind("CronJob"), &CronJob{}) |
| 131 | + s.AddKnownTypeWithName(v1alpha1.WithKind("CronJob"), &CronJobWithReplacement{introduced: alphaVersion}) |
| 132 | + s.AddKnownTypeWithName(v1beta1.WithKind("CronJob"), &CronJob{introduced: betaVersion}) |
| 133 | + utilruntime.Must(runtimetesting.RegisterConversions(s)) |
| 134 | + return s |
| 135 | +} |
0 commit comments