@@ -18,14 +18,11 @@ package client
18
18
19
19
import (
20
20
"context"
21
- "errors"
22
21
"fmt"
23
22
24
23
"k8s.io/apimachinery/pkg/api/meta"
25
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
24
"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"
29
26
)
30
27
31
28
// NewNamespacedClient wraps an existing client enforcing the namespace value.
@@ -55,49 +52,9 @@ func (n *namespacedClient) RESTMapper() meta.RESTMapper {
55
52
return n .client .RESTMapper ()
56
53
}
57
54
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
-
98
55
// Create implements clinet.Client.
99
56
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 () )
101
58
if err != nil {
102
59
return fmt .Errorf ("error finding the scope of the object: %v" , err )
103
60
}
@@ -115,7 +72,7 @@ func (n *namespacedClient) Create(ctx context.Context, obj Object, opts ...Creat
115
72
116
73
// Update implements client.Client.
117
74
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 () )
119
76
if err != nil {
120
77
return fmt .Errorf ("error finding the scope of the object: %v" , err )
121
78
}
@@ -133,7 +90,7 @@ func (n *namespacedClient) Update(ctx context.Context, obj Object, opts ...Updat
133
90
134
91
// Delete implements client.Client.
135
92
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 () )
137
94
if err != nil {
138
95
return fmt .Errorf ("error finding the scope of the object: %v" , err )
139
96
}
@@ -151,7 +108,7 @@ func (n *namespacedClient) Delete(ctx context.Context, obj Object, opts ...Delet
151
108
152
109
// DeleteAllOf implements client.Client.
153
110
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 () )
155
112
if err != nil {
156
113
return fmt .Errorf ("error finding the scope of the object: %v" , err )
157
114
}
@@ -164,7 +121,7 @@ func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...
164
121
165
122
// Patch implements client.Client.
166
123
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 () )
168
125
if err != nil {
169
126
return fmt .Errorf ("error finding the scope of the object: %v" , err )
170
127
}
@@ -182,7 +139,7 @@ func (n *namespacedClient) Patch(ctx context.Context, obj Object, patch Patch, o
182
139
183
140
// Get implements client.Client.
184
141
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 () )
186
143
if err != nil {
187
144
return fmt .Errorf ("error finding the scope of the object: %v" , err )
188
145
}
@@ -219,7 +176,8 @@ type namespacedClientStatusWriter struct {
219
176
220
177
// Update implements client.StatusWriter.
221
178
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
+
223
181
if err != nil {
224
182
return fmt .Errorf ("error finding the scope of the object: %v" , err )
225
183
}
@@ -237,7 +195,8 @@ func (nsw *namespacedClientStatusWriter) Update(ctx context.Context, obj Object,
237
195
238
196
// Patch implements client.StatusWriter.
239
197
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
+
241
200
if err != nil {
242
201
return fmt .Errorf ("error finding the scope of the object: %v" , err )
243
202
}
0 commit comments