Skip to content

🐛Use k8s.io/apimachinery/pkg/util/json to unmarshal in fakeclient #3208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package fake
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -58,6 +57,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/strategicpatch"
Expand Down
96 changes: 96 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,56 @@ var _ = Describe("Fake client", func() {
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
})

It("should Unmarshal the schemaless object with int64 to preserve ints", func() {
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "test", Version: "v1"}}
schemeBuilder.Register(&WithSchemalessSpec{})

scheme := runtime.NewScheme()
Expect(schemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())

spec := Schemaless{
"key": int64(1),
}

obj := &WithSchemalessSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "a-foo",
},
Spec: spec,
}
cl := NewClientBuilder().WithScheme(scheme).WithStatusSubresource(obj).WithObjects(obj).Build()

Expect(cl.Update(context.Background(), obj)).To(Succeed())
Expect(obj.Spec).To(BeEquivalentTo(spec))
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
Expect(obj.Spec).To(BeEquivalentTo(spec))
})

It("should Unmarshal the schemaless object with float64 to preserve ints", func() {
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "test", Version: "v1"}}
schemeBuilder.Register(&WithSchemalessSpec{})

scheme := runtime.NewScheme()
Expect(schemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())

spec := Schemaless{
"key": 1.1,
}

obj := &WithSchemalessSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "a-foo",
},
Spec: spec,
}
cl := NewClientBuilder().WithScheme(scheme).WithStatusSubresource(obj).WithObjects(obj).Build()

Expect(cl.Update(context.Background(), obj)).To(Succeed())
Expect(obj.Spec).To(BeEquivalentTo(spec))
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
Expect(obj.Spec).To(BeEquivalentTo(spec))
})

It("should not change the status of unstructured objects that are configured to have a status subresource on update", func() {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion("foo/v1")
Expand Down Expand Up @@ -2695,6 +2745,52 @@ var _ = Describe("Fake client", func() {
}
})

type Schemaless map[string]interface{}

type WithSchemalessSpec struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec Schemaless `json:"spec,omitempty"`
}

func (t *WithSchemalessSpec) DeepCopy() *WithSchemalessSpec {
w := &WithSchemalessSpec{
ObjectMeta: *t.ObjectMeta.DeepCopy(),
}
w.TypeMeta = metav1.TypeMeta{
APIVersion: t.APIVersion,
Kind: t.Kind,
}
t.Spec.DeepCopyInto(&w.Spec)

return w
}

func (t *WithSchemalessSpec) DeepCopyObject() runtime.Object {
return t.DeepCopy()
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Schemaless) DeepCopyInto(out *Schemaless) {
if *in != nil {
*out = make(Schemaless, len(*in))
for key := range *in {
(*out)[key] = (*in)[key]
}
}
}

// DeepCopy copies the receiver, creating a new Schemaless.
func (in *Schemaless) DeepCopy() *Schemaless {
if in == nil {
return nil
}
out := new(Schemaless)
in.DeepCopyInto(out)
return out
}

type WithPointerMetaList struct {
*metav1.ListMeta
*metav1.TypeMeta
Expand Down
Loading