Skip to content

Commit 0448598

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 c2583d3 commit 0448598

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

kustomize/resource_kustomization.go

+30-22
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
213213
mapping, err := mapper.RESTMapping(mu.GroupVersionKind().GroupKind(), mu.GroupVersionKind().Version)
214214
if err != nil {
215215
return logErrorForResource(
216-
ou,
216+
mu,
217217
fmt.Errorf("failed to query GVR: %s", err),
218218
)
219219
}
@@ -225,15 +225,15 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
225225
m)
226226
if err != nil {
227227
return logErrorForResource(
228-
ou,
228+
mu,
229229
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
230230
)
231231
}
232232

233-
patch, patchType, err := getPatch(ou.GroupVersionKind(), original, modified, current)
233+
patch, patchType, err := getPatch(mu.GroupVersionKind(), original, modified, current)
234234
if err != nil {
235235
return logErrorForResource(
236-
ou,
236+
mu,
237237
fmt.Errorf("getPatch failed: %s", err),
238238
)
239239
}
@@ -242,8 +242,8 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
242242

243243
_, err = client.
244244
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)
247247
if err != nil {
248248
// Handle specific invalid errors
249249
if k8serrors.IsInvalid(err) {
@@ -269,7 +269,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
269269
}
270270

271271
return logErrorForResource(
272-
ou,
272+
mu,
273273
fmt.Errorf("patch failed '%s': %s", patchType, err),
274274
)
275275
}
@@ -287,7 +287,7 @@ func kustomizationResourceExists(d *schema.ResourceData, m interface{}) (bool, e
287287
return false, logError(fmt.Errorf("JSON parse error: %s", err))
288288
}
289289

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

300300
_, err = client.
301-
Resource(mapping.Resource).
301+
Resource(mappings[0].Resource).
302302
Namespace(u.GetNamespace()).
303303
Get(context.TODO(), u.GetName(), k8smetav1.GetOptions{})
304304
if err != nil {
@@ -321,22 +321,28 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
321321
originalJSON, modifiedJSON := d.GetChange("manifest")
322322

323323
srcJSON := originalJSON.(string)
324-
u, err := parseJSON(srcJSON)
324+
ou, err := parseJSON(srcJSON)
325325
if err != nil {
326326
return logError(fmt.Errorf("JSON parse error: %s", err))
327327
}
328328

329329
if !d.HasChange("manifest") {
330330
return logErrorForResource(
331-
u,
331+
ou,
332332
errors.New("update called without diff"),
333333
)
334334
}
335335

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)
337343
if err != nil {
338344
return logErrorForResource(
339-
u,
345+
mu,
340346
fmt.Errorf("failed to query GVR: %s", err),
341347
)
342348
}
@@ -348,27 +354,27 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
348354
m)
349355
if err != nil {
350356
return logErrorForResource(
351-
u,
357+
mu,
352358
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
353359
)
354360
}
355361

356-
patch, patchType, err := getPatch(u.GroupVersionKind(), original, modified, current)
362+
patch, patchType, err := getPatch(mu.GroupVersionKind(), original, modified, current)
357363
if err != nil {
358364
return logErrorForResource(
359-
u,
365+
mu,
360366
fmt.Errorf("getPatch failed: %s", err),
361367
)
362368
}
363369

364370
var patchResp *unstructured.Unstructured
365371
patchResp, err = client.
366372
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{})
369375
if err != nil {
370376
return logErrorForResource(
371-
u,
377+
mu,
372378
fmt.Errorf("patch failed '%s': %s", patchType, err),
373379
)
374380
}
@@ -391,7 +397,9 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
391397
return logError(fmt.Errorf("JSON parse error: %s", err))
392398
}
393399

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())
395403
if err != nil {
396404
if k8smeta.IsNoMatchError(err) {
397405
// If the Kind does not exist in the K8s API,
@@ -405,7 +413,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
405413
name := u.GetName()
406414

407415
err = client.
408-
Resource(mapping.Resource).
416+
Resource(mappings[0].Resource).
409417
Namespace(namespace).
410418
Delete(context.TODO(), name, k8smetav1.DeleteOptions{})
411419
if err != nil {
@@ -427,7 +435,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
427435
Timeout: d.Timeout(schema.TimeoutDelete),
428436
Refresh: func() (interface{}, string, error) {
429437
resp, err := client.
430-
Resource(mapping.Resource).
438+
Resource(mappings[0].Resource).
431439
Namespace(namespace).
432440
Get(context.TODO(), name, k8smetav1.GetOptions{})
433441
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["_/Namespace/_/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)