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

Compute "tags" in Check #2655

Merged
merged 15 commits into from
Aug 4, 2023
Merged

Compute "tags" in Check #2655

merged 15 commits into from
Aug 4, 2023

Conversation

iwahbe
Copy link
Member

@iwahbe iwahbe commented Jul 31, 2023

Fixes #2633
Fixes #2550

Relies on pulumi/pulumi-terraform-bridge#1310.

Description copied from applyTags's doc comment:

Historically, Pulumi has struggles to handle the "tags" and "tags_all" fields correctly:

terraform-provider-aws has also struggled with implementing their desired behavior:

The Terraform lifecycle simply does not have a good way to map provider configuration onto resource values, so terraform-provider-aws is forced to work around limitations in unreliable ways. For example, terraform-provider-aws does not apply tags correctly with -refresh=false.

This gives pulumi the same limitations by default. However, unlike Terraform, Pulumi does have a clear way to insert provider configuration into resource properties: Check. By writing a custom check function that applies "default_tags" to "tags" before the Terraform provider sees any resource configuration, we can give a consistent, reliable and good experience for Pulumi users.


This strategy gives concrete benefits for Pulumi users. Diffs are applied correctly with high resolution. Consider this change:

name: our-example
runtime: yaml
resources:
  p:
    type: pulumi:providers:aws
    properties:
      defaultTags:
        tags:
          foo: buzz
          abc: def
      region: us-west-2
  b:
    type: aws:s3:BucketV2
-   properties:
-     tags:
-       foo: fizz
    options:
      provider: ${p}

We now can give users an accurate diff:

𝛌 pulumi preview --diff
Previewing update (test)

View Live: https://app.pulumi.com/pulumi/our-example/test/previews/*

  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:test::our-example::pulumi:pulumi:Stack::our-example-test]
warning: provider config warning: skip_metadata_api_check: the use of values other than "true" and "false" is deprecated and will be removed in a future version of the provider
    ~ aws:s3/bucketV2:BucketV2: (update)
        [id=b-f4d2684]
        [urn=urn:pulumi:test::our-example::aws:s3/bucketV2:BucketV2::b]
        [provider=urn:pulumi:test::our-example::pulumi:providers:aws::p::aaefc0bd-e75f-47b8-9d61-dc8c5cba05b0]
      ~ tags: {
          ~ foo: "fizz" => "buzz"
        }
Resources:
    ~ 1 to update
    2 unchanged

Changing defaultTags in a way that doesn't effect a resource no longer shows as an update for that resource, just for the provider:

name: our-example
runtime: yaml
resources:
  p:
    type: pulumi:providers:aws
    properties:
      defaultTags:
        tags:
-         foo: buzz
          abc: def
      region: us-west-2
  b:
    type: aws:s3:BucketV2
    properties:
      tags:
        foo: fizz
    options:
      provider: ${p}
𝛌 pulumi preview --diff
Previewing update (test)

View Live: https://app.pulumi.com/pulumi/our-example/test/previews/0*

  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:test::our-example::pulumi:pulumi:Stack::our-example-test]
warning: provider config warning: skip_metadata_api_check: the use of values other than "true" and "false" is deprecated and will be removed in a future version of the provider
    ~ pulumi:providers:aws: (update)
        [id=aaefc0bd-e75f-47b8-9d61-dc8c5cba05b0]
        [urn=urn:pulumi:our-example::pulumi:providers:aws::p]
      ~ defaultTags              : "\"{\\\"tags\\\":{\\\"abc\\\":\\\"def\\\",\\\"foo\\\":\\\"buzz\\\"}}\"" => "\"{\\\"tags\\\":{\\\"abc\\\":\\\"def\\\"}}\""
        region                   : "us-west-2"
        skipCredentialsValidation: "false"
        skipMetadataApiCheck     : "true"
        skipRegionValidation     : "true"
Resources:              
    ~ 1 to update
    2 unchanged

Of course, this also fixes the original issue; removing all defaultTags correctly removes them from the resource.

name: our-example
runtime: yaml
resources:
  p:
    type: pulumi:providers:aws
    properties:
-     defaultTags:
-       tags:
-         foo: buzz
-         abc: def
      region: us-west-2
  b:
    type: aws:s3:BucketV2
    options:
      provider: ${p}
𝛌 pulumi preview --diff
Previewing update (test)

View Live: https://app.pulumi.com/pulumi/our-example/test/previews/*

  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:test::our-example::pulumi:pulumi:Stack::our-example-test]
warning: provider config warning: skip_metadata_api_check: the use of values other than "true" and "false" is deprecated and will be removed in a future version of the provider
    ~ pulumi:providers:aws: (update)
        [id=aaefc0bd-e75f-47b8-9d61-dc8c5cba05b0]
        [urn=urn:pulumi:test::our-example::pulumi:providers:aws::p]
      - defaultTags              : (json) {
          - tags: {
              - abc: "def"
              - foo: "buzz"
            }
        }

        region                   : "us-west-2"
        skipCredentialsValidation: "false"
        skipMetadataApiCheck     : "true"
        skipRegionValidation     : "true"
    ~ aws:s3/bucketV2:BucketV2: (update)
        [id=b-f4d2684]
        [urn=urn:pulumi:test::our-example::aws:s3/bucketV2:BucketV2::b]
        [provider=urn:pulumi:test::our-example::pulumi:providers:aws::p::aaefc0bd-e75f-47b8-9d61-dc8c5cba05b0]
      - tags: {
          - abc: "def"
          - foo: "buzz"
        }
    --outputs:--        
  ~ tags: {
      - abc: "def"
      - foo: "buzz"
    }
Resources:
    ~ 2 to update
    1 unchanged

A bonus for maintainers is that because this change takes effect at the pulumi level, we no longer rely on changing how our involved diff method works, allowing us to remove XComputedInput from this provider (and then from the bridge).

@iwahbe iwahbe requested review from t0yv0 and AaronFriel July 31, 2023 11:31
@iwahbe iwahbe self-assigned this Jul 31, 2023
@iwahbe
Copy link
Member Author

iwahbe commented Jul 31, 2023

This will show diffs on tags when upgrading from v5 to v6, so it must be introduced as part of the major version upgrade and called out in the changelog.

@pulumi pulumi deleted a comment from github-actions bot Jul 31, 2023
@iwahbe
Copy link
Member Author

iwahbe commented Jul 31, 2023

Currently, this works for:

  • Create
  • Check (by construction)
  • Diff
  • Update
  • Destroy (noop)
  • Read

Read is the only CRUD+ method that does not go through Check, so it will also need special behavior so that pulumi up --yes && pulumi refresh --expect-no-changes doesn't show a diff.

@t0yv0
Copy link
Member

t0yv0 commented Jul 31, 2023

For example, terraform-provider-aws does not apply tags correctly with -refresh=false.

I was not able to isolate this, do you have a repro? Can we file a bug upstream with a repro?

@t0yv0
Copy link
Member

t0yv0 commented Jul 31, 2023

How can we demonstrate that import and refresh scenarios exercising Read work as expected?

@t0yv0
Copy link
Member

t0yv0 commented Jul 31, 2023

What happens if a provider with this set of changes runs against stacks that used the old method and perhaps populated tags_all?

@t0yv0
Copy link
Member

t0yv0 commented Jul 31, 2023

Overall I appreciate this approach, if we cannot find a clean way to interface with the functionality in upstream provider, maintaining custom code around default tags seems worth it as it is a feature impacting every resource; if we can do this cleanly. There is a cost of doing so in maintaining this new code and ensuring that it does not interact in weird ways with the upstream machinery that attempts to solve the problem as well.

@iwahbe
Copy link
Member Author

iwahbe commented Jul 31, 2023

How can we demonstrate that import and refresh scenarios exercising Read work as expected?

By adding a test that exercises the behavior. We can't do that yet, since any such test will fail until pulumi/pulumi-terraform-bridge#1312 merges.

@iwahbe
Copy link
Member Author

iwahbe commented Jul 31, 2023

What happens if a provider with this set of changes runs against stacks that used the old method and perhaps populated tags_all?

They will see a diff on "tags" (see #2655 (comment)), but otherwise everything will work as expected. This doesn't change anything about "tags_all", we won't see a problem here.

@t0yv0
Copy link
Member

t0yv0 commented Jul 31, 2023

We can't do that yet, since any such test will fail until

Ah super, you have a way to demonstrate in a test, that's great, let's try it. I think you can make the CI exercise pre-release code via go mod-replace. Alternatively, I had some nits on pulumi/pulumi-terraform-bridge#1312 but it's also mergeable once we add a quick test in there.

@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch 2 times, most recently from 62c00f7 to edf865e Compare August 1, 2023 12:32
@pulumi pulumi deleted a comment from github-actions bot Aug 1, 2023
@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch from edf865e to 304f439 Compare August 1, 2023 19:25
@pulumi pulumi deleted a comment from github-actions bot Aug 2, 2023
@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch from 304f439 to 1f1737c Compare August 2, 2023 09:05
@github-actions

This comment was marked as off-topic.

@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch from 05a87ac to 16f0081 Compare August 2, 2023 12:28
@iwahbe iwahbe requested a review from t0yv0 August 2, 2023 13:32
@t0yv0
Copy link
Member

t0yv0 commented Aug 2, 2023

Having a look at this again shortly.

diff --git a/internal/service/s3legacy/bucket_legacy.go b/internal/service/s3legacy/bucket_legacy.go
index 0563bd3a25..e6bd8e7013 100644
--- a/internal/service/s3legacy/bucket_legacy.go
+++ b/internal/service/s3legacy/bucket_legacy.go
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to just combine this into, or put this this change alongside patch 003 where we add this code?

Copy link
Member

Choose a reason for hiding this comment

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

This is interesting. The patch is still needed it just got shorter?

@@ -0,0 +1,217 @@
module github.com/pulumi/pulumi-aws/provider/tests
Copy link
Member

Choose a reason for hiding this comment

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

OK with introducing a test-specific module? I've done it elsewhere but was wondering if that's idiomatic Go for the problem of isolating test dependencies.

@t0yv0
Copy link
Member

t0yv0 commented Aug 2, 2023

I had a question about the semantic of one of the tests, and I'm not familiar with the underlying patch being edited. Otherwise LGTM.

@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch 4 times, most recently from 725c1f9 to fa2587d Compare August 3, 2023 14:27
TF does, and if we assume that "tags" is set correctly, we then get the expected diff.
This fix takes effect during Check, which occurs before Diff. We don't need to do anything
special for Diff now.
The previous fix works on all resources... except our s3legacy bucket because it was not
updated to use upstream's new tags strategy. I have changed our fork to use the new
strategy.

The change is here:
https://github.com/pulumi/terraform-provider-aws/compare/patched-v5.9.0...patched-v5.9.0-with-modern-s3legacy-tags?expand=1
We are seeing tests fail for aws:cognito:UserPool but I know this works for
aws:s3:BucketV2. These are both SDKv2 resources. Since they work differently I have
ensured that both are tested.
This resource is broken upstream, so we skip the test here.
I have left skeleton code for GRPC based testing, as I often find it useful when debugging
locally. I have noted that it does not run in CI, and is just there for local development.
@iwahbe iwahbe force-pushed the iwahbe/2633/default-tags-fix-untag branch from 8072478 to fd37ad2 Compare August 4, 2023 14:31
@iwahbe iwahbe merged commit e17996a into v6 Aug 4, 2023
15 checks passed
@iwahbe iwahbe deleted the iwahbe/2633/default-tags-fix-untag branch August 4, 2023 15:38
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.

3 participants