Debian Codegraph @

pkg/server/storage/resource_encoding_config.go

raw
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 "fmt"
21
22 "k8s.io/apimachinery/pkg/runtime"
23 "k8s.io/apimachinery/pkg/runtime/schema"
24 apimachineryversion "k8s.io/apimachinery/pkg/util/version"
25 "k8s.io/apiserver/pkg/util/compatibility"
26 basecompatibility "k8s.io/component-base/compatibility"
27 )
28
29 type ResourceEncodingConfig interface {
30 // StorageEncoding returns the serialization format for the resource.
31 // TODO this should actually return a GroupVersionKind since you can logically have multiple "matching" Kinds
32 // For now, it returns just the GroupVersion for consistency with old behavior
33 StorageEncodingFor(schema.GroupResource) (schema.GroupVersion, error)
34
35 // InMemoryEncodingFor returns the groupVersion for the in memory representation the storage should convert to.
36 InMemoryEncodingFor(schema.GroupResource) (schema.GroupVersion, error)
37 }
38
39 type CompatibilityResourceEncodingConfig interface {
40 BackwardCompatibileStorageEncodingFor(schema.GroupResource, runtime.Object) (schema.GroupVersion, error)
41 }
42
43 type DefaultResourceEncodingConfig struct {
44 // resources records the overriding encoding configs for individual resources.
45 resources map[schema.GroupResource]*OverridingResourceEncoding
46 scheme *runtime.Scheme
47 effectiveVersion basecompatibility.EffectiveVersion
48 }
49
50 type OverridingResourceEncoding struct {
51 ExternalResourceEncoding schema.GroupVersion
52 InternalResourceEncoding schema.GroupVersion
53 }
54
55 var _ ResourceEncodingConfig = &DefaultResourceEncodingConfig{}
56
57 func NewDefaultResourceEncodingConfig(scheme *runtime.Scheme) *DefaultResourceEncodingConfig {
58 return NewDefaultResourceEncodingConfigForEffectiveVersion(scheme, compatibility.DefaultComponentGlobalsRegistry.EffectiveVersionFor(basecompatibility.DefaultKubeComponent))
59 }
60
61 func NewDefaultResourceEncodingConfigForEffectiveVersion(scheme *runtime.Scheme, effectiveVersion basecompatibility.EffectiveVersion) *DefaultResourceEncodingConfig {
62 return &DefaultResourceEncodingConfig{resources: map[schema.GroupResource]*OverridingResourceEncoding{}, scheme: scheme, effectiveVersion: effectiveVersion}
63 }
64
65 func (o *DefaultResourceEncodingConfig) SetResourceEncoding(resourceBeingStored schema.GroupResource, externalEncodingVersion, internalVersion schema.GroupVersion) {
66 o.resources[resourceBeingStored] = &OverridingResourceEncoding{
67 ExternalResourceEncoding: externalEncodingVersion,
68 InternalResourceEncoding: internalVersion,
69 }
70 }
71
72 func (o *DefaultResourceEncodingConfig) SetEffectiveVersion(effectiveVersion basecompatibility.EffectiveVersion) {
73 o.effectiveVersion = effectiveVersion
74 }
75
76 func (o *DefaultResourceEncodingConfig) StorageEncodingFor(resource schema.GroupResource) (schema.GroupVersion, error) {
77 if !o.scheme.IsGroupRegistered(resource.Group) {
78 return schema.GroupVersion{}, fmt.Errorf("group %q is not registered in scheme", resource.Group)
79 }
80
81 resourceOverride, resourceExists := o.resources[resource]
82 if resourceExists {
83 return resourceOverride.ExternalResourceEncoding, nil
84 }
85
86 // return the most preferred external version for the group
87 return o.scheme.PrioritizedVersionsForGroup(resource.Group)[0], nil
88 }
89
90 func (o *DefaultResourceEncodingConfig) BackwardCompatibileStorageEncodingFor(resource schema.GroupResource, example runtime.Object) (schema.GroupVersion, error) {
91 if !o.scheme.IsGroupRegistered(resource.Group) {
92 return schema.GroupVersion{}, fmt.Errorf("group %q is not registered in scheme", resource.Group)
93 }
94
95 // Always respect overrides
96 resourceOverride, resourceExists := o.resources[resource]
97 if resourceExists {
98 return resourceOverride.ExternalResourceEncoding, nil
99 }
100
101 return emulatedStorageVersion(
102 o.scheme.PrioritizedVersionsForGroup(resource.Group)[0],
103 example,
104 o.effectiveVersion,
105 o.scheme)
106 }
107
108 func (o *DefaultResourceEncodingConfig) InMemoryEncodingFor(resource schema.GroupResource) (schema.GroupVersion, error) {
109 if !o.scheme.IsGroupRegistered(resource.Group) {
110 return schema.GroupVersion{}, fmt.Errorf("group %q is not registered in scheme", resource.Group)
111 }
112
113 resourceOverride, resourceExists := o.resources[resource]
114 if resourceExists {
115 return resourceOverride.InternalResourceEncoding, nil
116 }
117 return schema.GroupVersion{Group: resource.Group, Version: runtime.APIVersionInternal}, nil
118 }
119
120 // Object interface generated from "k8s:prerelease-lifecycle-gen:introduced" tags in types.go.
121 type introducedInterface interface {
122 APILifecycleIntroduced() (major, minor int)
123 }
124
125 type replacementInterface interface {
126 APILifecycleReplacement() schema.GroupVersionKind
127 }
128
129 func emulatedStorageVersion(binaryVersionOfResource schema.GroupVersion, example runtime.Object, effectiveVersion basecompatibility.EffectiveVersion, scheme *runtime.Scheme) (schema.GroupVersion, error) {
130 if example == nil || effectiveVersion == nil {
131 return binaryVersionOfResource, nil
132 }
133
134 // Look up example in scheme to find all objects of the same Group-Kind
135 // Use the highest priority version for that group-kind whose lifecycle window
136 // includes the current emulation version.
137 // If no version is found, use the binary version
138 // (in this case the API should be disabled anyway)
139 gvks, _, err := scheme.ObjectKinds(example)
140 if err != nil {
141 return schema.GroupVersion{}, err
142 }
143
144 var gvk schema.GroupVersionKind
145 for _, item := range gvks {
146 if item.Group != binaryVersionOfResource.Group {
147 continue
148 }
149
150 gvk = item
151 break
152 }
153
154 if len(gvk.Kind) == 0 {
155 return schema.GroupVersion{}, fmt.Errorf("object %T has no GVKs registered in scheme", example)
156 }
157
158 // VersionsForGroupKind returns versions in priority order
159 versions := scheme.VersionsForGroupKind(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind})
160
161 compatibilityVersion := effectiveVersion.MinCompatibilityVersion()
162
163 for _, gv := range versions {
164 if gv.Version == runtime.APIVersionInternal {
165 continue
166 }
167
168 gvk := schema.GroupVersionKind{
169 Group: gv.Group,
170 Version: gv.Version,
171 Kind: gvk.Kind,
172 }
173
174 exampleOfGVK, err := scheme.New(gvk)
175 if err != nil {
176 return schema.GroupVersion{}, err
177 }
178
179 // If it was introduced after current compatibility version, don't use it
180 // skip the introduced check for test when current compatibility version is 0.0 to test all apis
181 if introduced, hasIntroduced := exampleOfGVK.(introducedInterface); hasIntroduced && (compatibilityVersion.Major() > 0 || compatibilityVersion.Minor() > 0) {
182
183 // Skip versions that have a replacement.
184 // This can be used to override this storage version selection by
185 // marking a storage version has having a replacement and preventing a
186 // that storage version from being selected.
187 if _, hasReplacement := exampleOfGVK.(replacementInterface); hasReplacement {
188 continue
189 }
190 // API resource lifecycles should be relative to k8s api version
191 majorIntroduced, minorIntroduced := introduced.APILifecycleIntroduced()
192 introducedVer := apimachineryversion.MajorMinor(uint(majorIntroduced), uint(minorIntroduced))
193 if introducedVer.GreaterThan(compatibilityVersion) {
194 continue
195 }
196 }
197
198 // versions is returned in priority order, so just use first result
199 return gvk.GroupVersion(), nil
200 }
201
202 // Getting here means we're serving a version that is unknown to the
203 // min-compatibility-version server.
204 //
205 // This is only expected to happen when serving an alpha API type due
206 // to missing pre-release lifecycle information
207 // (which doesn't happen by default), or when emulation-version and
208 // min-compatibility-version are several versions apart so a beta or GA API
209 // was being served which didn't exist at all in min-compatibility-version.
210 //
211 // In the alpha case - we do not support compatibility versioning of
212 // alpha types and recommend users do not mix the two.
213 // In the skip-level case - The version of apiserver we are retaining
214 // compatibility with has no knowledge of the type,
215 // so storing it in another type is no issue.
216 return binaryVersionOfResource, nil
217 }