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

Use TF Diff Attributes to set diff response changes when SkipDetailedDiffForChanges feature flag is set #1502

Merged
merged 4 commits into from
Nov 2, 2023

Conversation

guineveresaenger
Copy link
Contributor

This PR presents an alternative fix for #1491.

Because makeDetailedDiff can distort the expected changes for a diff set via custom diffs in the upstream provider logic, we should not, in all cases, rely on it to provide us with an accurate pulumirpc.DiffResponse.Changes result. This PR does two things:

  • Rely on the terraform Diff Attributes length to tell the engine that there are changes via the diff
  • Gate this behavior behind a new feature flag, SkipDetailedDiffForChanges.

This code will continue to rely on makeDetailedDiff for setting the DetailedDiff field in all cases.

I have verified that this succeeds against these test cases when the GCP provider sets SkipDetailedDiffForChanges to True, and fails when set to False.

Fixes #1491 in the context of unblocking the GCP release.

Copy link

codecov bot commented Nov 2, 2023

Codecov Report

Merging #1502 (3aa4ce5) into master (b83370c) will decrease coverage by 4.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1502      +/-   ##
==========================================
- Coverage   61.93%   57.91%   -4.02%     
==========================================
  Files         184      285     +101     
  Lines       32836    39629    +6793     
==========================================
+ Hits        20336    22953    +2617     
- Misses      11362    15339    +3977     
- Partials     1138     1337     +199     
Files Coverage Δ
internal/testprovider/schema.go 75.45% <100.00%> (+0.54%) ⬆️
pkg/tfbridge/info.go 93.31% <ø> (ø)
pkg/tfbridge/provider.go 48.64% <100.00%> (+0.48%) ⬆️

... and 102 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -661,6 +661,15 @@ func (p *Provider) Diff(ctx context.Context, req *pulumirpc.DiffRequest) (*pulum
doIgnoreChanges(ctx, res.TF.Schema(), res.Schema.Fields, olds, news, req.GetIgnoreChanges(), diff)
detailedDiff, changes := makeDetailedDiff(ctx, res.TF.Schema(), res.Schema.Fields, olds, news, diff)

// There are some providers/situations which `makeDetailedDiff` distorts the expected changes, leading
// to changes being dropped by Pulumi.
// Until we address https://github.com/pulumi/pulumi-terraform-bridge/issues/1501, it is safer to refer
Copy link
Member

Choose a reason for hiding this comment

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

This change literally addresses 1501

Copy link
Contributor Author

@guineveresaenger guineveresaenger Nov 2, 2023

Choose a reason for hiding this comment

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

It does; but I'm under the impression we'll want to keep it open so we can fix the bugs in makeDetailedDiff "for real".

I'll amend the wording to reflect more nuance.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so, I'm happy to close. We can open new issues for makeDetailedDiff referencing the closed issue.

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'd like at least 1 synthetic test case to make sure this is hooked up (and to prevent regressions).

Comment on lines 132 to 135
// Disables using detailed diff to determine diff changes and falls back on the length of TF Diff Attributes.
//
// See https://github.com/pulumi/pulumi-terraform-bridge/issues/1501
SkipDetailedDiffForChanges bool
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we want to support this feature until v4.0.0. When we close #1501 for real, we will want to remove this flag.

Suggested change
// Disables using detailed diff to determine diff changes and falls back on the length of TF Diff Attributes.
//
// See https://github.com/pulumi/pulumi-terraform-bridge/issues/1501
SkipDetailedDiffForChanges bool
// Disables using detailed diff to determine diff changes and falls back on the length of TF Diff Attributes.
//
// See https://github.com/pulumi/pulumi-terraform-bridge/issues/1501
//
// This flag is experimental and may be removed in a future bridge release.
XSkipDetailedDiffForChanges bool

Comment on lines 664 to 668
// There are some providers/situations which `makeDetailedDiff` distorts the expected changes, leading
// to changes being dropped by Pulumi.
// Until we address https://github.com/pulumi/pulumi-terraform-bridge/issues/1501, it is safer to refer
// to the Terraform Diff attribute length for setting the DiffResponse.
// We will still use `detailedDiff` for diff display purposes.
Copy link
Member

Choose a reason for hiding this comment

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

I really appreciate the comment explaining what is going on!

// to the Terraform Diff attribute length for setting the DiffResponse.
// We will still use `detailedDiff` for diff display purposes.
if p.info.SkipDetailedDiffForChanges && len(diff.Attributes()) > 0 {
changes = pulumirpc.DiffResponse_DIFF_SOME
Copy link
Member

Choose a reason for hiding this comment

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

Noting that it doesn't do the inverse, that is if len(diff.Attributes()) == 0 but makeDetailedDiff returned CHANGES, it does not override it back to NO_CHANGES. Is that perhaps a degenerate/ non-existent situation? Reading the code seems to be the case, so we're probably fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have not verified such a situation as of today to the best of my knowledge.

@t0yv0
Copy link
Member

t0yv0 commented Nov 2, 2023

Also fixes AWS P1.. LGTM once tested, consider #1498 test cases I believe this change makes them pass, adding empty tags to map at least certainly should pass.

…ction and when XSkipDetailedDiff is set

Adds a new test provider that has a CuztomizeDiff field
This change does NOT add a test for the False() -> DIFF_NONE case which occurs today but is a bug we need to fix.
@iwahbe iwahbe self-requested a review November 2, 2023 20:19
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.

1 nit on the test, but LGTM!

@@ -880,3 +880,54 @@ func AssertProvider(f func(data *schemav2.ResourceData)) *schemav2.Provider {
},
}
}

func AssertCustomizedDiffProvider(f func(data *schemav2.ResourceData)) *schemav2.Provider {
Copy link
Member

Choose a reason for hiding this comment

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

Since we don't care about calling f here, we can just remove it.

Suggested change
func AssertCustomizedDiffProvider(f func(data *schemav2.ResourceData)) *schemav2.Provider {
func CustomizedDiffProvider() *schemav2.Provider {

Schema: map[string]*schemav2.Schema{},
ResourcesMap: map[string]*schemav2.Resource{
"test_resource": {
Schema: map[string]*schemav2.Schema{
Copy link
Member

Choose a reason for hiding this comment

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

You could probably delete a lot here the test will only exercise CustomizeDiff

@guineveresaenger guineveresaenger merged commit ed35c1e into master Nov 2, 2023
8 checks passed
@guineveresaenger guineveresaenger deleted the guin/skipdetailedDiff branch November 2, 2023 20:59
VenelinMartinov added a commit that referenced this pull request Sep 20, 2024
Adds a `DiffEqualDecisionOverride` to the shim layer which allows
provider implementations to specify a diff decision instead of relying
on the shim layer to do that.

Also adds a `DiffEqualDecisionOverride` to the PRC sdkv2 implementation
which opentofu does.

Also adds a feature flag `EnableAccurateBridgePreview` which guards this
feature.

will fix once rolled out:
#2293
will fix once rolled out:
#1501
will undo once rolled out:
#1502 as the
underlying issue was fixed during the PRC work.
Stacked on #2380
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.

tfbridge/diff.go does not consider terraform diff
3 participants