Skip to content

Conversation

@EronWright
Copy link
Contributor

@EronWright EronWright commented Jun 4, 2025

This PR is a follow-up to #3040 to fix another variation of same basic problem, that the writePropertiesToBody function generates a malformed REST body. This variation happens in update cases, where the remoteState has existing values and the bodyParams are empty (see test case).

Note that the problem is not a 3.x regression, it repros in 2.x.

For example, given a basic NSG:

new NetworkSecurityGroup($"data-center-{_name}-nsg", new NetworkSecurityGroupArgs

        ResourceGroupName = resourceGroup.Name,
        Tags = 
        {
            { "revision", "1" },
        },
})

Imagine that the NSG has been deployed, and then we change the "revision" tag to force an update of the NSG.

The provider would a generate REST body containing a spurious securityRules container node, apparently due to how writePropertiesToBody ranges over the whole property path, and how it misinterprets the propertyName field.

I0602 14:39:34.104400   43876 eventsink.go:59] {"location":"WestUS2","properties":{"securityRules":{}},"tags":{"revision":"2"}}
I0602 14:39:34.617896   43876 eventsink.go:59]    RESPONSE Status: 400 Bad Request

To make writePropertiesToBody easier to understand and be more reliable, this PR brings in a simpler approach. For each property path, we get the value from the remote state, and if present, we set the value into the body parameters. This approach avoids prematurely creating intermediate container nodes. Note that nestedField and setNestedField implementations were drawn from: https://github.com/kubernetes/apimachinery/blob/a925cd7fb3fd137a00c75870fbf7381f038993d0/pkg/apis/meta/v1/unstructured/helpers.go#L54

With the fix, we see the actual securityRules be maintained:

I0604 10:18:04.029199   72054 eventsink.go:59] {"location":"WestUS2","properties":{"securityRules":[]},"tags":{"revision":"2"}}

Some test cases were updated to use the propertyPath structure correctly. Looking at findUnsetPropertiesToMaintain that generates these propertyPath structs, the path field always contains the full path, and propertyName contains the path element that wasn't found in the input map (which may be an intermediate node like properties). Also, propertyName is not actually used anymore, is purely informational.

image

Closes #4094

@EronWright EronWright requested a review from danielrbradley June 4, 2025 00:38
@github-actions
Copy link

github-actions bot commented Jun 4, 2025

Does the PR have any schema changes?

Looking good! No breaking changes found.
No new resources/functions.

@codecov
Copy link

codecov bot commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 81.63265% with 9 lines in your changes missing coverage. Please review.

Project coverage is 58.08%. Comparing base (b08c8f0) to head (d4a08c0).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
provider/pkg/provider/crud/crud.go 81.63% 8 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4177      +/-   ##
==========================================
- Coverage   58.08%   58.08%   -0.01%     
==========================================
  Files          84       84              
  Lines       13535    13546      +11     
==========================================
+ Hits         7862     7868       +6     
- Misses       5081     5085       +4     
- Partials      592      593       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@EronWright EronWright changed the title Don't add empty objects that may be of the wrong type when maintaining subresource properties (update case) cleanup for maintaining subresource properties Jun 4, 2025
@EronWright EronWright marked this pull request as ready for review June 4, 2025 16:34
@EronWright EronWright requested a review from a team June 4, 2025 16:35
Copy link
Contributor

@blampe blampe left a comment

Choose a reason for hiding this comment

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

It seems reasonable but Daniel should also take a look.

All the talk of deep copying makes me nervous some other kind of mutation bug could sneak in, but I don't know enough about this.

@EronWright
Copy link
Contributor Author

This change is part of the following stack:

Change managed by git-spice.

Copy link
Contributor

@danielrbradley danielrbradley left a comment

Choose a reason for hiding this comment

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

I'm a little nervous that the tests have changed in the same commit as the refactor work. Was the behavour documented by the tests incorrect or was there another reason for the test reworks?

I can't see anything obviously wrong about the refactored method - it's certainly nicer split into the smaller methods.

Copy link
Contributor

@danielrbradley danielrbradley left a comment

Choose a reason for hiding this comment

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

Approved with a final cleanup of the unused propertyName struct property 👍

@EronWright EronWright enabled auto-merge (squash) June 5, 2025 21:49
@EronWright EronWright merged commit 65208e8 into master Jun 5, 2025
23 checks passed
@EronWright EronWright deleted the issue-4094 branch June 5, 2025 22:16
@pulumi-bot
Copy link
Contributor

This PR has been shipped in release v3.5.1.

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.

Provider v3 breaks azure-native:network:NetworkSecurityGroup update

5 participants