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