Skip to content

Allow kubernetes apiVersion upgrades #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kustomize/data_source_kustomization_overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down
4 changes: 2 additions & 2 deletions kustomize/data_source_kustomization_overlay_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ data "kustomization_overlay" "test2" {
}

output "check1" {
value = data.kustomization_overlay.test1.manifests["~G_v1_Namespace|~X|test-overlay1"]
value = data.kustomization_overlay.test1.manifests["_/Namespace/_/test-overlay1"]
}

output "check2" {
value = data.kustomization_overlay.test2.manifests["~G_v1_Namespace|~X|test-overlay2"]
value = data.kustomization_overlay.test2.manifests["_/Namespace/_/test-overlay2"]
}

`
Expand Down
44 changes: 22 additions & 22 deletions kustomize/data_source_kustomization_overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_Namespace|~X|test-basic"]
value = data.kustomization_overlay.test.manifests["_/Namespace/_/test-basic"]
}
`
}
Expand Down Expand Up @@ -162,7 +162,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_Namespace|~X|test-basic"]
value = data.kustomization_overlay.test.manifests["_/Namespace/_/test-basic"]
}
`
}
Expand Down Expand Up @@ -195,7 +195,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_ConfigMap|~X|from-component-6ct58987ht"]
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/from-component-6ct58987ht"]
}
`
}
Expand Down Expand Up @@ -252,11 +252,11 @@ data "kustomization_overlay" "test" {
}

output "check_cm1" {
value = data.kustomization_overlay.test.manifests["~G_v1_ConfigMap|~X|test-configmap1"]
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/test-configmap1"]
}

output "check_cm2" {
value = data.kustomization_overlay.test.manifests["~G_v1_ConfigMap|~X|test-configmap2-5tgmgc9cmf"]
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/test-configmap2-5tgmgc9cmf"]
}
`
}
Expand Down Expand Up @@ -291,7 +291,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_Namespace|~X|test-overlay-namespace"]
value = data.kustomization_overlay.test.manifests["_/Namespace/_/test-overlay-namespace"]
}

`
Expand Down Expand Up @@ -328,7 +328,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_Service|test-basic|test-test-test"]
value = data.kustomization_overlay.test.manifests["_/Service/test-basic/test-test-test"]
}

`
Expand Down Expand Up @@ -362,7 +362,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_Namespace|~X|test-basic"]
value = data.kustomization_overlay.test.manifests["_/Namespace/_/test-basic"]
}

`
Expand Down Expand Up @@ -398,7 +398,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["apps_v1_Deployment|test-transformer-config|test"]
value = data.kustomization_overlay.test.manifests["apps/Deployment/test-transformer-config/test"]
}
`
}
Expand Down Expand Up @@ -459,7 +459,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["~G_v1_ConfigMap|~X|testcm-6ct58987ht"]
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/testcm-6ct58987ht"]
}
`
}
Expand Down Expand Up @@ -511,11 +511,11 @@ data "kustomization_overlay" "test" {
}

output "check_configmap" {
value = data.kustomization_overlay.test.manifests["~G_v1_ConfigMap|~X|test-configmap"]
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/test-configmap"]
}

output "check_secret" {
value = data.kustomization_overlay.test.manifests["~G_v1_Secret|~X|test-secret"]
value = data.kustomization_overlay.test.manifests["_/Secret/_/test-secret"]
}
`
}
Expand Down Expand Up @@ -553,7 +553,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["apps_v1_Deployment|test-basic|test"]
value = data.kustomization_overlay.test.manifests["apps/Deployment/test-basic/test"]
}
`
}
Expand Down Expand Up @@ -610,11 +610,11 @@ data "kustomization_overlay" "test" {
}

output "check_dep" {
value = data.kustomization_overlay.test.manifests["apps_v1_Deployment|test-basic|test"]
value = data.kustomization_overlay.test.manifests["apps/Deployment/test-basic/test"]
}

output "check_ingress" {
value = data.kustomization_overlay.test.manifests["networking.k8s.io_v1beta1_Ingress|test-basic|test"]
value = data.kustomization_overlay.test.manifests["networking.k8s.io/Ingress/test-basic/test"]
}
`
}
Expand Down Expand Up @@ -650,7 +650,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["apps_v1_Deployment|test-basic|test"]
value = data.kustomization_overlay.test.manifests["apps/Deployment/test-basic/test"]
}
`
}
Expand Down Expand Up @@ -707,11 +707,11 @@ data "kustomization_overlay" "test" {
}

output "check_cm1" {
value = data.kustomization_overlay.test.manifests["~G_v1_Secret|~X|test-secret1"]
value = data.kustomization_overlay.test.manifests["_/Secret/_/test-secret1"]
}

output "check_cm2" {
value = data.kustomization_overlay.test.manifests["~G_v1_Secret|~X|test-secret2-h55cfd6gfg"]
value = data.kustomization_overlay.test.manifests["_/Secret/_/test-secret2-h55cfd6gfg"]
}
`
}
Expand Down Expand Up @@ -768,7 +768,7 @@ data "kustomization_overlay" "test" {
}

output "check" {
value = data.kustomization_overlay.test.manifests["apps_v1_Deployment|test-basic|test"]
value = data.kustomization_overlay.test.manifests["apps/Deployment/test-basic/test"]
}
`
}
Expand Down Expand Up @@ -895,15 +895,15 @@ module "test" {
}

output "check_dep" {
value = module.test.kustomization.manifests["apps_v1_Deployment|test-module|tp-test-ts"]
value = module.test.kustomization.manifests["apps/Deployment/test-module/tp-test-ts"]
}

output "check_cm" {
value = module.test.kustomization.manifests["~G_v1_ConfigMap|test-module|tp-ocm-ts"]
value = module.test.kustomization.manifests["_/ConfigMap/test-module/tp-ocm-ts"]
}

output "check_s" {
value = module.test.kustomization.manifests["~G_v1_Secret|test-module|tp-os-ts-46f8b28mk5"]
value = module.test.kustomization.manifests["_/Secret/test-module/tp-os-ts-46f8b28mk5"]
}
`, modulePath, modulePath)
}
6 changes: 3 additions & 3 deletions kustomize/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func Provider() *schema.Provider {
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
DefaultFunc: schema.EnvDefaultFunc("KUBECONFIG_CONTEXT", nil),
Description: "Context to use in kubeconfig with multiple contexts, if not specified the default context is to be used.",
},
"legacy_id_format": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Deprecated: "legacy_id_format will be set to false in a future version and be removed in a further later version",
Default: false,
Deprecated: "legacy_id_format will be removed in a future version",
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",
},
},
Expand Down
3 changes: 0 additions & 3 deletions kustomize/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

var testAccLegacyProviders map[string]terraform.ResourceProvider
var testAccLegacyProvider *schema.Provider

func init() {
testAccProvider = Provider()
testAccProviders = map[string]terraform.ResourceProvider{
Expand Down
54 changes: 31 additions & 23 deletions kustomize/resource_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
return nil
}

mapping, err := mapper.RESTMapping(ou.GroupVersionKind().GroupKind(), ou.GroupVersionKind().Version)
mapping, err := mapper.RESTMapping(mu.GroupVersionKind().GroupKind(), mu.GroupVersionKind().Version)
if err != nil {
return logErrorForResource(
ou,
mu,
fmt.Errorf("failed to query GVR: %s", err),
)
}
Expand All @@ -225,15 +225,15 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
m)
if err != nil {
return logErrorForResource(
ou,
mu,
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
)
}

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

_, err = client.
Resource(mapping.Resource).
Namespace(ou.GetNamespace()).
Patch(context.TODO(), ou.GetName(), patchType, patch, dryRunPatch)
Namespace(mu.GetNamespace()).
Patch(context.TODO(), mu.GetName(), patchType, patch, dryRunPatch)
if err != nil {
// Handle specific invalid errors
if k8serrors.IsInvalid(err) {
Expand All @@ -269,7 +269,7 @@ func kustomizationResourceDiff(d *schema.ResourceDiff, m interface{}) error {
}

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

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

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

srcJSON := originalJSON.(string)
u, err := parseJSON(srcJSON)
ou, err := parseJSON(srcJSON)
if err != nil {
return logError(fmt.Errorf("JSON parse error: %s", err))
}

if !d.HasChange("manifest") {
return logErrorForResource(
u,
ou,
errors.New("update called without diff"),
)
}

mapping, err := mapper.RESTMapping(u.GroupVersionKind().GroupKind(), u.GroupVersionKind().Version)
modifiedSrcJSON := modifiedJSON.(string)
mu, err := parseJSON(modifiedSrcJSON)
if err != nil {
return logError(fmt.Errorf("JSON parse error: %s", err))
}

mapping, err := mapper.RESTMapping(mu.GroupVersionKind().GroupKind(), mu.GroupVersionKind().Version)
if err != nil {
return logErrorForResource(
u,
mu,
fmt.Errorf("failed to query GVR: %s", err),
)
}
Expand All @@ -348,27 +354,27 @@ func kustomizationResourceUpdate(d *schema.ResourceData, m interface{}) error {
m)
if err != nil {
return logErrorForResource(
u,
mu,
fmt.Errorf("getOriginalModifiedCurrent failed: %s", err),
)
}

patch, patchType, err := getPatch(u.GroupVersionKind(), original, modified, current)
patch, patchType, err := getPatch(mu.GroupVersionKind(), original, modified, current)
if err != nil {
return logErrorForResource(
u,
mu,
fmt.Errorf("getPatch failed: %s", err),
)
}

var patchResp *unstructured.Unstructured
patchResp, err = client.
Resource(mapping.Resource).
Namespace(u.GetNamespace()).
Patch(context.TODO(), u.GetName(), patchType, patch, k8smetav1.PatchOptions{})
Namespace(mu.GetNamespace()).
Patch(context.TODO(), mu.GetName(), patchType, patch, k8smetav1.PatchOptions{})
if err != nil {
return logErrorForResource(
u,
mu,
fmt.Errorf("patch failed '%s': %s", patchType, err),
)
}
Expand All @@ -391,7 +397,9 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
return logError(fmt.Errorf("JSON parse error: %s", err))
}

mapping, err := mapper.RESTMapping(u.GroupVersionKind().GroupKind(), u.GroupVersionKind().Version)
// look for all versions of the GroupKind in case the resource uses a
// version that is no longer current
mappings, err := mapper.RESTMappings(u.GroupVersionKind().GroupKind())
if err != nil {
if k8smeta.IsNoMatchError(err) {
// If the Kind does not exist in the K8s API,
Expand All @@ -405,7 +413,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
name := u.GetName()

err = client.
Resource(mapping.Resource).
Resource(mappings[0].Resource).
Namespace(namespace).
Delete(context.TODO(), name, k8smetav1.DeleteOptions{})
if err != nil {
Expand All @@ -427,7 +435,7 @@ func kustomizationResourceDelete(d *schema.ResourceData, m interface{}) error {
Timeout: d.Timeout(schema.TimeoutDelete),
Refresh: func() (interface{}, string, error) {
resp, err := client.
Resource(mapping.Resource).
Resource(mappings[0].Resource).
Namespace(namespace).
Get(context.TODO(), name, k8smetav1.GetOptions{})
if err != nil {
Expand Down
Loading