codec_factory.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package serializer
  14. import (
  15. "k8s.io/kubernetes/pkg/api/unversioned"
  16. "k8s.io/kubernetes/pkg/runtime"
  17. "k8s.io/kubernetes/pkg/runtime/serializer/json"
  18. "k8s.io/kubernetes/pkg/runtime/serializer/recognizer"
  19. "k8s.io/kubernetes/pkg/runtime/serializer/versioning"
  20. )
  21. // serializerExtensions are for serializers that are conditionally compiled in
  22. var serializerExtensions = []func(*runtime.Scheme) (serializerType, bool){}
  23. type serializerType struct {
  24. AcceptContentTypes []string
  25. ContentType string
  26. FileExtensions []string
  27. // EncodesAsText should be true if this content type can be represented safely in UTF-8
  28. EncodesAsText bool
  29. Serializer runtime.Serializer
  30. PrettySerializer runtime.Serializer
  31. // RawSerializer serializes an object without adding a type wrapper. Some serializers, like JSON
  32. // automatically include identifying type information with the JSON. Others, like Protobuf, need
  33. // a wrapper object that includes type information. This serializer should be set if the serializer
  34. // can serialize / deserialize objects without type info. Note that this serializer will always
  35. // be expected to pass into or a gvk to Decode, since no type information will be available on
  36. // the object itself.
  37. RawSerializer runtime.Serializer
  38. // Specialize gives the type the opportunity to return a different serializer implementation if
  39. // the content type contains alternate operations. Here it is used to implement "pretty" as an
  40. // option to application/json, but could also be used to allow serializers to perform type
  41. // defaulting or alter output.
  42. Specialize func(map[string]string) (runtime.Serializer, bool)
  43. AcceptStreamContentTypes []string
  44. StreamContentType string
  45. Framer runtime.Framer
  46. StreamSerializer runtime.Serializer
  47. StreamSpecialize func(map[string]string) (runtime.Serializer, bool)
  48. }
  49. func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []serializerType {
  50. jsonSerializer := json.NewSerializer(mf, scheme, scheme, false)
  51. jsonPrettySerializer := json.NewSerializer(mf, scheme, scheme, true)
  52. yamlSerializer := json.NewYAMLSerializer(mf, scheme, scheme)
  53. serializers := []serializerType{
  54. {
  55. AcceptContentTypes: []string{"application/json"},
  56. ContentType: "application/json",
  57. FileExtensions: []string{"json"},
  58. EncodesAsText: true,
  59. Serializer: jsonSerializer,
  60. PrettySerializer: jsonPrettySerializer,
  61. AcceptStreamContentTypes: []string{"application/json", "application/json;stream=watch"},
  62. StreamContentType: "application/json",
  63. Framer: json.Framer,
  64. StreamSerializer: jsonSerializer,
  65. },
  66. {
  67. AcceptContentTypes: []string{"application/yaml"},
  68. ContentType: "application/yaml",
  69. FileExtensions: []string{"yaml"},
  70. EncodesAsText: true,
  71. Serializer: yamlSerializer,
  72. // TODO: requires runtime.RawExtension to properly distinguish when the nested content is
  73. // yaml, because the yaml encoder invokes MarshalJSON first
  74. //AcceptStreamContentTypes: []string{"application/yaml", "application/yaml;stream=watch"},
  75. //StreamContentType: "application/yaml;stream=watch",
  76. //Framer: json.YAMLFramer,
  77. //StreamSerializer: yamlSerializer,
  78. },
  79. }
  80. for _, fn := range serializerExtensions {
  81. if serializer, ok := fn(scheme); ok {
  82. serializers = append(serializers, serializer)
  83. }
  84. }
  85. return serializers
  86. }
  87. // CodecFactory provides methods for retrieving codecs and serializers for specific
  88. // versions and content types.
  89. type CodecFactory struct {
  90. scheme *runtime.Scheme
  91. serializers []serializerType
  92. universal runtime.Decoder
  93. accepts []string
  94. streamingAccepts []string
  95. legacySerializer runtime.Serializer
  96. }
  97. // NewCodecFactory provides methods for retrieving serializers for the supported wire formats
  98. // and conversion wrappers to define preferred internal and external versions. In the future,
  99. // as the internal version is used less, callers may instead use a defaulting serializer and
  100. // only convert objects which are shared internally (Status, common API machinery).
  101. // TODO: allow other codecs to be compiled in?
  102. // TODO: accept a scheme interface
  103. func NewCodecFactory(scheme *runtime.Scheme) CodecFactory {
  104. serializers := newSerializersForScheme(scheme, json.DefaultMetaFactory)
  105. return newCodecFactory(scheme, serializers)
  106. }
  107. // newCodecFactory is a helper for testing that allows a different metafactory to be specified.
  108. func newCodecFactory(scheme *runtime.Scheme, serializers []serializerType) CodecFactory {
  109. decoders := make([]runtime.Decoder, 0, len(serializers))
  110. accepts := []string{}
  111. alreadyAccepted := make(map[string]struct{})
  112. var legacySerializer runtime.Serializer
  113. for _, d := range serializers {
  114. decoders = append(decoders, d.Serializer)
  115. for _, mediaType := range d.AcceptContentTypes {
  116. if _, ok := alreadyAccepted[mediaType]; ok {
  117. continue
  118. }
  119. alreadyAccepted[mediaType] = struct{}{}
  120. accepts = append(accepts, mediaType)
  121. if mediaType == "application/json" {
  122. legacySerializer = d.Serializer
  123. }
  124. }
  125. }
  126. if legacySerializer == nil {
  127. legacySerializer = serializers[0].Serializer
  128. }
  129. streamAccepts := []string{}
  130. alreadyAccepted = make(map[string]struct{})
  131. for _, d := range serializers {
  132. if len(d.StreamContentType) == 0 {
  133. continue
  134. }
  135. for _, mediaType := range d.AcceptStreamContentTypes {
  136. if _, ok := alreadyAccepted[mediaType]; ok {
  137. continue
  138. }
  139. alreadyAccepted[mediaType] = struct{}{}
  140. streamAccepts = append(streamAccepts, mediaType)
  141. }
  142. }
  143. return CodecFactory{
  144. scheme: scheme,
  145. serializers: serializers,
  146. universal: recognizer.NewDecoder(decoders...),
  147. accepts: accepts,
  148. streamingAccepts: streamAccepts,
  149. legacySerializer: legacySerializer,
  150. }
  151. }
  152. var _ runtime.NegotiatedSerializer = &CodecFactory{}
  153. // SupportedMediaTypes returns the RFC2046 media types that this factory has serializers for.
  154. func (f CodecFactory) SupportedMediaTypes() []string {
  155. return f.accepts
  156. }
  157. // SupportedStreamingMediaTypes returns the RFC2046 media types that this factory has stream serializers for.
  158. func (f CodecFactory) SupportedStreamingMediaTypes() []string {
  159. return f.streamingAccepts
  160. }
  161. // LegacyCodec encodes output to a given API versions, and decodes output into the internal form from
  162. // any recognized source. The returned codec will always encode output to JSON. If a type is not
  163. // found in the list of versions an error will be returned.
  164. //
  165. // This method is deprecated - clients and servers should negotiate a serializer by mime-type and
  166. // invoke CodecForVersions. Callers that need only to read data should use UniversalDecoder().
  167. //
  168. // TODO: make this call exist only in pkg/api, and initialize it with the set of default versions.
  169. // All other callers will be forced to request a Codec directly.
  170. func (f CodecFactory) LegacyCodec(version ...unversioned.GroupVersion) runtime.Codec {
  171. return versioning.NewCodecForScheme(f.scheme, f.legacySerializer, f.universal, unversioned.GroupVersions(version), runtime.InternalGroupVersioner)
  172. }
  173. // UniversalDeserializer can convert any stored data recognized by this factory into a Go object that satisfies
  174. // runtime.Object. It does not perform conversion. It does not perform defaulting.
  175. func (f CodecFactory) UniversalDeserializer() runtime.Decoder {
  176. return f.universal
  177. }
  178. // UniversalDecoder returns a runtime.Decoder capable of decoding all known API objects in all known formats. Used
  179. // by clients that do not need to encode objects but want to deserialize API objects stored on disk. Only decodes
  180. // objects in groups registered with the scheme. The GroupVersions passed may be used to select alternate
  181. // versions of objects to return - by default, runtime.APIVersionInternal is used. If any versions are specified,
  182. // unrecognized groups will be returned in the version they are encoded as (no conversion). This decoder performs
  183. // defaulting.
  184. //
  185. // TODO: the decoder will eventually be removed in favor of dealing with objects in their versioned form
  186. // TODO: only accept a group versioner
  187. func (f CodecFactory) UniversalDecoder(versions ...unversioned.GroupVersion) runtime.Decoder {
  188. var versioner runtime.GroupVersioner
  189. if len(versions) == 0 {
  190. versioner = runtime.InternalGroupVersioner
  191. } else {
  192. versioner = unversioned.GroupVersions(versions)
  193. }
  194. return f.CodecForVersions(nil, f.universal, nil, versioner)
  195. }
  196. // CodecForVersions creates a codec with the provided serializer. If an object is decoded and its group is not in the list,
  197. // it will default to runtime.APIVersionInternal. If encode is not specified for an object's group, the object is not
  198. // converted. If encode or decode are nil, no conversion is performed.
  199. func (f CodecFactory) CodecForVersions(encoder runtime.Encoder, decoder runtime.Decoder, encode runtime.GroupVersioner, decode runtime.GroupVersioner) runtime.Codec {
  200. // TODO: these are for backcompat, remove them in the future
  201. if encode == nil {
  202. encode = runtime.DisabledGroupVersioner
  203. }
  204. if decode == nil {
  205. decode = runtime.InternalGroupVersioner
  206. }
  207. return versioning.NewCodecForScheme(f.scheme, encoder, decoder, encode, decode)
  208. }
  209. // DecoderToVersion returns a decoder that targets the provided group version.
  210. func (f CodecFactory) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
  211. return f.CodecForVersions(nil, decoder, nil, gv)
  212. }
  213. // EncoderForVersion returns an encoder that targets the provided group version.
  214. func (f CodecFactory) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
  215. return f.CodecForVersions(encoder, nil, gv, nil)
  216. }
  217. // SerializerForMediaType returns a serializer that matches the provided RFC2046 mediaType, or false if no such
  218. // serializer exists
  219. func (f CodecFactory) SerializerForMediaType(mediaType string, params map[string]string) (runtime.SerializerInfo, bool) {
  220. for _, s := range f.serializers {
  221. for _, accepted := range s.AcceptContentTypes {
  222. if accepted == mediaType {
  223. // specialization abstracts variants to the content type
  224. if s.Specialize != nil && len(params) > 0 {
  225. serializer, ok := s.Specialize(params)
  226. // TODO: return formatted mediaType+params
  227. return runtime.SerializerInfo{Serializer: serializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, ok
  228. }
  229. // legacy support for ?pretty=1 continues, but this is more formally defined
  230. if v, ok := params["pretty"]; ok && v == "1" && s.PrettySerializer != nil {
  231. return runtime.SerializerInfo{Serializer: s.PrettySerializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, true
  232. }
  233. // return the base variant
  234. return runtime.SerializerInfo{Serializer: s.Serializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, true
  235. }
  236. }
  237. }
  238. return runtime.SerializerInfo{}, false
  239. }
  240. // StreamingSerializerForMediaType returns a serializer that matches the provided RFC2046 mediaType, or false if no such
  241. // serializer exists
  242. func (f CodecFactory) StreamingSerializerForMediaType(mediaType string, params map[string]string) (runtime.StreamSerializerInfo, bool) {
  243. for _, s := range f.serializers {
  244. for _, accepted := range s.AcceptStreamContentTypes {
  245. if accepted == mediaType {
  246. // TODO: accept params
  247. nested, ok := f.SerializerForMediaType(s.ContentType, nil)
  248. if !ok {
  249. panic("no serializer defined for internal content type")
  250. }
  251. if s.StreamSpecialize != nil && len(params) > 0 {
  252. serializer, ok := s.StreamSpecialize(params)
  253. // TODO: return formatted mediaType+params
  254. return runtime.StreamSerializerInfo{
  255. SerializerInfo: runtime.SerializerInfo{
  256. Serializer: serializer,
  257. MediaType: s.StreamContentType,
  258. EncodesAsText: s.EncodesAsText,
  259. },
  260. Framer: s.Framer,
  261. Embedded: nested,
  262. }, ok
  263. }
  264. return runtime.StreamSerializerInfo{
  265. SerializerInfo: runtime.SerializerInfo{
  266. Serializer: s.StreamSerializer,
  267. MediaType: s.StreamContentType,
  268. EncodesAsText: s.EncodesAsText,
  269. },
  270. Framer: s.Framer,
  271. Embedded: nested,
  272. }, true
  273. }
  274. }
  275. }
  276. return runtime.StreamSerializerInfo{}, false
  277. }
  278. // SerializerForFileExtension returns a serializer for the provided extension, or false if no serializer matches.
  279. func (f CodecFactory) SerializerForFileExtension(extension string) (runtime.Serializer, bool) {
  280. for _, s := range f.serializers {
  281. for _, ext := range s.FileExtensions {
  282. if extension == ext {
  283. return s.Serializer, true
  284. }
  285. }
  286. }
  287. return nil, false
  288. }
  289. // DirectCodecFactory provides methods for retrieving "DirectCodec"s, which do not do conversion.
  290. type DirectCodecFactory struct {
  291. CodecFactory
  292. }
  293. // EncoderForVersion returns an encoder that does not do conversion. gv is ignored.
  294. func (f DirectCodecFactory) EncoderForVersion(serializer runtime.Encoder, _ runtime.GroupVersioner) runtime.Encoder {
  295. return versioning.DirectEncoder{
  296. Encoder: serializer,
  297. ObjectTyper: f.CodecFactory.scheme,
  298. }
  299. }
  300. // DecoderToVersion returns an decoder that does not do conversion. gv is ignored.
  301. func (f DirectCodecFactory) DecoderToVersion(serializer runtime.Decoder, _ runtime.GroupVersioner) runtime.Decoder {
  302. return versioning.DirectDecoder{
  303. Decoder: serializer,
  304. }
  305. }