github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/apitesting/codec.go (about) 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 apitesting 18 19 import ( 20 "fmt" 21 "mime" 22 "os" 23 24 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 25 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 26 runtimeserializer "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/serializer" 27 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/serializer/recognizer" 28 ) 29 30 var ( 31 testCodecMediaType string 32 testStorageCodecMediaType string 33 ) 34 35 // TestCodec returns the codec for the API version to test against, as set by the 36 // KUBE_TEST_API_TYPE env var. 37 func TestCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec { 38 if len(testCodecMediaType) != 0 { 39 serializerInfo, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), testCodecMediaType) 40 if !ok { 41 panic(fmt.Sprintf("no serializer for %s", testCodecMediaType)) 42 } 43 return codecs.CodecForVersions(serializerInfo.Serializer, codecs.UniversalDeserializer(), schema.GroupVersions(gvs), nil) 44 } 45 return codecs.LegacyCodec(gvs...) 46 } 47 48 // TestStorageCodec returns the codec for the API version to test against used in storage, as set by the 49 // KUBE_TEST_API_STORAGE_TYPE env var. 50 func TestStorageCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec { 51 if len(testStorageCodecMediaType) != 0 { 52 serializerInfo, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), testStorageCodecMediaType) 53 if !ok { 54 panic(fmt.Sprintf("no serializer for %s", testStorageCodecMediaType)) 55 } 56 57 // etcd2 only supports string data - we must wrap any result before returning 58 // TODO: remove for etcd3 / make parameterizable 59 serializer := serializerInfo.Serializer 60 if !serializerInfo.EncodesAsText { 61 serializer = runtime.NewBase64Serializer(serializer, serializer) 62 } 63 64 decoder := recognizer.NewDecoder(serializer, codecs.UniversalDeserializer()) 65 return codecs.CodecForVersions(serializer, decoder, schema.GroupVersions(gvs), nil) 66 67 } 68 return codecs.LegacyCodec(gvs...) 69 } 70 71 func init() { 72 var err error 73 if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 { 74 testCodecMediaType, _, err = mime.ParseMediaType(apiMediaType) 75 if err != nil { 76 panic(err) 77 } 78 } 79 80 if storageMediaType := os.Getenv("KUBE_TEST_API_STORAGE_TYPE"); len(storageMediaType) > 0 { 81 testStorageCodecMediaType, _, err = mime.ParseMediaType(storageMediaType) 82 if err != nil { 83 panic(err) 84 } 85 } 86 } 87 88 // InstallOrDieFunc mirrors install functions that require success 89 type InstallOrDieFunc func(scheme *runtime.Scheme) 90 91 // SchemeForInstallOrDie builds a simple test scheme and codecfactory pair for easy unit testing from higher level install methods 92 func SchemeForInstallOrDie(installFns ...InstallOrDieFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) { 93 scheme := runtime.NewScheme() 94 codecFactory := runtimeserializer.NewCodecFactory(scheme) 95 for _, installFn := range installFns { 96 installFn(scheme) 97 } 98 99 return scheme, codecFactory 100 } 101 102 // InstallFunc mirrors install functions that can return an error 103 type InstallFunc func(scheme *runtime.Scheme) error 104 105 // SchemeForOrDie builds a simple test scheme and codecfactory pair for easy unit testing from the bare registration methods. 106 func SchemeForOrDie(installFns ...InstallFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) { 107 scheme := runtime.NewScheme() 108 codecFactory := runtimeserializer.NewCodecFactory(scheme) 109 for _, installFn := range installFns { 110 if err := installFn(scheme); err != nil { 111 panic(err) 112 } 113 } 114 115 return scheme, codecFactory 116 }