@@ -121,14 +121,11 @@ func (a *Action) get(
121
121
ns string ,
122
122
out * interface {},
123
123
) error {
124
- kind , name := a .Get .KindName ()
125
- gvk := schema.GroupVersionKind {
126
- Kind : kind ,
127
- }
128
- res , err := c .gvrFromGVK (gvk )
124
+ res , err := c .gvrFromArg (a .Get .Arg )
129
125
if err != nil {
130
126
return err
131
127
}
128
+ name := a .Get .Name
132
129
if name == "" {
133
130
list , err := a .doList (ctx , c , res , ns )
134
131
if err == nil {
@@ -154,7 +151,7 @@ func (a *Action) doList(
154
151
resName := res .Resource
155
152
labelSelString := ""
156
153
opts := metav1.ListOptions {}
157
- withlabels := a .Get .Labels ()
154
+ withlabels := a .Get .Labels
158
155
if withlabels != nil {
159
156
// We already validated the label selector during parse-time
160
157
labelsStr := labels .Set (withlabels ).String ()
@@ -250,21 +247,30 @@ func (a *Action) create(
250
247
}
251
248
for _ , obj := range objs {
252
249
gvk := obj .GetObjectKind ().GroupVersionKind ()
253
- ons := obj .GetNamespace ()
254
- if ons == "" {
255
- ons = ns
256
- }
257
250
res , err := c .gvrFromGVK (gvk )
258
251
if err != nil {
259
252
return err
260
253
}
261
254
resName := res .Resource
262
- debug .Println (ctx , "kube.create: %s (ns: %s)" , resName , ons )
263
- obj , err := c .client .Resource (res ).Namespace (ons ).Create (
264
- ctx ,
265
- obj ,
266
- metav1.CreateOptions {},
267
- )
255
+ if c .resourceNamespaced (res ) {
256
+ ons := obj .GetNamespace ()
257
+ if ons == "" {
258
+ ons = ns
259
+ }
260
+ debug .Println (ctx , "kube.create: %s (ns: %s)" , resName , ons )
261
+ obj , err = c .client .Resource (res ).Namespace (ons ).Create (
262
+ ctx ,
263
+ obj ,
264
+ metav1.CreateOptions {},
265
+ )
266
+ } else {
267
+ debug .Println (ctx , "kube.create: %s (non-namespaced resource)" , resName )
268
+ obj , err = c .client .Resource (res ).Create (
269
+ ctx ,
270
+ obj ,
271
+ metav1.CreateOptions {},
272
+ )
273
+ }
268
274
if err != nil {
269
275
return err
270
276
}
@@ -314,28 +320,44 @@ func (a *Action) apply(
314
320
}
315
321
for _ , obj := range objs {
316
322
gvk := obj .GetObjectKind ().GroupVersionKind ()
317
- ons := obj .GetNamespace ()
318
- if ons == "" {
319
- ons = ns
320
- }
321
323
res , err := c .gvrFromGVK (gvk )
322
324
if err != nil {
323
325
return err
324
326
}
325
327
resName := res .Resource
326
- debug .Println (ctx , "kube.apply: %s (ns: %s)" , resName , ons )
327
- obj , err := c .client .Resource (res ).Namespace (ns ).Apply (
328
- ctx ,
329
- // NOTE(jaypipes): Not sure why a separate name argument is
330
- // necessary considering `obj` is of type
331
- // `*unstructured.Unstructured` and therefore has the `GetName()`
332
- // method...
333
- obj .GetName (),
334
- obj ,
335
- // TODO(jaypipes): Not sure if this hard-coded options struct is
336
- // always going to work. Maybe add ability to control it?
337
- metav1.ApplyOptions {FieldManager : fieldManagerName , Force : true },
338
- )
328
+ if c .resourceNamespaced (res ) {
329
+ ons := obj .GetNamespace ()
330
+ if ons == "" {
331
+ ons = ns
332
+ }
333
+ debug .Println (ctx , "kube.apply: %s (ns: %s)" , resName , ons )
334
+ obj , err = c .client .Resource (res ).Namespace (ns ).Apply (
335
+ ctx ,
336
+ // NOTE(jaypipes): Not sure why a separate name argument is
337
+ // necessary considering `obj` is of type
338
+ // `*unstructured.Unstructured` and therefore has the `GetName()`
339
+ // method...
340
+ obj .GetName (),
341
+ obj ,
342
+ // TODO(jaypipes): Not sure if this hard-coded options struct is
343
+ // always going to work. Maybe add ability to control it?
344
+ metav1.ApplyOptions {FieldManager : fieldManagerName , Force : true },
345
+ )
346
+ } else {
347
+ debug .Println (ctx , "kube.apply: %s (non-namespaced resource)" , resName )
348
+ obj , err = c .client .Resource (res ).Apply (
349
+ ctx ,
350
+ // NOTE(jaypipes): Not sure why a separate name argument is
351
+ // necessary considering `obj` is of type
352
+ // `*unstructured.Unstructured` and therefore has the `GetName()`
353
+ // method...
354
+ obj .GetName (),
355
+ obj ,
356
+ // TODO(jaypipes): Not sure if this hard-coded options struct is
357
+ // always going to work. Maybe add ability to control it?
358
+ metav1.ApplyOptions {FieldManager : fieldManagerName , Force : true },
359
+ )
360
+ }
339
361
if err != nil {
340
362
return err
341
363
}
@@ -385,14 +407,11 @@ func (a *Action) delete(
385
407
return nil
386
408
}
387
409
388
- kind , name := a .Delete .KindName ()
389
- gvk := schema.GroupVersionKind {
390
- Kind : kind ,
391
- }
392
- res , err := c .gvrFromGVK (gvk )
410
+ res , err := c .gvrFromArg (a .Delete .Arg )
393
411
if err != nil {
394
412
return err
395
413
}
414
+ name := a .Delete .Name
396
415
if name == "" {
397
416
return a .doDeleteCollection (ctx , c , res , ns )
398
417
}
@@ -408,11 +427,22 @@ func (a *Action) doDelete(
408
427
name string ,
409
428
) error {
410
429
resName := res .Resource
430
+ if c .resourceNamespaced (res ) {
431
+ debug .Println (
432
+ ctx , "kube.delete: %s/%s (ns: %s)" ,
433
+ resName , name , ns ,
434
+ )
435
+ return c .client .Resource (res ).Namespace (ns ).Delete (
436
+ ctx ,
437
+ name ,
438
+ metav1.DeleteOptions {},
439
+ )
440
+ }
411
441
debug .Println (
412
- ctx , "kube.delete: %s/%s (ns: %s )" ,
413
- resName , name , ns ,
442
+ ctx , "kube.delete: %s/%s (non-namespaced resource )" ,
443
+ resName , name ,
414
444
)
415
- return c .client .Resource (res ).Namespace ( ns ). Delete (
445
+ return c .client .Resource (res ).Delete (
416
446
ctx ,
417
447
name ,
418
448
metav1.DeleteOptions {},
@@ -428,7 +458,7 @@ func (a *Action) doDeleteCollection(
428
458
ns string ,
429
459
) error {
430
460
opts := metav1.ListOptions {}
431
- withlabels := a .Delete .Labels ()
461
+ withlabels := a .Delete .Labels
432
462
labelSelString := ""
433
463
if withlabels != nil {
434
464
// We already validated the label selector during parse-time
@@ -437,11 +467,22 @@ func (a *Action) doDeleteCollection(
437
467
opts .LabelSelector = labelsStr
438
468
}
439
469
resName := res .Resource
470
+ if c .resourceNamespaced (res ) {
471
+ debug .Println (
472
+ ctx , "kube.delete: %s%s (ns: %s)" ,
473
+ resName , labelSelString , ns ,
474
+ )
475
+ return c .client .Resource (res ).Namespace (ns ).DeleteCollection (
476
+ ctx ,
477
+ metav1.DeleteOptions {},
478
+ opts ,
479
+ )
480
+ }
440
481
debug .Println (
441
- ctx , "kube.delete: %s%s (ns: %s )" ,
442
- resName , labelSelString , ns ,
482
+ ctx , "kube.delete: %s%s (non-namespaced resource )" ,
483
+ resName , labelSelString ,
443
484
)
444
- return c .client .Resource (res ).Namespace ( ns ). DeleteCollection (
485
+ return c .client .Resource (res ).DeleteCollection (
445
486
ctx ,
446
487
metav1.DeleteOptions {},
447
488
opts ,
0 commit comments