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

Refactor makeTerraformInputs and friends #1725

Merged
merged 44 commits into from
Mar 19, 2024

Conversation

VenelinMartinov
Copy link
Contributor

@VenelinMartinov VenelinMartinov commented Mar 4, 2024

This PR refactors the various functions related to converting pulumi inputs to TF inputs.

It also exposes a new option when making inputs: EnableMaxItemsOneDefaults which will allow us to fix pulumi/pulumi-aws#3427 once we can properly test the changes.

Opted not to land the behavioural changes for EnableMaxItemsOneDefaults yet because of the multiple rounds of varied downstream regressions - seems like we need to add a framework for testing against the TF behaviour in order to land this.

@VenelinMartinov VenelinMartinov marked this pull request as draft March 4, 2024 14:00
Copy link

codecov bot commented Mar 4, 2024

Codecov Report

Attention: Patch coverage is 72.88136% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 59.45%. Comparing base (54802d5) to head (09de622).
Report is 3 commits behind head on master.

Files Patch % Lines
pkg/tfbridge/provider.go 43.47% 12 Missing and 1 partial ⚠️
pkg/tfbridge/schema.go 91.66% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1725      +/-   ##
==========================================
- Coverage   59.79%   59.45%   -0.35%     
==========================================
  Files         300      309       +9     
  Lines       42025    42511     +486     
==========================================
+ Hits        25129    25274     +145     
- Misses      15474    15810     +336     
- Partials     1422     1427       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@VenelinMartinov VenelinMartinov marked this pull request as ready for review March 5, 2024 15:18
@VenelinMartinov
Copy link
Contributor Author

@VenelinMartinov VenelinMartinov marked this pull request as draft March 5, 2024 16:46
pkg/tfbridge/schema_test.go Outdated Show resolved Hide resolved
pkg/tfbridge/provider_test.go Outdated Show resolved Hide resolved
@t0yv0 t0yv0 self-requested a review March 5, 2024 16:57
Copy link
Member

@t0yv0 t0yv0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you very much. Conceptually this makes a lot of sense to me - the missing MaxItems=1 properties should be presenting as empty collections to the Terraform provider but not to the provider's validators. We have a slightly convoluted interaction with these validators already so the fix has to accommodate. This is really driving toward the correct behavior though! Let's do a pass to try to increase the longevity of the tests so we can still keep these tests around and understand what they're testing as the implementation itself evolves, this is an important property - left some style nits and suggestions, otherwise LGTM.

Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with all of t0yv0's comments.

pkg/tfbridge/provider.go Outdated Show resolved Hide resolved
pkg/tfbridge/provider_test.go Outdated Show resolved Hide resolved
@VenelinMartinov
Copy link
Contributor Author

VenelinMartinov commented Mar 15, 2024

From https://github.com/pulumi/terraform-plugin-sdk/blob/7ac578ce47fc07e0888beee6d4ab9db09369274f/helper/schema/schema.go#L78-L96:

If Elem is *schema.Resource then setting ConfigMode to SchemaConfigModeAttr will force it to be represented in configuration as an attribute, which means that the Computed flag can be used to provide default elements when the argument isn't set at all, while still allowing the user to force zero elements by explicitly assigning an empty list.

I have a feeling this is used to set a default for the rulesets and that's the reason TF does not error out here. The resource https://github.com/hashicorp/terraform-provider-azurerm/blob/61ac20e312db3e460ccb2324fdc04bc9eb2e503e/internal/services/eventhub/eventhub_namespace_resource.go#L128
has ConfigMode: pluginsdk.SchemaConfigModeAttr.

Note: I looked for the combination of attributes

	Computed:   true,
	MaxItems:   1,
	ConfigMode: pluginsdk.SchemaConfigModeAttr,

It looks like it's used in a few places in Azure but SchemaConfigModeAttr is virtually unused in AWS.

Will do a more in-depth comparison of the TF behaviour here with what the bridge does next week.

@VenelinMartinov
Copy link
Contributor Author

Terraform docs on SchemaConfigModeAttr:
https://developer.hashicorp.com/terraform/plugin/sdkv2/guides/terraform-0.12-compatibility#computed-resource-attributes

Only activate this mode if your provider has existing functionality that is depending on the ability to distinguish unset vs. explicitly empty for a nested resource collection.

which sounds very very relevant.

@VenelinMartinov
Copy link
Contributor Author

VenelinMartinov commented Mar 18, 2024

Given the multiple rounds of different downstream regressions caused by the stages of this PR, combined with the slow iteration loop, I don't see how we can safely land this right now. Instead, I'd like to make the refactoring changes which were part of this PR and then make the behavioural changes once we can properly test them.

Old description for posterity:
This reverts commit 6241673 and gets #1688 back in.

This should allow us to address pulumi/pulumi-aws#3427 and unblock further upstream updates there.

The issue was that TF seems to pass empty arrays for not-specified MaxItemsOne properties, while we were passing nil, resulting in differences in behaviour between the bridge and TF.

The original PR went a bit too far and made the change for Check too, which caused some issues: pulumi/pulumi-aws#3427 (comment).

I've reused the mechanism added #1583 and only apply the empty arrays for MaxItemsOne properties in makeTerraformInputs when the ApplyMaxItemsOneDefaults flag is set. This makes sure we don't mess with the parameters when doing Check but pass the correct parameters to other methods.

This PR introduces a new makeTerraformInputs variant: makeTerraformInputsWithoutMaxItemsOneDefaults - this is only used for CheckConfig, since CheckConfig depends on the null MaxItemsOne properties but needs the TF defaults.

@VenelinMartinov VenelinMartinov changed the title Revert "Revert "Make sure no nil lists are passed to TF (#1688)"" Refactor makeTerraformInputs and friends Mar 18, 2024
@VenelinMartinov
Copy link
Contributor Author

I've opened #1767 for the follow up work.

@t0yv0
Copy link
Member

t0yv0 commented Mar 18, 2024

I was a bit uncomfortable at first but approving after our chat and another round of looking at it in detail. Behavior is not changing, some new code is added but it's flagged off, and new tests are added which is excellent.

A bit of a nit but I'd prefer in the future not to remove public members MakeTerraformState cold-turkey but mark them deprecated or call out they're for internal use and subject to change in a comment. It does not take a lot more time and is good on the off chance it's called into by some codebase out there. These sort of small PRs also can be quick approve.

Thanks for the substantial work on the foundations here!

@VenelinMartinov
Copy link
Contributor Author

Thanks for the reviews! After discussing with @t0yv0, I've removed the breaking changes here - we really have no reason to do that. I've cleaned up other unnecessary changes from this PR and will merge once tests pass.

@VenelinMartinov VenelinMartinov enabled auto-merge (squash) March 19, 2024 12:50
@VenelinMartinov VenelinMartinov merged commit 90733a0 into master Mar 19, 2024
9 checks passed
@VenelinMartinov VenelinMartinov deleted the vvm/nil_lists_max_items_one branch March 19, 2024 13:05
VenelinMartinov added a commit to pulumi/pulumi-aws that referenced this pull request Mar 25, 2024
Adds another regression test for
#3421

The issue is not only present in Create but in Update - almost shipped a
half-fix in pulumi/pulumi-terraform-bridge#1725.

This test should ensure we don't ship a half-fix.
VenelinMartinov added a commit that referenced this pull request May 31, 2024
This is a pure refactor.

Reverts the options added in
#1725 as these
were the wrong way to do the change.

#1971 should be
the proper fix to the issues encountered there, now backed by
cross-tests.

This should simplify the code around makeTerraformInputs a bit.
VenelinMartinov added a commit that referenced this pull request Jun 5, 2024
part of #1785

This change adds a normalisation step for collections when recovering
cty values to pass to terraform. This ensures we represent them
similarly to terraform.

In practice this means that all block collections need to be passed to
TF providers as an empty collection instead of nil. This should get rid
of quite a few subtle discrepancies in the data we pass to the TF
provider code. These sometimes result in panics since we pass unexpected
nils.

This gets rid of all known input discrepancies discovered so far through
cross-testing.

The full rules for what is a block are
[here](https://github.com/hashicorp/terraform-plugin-sdk/blob/1f499688ebd9420768f501d4ed622a51b2135ced/helper/schema/core_schema.go#L60).
It is essentially properties with schema: typeList or typeSet with a
Resource Elem.

fixes #1970
fixes #1915
fixes #1964
fixes #1767 
fixes #1762 

TODO: [DONE] remove the MaxItemsOne default hacks introduced in
#1725 (opened
#2049)

---------

Co-authored-by: Anton Tayanovskyy <anton@pulumi.com>
Co-authored-by: Ian Wahbe <ian@wahbe.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Root cause 3421 and revert patches 0042 and 0043
3 participants