negotiate.go 3.7 KB

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