Skip to content

Commit 6ea8caf

Browse files
committed
Revert "remove unnecessary resetGroupVersionKind function and its calls"
This reverts commit d7d38e8.
1 parent d7d38e8 commit 6ea8caf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pkg/client/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/apimachinery/pkg/api/meta"
2525
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2626
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
2728
"k8s.io/apimachinery/pkg/runtime/serializer"
2829
"k8s.io/client-go/dynamic"
2930
"k8s.io/client-go/kubernetes/scheme"
@@ -103,6 +104,16 @@ type client struct {
103104
unstructuredClient unstructuredClient
104105
}
105106

107+
// resetGroupVersionKind is a helper function to restore and preserve GroupVersionKind on an object.
108+
// TODO(vincepri): Remove this function and its calls once controller-runtime dependencies are upgraded to 1.15.
109+
func (c *client) resetGroupVersionKind(obj runtime.Object, gvk schema.GroupVersionKind) {
110+
if gvk != schema.EmptyObjectKind.GroupVersionKind() {
111+
if v, ok := obj.(schema.ObjectKind); ok {
112+
v.SetGroupVersionKind(gvk)
113+
}
114+
}
115+
}
116+
106117
// Create implements client.Client
107118
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
108119
_, ok := obj.(*unstructured.Unstructured)
@@ -114,6 +125,7 @@ func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateO
114125

115126
// Update implements client.Client
116127
func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error {
128+
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
117129
_, ok := obj.(*unstructured.Unstructured)
118130
if ok {
119131
return c.unstructuredClient.Update(ctx, obj, opts...)
@@ -141,6 +153,7 @@ func (c *client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...De
141153

142154
// Patch implements client.Client
143155
func (c *client) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
156+
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
144157
_, ok := obj.(*unstructured.Unstructured)
145158
if ok {
146159
return c.unstructuredClient.Patch(ctx, obj, patch, opts...)
@@ -181,6 +194,7 @@ var _ StatusWriter = &statusWriter{}
181194

182195
// Update implements client.StatusWriter
183196
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error {
197+
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
184198
_, ok := obj.(*unstructured.Unstructured)
185199
if ok {
186200
return sw.client.unstructuredClient.UpdateStatus(ctx, obj, opts...)
@@ -190,6 +204,7 @@ func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...
190204

191205
// Patch implements client.Client
192206
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
207+
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
193208
_, ok := obj.(*unstructured.Unstructured)
194209
if ok {
195210
return sw.client.unstructuredClient.PatchStatus(ctx, obj, patch, opts...)

0 commit comments

Comments
 (0)