extension.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 runtime
  14. import (
  15. "bytes"
  16. "errors"
  17. "fmt"
  18. cbor "k8s.io/apimachinery/pkg/runtime/serializer/cbor/direct"
  19. "k8s.io/apimachinery/pkg/util/json"
  20. )
  21. // RawExtension intentionally avoids implementing value.UnstructuredConverter for now because the
  22. // signature of ToUnstructured does not allow returning an error value in cases where the conversion
  23. // is not possible (content type is unrecognized or bytes don't match content type).
  24. func rawToUnstructured(raw []byte, contentType string) (interface{}, error) {
  25. switch contentType {
  26. case ContentTypeJSON:
  27. var u interface{}
  28. if err := json.Unmarshal(raw, &u); err != nil {
  29. return nil, fmt.Errorf("failed to parse RawExtension bytes as JSON: %w", err)
  30. }
  31. return u, nil
  32. case ContentTypeCBOR:
  33. var u interface{}
  34. if err := cbor.Unmarshal(raw, &u); err != nil {
  35. return nil, fmt.Errorf("failed to parse RawExtension bytes as CBOR: %w", err)
  36. }
  37. return u, nil
  38. default:
  39. return nil, fmt.Errorf("cannot convert RawExtension with unrecognized content type to unstructured")
  40. }
  41. }
  42. func (re RawExtension) guessContentType() string {
  43. switch {
  44. case bytes.HasPrefix(re.Raw, cborSelfDescribed):
  45. return ContentTypeCBOR
  46. case len(re.Raw) > 0:
  47. switch re.Raw[0] {
  48. case '\t', '\r', '\n', ' ', '{', '[', 'n', 't', 'f', '"', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  49. // Prefixes for the four whitespace characters, objects, arrays, strings, numbers, true, false, and null.
  50. return ContentTypeJSON
  51. }
  52. }
  53. return ""
  54. }
  55. func (re *RawExtension) UnmarshalJSON(in []byte) error {
  56. if re == nil {
  57. return errors.New("runtime.RawExtension: UnmarshalJSON on nil pointer")
  58. }
  59. if bytes.Equal(in, []byte("null")) {
  60. return nil
  61. }
  62. re.Raw = append(re.Raw[0:0], in...)
  63. return nil
  64. }
  65. var (
  66. cborNull = []byte{0xf6}
  67. cborSelfDescribed = []byte{0xd9, 0xd9, 0xf7}
  68. )
  69. func (re *RawExtension) UnmarshalCBOR(in []byte) error {
  70. if re == nil {
  71. return errors.New("runtime.RawExtension: UnmarshalCBOR on nil pointer")
  72. }
  73. if !bytes.Equal(in, cborNull) {
  74. if !bytes.HasPrefix(in, cborSelfDescribed) {
  75. // The self-described CBOR tag doesn't change the interpretation of the data
  76. // item it encloses, but it is useful as a magic number. Its encoding is
  77. // also what is used to implement the CBOR RecognizingDecoder.
  78. re.Raw = append(re.Raw[:0], cborSelfDescribed...)
  79. }
  80. re.Raw = append(re.Raw, in...)
  81. }
  82. return nil
  83. }
  84. // MarshalJSON may get called on pointers or values, so implement MarshalJSON on value.
  85. // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
  86. func (re RawExtension) MarshalJSON() ([]byte, error) {
  87. if re.Raw == nil {
  88. // TODO: this is to support legacy behavior of JSONPrinter and YAMLPrinter, which
  89. // expect to call json.Marshal on arbitrary versioned objects (even those not in
  90. // the scheme). pkg/kubectl/resource#AsVersionedObjects and its interaction with
  91. // kubectl get on objects not in the scheme needs to be updated to ensure that the
  92. // objects that are not part of the scheme are correctly put into the right form.
  93. if re.Object != nil {
  94. return json.Marshal(re.Object)
  95. }
  96. return []byte("null"), nil
  97. }
  98. contentType := re.guessContentType()
  99. if contentType == ContentTypeJSON {
  100. return re.Raw, nil
  101. }
  102. u, err := rawToUnstructured(re.Raw, contentType)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return json.Marshal(u)
  107. }
  108. func (re RawExtension) MarshalCBOR() ([]byte, error) {
  109. if re.Raw == nil {
  110. if re.Object != nil {
  111. return cbor.Marshal(re.Object)
  112. }
  113. return cbor.Marshal(nil)
  114. }
  115. contentType := re.guessContentType()
  116. if contentType == ContentTypeCBOR {
  117. return re.Raw, nil
  118. }
  119. u, err := rawToUnstructured(re.Raw, contentType)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return cbor.Marshal(u)
  124. }