Skip to content

Commit 2b94858

Browse files
author
Shawn Hurley
committed
unstructured clients testing.
1 parent bd5262e commit 2b94858

File tree

2 files changed

+1091
-620
lines changed

2 files changed

+1091
-620
lines changed

pkg/client/apiutil/apimachinery.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package apiutil
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io"
7+
"strings"
8+
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
510

611
"k8s.io/apimachinery/pkg/api/meta"
712
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -66,7 +71,7 @@ func RESTUnstructuredClientForGVK(gvk schema.GroupVersionKind, baseConfig *rest.
6671
break
6772
}
6873
}
69-
jsonInfo.Serializer = unstructured.UnstructuredJSONScheme
74+
jsonInfo.Serializer = dynamicCodec{}
7075
cfg.NegotiatedSerializer = serializer.NegotiatedSerializerWrapper(jsonInfo)
7176

7277
return rest.RESTClientFor(cfg)
@@ -89,3 +94,30 @@ func createRestConfig(gvk schema.GroupVersionKind, baseConfig *rest.Config) *res
8994
return cfg
9095

9196
}
97+
98+
//Copied from deprecated-dynamic/bad_debt.go
99+
// dynamicCodec is a codec that wraps the standard unstructured codec
100+
// with special handling for Status objects.
101+
// Deprecated only used by test code and its wrong
102+
type dynamicCodec struct{}
103+
104+
func (dynamicCodec) Decode(data []byte, gvk *schema.GroupVersionKind, obj runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
105+
obj, gvk, err := unstructured.UnstructuredJSONScheme.Decode(data, gvk, obj)
106+
if err != nil {
107+
return nil, nil, err
108+
}
109+
110+
if _, ok := obj.(*metav1.Status); !ok && strings.ToLower(gvk.Kind) == "status" {
111+
obj = &metav1.Status{}
112+
err := json.Unmarshal(data, obj)
113+
if err != nil {
114+
return nil, nil, err
115+
}
116+
}
117+
118+
return obj, gvk, nil
119+
}
120+
121+
func (dynamicCodec) Encode(obj runtime.Object, w io.Writer) error {
122+
return unstructured.UnstructuredJSONScheme.Encode(obj, w)
123+
}

0 commit comments

Comments
 (0)