Skip to content

Commit ef5c8a3

Browse files
authored
Merge pull request #1573 from pigletfly/fix-is-namespaced
🌱 cleanup Use objectutil IsAPINamespaced
2 parents 985e819 + 67794ff commit ef5c8a3

File tree

1 file changed

+11
-52
lines changed

1 file changed

+11
-52
lines changed

pkg/client/namespaced_client.go

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ package client
1818

1919
import (
2020
"context"
21-
"errors"
2221
"fmt"
2322

2423
"k8s.io/apimachinery/pkg/api/meta"
25-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2624
"k8s.io/apimachinery/pkg/runtime"
27-
"k8s.io/apimachinery/pkg/runtime/schema"
28-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
25+
"sigs.k8s.io/controller-runtime/pkg/internal/objectutil"
2926
)
3027

3128
// NewNamespacedClient wraps an existing client enforcing the namespace value.
@@ -55,49 +52,9 @@ func (n *namespacedClient) RESTMapper() meta.RESTMapper {
5552
return n.client.RESTMapper()
5653
}
5754

58-
// isNamespaced returns true if the object is namespace scoped.
59-
// For unstructured objects the gvk is found from the object itself.
60-
// TODO: this is repetitive code. Remove this and use ojectutil.IsNamespaced.
61-
func isNamespaced(c Client, obj runtime.Object) (bool, error) {
62-
var gvk schema.GroupVersionKind
63-
var err error
64-
65-
_, isUnstructured := obj.(*unstructured.Unstructured)
66-
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
67-
68-
isUnstructured = isUnstructured || isUnstructuredList
69-
if isUnstructured {
70-
gvk = obj.GetObjectKind().GroupVersionKind()
71-
} else {
72-
gvk, err = apiutil.GVKForObject(obj, c.Scheme())
73-
if err != nil {
74-
return false, err
75-
}
76-
}
77-
78-
gk := schema.GroupKind{
79-
Group: gvk.Group,
80-
Kind: gvk.Kind,
81-
}
82-
restmapping, err := c.RESTMapper().RESTMapping(gk)
83-
if err != nil {
84-
return false, fmt.Errorf("failed to get restmapping: %w", err)
85-
}
86-
scope := restmapping.Scope.Name()
87-
88-
if scope == "" {
89-
return false, errors.New("scope cannot be identified, empty scope returned")
90-
}
91-
92-
if scope != meta.RESTScopeNameRoot {
93-
return true, nil
94-
}
95-
return false, nil
96-
}
97-
9855
// Create implements clinet.Client.
9956
func (n *namespacedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
100-
isNamespaceScoped, err := isNamespaced(n.client, obj)
57+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
10158
if err != nil {
10259
return fmt.Errorf("error finding the scope of the object: %v", err)
10360
}
@@ -115,7 +72,7 @@ func (n *namespacedClient) Create(ctx context.Context, obj Object, opts ...Creat
11572

11673
// Update implements client.Client.
11774
func (n *namespacedClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
118-
isNamespaceScoped, err := isNamespaced(n.client, obj)
75+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
11976
if err != nil {
12077
return fmt.Errorf("error finding the scope of the object: %v", err)
12178
}
@@ -133,7 +90,7 @@ func (n *namespacedClient) Update(ctx context.Context, obj Object, opts ...Updat
13390

13491
// Delete implements client.Client.
13592
func (n *namespacedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
136-
isNamespaceScoped, err := isNamespaced(n.client, obj)
93+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
13794
if err != nil {
13895
return fmt.Errorf("error finding the scope of the object: %v", err)
13996
}
@@ -151,7 +108,7 @@ func (n *namespacedClient) Delete(ctx context.Context, obj Object, opts ...Delet
151108

152109
// DeleteAllOf implements client.Client.
153110
func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
154-
isNamespaceScoped, err := isNamespaced(n.client, obj)
111+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
155112
if err != nil {
156113
return fmt.Errorf("error finding the scope of the object: %v", err)
157114
}
@@ -164,7 +121,7 @@ func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...
164121

165122
// Patch implements client.Client.
166123
func (n *namespacedClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
167-
isNamespaceScoped, err := isNamespaced(n.client, obj)
124+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
168125
if err != nil {
169126
return fmt.Errorf("error finding the scope of the object: %v", err)
170127
}
@@ -182,7 +139,7 @@ func (n *namespacedClient) Patch(ctx context.Context, obj Object, patch Patch, o
182139

183140
// Get implements client.Client.
184141
func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj Object) error {
185-
isNamespaceScoped, err := isNamespaced(n.client, obj)
142+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
186143
if err != nil {
187144
return fmt.Errorf("error finding the scope of the object: %v", err)
188145
}
@@ -219,7 +176,8 @@ type namespacedClientStatusWriter struct {
219176

220177
// Update implements client.StatusWriter.
221178
func (nsw *namespacedClientStatusWriter) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
222-
isNamespaceScoped, err := isNamespaced(nsw.namespacedclient, obj)
179+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, nsw.namespacedclient.Scheme(), nsw.namespacedclient.RESTMapper())
180+
223181
if err != nil {
224182
return fmt.Errorf("error finding the scope of the object: %v", err)
225183
}
@@ -237,7 +195,8 @@ func (nsw *namespacedClientStatusWriter) Update(ctx context.Context, obj Object,
237195

238196
// Patch implements client.StatusWriter.
239197
func (nsw *namespacedClientStatusWriter) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
240-
isNamespaceScoped, err := isNamespaced(nsw.namespacedclient, obj)
198+
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, nsw.namespacedclient.Scheme(), nsw.namespacedclient.RESTMapper())
199+
241200
if err != nil {
242201
return fmt.Errorf("error finding the scope of the object: %v", err)
243202
}

0 commit comments

Comments
 (0)