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

Validate resources only during build time #37

Conversation

VenelinMartinov
Copy link

@VenelinMartinov VenelinMartinov commented Feb 1, 2024

Should address pulumi/pulumi-aws#3330 after aws picks this up.

The resource validation runs the SchemaFuncs which is quite slow for some resources. This PR makes this run only during tfgen, to save time and memory during runtime.

Tested in pulumi/pulumi-aws#3368 but I CI doesn't seem to run for some reason.

@VenelinMartinov VenelinMartinov added the impact/no-changelog-required This issue doesn't require a CHANGELOG update label Feb 1, 2024
@t0yv0
Copy link
Member

t0yv0 commented Feb 1, 2024

// InternalValidate should be called to validate the structure
// of the provider.
//
// This should be called in a unit test for any provider to verify
// before release that a provider is properly configured for use with
// this library.

This is interesting.. Can you help me understand why this is called at runtime by us at all? Stack trace perhaps from the panic?

@@ -0,0 +1,31 @@
// Copied from https://github.com/pulumi/pulumi-terraform-bridge/blob/7c411fc807e709a85170cab0add5cb3a856c0c25/pkg/tfbridge/runtime.go#L1
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 a really gnarly approach and I'm a little sad it's getting copied :) This for example does not detect unit test context properly. We don't have context.Context to propagate an option with but perhaps we could use a public global var that can be set by the unit test and TFGEN to enable the check.

@t0yv0
Copy link
Member

t0yv0 commented Feb 1, 2024

Apart from the implementation nits I'm very eager to land this.

@t0yv0 t0yv0 self-requested a review February 1, 2024 21:22
@VenelinMartinov
Copy link
Author

VenelinMartinov commented Feb 2, 2024

This is interesting.. Can you help me understand why this is called at runtime by us at all? Stack trace perhaps from the panic?

Yeah, here is the full stack trace.

panic: WAFV2 resource schema func called!

goroutine 14 [running]:
github.com/hashicorp/terraform-provider-aws/internal/service/wafv2.ResourceIPSet.func2()
	/Users/vvm/code/pulumi-aws/upstream/internal/service/wafv2/ip_set.go:59 +0x2c
github.com/hashicorp/terraform-provider-aws/shim.markTagsAllNotComputedForResource.func1()
	/Users/vvm/code/pulumi-aws/upstream/shim/shim.go:60 +0x38
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).SchemaMap(...)
	/Users/vvm/code/terraform-plugin-sdk/helper/schema/resource.go:655
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).InternalValidate(0x140022f5500, 0x0, 0x1)
	/Users/vvm/code/terraform-plugin-sdk/helper/schema/resource.go:1148 +0x9c
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Provider).InternalValidate(0x14000c0a6c0)
	/Users/vvm/code/terraform-plugin-sdk/helper/schema/provider.go:148 +0x208
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Provider).Validate(0x14000c0a6c0, 0x9cb?)
	/Users/vvm/code/terraform-plugin-sdk/helper/schema/provider.go:224 +0x24
github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2.v2Provider.Validate({0x14000c0a6c0?, {0x1400108a470?, 0x1130e05b8?, 0x14000f11680?}}, {0x0?, 0x0?}, {0x112f13760?, 0x14002b0a330?})
	/Users/vvm/code/pulumi-terraform-bridge/pkg/tfshim/sdk-v2/provider.go:82 +0x54
github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge.validateProviderConfig({0x11308a770?, 0x14002b0a180?}, {0x14000d72ac0, 0x40}, 0x14000c39980, {0x112f13760?, 0x14002b0a330})
	/Users/vvm/code/pulumi-terraform-bridge/pkg/tfbridge/provider.go:559 +0x210
github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge.(*Provider).CheckConfig(0x14000c39980, {0x11308a1c0, 0x11aa9ff80}, 0x14002b00000)
	/Users/vvm/code/pulumi-terraform-bridge/pkg/tfbridge/provider.go:457 +0x6e8
github.com/pulumi/pulumi-terraform-bridge/x/muxer.(*muxer).CheckConfig.func1()
	/Users/vvm/code/pulumi-terraform-bridge/x/muxer/muxer.go:127 +0x74
github.com/pulumi/pulumi-terraform-bridge/x/muxer.asyncJoin[...].func1()
	/Users/vvm/code/pulumi-terraform-bridge/x/muxer/muxer.go:583 +0x48
created by github.com/pulumi/pulumi-terraform-bridge/x/muxer.asyncJoin[...] in goroutine 13
	/Users/vvm/code/pulumi-terraform-bridge/x/muxer/muxer.go:582 +0x6c

We call the tf provider's Validate method:

func (p *Provider) Validate(c *terraform.ResourceConfig) diag.Diagnostics {
if err := p.InternalValidate(); err != nil {
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: "InternalValidate",
Detail: fmt.Sprintf("Internal validation of the provider failed! This is always a bug\n"+
"with the provider itself, and not a user issue. Please report\n"+
"this bug:\n\n%s", err),
},
}
}
return schemaMap(p.Schema).Validate(c)
}

from

https://github.com/pulumi/pulumi-terraform-bridge/blob/e91ed7ee525a89502467902ae4f8e52d1e2fa694/pkg/tfbridge/provider.go#L559

It looks like the tf Validate method then calls the InternalValidate, which doesn't look like is necessary during runtime since it only deals with the schema. It then validates the config.

I'll change the PR to only call the InternalValidate during tfgen.

@VenelinMartinov
Copy link
Author

I'll add a test for this behaviour in the bridge

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.

This change looks good.

Is there a reason why we are also not skipping schemaMap(p.Schema).Validate(c) on line (234)?

@VenelinMartinov
Copy link
Author

I believe that validates the config passed to the provider, which depends on runtime behaviour, so sounds useful

@VenelinMartinov
Copy link
Author

Test added in pulumi/pulumi-terraform-bridge#1669, I'm going to merge this so I can get the right dependencies.

@VenelinMartinov VenelinMartinov merged commit e2a20ae into upstream-v2.29.0 Feb 2, 2024
1 check passed
@VenelinMartinov VenelinMartinov deleted the vvm/validate_resources_only_during_build_time branch February 2, 2024 16:33
VenelinMartinov added a commit to pulumi/pulumi-terraform-bridge that referenced this pull request Feb 2, 2024
@t0yv0
Copy link
Member

t0yv0 commented Feb 2, 2024

This is VERY unfortunate that their code calls InternalValidate which was intended for the static world of unit tests as part of validating the actual configuration data.

@t0yv0
Copy link
Member

t0yv0 commented Feb 2, 2024

I like how this change turned out. That is perfect. This internal validation should not be the default behavior. Thank you!

VenelinMartinov pushed a commit to pulumi/pulumi-aws that referenced this pull request Feb 2, 2024
…5137be (#3377)

Should address #3330

This SHA is one beyond the last bridge upgrade and only pulls in
pulumi/pulumi-terraform-bridge@6d1962d
which pulls in pulumi/terraform-plugin-sdk#37

That should improve startup times in AWS by ~300ms

This PR was generated via `$ upgrade-provider pulumi/pulumi-aws
--kind=bridge
--target-bridge-version=6d1962d8367a055a8859efa62118ec832e5137be
--pr-reviewers=VenelinMartinov`.

---

- Upgrading pulumi-terraform-bridge from v3.73.0 to
6d1962d8367a055a8859efa62118ec832e5137be.
- Upgrading pulumi-terraform-bridge/pf from v0.26.0 to
6d1962d8367a055a8859efa62118ec832e5137be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact/no-changelog-required This issue doesn't require a CHANGELOG update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants