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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be in with the others ones?

Copy link
Member Author

@troy0820 troy0820 May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is actually in k8s.io/apimachinery/pkg/util/json which has an Unmarshal method. That method calls the method UnmarshalCaseSensitivePreserveInts

https://github.com/kubernetes/apimachinery/blob/d56afd172ac5d1ce5afe66caedf3e663a56433f5/pkg/util/json/json.go#L45C1-L47C2


require (
cel.dev/expr v0.19.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
Expand Down Expand Up @@ -94,7 +96,6 @@ require (
k8s.io/component-base v0.33.0 // indirect
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
)
19 changes: 10 additions & 9 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/testing"
"k8s.io/utils/ptr"
kjson "sigs.k8s.io/json"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -377,7 +378,7 @@ func convertFromUnstructuredIfNecessary(s *runtime.Scheme, o runtime.Object) (ru
if err != nil {
return nil, fmt.Errorf("failed to serialize %T: %w", unstructuredSerialized, err)
}
if err := json.Unmarshal(unstructuredSerialized, typed); err != nil {
if err := kjson.UnmarshalCaseSensitivePreserveInts(unstructuredSerialized, typed); err != nil {
return nil, fmt.Errorf("failed to unmarshal the content of %T into %T: %w", u, typed, err)
}

Expand Down Expand Up @@ -556,7 +557,7 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.O
return err
}
zero(obj)
return json.Unmarshal(j, obj)
return kjson.UnmarshalCaseSensitivePreserveInts(j, obj)
}

func (c *fakeClient) Watch(ctx context.Context, list client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
Expand Down Expand Up @@ -620,7 +621,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
}
zero(obj)
objCopy := obj.DeepCopyObject().(client.ObjectList)
if err := json.Unmarshal(j, objCopy); err != nil {
if err := kjson.UnmarshalCaseSensitivePreserveInts(j, objCopy); err != nil {
return err
}

Expand Down Expand Up @@ -995,7 +996,7 @@ func (c *fakeClient) patch(obj client.Object, patch client.Patch, opts ...client
return err
}
zero(obj)
return json.Unmarshal(j, obj)
return kjson.UnmarshalCaseSensitivePreserveInts(j, obj)
}

// Applying a patch results in a deletionTimestamp that is truncated to the nearest second.
Expand Down Expand Up @@ -1047,7 +1048,7 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru
return nil, err
}

if err = json.Unmarshal(modified, obj); err != nil {
if err = kjson.UnmarshalCaseSensitivePreserveInts(modified, obj); err != nil {
return nil, err
}
case types.MergePatchType:
Expand All @@ -1056,15 +1057,15 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru
return nil, err
}

if err := json.Unmarshal(modified, obj); err != nil {
if err := kjson.UnmarshalCaseSensitivePreserveInts(modified, obj); err != nil {
return nil, err
}
case types.StrategicMergePatchType:
mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj)
if err != nil {
return nil, err
}
if err = json.Unmarshal(mergedByte, obj); err != nil {
if err = kjson.UnmarshalCaseSensitivePreserveInts(mergedByte, obj); err != nil {
return nil, err
}
case types.ApplyPatchType:
Expand Down Expand Up @@ -1121,7 +1122,7 @@ func toMapStringAny(obj runtime.Object) (map[string]any, error) {
}

u := map[string]any{}
return u, json.Unmarshal(serialized, &u)
return u, kjson.UnmarshalCaseSensitivePreserveInts(serialized, &u)
}

func fromMapStringAny(u map[string]any, target runtime.Object) error {
Expand All @@ -1136,7 +1137,7 @@ func fromMapStringAny(u map[string]any, target runtime.Object) error {
}

zero(target)
if err := json.Unmarshal(serialized, &target); err != nil {
if err := kjson.UnmarshalCaseSensitivePreserveInts(serialized, &target); err != nil {
return fmt.Errorf("failed to deserialize: %w", err)
}

Expand Down
Loading