Skip to content

Commit 11c0caa

Browse files
committed
Handle old api versions going away
When checking whether a resource exists, use any api version. Only need to use specific versions when modifying an existing resource.
1 parent 6bb84f8 commit 11c0caa

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

kustomize/provider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func Provider() *schema.Provider {
7373
"legacy_id_format": {
7474
Type: schema.TypeBool,
7575
Optional: true,
76-
Default: true,
77-
Deprecated: "legacy_id_format will be set to false in a future version and be removed in a further later version",
76+
Default: false,
77+
Deprecated: "legacy_id_format will be removed in a future version",
7878
Description: "If legacy_id_format is true, then resource IDs will look like group_version_kind|namespace|name. If legacy_id_format is false, then resource IDs will look like group/kind/namespace/name",
7979
},
8080
},

kustomize/resource_kustomization.go

+30-24
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,14 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
224224
true,
225225
m)
226226
if err != nil {
227-
return logErrorForResource(
228-
ou,
229-
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
230-
)
227+
d.ForceNew("manifest")
228+
return nil
231229
}
232230

233-
patch, patchType, err := getPatch(ou.GroupVersionKind(), original, modified, current)
231+
patch, patchType, err := getPatch(mu.GroupVersionKind(), original, modified, current)
234232
if err != nil {
235233
return logErrorForResource(
236-
ou,
234+
mu,
237235
fmt.Errorf("getPatch failed: %s", err),
238236
)
239237
}
@@ -242,8 +240,8 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
242240

243241
_, err = client.
244242
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)
247245
if err != nil {
248246
// Handle specific invalid errors
249247
if k8serrors.IsInvalid(err) {
@@ -269,7 +267,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
269267
}
270268

271269
return logErrorForResource(
272-
ou,
270+
mu,
273271
fmt.Errorf("patch failed '%s': %s", patchType, err),
274272
)
275273
}
@@ -287,7 +285,7 @@ func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, e
287285
return false, logError(fmt.Errorf("JSON parse error: %s", err))
288286
}
289287

290-
mapping, err := mapper.RESTMapping(u.GroupVersionKind().GroupKind(), u.GroupVersionKind().Version)
288+
mappings, err := mapper.RESTMappings(u.GroupVersionKind().GroupKind())
291289
if err != nil {
292290
if k8smeta.IsNoMatchError(err) {
293291
// If the Kind does not exist in the K8s API,
@@ -298,7 +296,7 @@ func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, e
298296
}
299297

300298
_, err = client.
301-
Resource(mapping.Resource).
299+
Resource(mappings[0].Resource).
302300
Namespace(u.GetNamespace()).
303301
Get(context.TODO(), u.GetName(), k8smetav1.GetOptions{})
304302
if err != nil {
@@ -321,22 +319,28 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
321319
originalJSON, modifiedJSON := d.GetChange("manifest")
322320

323321
srcJSON := originalJSON.(string)
324-
u, err := parseJSON(srcJSON)
322+
ou, err := parseJSON(srcJSON)
325323
if err != nil {
326324
return logError(fmt.Errorf("JSON parse error: %s", err))
327325
}
328326

329327
if !d.HasChange("manifest") {
330328
return logErrorForResource(
331-
u,
329+
ou,
332330
errors.New("update called without diff"),
333331
)
334332
}
335333

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)
337341
if err != nil {
338342
return logErrorForResource(
339-
u,
343+
mu,
340344
fmt.Errorf("failed to query GVR: %s", err),
341345
)
342346
}
@@ -348,27 +352,27 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
348352
m)
349353
if err != nil {
350354
return logErrorForResource(
351-
u,
355+
mu,
352356
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
353357
)
354358
}
355359

356-
patch, patchType, err := getPatch(u.GroupVersionKind(), original, modified, current)
360+
patch, patchType, err := getPatch(mu.GroupVersionKind(), original, modified, current)
357361
if err != nil {
358362
return logErrorForResource(
359-
u,
363+
mu,
360364
fmt.Errorf("getPatch failed: %s", err),
361365
)
362366
}
363367

364368
var patchResp *unstructured.Unstructured
365369
patchResp, err = client.
366370
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{})
369373
if err != nil {
370374
return logErrorForResource(
371-
u,
375+
mu,
372376
fmt.Errorf("patch failed '%s': %s", patchType, err),
373377
)
374378
}
@@ -391,7 +395,9 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
391395
return logError(fmt.Errorf("JSON parse error: %s", err))
392396
}
393397

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())
395401
if err != nil {
396402
if k8smeta.IsNoMatchError(err) {
397403
// If the Kind does not exist in the K8s API,
@@ -405,7 +411,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
405411
name := u.GetName()
406412

407413
err = client.
408-
Resource(mapping.Resource).
414+
Resource(mappings[0].Resource).
409415
Namespace(namespace).
410416
Delete(context.TODO(), name, k8smetav1.DeleteOptions{})
411417
if err != nil {
@@ -427,7 +433,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
427433
Timeout: d.Timeout(schema.TimeoutDelete),
428434
Refresh: func() (interface{}, string, error) {
429435
resp, err := client.
430-
Resource(mapping.Resource).
436+
Resource(mappings[0].Resource).
431437
Namespace(namespace).
432438
Get(context.TODO(), name, k8smetav1.GetOptions{})
433439
if err != nil {

kustomize/resource_kustomization_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func TestAccResourceKustomization_upgradeAPIVersion(t *testing.T) {
406406
}
407407

408408
func testAccResourceKustomizationConfig_upgradeAPIVersion(path string) string {
409-
return testAccDataSourceKustomizationConfig_basic(path) + `
409+
return testAccDataSourceKustomizationConfig_basic(path, false) + `
410410
resource "kustomization_resource" "ns" {
411411
manifest = data.kustomization_build.test.manifests["~G_~V_Namespace|~X|test-upgrade-api-version"]
412412
}

kustomize/util.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"runtime"
7+
"strings"
78

89
k8scorev1 "k8s.io/api/core/v1"
910
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -31,7 +32,7 @@ func setLastAppliedConfig(u *k8sunstructured.Unstructured, srcJSON string) {
3132
}
3233

3334
func getLastAppliedConfig(u *k8sunstructured.Unstructured) string {
34-
return u.GetAnnotations()[lastAppliedConfig]
35+
return strings.TrimRight(u.GetAnnotations()[lastAppliedConfig], "\r\n")
3536
}
3637

3738
func getOriginalModifiedCurrent(originalJSON string, modifiedJSON string, currentAllowNotFound bool, m interface{}) (original []byte, modified []byte, current []byte, err error) {

kustomize/util_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func TestLastAppliedConfig(t *testing.T) {
1818
annotations := u.GetAnnotations()
1919
count := len(annotations)
2020
if count != 1 {
21-
t.Errorf("TestLastAppliedConfig: incorect number of annotations, got: %d, want: %d.", count, 1)
21+
t.Errorf("TestLastAppliedConfig: incorrect number of annotations, got: %d, want: %d.", count, 1)
2222
}
2323

2424
lac := getLastAppliedConfig(u)
2525
if lac != srcJSON {
26-
t.Errorf("TestLastAppliedConfig: incorect annotation value, got: %s, want: %s.", srcJSON, lac)
26+
t.Errorf("TestLastAppliedConfig: incorrect annotation value, got: %s, want: %s.", srcJSON, lac)
2727
}
2828
}
2929

0 commit comments

Comments
 (0)