Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 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 | ) |
| 25 | |
| 26 | type ResourceEncodingConfig interface { |
| 27 | // StorageEncoding returns the serialization format for the resource. |
| 28 | // TODO this should actually return a GroupVersionKind since you can logically have multiple "matching" Kinds |
| 29 | // For now, it returns just the GroupVersion for consistency with old behavior |
| 30 | StorageEncodingFor(schema.GroupResource) (schema.GroupVersion, error) |
| 31 | |
| 32 | // InMemoryEncodingFor returns the groupVersion for the in memory representation the storage should convert to. |
| 33 | InMemoryEncodingFor(schema.GroupResource) (schema.GroupVersion, error) |
| 34 | } |
| 35 | |
| 36 | type DefaultResourceEncodingConfig struct { |
| 37 | groups map[string]*GroupResourceEncodingConfig |
| 38 | scheme *runtime.Scheme |
| 39 | } |
| 40 | |
| 41 | type GroupResourceEncodingConfig struct { |
| 42 | DefaultExternalEncoding schema.GroupVersion |
| 43 | ExternalResourceEncodings map[string]schema.GroupVersion |
| 44 | |
| 45 | DefaultInternalEncoding schema.GroupVersion |
| 46 | InternalResourceEncodings map[string]schema.GroupVersion |
| 47 | } |
| 48 | |
| 49 | var _ ResourceEncodingConfig = &DefaultResourceEncodingConfig{} |
| 50 | |
| 51 | func NewDefaultResourceEncodingConfig(scheme *runtime.Scheme) *DefaultResourceEncodingConfig { |
| 52 | return &DefaultResourceEncodingConfig{groups: map[string]*GroupResourceEncodingConfig{}, scheme: scheme} |
| 53 | } |
| 54 | |
| 55 | func newGroupResourceEncodingConfig(defaultEncoding, defaultInternalVersion schema.GroupVersion) *GroupResourceEncodingConfig { |
| 56 | return &GroupResourceEncodingConfig{ |
| 57 | DefaultExternalEncoding: defaultEncoding, ExternalResourceEncodings: map[string]schema.GroupVersion{}, |
| 58 | DefaultInternalEncoding: defaultInternalVersion, InternalResourceEncodings: map[string]schema.GroupVersion{}, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func (o *DefaultResourceEncodingConfig) SetVersionEncoding(group string, externalEncodingVersion, internalVersion schema.GroupVersion) { |
| 63 | _, groupExists := o.groups[group] |
| 64 | if !groupExists { |
| 65 | o.groups[group] = newGroupResourceEncodingConfig(externalEncodingVersion, internalVersion) |
| 66 | } |
| 67 | |
| 68 | o.groups[group].DefaultExternalEncoding = externalEncodingVersion |
| 69 | o.groups[group].DefaultInternalEncoding = internalVersion |
| 70 | } |
| 71 | |
| 72 | func (o *DefaultResourceEncodingConfig) SetResourceEncoding(resourceBeingStored schema.GroupResource, externalEncodingVersion, internalVersion schema.GroupVersion) { |
| 73 | group := resourceBeingStored.Group |
| 74 | _, groupExists := o.groups[group] |
| 75 | if !groupExists { |
| 76 | o.groups[group] = newGroupResourceEncodingConfig(externalEncodingVersion, internalVersion) |
| 77 | } |
| 78 | |
| 79 | o.groups[group].ExternalResourceEncodings[resourceBeingStored.Resource] = externalEncodingVersion |
| 80 | o.groups[group].InternalResourceEncodings[resourceBeingStored.Resource] = internalVersion |
| 81 | } |
| 82 | |
| 83 | func (o *DefaultResourceEncodingConfig) StorageEncodingFor(resource schema.GroupResource) (schema.GroupVersion, error) { |
| 84 | if !o.scheme.IsGroupRegistered(resource.Group) { |
| 85 | return schema.GroupVersion{}, fmt.Errorf("group %q is not registered in scheme", resource.Group) |
| 86 | } |
| 87 | |
| 88 | groupEncoding, groupExists := o.groups[resource.Group] |
| 89 | |
| 90 | if !groupExists { |
| 91 | // return the most preferred external version for the group |
| 92 | return o.scheme.PrioritizedVersionsForGroup(resource.Group)[0], nil |
| 93 | } |
| 94 | |
| 95 | resourceOverride, resourceExists := groupEncoding.ExternalResourceEncodings[resource.Resource] |
| 96 | if !resourceExists { |
| 97 | return groupEncoding.DefaultExternalEncoding, nil |
| 98 | } |
| 99 | |
| 100 | return resourceOverride, nil |
| 101 | } |
| 102 | |
| 103 | func (o *DefaultResourceEncodingConfig) InMemoryEncodingFor(resource schema.GroupResource) (schema.GroupVersion, error) { |
| 104 | if !o.scheme.IsGroupRegistered(resource.Group) { |
| 105 | return schema.GroupVersion{}, fmt.Errorf("group %q is not registered in scheme", resource.Group) |
| 106 | } |
| 107 | |
| 108 | groupEncoding, groupExists := o.groups[resource.Group] |
| 109 | if !groupExists { |
| 110 | return schema.GroupVersion{Group: resource.Group, Version: runtime.APIVersionInternal}, nil |
| 111 | } |
| 112 | |
| 113 | resourceOverride, resourceExists := groupEncoding.InternalResourceEncodings[resource.Resource] |
| 114 | if !resourceExists { |
| 115 | return groupEncoding.DefaultInternalEncoding, nil |
| 116 | } |
| 117 | |
| 118 | return resourceOverride, nil |
| 119 | } |