Skip to content
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

Enabling JSON output for Terraform instead of writing the HCL syntax … #8145

Merged
merged 1 commit into from
Jan 23, 2020

Conversation

mccare
Copy link
Contributor

@mccare mccare commented Dec 18, 2019

Enabling output of JSON instead of HCL v1, writing kubernetes.tf.json instead of kubernetes.tf. JSON can be consumed by terraform 0.12. ( https://www.terraform.io/docs/configuration/syntax-json.html )

Tried it on AWS and GCE test cluster.

If you enable this feature, you need to delete the previously generated kubernetes.tf file. Also, this feature will require minimum version of terraform 0.12.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Dec 18, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @mccare. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 18, 2019
@tanjunchen
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 18, 2019
@rifelpet
Copy link
Member

This is great! I was working on adding native HCL2 support but it will require touching every task's terraform struct to add hcl tags which is much more invasive than i was hoping, so having json support is a good middle ground.

Are you able to test the output with both 0.11 and 0.12? If it only works with 0.12 we may want to make the feature flag's name specifically call that out.

I believe we still run into the issue with the default route and VPC additional CIDR block names that start with digits, is that correct? We'll still need to conditionally rename those somehow.

we'll want to mention this in docs/terraform.md as well.

Can you add an integration test for this? The test would go in cmd/kops/integration_test.go and the input and output files would go in tests/integration/update_cluster. I think we may run into an issue with enabling this feature flag for a specific test (the GCE tests in that file already do that of you need an example). if we run tests in parallel other tests may use the enabled flag which would break them... hoping that's not the case but we'll see.

@mccare
Copy link
Contributor Author

mccare commented Dec 18, 2019

On first sight it seems that terraform 0.11 supports .tf.json files. Release notes for 0.12.0 list in the improvement section that

In prior versions, certain Terraform configuration features did not function as expected or were not usable via the JSON-based forms.

But I couldn't find what features this were exactly. (and if they are used by kops)

So I would restrict JSON output to terraform 0.12 (also creating the required_version). In addition, I could fail generating json output if kubernetes.tf is present in the target directory (since terraform seems combines the two and then complains)

Naming issues are not resolved by using JSON as format (so a name beginning with a number is still invalid).

I'll have a look at the integration tests.

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 19, 2019
@mccare
Copy link
Contributor Author

mccare commented Dec 19, 2019

Added one integration test. You have to unset the feature flag after the test otherwise the test suite would fail.

I needed to add (yet) another parameter to the runTestAWS to check for the kubernetes.tf.json file (instead of kubernetes.tf). Are there other options? (one struct for parameters?) I'm new to go.

@mccare mccare force-pushed the gce-terraform-json-output branch 2 times, most recently from f0ecd69 to a2630ff Compare December 19, 2019 11:58
@rifelpet
Copy link
Member

Ok, I renamed the route resource and tested terraform plan with both 0.11 and 0.12 and they both succeeded. This feature flag wont be fully functional until we get the route and vpc additional cidr resources renamed though. I'll try to test the applys sometime soon but if someone is willing to do that, that would be great.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 19, 2019
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 19, 2019
@johngmyers
Copy link
Member

/test pull-kops-verify-staticcheck

@mccare
Copy link
Contributor Author

mccare commented Dec 23, 2019

Added an error if the kubernetes.tf file is still present (no JSON file will be generated then). The error message contains a note to read the release notes.

For moving state (for the rename)

  • created a cluster with terraform 0.11
  • applied the PR Update Terraform resource names to be 0.12 compatible. #7957 to kops
  • removed kubernetes.tf file
  • ran kops with the JSON flag.
  • moved the resource for the default route:
    terraform011 state mv aws_route.0-0-0-0--0 aws_route.route-0-0-0-0--0
  • terraform plan --out planfile (0.12) did say "nothing to do"

Moving the state with terraform 0.12 did not work since the name was invalid.

@rifelpet
Copy link
Member

@mccare can you rebase to pull in the resource renaming that merged as well as fix the integration_test conflict?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 17, 2020
…tf file. JSON syntax is officially supported in 0.12 and a terraform version requirement will be set. For previous installations you need to delete the .tf file by hand. JSON generation will fail if kubernetes.tf is present.

Added Integration Test using minimal test setup

Added documentation. For terraform 0.12 support the resource names need to be changed still
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 17, 2020
@mccare
Copy link
Contributor Author

mccare commented Jan 17, 2020

@rifelpet fixed the tests and rebased the commit.

@rifelpet
Copy link
Member

I tested this on a terraform plan and it works perfectly.

I also tested this on an existing cluster and kops binary: generate terraform 0.11 output, plan and apply it with 0.11. Then use this branch to generate 0.12 output, plan and apply that with 0.12 and confirm no changes. Everything looks great! This is a big next step 🎉

I'm thinking of adding a test that downloads terraform and actually runs a terraform validate against some of our integration test expected outputs, but thatll happen in a separate PR.

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 23, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mccare, rifelpet

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 23, 2020
@k8s-ci-robot k8s-ci-robot merged commit 34caaf2 into kubernetes:master Jan 23, 2020
@k8s-ci-robot k8s-ci-robot added this to the v1.18 milestone Jan 23, 2020
k8s-ci-robot added a commit that referenced this pull request Apr 23, 2020
…of-#8145-upstream-release-1.17

Automated cherry pick of #8145: Enabling JSON output for Terraform instead of writing the HCL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants