...
1
16
17 package runtime
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 )
24
25
26
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
46
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
86
87
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 }
114
View as plain text