aboutsummaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/runtime/codec.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/runtime/codec.go')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/runtime/codec.go92
1 files changed, 78 insertions, 14 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/runtime/codec.go b/vendor/k8s.io/apimachinery/pkg/runtime/codec.go
index 284e32bc3..0bccf9dd9 100644
--- a/vendor/k8s.io/apimachinery/pkg/runtime/codec.go
+++ b/vendor/k8s.io/apimachinery/pkg/runtime/codec.go
@@ -19,13 +19,17 @@ package runtime
import (
"bytes"
"encoding/base64"
+ "encoding/json"
"fmt"
"io"
"net/url"
"reflect"
+ "strconv"
+ "strings"
"k8s.io/apimachinery/pkg/conversion/queryparams"
"k8s.io/apimachinery/pkg/runtime/schema"
+ "k8s.io/klog"
)
// codec binds an encoder and decoder.
@@ -100,10 +104,19 @@ type NoopEncoder struct {
var _ Serializer = NoopEncoder{}
+const noopEncoderIdentifier Identifier = "noop"
+
func (n NoopEncoder) Encode(obj Object, w io.Writer) error {
+ // There is no need to handle runtime.CacheableObject, as we don't
+ // process the obj at all.
return fmt.Errorf("encoding is not allowed for this codec: %v", reflect.TypeOf(n.Decoder))
}
+// Identifier implements runtime.Encoder interface.
+func (n NoopEncoder) Identifier() Identifier {
+ return noopEncoderIdentifier
+}
+
// NoopDecoder converts an Encoder to a Serializer or Codec for code that expects them but only uses encoding.
type NoopDecoder struct {
Encoder
@@ -193,19 +206,51 @@ func (c *parameterCodec) EncodeParameters(obj Object, to schema.GroupVersion) (u
type base64Serializer struct {
Encoder
Decoder
+
+ identifier Identifier
}
func NewBase64Serializer(e Encoder, d Decoder) Serializer {
- return &base64Serializer{e, d}
+ return &base64Serializer{
+ Encoder: e,
+ Decoder: d,
+ identifier: identifier(e),
+ }
+}
+
+func identifier(e Encoder) Identifier {
+ result := map[string]string{
+ "name": "base64",
+ }
+ if e != nil {
+ result["encoder"] = string(e.Identifier())
+ }
+ identifier, err := json.Marshal(result)
+ if err != nil {
+ klog.Fatalf("Failed marshaling identifier for base64Serializer: %v", err)
+ }
+ return Identifier(identifier)
}
func (s base64Serializer) Encode(obj Object, stream io.Writer) error {
+ if co, ok := obj.(CacheableObject); ok {
+ return co.CacheEncode(s.Identifier(), s.doEncode, stream)
+ }
+ return s.doEncode(obj, stream)
+}
+
+func (s base64Serializer) doEncode(obj Object, stream io.Writer) error {
e := base64.NewEncoder(base64.StdEncoding, stream)
err := s.Encoder.Encode(obj, e)
e.Close()
return err
}
+// Identifier implements runtime.Encoder interface.
+func (s base64Serializer) Identifier() Identifier {
+ return s.identifier
+}
+
func (s base64Serializer) Decode(data []byte, defaults *schema.GroupVersionKind, into Object) (Object, *schema.GroupVersionKind, error) {
out := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
n, err := base64.StdEncoding.Decode(out, data)
@@ -238,6 +283,11 @@ var (
DisabledGroupVersioner GroupVersioner = disabledGroupVersioner{}
)
+const (
+ internalGroupVersionerIdentifier = "internal"
+ disabledGroupVersionerIdentifier = "disabled"
+)
+
type internalGroupVersioner struct{}
// KindForGroupVersionKinds returns an internal Kind if one is found, or converts the first provided kind to the internal version.
@@ -253,6 +303,11 @@ func (internalGroupVersioner) KindForGroupVersionKinds(kinds []schema.GroupVersi
return schema.GroupVersionKind{}, false
}
+// Identifier implements GroupVersioner interface.
+func (internalGroupVersioner) Identifier() string {
+ return internalGroupVersionerIdentifier
+}
+
type disabledGroupVersioner struct{}
// KindForGroupVersionKinds returns false for any input.
@@ -260,19 +315,9 @@ func (disabledGroupVersioner) KindForGroupVersionKinds(kinds []schema.GroupVersi
return schema.GroupVersionKind{}, false
}
-// GroupVersioners implements GroupVersioner and resolves to the first exact match for any kind.
-type GroupVersioners []GroupVersioner
-
-// KindForGroupVersionKinds returns the first match of any of the group versioners, or false if no match occurred.
-func (gvs GroupVersioners) KindForGroupVersionKinds(kinds []schema.GroupVersionKind) (schema.GroupVersionKind, bool) {
- for _, gv := range gvs {
- target, ok := gv.KindForGroupVersionKinds(kinds)
- if !ok {
- continue
- }
- return target, true
- }
- return schema.GroupVersionKind{}, false
+// Identifier implements GroupVersioner interface.
+func (disabledGroupVersioner) Identifier() string {
+ return disabledGroupVersionerIdentifier
}
// Assert that schema.GroupVersion and GroupVersions implement GroupVersioner
@@ -330,3 +375,22 @@ func (v multiGroupVersioner) KindForGroupVersionKinds(kinds []schema.GroupVersio
}
return schema.GroupVersionKind{}, false
}
+
+// Identifier implements GroupVersioner interface.
+func (v multiGroupVersioner) Identifier() string {
+ groupKinds := make([]string, 0, len(v.acceptedGroupKinds))
+ for _, gk := range v.acceptedGroupKinds {
+ groupKinds = append(groupKinds, gk.String())
+ }
+ result := map[string]string{
+ "name": "multi",
+ "target": v.target.String(),
+ "accepted": strings.Join(groupKinds, ","),
+ "coerce": strconv.FormatBool(v.coerce),
+ }
+ identifier, err := json.Marshal(result)
+ if err != nil {
+ klog.Fatalf("Failed marshaling Identifier for %#v: %v", v, err)
+ }
+ return string(identifier)
+}