Skip to content

Commit d9721fa

Browse files
committed
use sigs.k8s.io/json to unmarshal in fakeclient
Signed-off-by: Troy Connor <[email protected]>
1 parent b6c5897 commit d9721fa

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

pkg/client/fake/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package fake
1919
import (
2020
"bytes"
2121
"context"
22-
"encoding/json"
2322
"errors"
2423
"fmt"
2524
"reflect"
@@ -58,6 +57,7 @@ import (
5857
"k8s.io/apimachinery/pkg/runtime"
5958
"k8s.io/apimachinery/pkg/runtime/schema"
6059
"k8s.io/apimachinery/pkg/types"
60+
"k8s.io/apimachinery/pkg/util/json"
6161
utilrand "k8s.io/apimachinery/pkg/util/rand"
6262
"k8s.io/apimachinery/pkg/util/sets"
6363
"k8s.io/apimachinery/pkg/util/strategicpatch"

pkg/client/fake/client_test.go

+126
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,57 @@ var _ = Describe("Fake client", func() {
18101810
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
18111811
})
18121812

1813+
It("should Unmarshal the schemaless object with int64 to preserve ints", func() {
1814+
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "test", Version: "v1"}}
1815+
schemeBuilder.Register(&WithSchemalessSpec{}, &WithSchemalessSpecList{})
1816+
1817+
scheme := runtime.NewScheme()
1818+
Expect(schemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())
1819+
1820+
spec := Schemaless{
1821+
"key": int64(1),
1822+
}
1823+
1824+
obj := &WithSchemalessSpec{
1825+
ObjectMeta: metav1.ObjectMeta{
1826+
Name: "a-foo",
1827+
},
1828+
Spec: spec,
1829+
}
1830+
cl := NewClientBuilder().WithScheme(scheme).WithStatusSubresource(obj).WithObjects(obj).Build()
1831+
1832+
Expect(cl.Update(context.Background(), obj)).To(Succeed())
1833+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
1834+
1835+
Expect(obj.Spec).To(BeEquivalentTo(spec))
1836+
})
1837+
1838+
It("should Unmarshal the schemaless object with float64 to preserve ints", func() {
1839+
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "test", Version: "v1"}}
1840+
schemeBuilder.Register(&WithSchemalessSpec{}, &WithSchemalessSpecList{})
1841+
1842+
scheme := runtime.NewScheme()
1843+
Expect(schemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())
1844+
1845+
spec := Schemaless{
1846+
"key": 1.1,
1847+
}
1848+
1849+
obj := &WithSchemalessSpec{
1850+
1851+
ObjectMeta: metav1.ObjectMeta{
1852+
Name: "a-foo",
1853+
},
1854+
Spec: spec,
1855+
}
1856+
cl := NewClientBuilder().WithScheme(scheme).WithStatusSubresource(obj).WithObjects(obj).Build()
1857+
1858+
Expect(cl.Update(context.Background(), obj)).To(Succeed())
1859+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
1860+
1861+
Expect(obj.Spec).To(BeEquivalentTo(spec))
1862+
})
1863+
18131864
It("should not change the status of unstructured objects that are configured to have a status subresource on update", func() {
18141865
obj := &unstructured.Unstructured{}
18151866
obj.SetAPIVersion("foo/v1")
@@ -2695,6 +2746,81 @@ var _ = Describe("Fake client", func() {
26952746
}
26962747
})
26972748

2749+
type WithSchemalessSpecList struct {
2750+
*metav1.ListMeta
2751+
*metav1.TypeMeta
2752+
Items []*WithPointerMeta
2753+
}
2754+
2755+
func (t *WithSchemalessSpecList) DeepCopy() *WithSchemalessSpecList {
2756+
l := &WithSchemalessSpecList{
2757+
ListMeta: t.ListMeta.DeepCopy(),
2758+
}
2759+
if t.TypeMeta != nil {
2760+
l.TypeMeta = &metav1.TypeMeta{
2761+
2762+
APIVersion: t.APIVersion,
2763+
2764+
Kind: t.Kind,
2765+
}
2766+
}
2767+
for _, item := range t.Items {
2768+
l.Items = append(l.Items, item.DeepCopy())
2769+
}
2770+
2771+
return l
2772+
}
2773+
2774+
func (t *WithSchemalessSpecList) DeepCopyObject() runtime.Object {
2775+
return t.DeepCopy()
2776+
}
2777+
2778+
type Schemaless map[string]interface{}
2779+
2780+
type WithSchemalessSpec struct {
2781+
metav1.TypeMeta `json:",inline"`
2782+
metav1.ObjectMeta `json:"metadata,omitempty"`
2783+
2784+
Spec Schemaless `json:"spec,omitempty"`
2785+
}
2786+
2787+
func (t *WithSchemalessSpec) DeepCopy() *WithSchemalessSpec {
2788+
w := &WithSchemalessSpec{
2789+
ObjectMeta: *t.ObjectMeta.DeepCopy(),
2790+
}
2791+
w.TypeMeta = metav1.TypeMeta{
2792+
APIVersion: t.APIVersion,
2793+
Kind: t.Kind,
2794+
}
2795+
t.Spec.DeepCopyInto(&w.Spec)
2796+
2797+
return w
2798+
}
2799+
2800+
func (t *WithSchemalessSpec) DeepCopyObject() runtime.Object {
2801+
return t.DeepCopy()
2802+
}
2803+
2804+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
2805+
func (in *Schemaless) DeepCopyInto(out *Schemaless) {
2806+
if *in != nil {
2807+
*out = make(Schemaless, len(*in))
2808+
for key := range *in {
2809+
(*out)[key] = (*in)[key]
2810+
}
2811+
}
2812+
}
2813+
2814+
// DeepCopy copies the receiver, creating a new Schemaless.
2815+
func (in *Schemaless) DeepCopy() *Schemaless {
2816+
if in == nil {
2817+
return nil
2818+
}
2819+
out := new(Schemaless)
2820+
in.DeepCopyInto(out)
2821+
return out
2822+
}
2823+
26982824
type WithPointerMetaList struct {
26992825
*metav1.ListMeta
27002826
*metav1.TypeMeta

0 commit comments

Comments
 (0)