@@ -24,6 +24,7 @@ import (
24
24
"k8s.io/apimachinery/pkg/api/meta"
25
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
26
"k8s.io/apimachinery/pkg/runtime"
27
+ "k8s.io/apimachinery/pkg/runtime/schema"
27
28
"k8s.io/apimachinery/pkg/runtime/serializer"
28
29
"k8s.io/client-go/dynamic"
29
30
"k8s.io/client-go/kubernetes/scheme"
@@ -103,6 +104,16 @@ type client struct {
103
104
unstructuredClient unstructuredClient
104
105
}
105
106
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
+
106
117
// Create implements client.Client
107
118
func (c * client ) Create (ctx context.Context , obj runtime.Object , opts ... CreateOption ) error {
108
119
_ , ok := obj .(* unstructured.Unstructured )
@@ -114,6 +125,7 @@ func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateO
114
125
115
126
// Update implements client.Client
116
127
func (c * client ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
128
+ defer c .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
117
129
_ , ok := obj .(* unstructured.Unstructured )
118
130
if ok {
119
131
return c .unstructuredClient .Update (ctx , obj , opts ... )
@@ -141,6 +153,7 @@ func (c *client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...De
141
153
142
154
// Patch implements client.Client
143
155
func (c * client ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
156
+ defer c .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
144
157
_ , ok := obj .(* unstructured.Unstructured )
145
158
if ok {
146
159
return c .unstructuredClient .Patch (ctx , obj , patch , opts ... )
@@ -181,6 +194,7 @@ var _ StatusWriter = &statusWriter{}
181
194
182
195
// Update implements client.StatusWriter
183
196
func (sw * statusWriter ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
197
+ defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
184
198
_ , ok := obj .(* unstructured.Unstructured )
185
199
if ok {
186
200
return sw .client .unstructuredClient .UpdateStatus (ctx , obj , opts ... )
@@ -190,6 +204,7 @@ func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...
190
204
191
205
// Patch implements client.Client
192
206
func (sw * statusWriter ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
207
+ defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
193
208
_ , ok := obj .(* unstructured.Unstructured )
194
209
if ok {
195
210
return sw .client .unstructuredClient .PatchStatus (ctx , obj , patch , opts ... )
0 commit comments