Skip to content

Commit 304c53c

Browse files
authored
Merge pull request #2168 from vincepri/use-runtime-unstructured
🌱 Use runtime.Unstructured interface instead of Unstructured struct
2 parents 50edfaa + 4357393 commit 304c53c

File tree

9 files changed

+60
-63
lines changed

9 files changed

+60
-63
lines changed

pkg/cache/informer_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (ic *informerCache) objectTypeForListObject(list client.ObjectList) (*schem
102102
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
103103

104104
// Handle unstructured.UnstructuredList.
105-
if _, isUnstructured := list.(*unstructured.UnstructuredList); isUnstructured {
105+
if _, isUnstructured := list.(runtime.Unstructured); isUnstructured {
106106
u := &unstructured.Unstructured{}
107107
u.SetGroupVersionKind(gvk)
108108
return &gvk, u, nil

pkg/cache/internal/informers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
"k8s.io/apimachinery/pkg/api/meta"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3130
"k8s.io/apimachinery/pkg/runtime"
3231
"k8s.io/apimachinery/pkg/runtime/schema"
3332
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -307,7 +306,7 @@ func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj r
307306

308307
func (ip *Informers) informersByType(obj runtime.Object) map[schema.GroupVersionKind]*Cache {
309308
switch obj.(type) {
310-
case *unstructured.Unstructured, *unstructured.UnstructuredList:
309+
case runtime.Unstructured:
311310
return ip.tracker.Unstructured
312311
case *metav1.PartialObjectMetadata, *metav1.PartialObjectMetadataList:
313312
return ip.tracker.Metadata
@@ -394,7 +393,7 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
394393
//
395394
// Unstructured
396395
//
397-
case *unstructured.Unstructured, *unstructured.UnstructuredList:
396+
case runtime.Unstructured:
398397
// If the rest configuration has a negotiated serializer passed in,
399398
// we should remove it and use the one that the dynamic client sets for us.
400399
cfg := rest.CopyConfig(ip.config)

pkg/client/client.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"k8s.io/apimachinery/pkg/api/meta"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2928
"k8s.io/apimachinery/pkg/runtime"
3029
"k8s.io/apimachinery/pkg/runtime/schema"
3130
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -230,9 +229,8 @@ func (c *client) shouldBypassCache(obj runtime.Object) (bool, error) {
230229
return true, nil
231230
}
232231
if !c.cacheUnstructured {
233-
_, isUnstructured := obj.(*unstructured.Unstructured)
234-
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
235-
return isUnstructured || isUnstructuredList, nil
232+
_, isUnstructured := obj.(runtime.Unstructured)
233+
return isUnstructured, nil
236234
}
237235
return false, nil
238236
}
@@ -269,7 +267,7 @@ func (c *client) RESTMapper() meta.RESTMapper {
269267
// Create implements client.Client.
270268
func (c *client) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
271269
switch obj.(type) {
272-
case *unstructured.Unstructured:
270+
case runtime.Unstructured:
273271
return c.unstructuredClient.Create(ctx, obj, opts...)
274272
case *metav1.PartialObjectMetadata:
275273
return fmt.Errorf("cannot create using only metadata")
@@ -282,7 +280,7 @@ func (c *client) Create(ctx context.Context, obj Object, opts ...CreateOption) e
282280
func (c *client) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
283281
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
284282
switch obj.(type) {
285-
case *unstructured.Unstructured:
283+
case runtime.Unstructured:
286284
return c.unstructuredClient.Update(ctx, obj, opts...)
287285
case *metav1.PartialObjectMetadata:
288286
return fmt.Errorf("cannot update using only metadata -- did you mean to patch?")
@@ -294,7 +292,7 @@ func (c *client) Update(ctx context.Context, obj Object, opts ...UpdateOption) e
294292
// Delete implements client.Client.
295293
func (c *client) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
296294
switch obj.(type) {
297-
case *unstructured.Unstructured:
295+
case runtime.Unstructured:
298296
return c.unstructuredClient.Delete(ctx, obj, opts...)
299297
case *metav1.PartialObjectMetadata:
300298
return c.metadataClient.Delete(ctx, obj, opts...)
@@ -306,7 +304,7 @@ func (c *client) Delete(ctx context.Context, obj Object, opts ...DeleteOption) e
306304
// DeleteAllOf implements client.Client.
307305
func (c *client) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
308306
switch obj.(type) {
309-
case *unstructured.Unstructured:
307+
case runtime.Unstructured:
310308
return c.unstructuredClient.DeleteAllOf(ctx, obj, opts...)
311309
case *metav1.PartialObjectMetadata:
312310
return c.metadataClient.DeleteAllOf(ctx, obj, opts...)
@@ -319,7 +317,7 @@ func (c *client) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllO
319317
func (c *client) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
320318
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
321319
switch obj.(type) {
322-
case *unstructured.Unstructured:
320+
case runtime.Unstructured:
323321
return c.unstructuredClient.Patch(ctx, obj, patch, opts...)
324322
case *metav1.PartialObjectMetadata:
325323
return c.metadataClient.Patch(ctx, obj, patch, opts...)
@@ -337,7 +335,7 @@ func (c *client) Get(ctx context.Context, key ObjectKey, obj Object, opts ...Get
337335
}
338336

339337
switch obj.(type) {
340-
case *unstructured.Unstructured:
338+
case runtime.Unstructured:
341339
return c.unstructuredClient.Get(ctx, key, obj, opts...)
342340
case *metav1.PartialObjectMetadata:
343341
// Metadata only object should always preserve the GVK coming in from the caller.
@@ -357,7 +355,7 @@ func (c *client) List(ctx context.Context, obj ObjectList, opts ...ListOption) e
357355
}
358356

359357
switch x := obj.(type) {
360-
case *unstructured.UnstructuredList:
358+
case runtime.Unstructured:
361359
return c.unstructuredClient.List(ctx, obj, opts...)
362360
case *metav1.PartialObjectMetadataList:
363361
// Metadata only object should always preserve the GVK.
@@ -531,7 +529,7 @@ func (po *SubResourcePatchOptions) ApplyToSubResourcePatch(o *SubResourcePatchOp
531529

532530
func (sc *subResourceClient) Get(ctx context.Context, obj Object, subResource Object, opts ...SubResourceGetOption) error {
533531
switch obj.(type) {
534-
case *unstructured.Unstructured:
532+
case runtime.Unstructured:
535533
return sc.client.unstructuredClient.GetSubResource(ctx, obj, subResource, sc.subResource, opts...)
536534
case *metav1.PartialObjectMetadata:
537535
return errors.New("can not get subresource using only metadata")
@@ -546,7 +544,7 @@ func (sc *subResourceClient) Create(ctx context.Context, obj Object, subResource
546544
defer sc.client.resetGroupVersionKind(subResource, subResource.GetObjectKind().GroupVersionKind())
547545

548546
switch obj.(type) {
549-
case *unstructured.Unstructured:
547+
case runtime.Unstructured:
550548
return sc.client.unstructuredClient.CreateSubResource(ctx, obj, subResource, sc.subResource, opts...)
551549
case *metav1.PartialObjectMetadata:
552550
return fmt.Errorf("cannot update status using only metadata -- did you mean to patch?")
@@ -559,7 +557,7 @@ func (sc *subResourceClient) Create(ctx context.Context, obj Object, subResource
559557
func (sc *subResourceClient) Update(ctx context.Context, obj Object, opts ...SubResourceUpdateOption) error {
560558
defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
561559
switch obj.(type) {
562-
case *unstructured.Unstructured:
560+
case runtime.Unstructured:
563561
return sc.client.unstructuredClient.UpdateSubResource(ctx, obj, sc.subResource, opts...)
564562
case *metav1.PartialObjectMetadata:
565563
return fmt.Errorf("cannot update status using only metadata -- did you mean to patch?")
@@ -572,7 +570,7 @@ func (sc *subResourceClient) Update(ctx context.Context, obj Object, opts ...Sub
572570
func (sc *subResourceClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...SubResourcePatchOption) error {
573571
defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
574572
switch obj.(type) {
575-
case *unstructured.Unstructured:
573+
case runtime.Unstructured:
576574
return sc.client.unstructuredClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...)
577575
case *metav1.PartialObjectMetadata:
578576
return sc.client.metadataClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...)

pkg/client/client_rest_resources.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"k8s.io/apimachinery/pkg/api/meta"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2726
"k8s.io/apimachinery/pkg/runtime"
2827
"k8s.io/apimachinery/pkg/runtime/schema"
2928
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -82,9 +81,7 @@ func (c *clientRestResources) getResource(obj runtime.Object) (*resourceMeta, er
8281
return nil, err
8382
}
8483

85-
_, isUnstructured := obj.(*unstructured.Unstructured)
86-
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
87-
isUnstructured = isUnstructured || isUnstructuredList
84+
_, isUnstructured := obj.(runtime.Unstructured)
8885

8986
// It's better to do creation work twice than to not let multiple
9087
// people make requests at once

pkg/client/fake/client.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,22 @@ func (t versionedTracker) Create(gvr schema.GroupVersionResource, obj runtime.Ob
290290
return nil
291291
}
292292

293-
// convertFromUnstructuredIfNecessary will convert *unstructured.Unstructured for a GVK that is recocnized
293+
// convertFromUnstructuredIfNecessary will convert runtime.Unstructured for a GVK that is recocnized
294294
// by the schema into the whatever the schema produces with New() for said GVK.
295295
// This is required because the tracker unconditionally saves on manipulations, but its List() implementation
296296
// tries to assign whatever it finds into a ListType it gets from schema.New() - Thus we have to ensure
297297
// we save as the very same type, otherwise subsequent List requests will fail.
298298
func convertFromUnstructuredIfNecessary(s *runtime.Scheme, o runtime.Object) (runtime.Object, error) {
299-
u, isUnstructured := o.(*unstructured.Unstructured)
300-
if !isUnstructured || !s.Recognizes(u.GroupVersionKind()) {
299+
gvk := o.GetObjectKind().GroupVersionKind()
300+
301+
u, isUnstructured := o.(runtime.Unstructured)
302+
if !isUnstructured || !s.Recognizes(gvk) {
301303
return o, nil
302304
}
303305

304-
typed, err := s.New(u.GroupVersionKind())
306+
typed, err := s.New(gvk)
305307
if err != nil {
306-
return nil, fmt.Errorf("scheme recognizes %s but failed to produce an object for it: %w", u.GroupVersionKind().String(), err)
308+
return nil, fmt.Errorf("scheme recognizes %s but failed to produce an object for it: %w", gvk, err)
307309
}
308310

309311
unstructuredSerialized, err := json.Marshal(u)
@@ -436,7 +438,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
436438

437439
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
438440

439-
if _, isUnstructuredList := obj.(*unstructured.UnstructuredList); isUnstructuredList && !c.scheme.Recognizes(gvk) {
441+
if _, isUnstructuredList := obj.(runtime.Unstructured); isUnstructuredList && !c.scheme.Recognizes(gvk) {
440442
// We need to register the ListKind with UnstructuredList:
441443
// https://github.com/kubernetes/kubernetes/blob/7b2776b89fb1be28d4e9203bdeec079be903c103/staging/src/k8s.io/client-go/dynamic/fake/simple.go#L44-L51
442444
c.schemeWriteLock.Lock()

pkg/client/unstructured_client.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2524
"k8s.io/apimachinery/pkg/runtime"
2625
)
2726

@@ -35,12 +34,12 @@ type unstructuredClient struct {
3534

3635
// Create implements client.Client.
3736
func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
38-
u, ok := obj.(*unstructured.Unstructured)
37+
u, ok := obj.(runtime.Unstructured)
3938
if !ok {
4039
return fmt.Errorf("unstructured client did not understand object: %T", obj)
4140
}
4241

43-
gvk := u.GroupVersionKind()
42+
gvk := u.GetObjectKind().GroupVersionKind()
4443

4544
o, err := uc.resources.getObjMeta(obj)
4645
if err != nil {
@@ -58,18 +57,18 @@ func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...Cr
5857
Do(ctx).
5958
Into(obj)
6059

61-
u.SetGroupVersionKind(gvk)
60+
u.GetObjectKind().SetGroupVersionKind(gvk)
6261
return result
6362
}
6463

6564
// Update implements client.Client.
6665
func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
67-
u, ok := obj.(*unstructured.Unstructured)
66+
u, ok := obj.(runtime.Unstructured)
6867
if !ok {
6968
return fmt.Errorf("unstructured client did not understand object: %T", obj)
7069
}
7170

72-
gvk := u.GroupVersionKind()
71+
gvk := u.GetObjectKind().GroupVersionKind()
7372

7473
o, err := uc.resources.getObjMeta(obj)
7574
if err != nil {
@@ -88,13 +87,13 @@ func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...Up
8887
Do(ctx).
8988
Into(obj)
9089

91-
u.SetGroupVersionKind(gvk)
90+
u.GetObjectKind().SetGroupVersionKind(gvk)
9291
return result
9392
}
9493

9594
// Delete implements client.Client.
9695
func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
97-
if _, ok := obj.(*unstructured.Unstructured); !ok {
96+
if _, ok := obj.(runtime.Unstructured); !ok {
9897
return fmt.Errorf("unstructured client did not understand object: %T", obj)
9998
}
10099

@@ -117,7 +116,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De
117116

118117
// DeleteAllOf implements client.Client.
119118
func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
120-
if _, ok := obj.(*unstructured.Unstructured); !ok {
119+
if _, ok := obj.(runtime.Unstructured); !ok {
121120
return fmt.Errorf("unstructured client did not understand object: %T", obj)
122121
}
123122

@@ -140,7 +139,7 @@ func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts
140139

141140
// Patch implements client.Client.
142141
func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
143-
if _, ok := obj.(*unstructured.Unstructured); !ok {
142+
if _, ok := obj.(runtime.Unstructured); !ok {
144143
return fmt.Errorf("unstructured client did not understand object: %T", obj)
145144
}
146145

@@ -169,12 +168,12 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch
169168

170169
// Get implements client.Client.
171170
func (uc *unstructuredClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
172-
u, ok := obj.(*unstructured.Unstructured)
171+
u, ok := obj.(runtime.Unstructured)
173172
if !ok {
174173
return fmt.Errorf("unstructured client did not understand object: %T", obj)
175174
}
176175

177-
gvk := u.GroupVersionKind()
176+
gvk := u.GetObjectKind().GroupVersionKind()
178177

179178
getOpts := GetOptions{}
180179
getOpts.ApplyOptions(opts)
@@ -192,19 +191,19 @@ func (uc *unstructuredClient) Get(ctx context.Context, key ObjectKey, obj Object
192191
Do(ctx).
193192
Into(obj)
194193

195-
u.SetGroupVersionKind(gvk)
194+
u.GetObjectKind().SetGroupVersionKind(gvk)
196195

197196
return result
198197
}
199198

200199
// List implements client.Client.
201200
func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
202-
u, ok := obj.(*unstructured.UnstructuredList)
201+
u, ok := obj.(runtime.Unstructured)
203202
if !ok {
204203
return fmt.Errorf("unstructured client did not understand object: %T", obj)
205204
}
206205

207-
gvk := u.GroupVersionKind()
206+
gvk := u.GetObjectKind().GroupVersionKind()
208207
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
209208

210209
r, err := uc.resources.getResource(obj)
@@ -224,11 +223,11 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...
224223
}
225224

226225
func (uc *unstructuredClient) GetSubResource(ctx context.Context, obj, subResourceObj Object, subResource string, opts ...SubResourceGetOption) error {
227-
if _, ok := obj.(*unstructured.Unstructured); !ok {
226+
if _, ok := obj.(runtime.Unstructured); !ok {
228227
return fmt.Errorf("unstructured client did not understand object: %T", subResource)
229228
}
230229

231-
if _, ok := subResourceObj.(*unstructured.Unstructured); !ok {
230+
if _, ok := subResourceObj.(runtime.Unstructured); !ok {
232231
return fmt.Errorf("unstructured client did not understand object: %T", obj)
233232
}
234233

@@ -255,11 +254,11 @@ func (uc *unstructuredClient) GetSubResource(ctx context.Context, obj, subResour
255254
}
256255

257256
func (uc *unstructuredClient) CreateSubResource(ctx context.Context, obj, subResourceObj Object, subResource string, opts ...SubResourceCreateOption) error {
258-
if _, ok := obj.(*unstructured.Unstructured); !ok {
257+
if _, ok := obj.(runtime.Unstructured); !ok {
259258
return fmt.Errorf("unstructured client did not understand object: %T", subResourceObj)
260259
}
261260

262-
if _, ok := subResourceObj.(*unstructured.Unstructured); !ok {
261+
if _, ok := subResourceObj.(runtime.Unstructured); !ok {
263262
return fmt.Errorf("unstructured client did not understand object: %T", obj)
264263
}
265264

@@ -287,7 +286,7 @@ func (uc *unstructuredClient) CreateSubResource(ctx context.Context, obj, subRes
287286
}
288287

289288
func (uc *unstructuredClient) UpdateSubResource(ctx context.Context, obj Object, subResource string, opts ...SubResourceUpdateOption) error {
290-
if _, ok := obj.(*unstructured.Unstructured); !ok {
289+
if _, ok := obj.(runtime.Unstructured); !ok {
291290
return fmt.Errorf("unstructured client did not understand object: %T", obj)
292291
}
293292

@@ -322,12 +321,12 @@ func (uc *unstructuredClient) UpdateSubResource(ctx context.Context, obj Object,
322321
}
323322

324323
func (uc *unstructuredClient) PatchSubResource(ctx context.Context, obj Object, subResource string, patch Patch, opts ...SubResourcePatchOption) error {
325-
u, ok := obj.(*unstructured.Unstructured)
324+
u, ok := obj.(runtime.Unstructured)
326325
if !ok {
327326
return fmt.Errorf("unstructured client did not understand object: %T", obj)
328327
}
329328

330-
gvk := u.GroupVersionKind()
329+
gvk := u.GetObjectKind().GroupVersionKind()
331330

332331
o, err := uc.resources.getObjMeta(obj)
333332
if err != nil {
@@ -357,6 +356,6 @@ func (uc *unstructuredClient) PatchSubResource(ctx context.Context, obj Object,
357356
Do(ctx).
358357
Into(body)
359358

360-
u.SetGroupVersionKind(gvk)
359+
u.GetObjectKind().SetGroupVersionKind(gvk)
361360
return result
362361
}

0 commit comments

Comments
 (0)