codec_factory.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. "mime"
  16. "strings"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/apimachinery/pkg/runtime/serializer/json"
  20. "k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
  21. "k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
  22. "k8s.io/apimachinery/pkg/runtime/serializer/versioning"
  23. )
  24. // serializerExtensions are for serializers that are conditionally compiled in
  25. var serializerExtensions = []func(*runtime.Scheme) (serializerType, bool){}
  26. type serializerType struct {
  27. AcceptContentTypes []string
  28. ContentType string
  29. FileExtensions []string
  30. // EncodesAsText should be true if this content type can be represented safely in UTF-8
  31. EncodesAsText bool
  32. Serializer runtime.Serializer
  33. PrettySerializer runtime.Serializer
  34. StrictSerializer runtime.Serializer
  35. AcceptStreamContentTypes []string
  36. StreamContentType string
  37. Framer runtime.Framer
  38. StreamSerializer runtime.Serializer
  39. }
  40. func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory, options CodecFactoryOptions) []serializerType {
  41. jsonSerializer := json.NewSerializerWithOptions(
  42. mf, scheme, scheme,
  43. json.SerializerOptions{Yaml: false, Pretty: false, Strict: options.Strict},
  44. )
  45. jsonSerializerType := serializerType{
  46. AcceptContentTypes: []string{runtime.ContentTypeJSON},
  47. ContentType: runtime.ContentTypeJSON,
  48. FileExtensions: []string{"json"},
  49. EncodesAsText: true,
  50. Serializer: jsonSerializer,
  51. Framer: json.Framer,
  52. StreamSerializer: jsonSerializer,
  53. }
  54. if options.Pretty {
  55. jsonSerializerType.PrettySerializer = json.NewSerializerWithOptions(
  56. mf, scheme, scheme,
  57. json.SerializerOptions{Yaml: false, Pretty: true, Strict: options.Strict},
  58. )
  59. }
  60. strictJSONSerializer := json.NewSerializerWithOptions(
  61. mf, scheme, scheme,
  62. json.SerializerOptions{Yaml: false, Pretty: false, Strict: true},
  63. )
  64. jsonSerializerType.StrictSerializer = strictJSONSerializer
  65. yamlSerializer := json.NewSerializerWithOptions(
  66. mf, scheme, scheme,
  67. json.SerializerOptions{Yaml: true, Pretty: false, Strict: options.Strict},
  68. )
  69. strictYAMLSerializer := json.NewSerializerWithOptions(
  70. mf, scheme, scheme,
  71. json.SerializerOptions{Yaml: true, Pretty: false, Strict: true},
  72. )
  73. protoSerializer := protobuf.NewSerializer(scheme, scheme)
  74. protoRawSerializer := protobuf.NewRawSerializer(scheme, scheme)
  75. serializers := []serializerType{
  76. jsonSerializerType,
  77. {
  78. AcceptContentTypes: []string{runtime.ContentTypeYAML},
  79. ContentType: runtime.ContentTypeYAML,
  80. FileExtensions: []string{"yaml"},
  81. EncodesAsText: true,
  82. Serializer: yamlSerializer,
  83. StrictSerializer: strictYAMLSerializer,
  84. },
  85. {
  86. AcceptContentTypes: []string{runtime.ContentTypeProtobuf},
  87. ContentType: runtime.ContentTypeProtobuf,
  88. FileExtensions: []string{"pb"},
  89. Serializer: protoSerializer,
  90. // note, strict decoding is unsupported for protobuf,
  91. // fall back to regular serializing
  92. StrictSerializer: protoSerializer,
  93. Framer: protobuf.LengthDelimitedFramer,
  94. StreamSerializer: protoRawSerializer,
  95. },
  96. }
  97. for _, fn := range serializerExtensions {
  98. if serializer, ok := fn(scheme); ok {
  99. serializers = append(serializers, serializer)
  100. }
  101. }
  102. return serializers
  103. }
  104. // CodecFactory provides methods for retrieving codecs and serializers for specific
  105. // versions and content types.
  106. type CodecFactory struct {
  107. scheme *runtime.Scheme
  108. universal runtime.Decoder
  109. accepts []runtime.SerializerInfo
  110. legacySerializer runtime.Serializer
  111. }
  112. // CodecFactoryOptions holds the options for configuring CodecFactory behavior
  113. type CodecFactoryOptions struct {
  114. // Strict configures all serializers in strict mode
  115. Strict bool
  116. // Pretty includes a pretty serializer along with the non-pretty one
  117. Pretty bool
  118. }
  119. // CodecFactoryOptionsMutator takes a pointer to an options struct and then modifies it.
  120. // Functions implementing this type can be passed to the NewCodecFactory() constructor.
  121. type CodecFactoryOptionsMutator func(*CodecFactoryOptions)
  122. // EnablePretty enables including a pretty serializer along with the non-pretty one
  123. func EnablePretty(options *CodecFactoryOptions) {
  124. options.Pretty = true
  125. }
  126. // DisablePretty disables including a pretty serializer along with the non-pretty one
  127. func DisablePretty(options *CodecFactoryOptions) {
  128. options.Pretty = false
  129. }
  130. // EnableStrict enables configuring all serializers in strict mode
  131. func EnableStrict(options *CodecFactoryOptions) {
  132. options.Strict = true
  133. }
  134. // DisableStrict disables configuring all serializers in strict mode
  135. func DisableStrict(options *CodecFactoryOptions) {
  136. options.Strict = false
  137. }
  138. // NewCodecFactory provides methods for retrieving serializers for the supported wire formats
  139. // and conversion wrappers to define preferred internal and external versions. In the future,
  140. // as the internal version is used less, callers may instead use a defaulting serializer and
  141. // only convert objects which are shared internally (Status, common API machinery).
  142. //
  143. // Mutators can be passed to change the CodecFactoryOptions before construction of the factory.
  144. // It is recommended to explicitly pass mutators instead of relying on defaults.
  145. // By default, Pretty is enabled -- this is conformant with previously supported behavior.
  146. //
  147. // TODO: allow other codecs to be compiled in?
  148. // TODO: accept a scheme interface
  149. func NewCodecFactory(scheme *runtime.Scheme, mutators ...CodecFactoryOptionsMutator) CodecFactory {
  150. options := CodecFactoryOptions{Pretty: true}
  151. for _, fn := range mutators {
  152. fn(&options)
  153. }
  154. serializers := newSerializersForScheme(scheme, json.DefaultMetaFactory, options)
  155. return newCodecFactory(scheme, serializers)
  156. }
  157. // newCodecFactory is a helper for testing that allows a different metafactory to be specified.
  158. func newCodecFactory(scheme *runtime.Scheme, serializers []serializerType) CodecFactory {
  159. decoders := make([]runtime.Decoder, 0, len(serializers))
  160. var accepts []runtime.SerializerInfo
  161. alreadyAccepted := make(map[string]struct{})
  162. var legacySerializer runtime.Serializer
  163. for _, d := range serializers {
  164. decoders = append(decoders, d.Serializer)
  165. for _, mediaType := range d.AcceptContentTypes {
  166. if _, ok := alreadyAccepted[mediaType]; ok {
  167. continue
  168. }
  169. alreadyAccepted[mediaType] = struct{}{}
  170. info := runtime.SerializerInfo{
  171. MediaType: d.ContentType,
  172. EncodesAsText: d.EncodesAsText,
  173. Serializer: d.Serializer,
  174. PrettySerializer: d.PrettySerializer,
  175. StrictSerializer: d.StrictSerializer,
  176. }
  177. mediaType, _, err := mime.ParseMediaType(info.MediaType)
  178. if err != nil {
  179. panic(err)
  180. }
  181. parts := strings.SplitN(mediaType, "/", 2)
  182. info.MediaTypeType = parts[0]
  183. info.MediaTypeSubType = parts[1]
  184. if d.StreamSerializer != nil {
  185. info.StreamSerializer = &runtime.StreamSerializerInfo{
  186. Serializer: d.StreamSerializer,
  187. EncodesAsText: d.EncodesAsText,
  188. Framer: d.Framer,
  189. }
  190. }
  191. accepts = append(accepts, info)
  192. if mediaType == runtime.ContentTypeJSON {
  193. legacySerializer = d.Serializer
  194. }
  195. }
  196. }
  197. if legacySerializer == nil {
  198. legacySerializer = serializers[0].Serializer
  199. }
  200. return CodecFactory{
  201. scheme: scheme,
  202. universal: recognizer.NewDecoder(decoders...),
  203. accepts: accepts,
  204. legacySerializer: legacySerializer,
  205. }
  206. }
  207. // WithoutConversion returns a NegotiatedSerializer that performs no conversion, even if the
  208. // caller requests it.
  209. func (f CodecFactory) WithoutConversion() runtime.NegotiatedSerializer {
  210. return WithoutConversionCodecFactory{f}
  211. }
  212. // SupportedMediaTypes returns the RFC2046 media types that this factory has serializers for.
  213. func (f CodecFactory) SupportedMediaTypes() []runtime.SerializerInfo {
  214. return f.accepts
  215. }
  216. // LegacyCodec encodes output to a given API versions, and decodes output into the internal form from
  217. // any recognized source. The returned codec will always encode output to JSON. If a type is not
  218. // found in the list of versions an error will be returned.
  219. //
  220. // This method is deprecated - clients and servers should negotiate a serializer by mime-type and
  221. // invoke CodecForVersions. Callers that need only to read data should use UniversalDecoder().
  222. //
  223. // TODO: make this call exist only in pkg/api, and initialize it with the set of default versions.
  224. // All other callers will be forced to request a Codec directly.
  225. func (f CodecFactory) LegacyCodec(version ...schema.GroupVersion) runtime.Codec {
  226. return versioning.NewDefaultingCodecForScheme(f.scheme, f.legacySerializer, f.universal, schema.GroupVersions(version), runtime.InternalGroupVersioner)
  227. }
  228. // UniversalDeserializer can convert any stored data recognized by this factory into a Go object that satisfies
  229. // runtime.Object. It does not perform conversion. It does not perform defaulting.
  230. func (f CodecFactory) UniversalDeserializer() runtime.Decoder {
  231. return f.universal
  232. }
  233. // UniversalDecoder returns a runtime.Decoder capable of decoding all known API objects in all known formats. Used
  234. // by clients that do not need to encode objects but want to deserialize API objects stored on disk. Only decodes
  235. // objects in groups registered with the scheme. The GroupVersions passed may be used to select alternate
  236. // versions of objects to return - by default, runtime.APIVersionInternal is used. If any versions are specified,
  237. // unrecognized groups will be returned in the version they are encoded as (no conversion). This decoder performs
  238. // defaulting.
  239. //
  240. // TODO: the decoder will eventually be removed in favor of dealing with objects in their versioned form
  241. // TODO: only accept a group versioner
  242. func (f CodecFactory) UniversalDecoder(versions ...schema.GroupVersion) runtime.Decoder {
  243. var versioner runtime.GroupVersioner
  244. if len(versions) == 0 {
  245. versioner = runtime.InternalGroupVersioner
  246. } else {
  247. versioner = schema.GroupVersions(versions)
  248. }
  249. return f.CodecForVersions(nil, f.universal, nil, versioner)
  250. }
  251. // CodecForVersions creates a codec with the provided serializer. If an object is decoded and its group is not in the list,
  252. // it will default to runtime.APIVersionInternal. If encode is not specified for an object's group, the object is not
  253. // converted. If encode or decode are nil, no conversion is performed.
  254. func (f CodecFactory) CodecForVersions(encoder runtime.Encoder, decoder runtime.Decoder, encode runtime.GroupVersioner, decode runtime.GroupVersioner) runtime.Codec {
  255. // TODO: these are for backcompat, remove them in the future
  256. if encode == nil {
  257. encode = runtime.DisabledGroupVersioner
  258. }
  259. if decode == nil {
  260. decode = runtime.InternalGroupVersioner
  261. }
  262. return versioning.NewDefaultingCodecForScheme(f.scheme, encoder, decoder, encode, decode)
  263. }
  264. // DecoderToVersion returns a decoder that targets the provided group version.
  265. func (f CodecFactory) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
  266. return f.CodecForVersions(nil, decoder, nil, gv)
  267. }
  268. // EncoderForVersion returns an encoder that targets the provided group version.
  269. func (f CodecFactory) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
  270. return f.CodecForVersions(encoder, nil, gv, nil)
  271. }
  272. // WithoutConversionCodecFactory is a CodecFactory that will explicitly ignore requests to perform conversion.
  273. // This wrapper is used while code migrates away from using conversion (such as external clients) and in the future
  274. // will be unnecessary when we change the signature of NegotiatedSerializer.
  275. type WithoutConversionCodecFactory struct {
  276. CodecFactory
  277. }
  278. // EncoderForVersion returns an encoder that does not do conversion, but does set the group version kind of the object
  279. // when serialized.
  280. func (f WithoutConversionCodecFactory) EncoderForVersion(serializer runtime.Encoder, version runtime.GroupVersioner) runtime.Encoder {
  281. return runtime.WithVersionEncoder{
  282. Version: version,
  283. Encoder: serializer,
  284. ObjectTyper: f.CodecFactory.scheme,
  285. }
  286. }
  287. // DecoderToVersion returns an decoder that does not do conversion.
  288. func (f WithoutConversionCodecFactory) DecoderToVersion(serializer runtime.Decoder, _ runtime.GroupVersioner) runtime.Decoder {
  289. return runtime.WithoutVersionDecoder{
  290. Decoder: serializer,
  291. }
  292. }