Skip to content

Commit b3278df

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

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-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

+96
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,56 @@ 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{})
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(obj.Spec).To(BeEquivalentTo(spec))
1834+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
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{})
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+
ObjectMeta: metav1.ObjectMeta{
1851+
Name: "a-foo",
1852+
},
1853+
Spec: spec,
1854+
}
1855+
cl := NewClientBuilder().WithScheme(scheme).WithStatusSubresource(obj).WithObjects(obj).Build()
1856+
1857+
Expect(cl.Update(context.Background(), obj)).To(Succeed())
1858+
Expect(obj.Spec).To(BeEquivalentTo(spec))
1859+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
1860+
Expect(obj.Spec).To(BeEquivalentTo(spec))
1861+
})
1862+
18131863
It("should not change the status of unstructured objects that are configured to have a status subresource on update", func() {
18141864
obj := &unstructured.Unstructured{}
18151865
obj.SetAPIVersion("foo/v1")
@@ -2695,6 +2745,52 @@ var _ = Describe("Fake client", func() {
26952745
}
26962746
})
26972747

2748+
type Schemaless map[string]interface{}
2749+
2750+
type WithSchemalessSpec struct {
2751+
metav1.TypeMeta `json:",inline"`
2752+
metav1.ObjectMeta `json:"metadata,omitempty"`
2753+
2754+
Spec Schemaless `json:"spec,omitempty"`
2755+
}
2756+
2757+
func (t *WithSchemalessSpec) DeepCopy() *WithSchemalessSpec {
2758+
w := &WithSchemalessSpec{
2759+
ObjectMeta: *t.ObjectMeta.DeepCopy(),
2760+
}
2761+
w.TypeMeta = metav1.TypeMeta{
2762+
APIVersion: t.APIVersion,
2763+
Kind: t.Kind,
2764+
}
2765+
t.Spec.DeepCopyInto(&w.Spec)
2766+
2767+
return w
2768+
}
2769+
2770+
func (t *WithSchemalessSpec) DeepCopyObject() runtime.Object {
2771+
return t.DeepCopy()
2772+
}
2773+
2774+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
2775+
func (in *Schemaless) DeepCopyInto(out *Schemaless) {
2776+
if *in != nil {
2777+
*out = make(Schemaless, len(*in))
2778+
for key := range *in {
2779+
(*out)[key] = (*in)[key]
2780+
}
2781+
}
2782+
}
2783+
2784+
// DeepCopy copies the receiver, creating a new Schemaless.
2785+
func (in *Schemaless) DeepCopy() *Schemaless {
2786+
if in == nil {
2787+
return nil
2788+
}
2789+
out := new(Schemaless)
2790+
in.DeepCopyInto(out)
2791+
return out
2792+
}
2793+
26982794
type WithPointerMetaList struct {
26992795
*metav1.ListMeta
27002796
*metav1.TypeMeta

0 commit comments

Comments
 (0)