Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 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 internalversion |
| 18 | |
| 19 | import ( |
| 20 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1" |
| 22 | "k8s.io/apimachinery/pkg/runtime" |
| 23 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | "k8s.io/apimachinery/pkg/runtime/serializer" |
| 25 | ) |
| 26 | |
| 27 | // GroupName is the group name for this API. |
| 28 | const GroupName = "meta.k8s.io" |
| 29 | |
| 30 | // Scheme is the registry for any type that adheres to the meta API spec. |
| 31 | var scheme = runtime.NewScheme() |
| 32 | |
| 33 | var ( |
| 34 | // TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api. |
| 35 | // localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes. |
| 36 | SchemeBuilder runtime.SchemeBuilder |
| 37 | localSchemeBuilder = &SchemeBuilder |
| 38 | AddToScheme = localSchemeBuilder.AddToScheme |
| 39 | ) |
| 40 | |
| 41 | // Codecs provides access to encoding and decoding for the scheme. |
| 42 | var Codecs = serializer.NewCodecFactory(scheme) |
| 43 | |
| 44 | // SchemeGroupVersion is group version used to register these objects |
| 45 | var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal} |
| 46 | |
| 47 | // ParameterCodec handles versioning of objects that are converted to query parameters. |
| 48 | var ParameterCodec = runtime.NewParameterCodec(scheme) |
| 49 | |
| 50 | // Kind takes an unqualified kind and returns a Group qualified GroupKind |
| 51 | func Kind(kind string) schema.GroupKind { |
| 52 | return SchemeGroupVersion.WithKind(kind).GroupKind() |
| 53 | } |
| 54 | |
| 55 | // addToGroupVersion registers common meta types into schemas. |
| 56 | func addToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion) error { |
| 57 | if err := scheme.AddIgnoredConversionType(&metav1.TypeMeta{}, &metav1.TypeMeta{}); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | scheme.AddConversionFuncs( |
| 61 | metav1.Convert_string_To_labels_Selector, |
| 62 | metav1.Convert_labels_Selector_To_string, |
| 63 | |
| 64 | metav1.Convert_string_To_fields_Selector, |
| 65 | metav1.Convert_fields_Selector_To_string, |
| 66 | |
| 67 | Convert_map_to_v1_LabelSelector, |
| 68 | Convert_v1_LabelSelector_to_map, |
| 69 | |
| 70 | Convert_internalversion_ListOptions_To_v1_ListOptions, |
| 71 | Convert_v1_ListOptions_To_internalversion_ListOptions, |
| 72 | ) |
| 73 | // ListOptions is the only options struct which needs conversion (it exposes labels and fields |
| 74 | // as selectors for convenience). The other types have only a single representation today. |
| 75 | scheme.AddKnownTypes(SchemeGroupVersion, |
| 76 | &ListOptions{}, |
| 77 | &metav1.GetOptions{}, |
| 78 | &metav1.ExportOptions{}, |
| 79 | &metav1.DeleteOptions{}, |
| 80 | ) |
| 81 | scheme.AddKnownTypes(SchemeGroupVersion, |
| 82 | &metav1beta1.Table{}, |
| 83 | &metav1beta1.TableOptions{}, |
| 84 | &metav1beta1.PartialObjectMetadata{}, |
| 85 | &metav1beta1.PartialObjectMetadataList{}, |
| 86 | ) |
| 87 | scheme.AddKnownTypes(metav1beta1.SchemeGroupVersion, |
| 88 | &metav1beta1.Table{}, |
| 89 | &metav1beta1.TableOptions{}, |
| 90 | &metav1beta1.PartialObjectMetadata{}, |
| 91 | &metav1beta1.PartialObjectMetadataList{}, |
| 92 | ) |
| 93 | // Allow delete options to be decoded across all version in this scheme (we may want to be more clever than this) |
| 94 | scheme.AddUnversionedTypes(SchemeGroupVersion, &metav1.DeleteOptions{}) |
| 95 | metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // Unlike other API groups, meta internal knows about all meta external versions, but keeps |
| 100 | // the logic for conversion private. |
| 101 | func init() { |
| 102 | if err := addToGroupVersion(scheme, SchemeGroupVersion); err != nil { |
| 103 | panic(err) |
| 104 | } |
| 105 | } |