Skip to content

Commit

Permalink
Schema-aware pulumi-level detailed diff calculation in the SDKv2 brid…
Browse files Browse the repository at this point in the history
…ge (#2405)

This PR adds a new algorithm for calculating the detailed diff which
acts on the pulumi property values for the SDKv2 bridge, comparing the
planned state returned by `Diff` to the prior state. This is flagged
under the existing `DiffEqualDecisionOverride` feature flag. The results
look very promising so far - all the detailed diff integration tests
pass and the issues previously reported are almost all fixed by this.

## Why ##
The current detailed diff acts on the `InstanceDiff` structure which is
internal to the plugin-sdk. This has a few shortcomings:
- TF does not actually refer to this for the detailed diff, so it might
point to diffs which are not present in TF.
- It refers to TF attribute paths, which are tricky to translate back in
some cases.
- It does not compare the planned state with the prior state but
compares the news vs olds - this misses properties added by TF planning.

## Implementation ##
The new algorithm is under `pkg/tfbridge/detailed_diff.go` and used in
`pkg/tfbridge/provider.go:Diff` only for the SDKv2 and only if the
`DiffEqualDecisionOverride` is enabled.
The main entrypoint is `makePulumiDetailedDiffV2` - which in turn calls
`makePropDiff` on each property. That branches on the type of the
property and we have a separate function responsible for calculating the
detailed diff for each property type.
There's a few interesting bits here:
- We always walk the full tree even when comparing against a nil
property and simplify the diff after in `simplifyDiff`. This is in order
to get replaces correct. More on that later.
- When returning the diff to the engine we only return the simplest
possible diff which explains the changes. So instead of `prop: Update,
prop.subprop: Add`, we only return `prop.subprop: Add`. This seems to
work much better in the engine and goes around some weird behaviour in
the detailed diff display (see
#2234 and
#2400).
Moreover, the first can be inferred from the second, so there is no
reason for the bridge to return the full tree if only a leaf was
changed.
- We can not correctly identify diffs between nil and empty collections
because of how the TF SDKv2 works without additional work. This is
studied in `TestNilVsEmptyListProperty` and `TestNilVsEmptyMapProperty`
in `pkg/cross-tests/diff_cross_test.go`. This is probably fine for now
and a full fix is not possible. We can make a partial fix for
non-computed properties by inspecting the pulumi inputs, before the
plan.
- There's a bit of an edge case with Unknowns and Replaces - we might
not have enough information to tell the user they'll get a replace
because the property which causes the replaces is nested inside an
unknown. There's not much to do here, except to choose which side to err
on. The algorithm currently does not say there's a replace.


### On Replaces ###
We do not short-circuit detailed diffs when comparing non-nil properties
against nil ones. The reason for that is that a replace might be
triggered by a `ForceNew` inside a nested property of a non-`ForceNew`
property. We instead always walk the full tree even when comparing
against a nil property. We then later do a simplification step for the
detailed diff in `simplifyDiff` in order to reduce the diff to what the
user expects to see.

For example:
This is a list of objects with two properties, one of which is
`ForceNew`
```
schema = {
  "list_prop": {
     Type: List,
     Elem: {
         "prop": String
         "force_new_prop": StringWithForceNew
     }
  }
}
```
We are diffing an unspecified list against a list with a single element
```
olds = {}
news = {
"list_prop": [
   {
    "prop": "val",
    "force_new_prop" : "val"
  }
]
```


The user expects to see:
```
+ list_prop
```
or because of how collections work in TF SDKv2 (see
#2233)
```
+ list_prop[0]
```
An element was added to the list. When calculating the detailed diff we
can short-circuit the diff when comparing the two lists, as we can see
they have different lengths. This would identify the correct element to
be displayed to the user as the reason for the diff but would fail to
identify the diff as a replace, since we never saw the `ForceNew` on the
nested property `force_new_prop` of the list elements.

That is why, instead of short-circuiting the diff, we walk the full tree
down and compare every property against a nil if it is not specified on
the other side. We then to a simplification pass over the detailed diff,
which respects any replaces triggered by nested properties and bubbles
these up.

There is a full case study of the TF behaviour around replaces in
`pkg/cross-tests/diff_cross_test.go` `TestAttributeCollectionForceNew`,
`TestBlockCollectionForceNew`, `TestBlockCollectionElementForceNew`.

## Testing ##
Unit tests for the detailed diff algorithm in
`pkg/tfbridge/detailed_diff_test.go` - this tries to cover all
meaningful permutations of schemas:
- `TestBasicDetailedDiff` tests all the meaningful pairs between nil
values, empty values, non-empty values and computed for each TF type.
- `TestDetailedDiffObject`, `TestDetailedDiffList`,
`TestDetailedDiffMap`, `TestDetailedDiffSet` covers the cases not
covered above for object and collection types.
- `TestDetailedDiffTFForceNewPlain`,
`TestDetailedDiffTFForceNewAttributeCollection`,
`TestDetailedDiffTFForceNewBlockCollection`,
`TestDetailedDiffTFForceNewElemBlockCollection`,
`TestDetailedDiffTFForceNewObject` cover `ForceNew` behaviour in all TF
types.
- `TestDetailedDiffPulumiSchemaOverride` covers pulumi schema overrides

Integration tests in `pkg/tests/schema_pulumi_test.go`, mostly
`TestDetailedDiffPlainTypes` and `TestUnknownBlocks`. Note that most of
the edge cases and issues previously discovered here are resolved by
this PR.

## Follow-up Work ##
Not done but will follow-up in separate PRs:
- Non-trivial set diffing - sets are currently diffed the same as lists,
which has all the previous issues with set diffs.
#2200
- Non-trivial list diffing - we can do something like
#2295 here. Note
that we still need to investigate how this interacts with ForceNew and
how TF preserves or does not preserve list element identity. We likely
need to respect that in order not to have confusing unexplained replaces
caused by list element changes.

## Related Issues ##

fixes:
- fixes #2294
- fixes #2296
- fixes #1504
- fixes #1895
- fixes #2141
- fixes #2235
- fixes #2325
- fixes #2400
- fixes #2234
- fixes #2427


does not fix:
- #2399 - we
must either fix the saved state to not contain redundant nils or fix the
display logic in the engine to ignore these.
- #2233 - This
works the same as TF and seems to be a limitation of the SDKv2.
  • Loading branch information
VenelinMartinov authored Oct 1, 2024
1 parent cb8c4ed commit deb3c39
Show file tree
Hide file tree
Showing 11 changed files with 3,025 additions and 342 deletions.
6 changes: 4 additions & 2 deletions pkg/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type diffResult struct {
}

func runDiffCheck(t T, tc diffTestCase) diffResult {
t.Helper()
tfwd := t.TempDir()

lifecycleArgs := lifecycleArgs{CreateBeforeDestroy: !tc.DeleteBeforeReplace}
Expand Down Expand Up @@ -108,6 +109,7 @@ func runDiffCheck(t T, tc diffTestCase) diffResult {
}

func (tc *diffTestCase) verifyBasicDiffAgreement(t T, tfActions []string, us auto.UpdateSummary, diffResponse pulumiDiffResp) {
t.Helper()
t.Logf("UpdateSummary.ResourceChanges: %#v", us.ResourceChanges)
// Action list from https://github.com/opentofu/opentofu/blob/main/internal/plans/action.go#L11
if len(tfActions) == 0 {
Expand Down Expand Up @@ -147,14 +149,14 @@ func (tc *diffTestCase) verifyBasicDiffAgreement(t T, tfActions []string, us aut
rc := *us.ResourceChanges
assert.Equalf(t, 1, rc[string(apitype.OpSame)], "expected the stack to stay the same")
assert.Equalf(t, 1, rc[string(apitype.OpReplace)], "expected the test resource to get a replace plan")
assert.Equalf(t, diffResponse.DeleteBeforeReplace, false, "expected deleteBeforeReplace to be true")
assert.Equalf(t, false, diffResponse.DeleteBeforeReplace, "expected deleteBeforeReplace to be true")
} else if tfActions[0] == "delete" && tfActions[1] == "create" {
require.NotNilf(t, us.ResourceChanges, "UpdateSummary.ResourceChanges should not be nil")
rc := *us.ResourceChanges
t.Logf("UpdateSummary.ResourceChanges: %#v", rc)
assert.Equalf(t, 1, rc[string(apitype.OpSame)], "expected the stack to stay the same")
assert.Equalf(t, 1, rc[string(apitype.OpReplace)], "expected the test resource to get a replace plan")
assert.Equalf(t, diffResponse.DeleteBeforeReplace, true, "expected deleteBeforeReplace to be true")
assert.Equalf(t, true, diffResponse.DeleteBeforeReplace, "expected deleteBeforeReplace to be true")
} else {
panic("TODO: do not understand this TF action yet: " + fmt.Sprint(tfActions))
}
Expand Down
Loading

0 comments on commit deb3c39

Please sign in to comment.