@@ -18,6 +18,7 @@ package client
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
23
24
"k8s.io/apimachinery/pkg/api/meta"
@@ -84,7 +85,7 @@ func isNamespaced(c Client, obj runtime.Object) (bool, error) {
84
85
scope := restmapping .Scope .Name ()
85
86
86
87
if scope == "" {
87
- return false , fmt . Errorf ("Scope cannot be identified. Empty scope returned" )
88
+ return false , errors . New ("Scope cannot be identified. Empty scope returned" )
88
89
}
89
90
90
91
if scope != meta .RESTScopeNameRoot {
@@ -94,60 +95,61 @@ func isNamespaced(c Client, obj runtime.Object) (bool, error) {
94
95
}
95
96
96
97
// Create implements clinet.Client
97
- func (n * namespacedClient ) Create (ctx context.Context , obj runtime.Object , opts ... CreateOption ) error {
98
- metaObj , err := meta .Accessor (obj )
99
- if err != nil {
100
- return err
101
- }
102
-
98
+ func (n * namespacedClient ) Create (ctx context.Context , obj Object , opts ... CreateOption ) error {
103
99
isNamespaceScoped , err := isNamespaced (n .client , obj )
104
100
if err != nil {
105
101
return fmt .Errorf ("error finding the scope of the object %v" , err )
106
102
}
107
- if isNamespaceScoped {
108
- metaObj .SetNamespace (n .namespace )
103
+
104
+ objectNamespace := obj .GetNamespace ()
105
+ if objectNamespace != n .namespace && objectNamespace != "" {
106
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), n .namespace )
107
+ }
108
+
109
+ if isNamespaceScoped && objectNamespace == "" {
110
+ obj .SetNamespace (n .namespace )
109
111
}
110
112
return n .client .Create (ctx , obj , opts ... )
111
113
}
112
114
113
115
// Update implements client.Client
114
- func (n * namespacedClient ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
115
- metaObj , err := meta .Accessor (obj )
116
- if err != nil {
117
- return err
118
- }
119
-
116
+ func (n * namespacedClient ) Update (ctx context.Context , obj Object , opts ... UpdateOption ) error {
120
117
isNamespaceScoped , err := isNamespaced (n .client , obj )
121
118
if err != nil {
122
119
return fmt .Errorf ("error finding the scope of the object %v" , err )
123
120
}
124
121
125
- if isNamespaceScoped && metaObj .GetNamespace () != n .namespace {
126
- metaObj .SetNamespace (n .namespace )
122
+ objectNamespace := obj .GetNamespace ()
123
+ if objectNamespace != n .namespace && objectNamespace != "" {
124
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), n .namespace )
125
+ }
126
+
127
+ if isNamespaceScoped && objectNamespace == "" {
128
+ obj .SetNamespace (n .namespace )
127
129
}
128
130
return n .client .Update (ctx , obj , opts ... )
129
131
}
130
132
131
133
// Delete implements client.Client
132
- func (n * namespacedClient ) Delete (ctx context.Context , obj runtime.Object , opts ... DeleteOption ) error {
133
- metaObj , err := meta .Accessor (obj )
134
- if err != nil {
135
- return err
136
- }
137
-
134
+ func (n * namespacedClient ) Delete (ctx context.Context , obj Object , opts ... DeleteOption ) error {
138
135
isNamespaceScoped , err := isNamespaced (n .client , obj )
139
136
if err != nil {
140
137
return fmt .Errorf ("error finding the scope of the object %v" , err )
141
138
}
142
139
143
- if isNamespaceScoped && metaObj .GetNamespace () != n .namespace {
144
- metaObj .SetNamespace (n .namespace )
140
+ objectNamespace := obj .GetNamespace ()
141
+ if objectNamespace != n .namespace && objectNamespace != "" {
142
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), n .namespace )
143
+ }
144
+
145
+ if isNamespaceScoped && objectNamespace == "" {
146
+ obj .SetNamespace (n .namespace )
145
147
}
146
148
return n .client .Delete (ctx , obj , opts ... )
147
149
}
148
150
149
151
// DeleteAllOf implements client.Client
150
- func (n * namespacedClient ) DeleteAllOf (ctx context.Context , obj runtime. Object , opts ... DeleteAllOfOption ) error {
152
+ func (n * namespacedClient ) DeleteAllOf (ctx context.Context , obj Object , opts ... DeleteAllOfOption ) error {
151
153
isNamespaceScoped , err := isNamespaced (n .client , obj )
152
154
if err != nil {
153
155
return fmt .Errorf ("error finding the scope of the object %v" , err )
@@ -160,25 +162,25 @@ func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj runtime.Object,
160
162
}
161
163
162
164
// Patch implements client.Client
163
- func (n * namespacedClient ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
164
- metaObj , err := meta .Accessor (obj )
165
- if err != nil {
166
- return err
167
- }
168
-
165
+ func (n * namespacedClient ) Patch (ctx context.Context , obj Object , patch Patch , opts ... PatchOption ) error {
169
166
isNamespaceScoped , err := isNamespaced (n .client , obj )
170
167
if err != nil {
171
168
return fmt .Errorf ("error finding the scope of the object %v" , err )
172
169
}
173
170
174
- if isNamespaceScoped && metaObj .GetNamespace () != n .namespace {
175
- metaObj .SetNamespace (n .namespace )
171
+ objectNamespace := obj .GetNamespace ()
172
+ if objectNamespace != n .namespace && objectNamespace != "" {
173
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), n .namespace )
174
+ }
175
+
176
+ if isNamespaceScoped && objectNamespace == "" {
177
+ obj .SetNamespace (n .namespace )
176
178
}
177
179
return n .client .Patch (ctx , obj , patch , opts ... )
178
180
}
179
181
180
182
// Get implements client.Client
181
- func (n * namespacedClient ) Get (ctx context.Context , key ObjectKey , obj runtime. Object ) error {
183
+ func (n * namespacedClient ) Get (ctx context.Context , key ObjectKey , obj Object ) error {
182
184
isNamespaceScoped , err := isNamespaced (n .client , obj )
183
185
if err != nil {
184
186
return fmt .Errorf ("error finding the scope of the object %v" , err )
@@ -190,7 +192,7 @@ func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj runtime.O
190
192
}
191
193
192
194
// List implements client.Client
193
- func (n * namespacedClient ) List (ctx context.Context , obj runtime. Object , opts ... ListOption ) error {
195
+ func (n * namespacedClient ) List (ctx context.Context , obj ObjectList , opts ... ListOption ) error {
194
196
if n .namespace != "" {
195
197
opts = append (opts , InNamespace (n .namespace ))
196
198
}
@@ -212,37 +214,37 @@ type namespacedClientStatusWriter struct {
212
214
}
213
215
214
216
// Update implements client.StatusWriter
215
- func (nsw * namespacedClientStatusWriter ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
216
- metaObj , err := meta .Accessor (obj )
217
- if err != nil {
218
- return err
219
- }
220
-
217
+ func (nsw * namespacedClientStatusWriter ) Update (ctx context.Context , obj Object , opts ... UpdateOption ) error {
221
218
isNamespaceScoped , err := isNamespaced (nsw .namespacedclient , obj )
222
219
if err != nil {
223
220
return fmt .Errorf ("error finding the scope of the object %v" , err )
224
221
}
225
222
226
- if isNamespaceScoped && metaObj .GetNamespace () != nsw .namespace {
227
- metaObj .SetNamespace (nsw .namespace )
223
+ objectNamespace := obj .GetNamespace ()
224
+ if objectNamespace != nsw .namespace && objectNamespace != "" {
225
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), nsw .namespace )
226
+ }
227
+
228
+ if isNamespaceScoped && objectNamespace == "" {
229
+ obj .SetNamespace (nsw .namespace )
228
230
}
229
231
return nsw .StatusClient .Update (ctx , obj , opts ... )
230
232
}
231
233
232
234
// Patch implements client.StatusWriter
233
- func (nsw * namespacedClientStatusWriter ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
234
- metaObj , err := meta .Accessor (obj )
235
- if err != nil {
236
- return err
237
- }
238
-
235
+ func (nsw * namespacedClientStatusWriter ) Patch (ctx context.Context , obj Object , patch Patch , opts ... PatchOption ) error {
239
236
isNamespaceScoped , err := isNamespaced (nsw .namespacedclient , obj )
240
237
if err != nil {
241
238
return fmt .Errorf ("error finding the scope of the object %v" , err )
242
239
}
243
240
244
- if isNamespaceScoped && metaObj .GetNamespace () != nsw .namespace {
245
- metaObj .SetNamespace (nsw .namespace )
241
+ objectNamespace := obj .GetNamespace ()
242
+ if objectNamespace != nsw .namespace && objectNamespace != "" {
243
+ return fmt .Errorf ("Namespace %s of the object %s does not match %s" , objectNamespace , obj .GetName (), nsw .namespace )
244
+ }
245
+
246
+ if isNamespaceScoped && objectNamespace == "" {
247
+ obj .SetNamespace (nsw .namespace )
246
248
}
247
249
return nsw .StatusClient .Patch (ctx , obj , patch , opts ... )
248
250
}
0 commit comments