@@ -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,63 +95,64 @@ 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 )
98
+ func (n * namespacedClient ) Create (ctx context.Context , obj Object , opts ... CreateOption ) error {
99
+ isNamespaceScoped , err := isNamespaced ( n . client , obj )
99
100
if err != nil {
100
- return err
101
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
101
102
}
102
103
103
- isNamespaceScoped , err := isNamespaced ( n . client , obj )
104
- if err != nil {
105
- return fmt .Errorf ("error finding the scope of the object %v " , err )
104
+ objectNamespace := obj . GetNamespace ( )
105
+ if objectNamespace != n . namespace && objectNamespace != "" {
106
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), n . namespace )
106
107
}
107
- if isNamespaceScoped {
108
- metaObj .SetNamespace (n .namespace )
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
+ func (n * namespacedClient ) Update (ctx context.Context , obj Object , opts ... UpdateOption ) error {
117
+ isNamespaceScoped , err := isNamespaced ( n . client , obj )
116
118
if err != nil {
117
- return err
119
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
118
120
}
119
121
120
- isNamespaceScoped , err := isNamespaced ( n . client , obj )
121
- if err != nil {
122
- return fmt .Errorf ("error finding the scope of the object %v " , err )
122
+ objectNamespace := obj . GetNamespace ( )
123
+ if objectNamespace != n . namespace && objectNamespace != "" {
124
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), n . namespace )
123
125
}
124
126
125
- if isNamespaceScoped && metaObj . GetNamespace () != n . namespace {
126
- metaObj .SetNamespace (n .namespace )
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
+ func (n * namespacedClient ) Delete (ctx context.Context , obj Object , opts ... DeleteOption ) error {
135
+ isNamespaceScoped , err := isNamespaced ( n . client , obj )
134
136
if err != nil {
135
- return err
137
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
136
138
}
137
139
138
- isNamespaceScoped , err := isNamespaced ( n . client , obj )
139
- if err != nil {
140
- return fmt .Errorf ("error finding the scope of the object %v " , err )
140
+ objectNamespace := obj . GetNamespace ( )
141
+ if objectNamespace != n . namespace && objectNamespace != "" {
142
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), n . namespace )
141
143
}
142
144
143
- if isNamespaceScoped && metaObj . GetNamespace () != n . namespace {
144
- metaObj .SetNamespace (n .namespace )
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
- return fmt .Errorf ("error finding the scope of the object %v" , err )
155
+ return fmt .Errorf ("error finding the scope of the object: %v" , err )
154
156
}
155
157
156
158
if isNamespaceScoped {
@@ -160,37 +162,40 @@ 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
+ func (n * namespacedClient ) Patch (ctx context.Context , obj Object , patch Patch , opts ... PatchOption ) error {
166
+ isNamespaceScoped , err := isNamespaced ( n . client , obj )
165
167
if err != nil {
166
- return err
168
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
167
169
}
168
170
169
- isNamespaceScoped , err := isNamespaced ( n . client , obj )
170
- if err != nil {
171
- return fmt .Errorf ("error finding the scope of the object %v " , err )
171
+ objectNamespace := obj . GetNamespace ( )
172
+ if objectNamespace != n . namespace && objectNamespace != "" {
173
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), n . namespace )
172
174
}
173
175
174
- if isNamespaceScoped && metaObj . GetNamespace () != n . namespace {
175
- metaObj .SetNamespace (n .namespace )
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
- return fmt .Errorf ("error finding the scope of the object %v" , err )
186
+ return fmt .Errorf ("error finding the scope of the object: %v" , err )
185
187
}
186
188
if isNamespaceScoped {
189
+ if key .Namespace != "" && key .Namespace != n .namespace {
190
+ return fmt .Errorf ("Namespace %s provided for the object %s does not match the namesapce %s on the client" , key .Namespace , obj .GetName (), n .namespace )
191
+ }
187
192
key .Namespace = n .namespace
188
193
}
189
194
return n .client .Get (ctx , key , obj )
190
195
}
191
196
192
197
// List implements client.Client
193
- func (n * namespacedClient ) List (ctx context.Context , obj runtime. Object , opts ... ListOption ) error {
198
+ func (n * namespacedClient ) List (ctx context.Context , obj ObjectList , opts ... ListOption ) error {
194
199
if n .namespace != "" {
195
200
opts = append (opts , InNamespace (n .namespace ))
196
201
}
@@ -212,37 +217,37 @@ type namespacedClientStatusWriter struct {
212
217
}
213
218
214
219
// Update implements client.StatusWriter
215
- func (nsw * namespacedClientStatusWriter ) Update (ctx context.Context , obj runtime. Object , opts ... UpdateOption ) error {
216
- metaObj , err := meta . Accessor ( obj )
220
+ func (nsw * namespacedClientStatusWriter ) Update (ctx context.Context , obj Object , opts ... UpdateOption ) error {
221
+ isNamespaceScoped , err := isNamespaced ( nsw . namespacedclient , obj )
217
222
if err != nil {
218
- return err
223
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
219
224
}
220
225
221
- isNamespaceScoped , err := isNamespaced ( nsw . namespacedclient , obj )
222
- if err != nil {
223
- return fmt .Errorf ("error finding the scope of the object %v " , err )
226
+ objectNamespace := obj . GetNamespace ( )
227
+ if objectNamespace != nsw . namespace && objectNamespace != "" {
228
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), nsw . namespace )
224
229
}
225
230
226
- if isNamespaceScoped && metaObj . GetNamespace () != nsw . namespace {
227
- metaObj .SetNamespace (nsw .namespace )
231
+ if isNamespaceScoped && objectNamespace == "" {
232
+ obj .SetNamespace (nsw .namespace )
228
233
}
229
234
return nsw .StatusClient .Update (ctx , obj , opts ... )
230
235
}
231
236
232
237
// 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 )
238
+ func (nsw * namespacedClientStatusWriter ) Patch (ctx context.Context , obj Object , patch Patch , opts ... PatchOption ) error {
239
+ isNamespaceScoped , err := isNamespaced ( nsw . namespacedclient , obj )
235
240
if err != nil {
236
- return err
241
+ return fmt . Errorf ( "error finding the scope of the object: %v" , err )
237
242
}
238
243
239
- isNamespaceScoped , err := isNamespaced ( nsw . namespacedclient , obj )
240
- if err != nil {
241
- return fmt .Errorf ("error finding the scope of the object %v " , err )
244
+ objectNamespace := obj . GetNamespace ( )
245
+ if objectNamespace != nsw . namespace && objectNamespace != "" {
246
+ return fmt .Errorf ("Namespace %s of the object %s does not match the namespace %s on the client " , objectNamespace , obj . GetName (), nsw . namespace )
242
247
}
243
248
244
- if isNamespaceScoped && metaObj . GetNamespace () != nsw . namespace {
245
- metaObj .SetNamespace (nsw .namespace )
249
+ if isNamespaceScoped && objectNamespace == "" {
250
+ obj .SetNamespace (nsw .namespace )
246
251
}
247
252
return nsw .StatusClient .Patch (ctx , obj , patch , opts ... )
248
253
}
0 commit comments