github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/negotiate.go (about) 1 /* 2 Copyright 2019 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 runtime 18 19 import ( 20 "fmt" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 23 ) 24 25 // NegotiateError is returned when a ClientNegotiator is unable to locate 26 // a serializer for the requested operation. 27 type NegotiateError struct { 28 ContentType string 29 Stream bool 30 } 31 32 func (e NegotiateError) Error() string { 33 if e.Stream { 34 return fmt.Sprintf("no stream serializers registered for %s", e.ContentType) 35 } 36 return fmt.Sprintf("no serializers registered for %s", e.ContentType) 37 } 38 39 type clientNegotiator struct { 40 serializer NegotiatedSerializer 41 encode, decode GroupVersioner 42 } 43 44 func (n *clientNegotiator) Encoder(contentType string, params map[string]string) (Encoder, error) { 45 // TODO: `pretty=1` is handled in NegotiateOutputMediaType, consider moving it to this method 46 // if client negotiators truly need to use it 47 mediaTypes := n.serializer.SupportedMediaTypes() 48 info, ok := SerializerInfoForMediaType(mediaTypes, contentType) 49 if !ok { 50 if len(contentType) != 0 || len(mediaTypes) == 0 { 51 return nil, NegotiateError{ContentType: contentType} 52 } 53 info = mediaTypes[0] 54 } 55 return n.serializer.EncoderForVersion(info.Serializer, n.encode), nil 56 } 57 58 func (n *clientNegotiator) Decoder(contentType string, params map[string]string) (Decoder, error) { 59 mediaTypes := n.serializer.SupportedMediaTypes() 60 info, ok := SerializerInfoForMediaType(mediaTypes, contentType) 61 if !ok { 62 if len(contentType) != 0 || len(mediaTypes) == 0 { 63 return nil, NegotiateError{ContentType: contentType} 64 } 65 info = mediaTypes[0] 66 } 67 return n.serializer.DecoderToVersion(info.Serializer, n.decode), nil 68 } 69 70 func (n *clientNegotiator) StreamDecoder(contentType string, params map[string]string) (Decoder, Serializer, Framer, error) { 71 mediaTypes := n.serializer.SupportedMediaTypes() 72 info, ok := SerializerInfoForMediaType(mediaTypes, contentType) 73 if !ok { 74 if len(contentType) != 0 || len(mediaTypes) == 0 { 75 return nil, nil, nil, NegotiateError{ContentType: contentType, Stream: true} 76 } 77 info = mediaTypes[0] 78 } 79 if info.StreamSerializer == nil { 80 return nil, nil, nil, NegotiateError{ContentType: info.MediaType, Stream: true} 81 } 82 return n.serializer.DecoderToVersion(info.Serializer, n.decode), info.StreamSerializer.Serializer, info.StreamSerializer.Framer, nil 83 } 84 85 // NewClientNegotiator will attempt to retrieve the appropriate encoder, decoder, or 86 // stream decoder for a given content type. Does not perform any conversion, but will 87 // encode the object to the desired group, version, and kind. Use when creating a client. 88 func NewClientNegotiator(serializer NegotiatedSerializer, gv schema.GroupVersion) ClientNegotiator { 89 return &clientNegotiator{ 90 serializer: serializer, 91 encode: gv, 92 } 93 } 94 95 type simpleNegotiatedSerializer struct { 96 info SerializerInfo 97 } 98 99 func NewSimpleNegotiatedSerializer(info SerializerInfo) NegotiatedSerializer { 100 return &simpleNegotiatedSerializer{info: info} 101 } 102 103 func (n *simpleNegotiatedSerializer) SupportedMediaTypes() []SerializerInfo { 104 return []SerializerInfo{n.info} 105 } 106 107 func (n *simpleNegotiatedSerializer) EncoderForVersion(e Encoder, _ GroupVersioner) Encoder { 108 return e 109 } 110 111 func (n *simpleNegotiatedSerializer) DecoderToVersion(d Decoder, _gv GroupVersioner) Decoder { 112 return d 113 }