@@ -213,7 +213,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
213
213
mapping , err := mapper .RESTMapping (mu .GroupVersionKind ().GroupKind (), mu .GroupVersionKind ().Version )
214
214
if err != nil {
215
215
return logErrorForResource (
216
- ou ,
216
+ mu ,
217
217
fmt .Errorf ("failed to query GVR: %s" , err ),
218
218
)
219
219
}
@@ -225,15 +225,15 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
225
225
m )
226
226
if err != nil {
227
227
return logErrorForResource (
228
- ou ,
228
+ mu ,
229
229
fmt .Errorf ("getOriginalModifiedCurrent failed: %s" , err ),
230
230
)
231
231
}
232
232
233
- patch , patchType , err := getPatch (ou .GroupVersionKind (), original , modified , current )
233
+ patch , patchType , err := getPatch (mu .GroupVersionKind (), original , modified , current )
234
234
if err != nil {
235
235
return logErrorForResource (
236
- ou ,
236
+ mu ,
237
237
fmt .Errorf ("getPatch failed: %s" , err ),
238
238
)
239
239
}
@@ -242,8 +242,8 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
242
242
243
243
_ , err = client .
244
244
Resource (mapping .Resource ).
245
- Namespace (ou .GetNamespace ()).
246
- Patch (context .TODO (), ou .GetName (), patchType , patch , dryRunPatch )
245
+ Namespace (mu .GetNamespace ()).
246
+ Patch (context .TODO (), mu .GetName (), patchType , patch , dryRunPatch )
247
247
if err != nil {
248
248
// Handle specific invalid errors
249
249
if k8serrors .IsInvalid (err ) {
@@ -269,7 +269,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
269
269
}
270
270
271
271
return logErrorForResource (
272
- ou ,
272
+ mu ,
273
273
fmt .Errorf ("patch failed '%s': %s" , patchType , err ),
274
274
)
275
275
}
@@ -287,7 +287,7 @@ func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, e
287
287
return false , logError (fmt .Errorf ("JSON parse error: %s" , err ))
288
288
}
289
289
290
- mapping , err := mapper .RESTMapping (u .GroupVersionKind ().GroupKind (), u . GroupVersionKind (). Version )
290
+ mappings , err := mapper .RESTMappings (u .GroupVersionKind ().GroupKind ())
291
291
if err != nil {
292
292
if k8smeta .IsNoMatchError (err ) {
293
293
// If the Kind does not exist in the K8s API,
@@ -298,7 +298,7 @@ func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, e
298
298
}
299
299
300
300
_ , err = client .
301
- Resource (mapping .Resource ).
301
+ Resource (mappings [ 0 ] .Resource ).
302
302
Namespace (u .GetNamespace ()).
303
303
Get (context .TODO (), u .GetName (), k8smetav1.GetOptions {})
304
304
if err != nil {
@@ -321,22 +321,28 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
321
321
originalJSON , modifiedJSON := d .GetChange ("manifest" )
322
322
323
323
srcJSON := originalJSON .(string )
324
- u , err := parseJSON (srcJSON )
324
+ ou , err := parseJSON (srcJSON )
325
325
if err != nil {
326
326
return logError (fmt .Errorf ("JSON parse error: %s" , err ))
327
327
}
328
328
329
329
if ! d .HasChange ("manifest" ) {
330
330
return logErrorForResource (
331
- u ,
331
+ ou ,
332
332
errors .New ("update called without diff" ),
333
333
)
334
334
}
335
335
336
- mapping , err := mapper .RESTMapping (u .GroupVersionKind ().GroupKind (), u .GroupVersionKind ().Version )
336
+ modifiedSrcJSON := modifiedJSON .(string )
337
+ mu , err := parseJSON (modifiedSrcJSON )
338
+ if err != nil {
339
+ return logError (fmt .Errorf ("JSON parse error: %s" , err ))
340
+ }
341
+
342
+ mapping , err := mapper .RESTMapping (mu .GroupVersionKind ().GroupKind (), mu .GroupVersionKind ().Version )
337
343
if err != nil {
338
344
return logErrorForResource (
339
- u ,
345
+ mu ,
340
346
fmt .Errorf ("failed to query GVR: %s" , err ),
341
347
)
342
348
}
@@ -348,27 +354,27 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
348
354
m )
349
355
if err != nil {
350
356
return logErrorForResource (
351
- u ,
357
+ mu ,
352
358
fmt .Errorf ("getOriginalModifiedCurrent failed: %s" , err ),
353
359
)
354
360
}
355
361
356
- patch , patchType , err := getPatch (u .GroupVersionKind (), original , modified , current )
362
+ patch , patchType , err := getPatch (mu .GroupVersionKind (), original , modified , current )
357
363
if err != nil {
358
364
return logErrorForResource (
359
- u ,
365
+ mu ,
360
366
fmt .Errorf ("getPatch failed: %s" , err ),
361
367
)
362
368
}
363
369
364
370
var patchResp * unstructured.Unstructured
365
371
patchResp , err = client .
366
372
Resource (mapping .Resource ).
367
- Namespace (u .GetNamespace ()).
368
- Patch (context .TODO (), u .GetName (), patchType , patch , k8smetav1.PatchOptions {})
373
+ Namespace (mu .GetNamespace ()).
374
+ Patch (context .TODO (), mu .GetName (), patchType , patch , k8smetav1.PatchOptions {})
369
375
if err != nil {
370
376
return logErrorForResource (
371
- u ,
377
+ mu ,
372
378
fmt .Errorf ("patch failed '%s': %s" , patchType , err ),
373
379
)
374
380
}
@@ -391,7 +397,9 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
391
397
return logError (fmt .Errorf ("JSON parse error: %s" , err ))
392
398
}
393
399
394
- mapping , err := mapper .RESTMapping (u .GroupVersionKind ().GroupKind (), u .GroupVersionKind ().Version )
400
+ // look for all versions of the GroupKind in case the resource uses a
401
+ // version that is no longer current
402
+ mappings , err := mapper .RESTMappings (u .GroupVersionKind ().GroupKind ())
395
403
if err != nil {
396
404
if k8smeta .IsNoMatchError (err ) {
397
405
// If the Kind does not exist in the K8s API,
@@ -405,7 +413,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
405
413
name := u .GetName ()
406
414
407
415
err = client .
408
- Resource (mapping .Resource ).
416
+ Resource (mappings [ 0 ] .Resource ).
409
417
Namespace (namespace ).
410
418
Delete (context .TODO (), name , k8smetav1.DeleteOptions {})
411
419
if err != nil {
@@ -427,7 +435,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
427
435
Timeout : d .Timeout (schema .TimeoutDelete ),
428
436
Refresh : func () (interface {}, string , error ) {
429
437
resp , err := client .
430
- Resource (mapping .Resource ).
438
+ Resource (mappings [ 0 ] .Resource ).
431
439
Namespace (namespace ).
432
440
Get (context .TODO (), name , k8smetav1.GetOptions {})
433
441
if err != nil {
0 commit comments