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

Allow tfgen to use pulumi convert for examples conversion #1401

Merged
merged 18 commits into from
Oct 3, 2023

Conversation

t0yv0
Copy link
Member

@t0yv0 t0yv0 commented Sep 26, 2023

Fixes #1352

Requires a modified pulumi/pulumi-converter-terraform#29

A new option is added for tfgen, via the environment variable PULUMI_CONVERT. IF this option is truthy, make tfgen will now be using pulumi convert when converting examples, specifically for the TF HCL to Pulumi PCL phase of the conversion.

How this was tested. Tried on these providers:

  • pulumi-wavefront
  • pulumi-gcp

Ensured conversion rates improve. Ensured COVERAGE_OUTPUT_DIR, if set, continues to populate sensible conversion metrics. Ensured printed warnings and example conversion metrics out of make tfgen are still sensible.

When rolling this out to providers, recommend making sure that Pulumi CLI in use is pinned first, as the repositories are sensitive to small changes in the generated examples and a floating Pulumi CLI reference can introduce breaking CI checks.

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 0679f5a

@github-actions
Copy link

Diff for pulumi-random with merge commit 0679f5a

@t0yv0
Copy link
Member Author

t0yv0 commented Sep 27, 2023

@Frassle @iwahbe @mjeffryes @mikhailshilkov

This is rough around the edges but it's promising. I've got it to render pulumi-gcp example set through the new converter engaged out of process via pulumi convert. It seems viable.

pulumi-converter-terraform spins CPU for about 3min, but is fairly low on memory utilization (<50mb).

We can parallelize it to stand up N*CPU copies.

Before parallelization as things stand the total timings for schema generation with this on GCP are:

      187.20 real       254.00 user        15.11 sys

That's not bad we can take that. The appeal of the new converter is high.

@t0yv0
Copy link
Member Author

t0yv0 commented Sep 27, 2023

The annoying bits:

  • I hit "limit reached on GitHub API" because something was hitting up GitHub.com/pulumi/pulumi-gcp to find latest release too often; I've modified the loader to try to recognize that it already has the partial schema for GCP in-place; I'm surprised and a little intrigued about how this works in prod without the changes here and without hitting the limit

  • The GCP provider names itself "google-beta" but there's also "google" and "gcp" and it's really finicky which one is the provider identifier; when talking to pulumi-convert apparently mapping files sent via --mappings have to be called by convention "gcp.json" but this is not obvious should it be "gcp.json" or "google.json" or..

  • It's extremely annoying that the code retains the package loader to perform pcl binding; this loader has to match what the underlying loader in pulumi convert is going to do; definitely for the current provider such as gcp, but really also for any dynamically loaded providers that show up in examples like "std"; there's no consistent pinning

  • by virtue of above, we also still build-depend on every language's codegen here

@t0yv0
Copy link
Member Author

t0yv0 commented Sep 27, 2023

On the positive side, the code shouldn't panic so we don't need to inject recover and lose track panics. I will hook up example metrics to get them right (that's on the TODO). Perhaps could also push through a bulk examples.json interface for all the other converters (ts,py,yaml,java,cs)? Or is that too ambitious?

pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
@t0yv0 t0yv0 force-pushed the t0yv0/convert-examples-via-cli branch from 440d362 to 99b08cd Compare September 28, 2023 19:24
@github-actions
Copy link

Diff for pulumi-random with merge commit 4ea49c1

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 4ea49c1

@github-actions
Copy link

Diff for pulumi-random with merge commit 6ab56be

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 6ab56be

@t0yv0 t0yv0 force-pushed the t0yv0/convert-examples-via-cli branch from def6b6c to 9ab59aa Compare September 29, 2023 18:31
@github-actions
Copy link

Diff for pulumi-random with merge commit de3eab7

@github-actions
Copy link

Diff for pulumi-azuread with merge commit de3eab7

@github-actions
Copy link

Diff for pulumi-random with merge commit 8eed677

@t0yv0 t0yv0 marked this pull request as ready for review September 29, 2023 19:20
@github-actions
Copy link

Diff for pulumi-azuread with merge commit 8eed677

@github-actions
Copy link

Diff for pulumi-random with merge commit 74e9ffa

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 74e9ffa

@t0yv0
Copy link
Member Author

t0yv0 commented Sep 29, 2023

limit reached on GitHub API is resolved; was a bug in this code. A few more low level issues are resolved. This is working better.

@t0yv0 t0yv0 changed the title Refactor: convert examples via cli Allow tfgen to use pulumi convert for examples conversion Sep 29, 2023
@t0yv0 t0yv0 requested review from iwahbe and Frassle September 29, 2023 19:28
@github-actions
Copy link

Diff for pulumi-random with merge commit 33f79a8

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 33f79a8

@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2023

Codecov Report

Merging #1401 (18166cf) into master (772baf6) will increase coverage by 0.36%.
The diff coverage is 73.19%.

@@            Coverage Diff             @@
##           master    #1401      +/-   ##
==========================================
+ Coverage   57.01%   57.37%   +0.36%     
==========================================
  Files         147      147              
  Lines       24400    24714     +314     
==========================================
+ Hits        13911    14180     +269     
- Misses       9505     9527      +22     
- Partials      984     1007      +23     
Files Coverage Δ
pkg/tfgen/docs.go 72.94% <100.00%> (+2.04%) ⬆️
pkg/tfgen/generate.go 57.68% <ø> (+0.39%) ⬆️
pkg/tfgen/generate_schema.go 81.25% <100.00%> (+0.06%) ⬆️
pkg/tfgen/convert_cli.go 69.62% <69.62%> (ø)

... and 2 files with indirect coverage changes

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

pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
pkg/tfgen/convert_cli.go Show resolved Hide resolved
@github-actions
Copy link

Diff for pulumi-random with merge commit 0e7435a

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 0e7435a

@t0yv0 t0yv0 force-pushed the t0yv0/convert-examples-via-cli branch from bf6dcc4 to e046f4e Compare October 2, 2023 21:00
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-azuread with merge commit 9d47b37

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-azuread with merge commit 837c1cd

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-random with merge commit 9d47b37

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-random with merge commit 837c1cd

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-azuread with merge commit 42a5c98

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-random with merge commit 42a5c98

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-azuread with merge commit 7b2c036

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-random with merge commit 7b2c036

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-azuread with merge commit 79ed526

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Diff for pulumi-random with merge commit 79ed526

@@ -106,3 +106,4 @@ tfgen, the command that generates Pulumi schema/code for a bridged provider supp
* `PULUMI_SKIP_MISSING_MAPPING_ERROR`: If truthy, tfgen will not fail if a data source or resource in the TF provider is not mapped to the Pulumi provider. Instead, a warning is printed. Default is `false`.
* `PULUMI_SKIP_EXTRA_MAPPING_ERROR`: If truthy, tfgen will not fail if a mapped data source or resource does not exist in the TF provider. Instead, warning is printed. Default is `false`.
* `PULUMI_MISSING_DOCS_ERROR`: If truthy, tfgen will fail if docs cannot be found for a data source or resource. Default is `false`.
* `PULUMI_CONVERT`: If truthy, tfgen will shell out to `pulumi convert` for converting example code from TF HCL to Pulumi PCL
Copy link
Member

Choose a reason for hiding this comment

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

Do we expect to use this ourselves?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. The idea is to roll this out to Tier 1 providers as soon as we can. In terms of retiring this flag, we'd need to make sure our entire fleet has migrated (including Tier 2-3) and then stakeholders and community have migrated, then we can retire it and make it the default.

pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
Comment on lines +225 to +226
// This may need to be coarse-grain parallelized to speed up larger providers at the cost of more
// memory, for example run 4 instances of `pulumi convert` on 25% of examples each.
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if this kind of speedup could happen in the engine, where we just pass the number of concurrent processes to run at once. It is safe to parallelize within process because there is no shared mutable state.1

Footnotes

  1. If there is shared mutable state, then parallelizing by process splitting will be non-deterministic.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is safe to parallelize within process because there is no shared mutable state.

Famous last words. Prove there's no shared mutable state in large go systems (think static var).

Copy link
Member

Choose a reason for hiding this comment

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

Hence the footnote: It will be either safe to parallelize within a single process or it isn't, in which case I'll be scared to parallelize across processes because each update shares mutable state.

Copy link
Member Author

Choose a reason for hiding this comment

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

At a first approximation, yes, but it's not apples to apples.

If you're parallelizing in-process, you can get issues related to say panics on concurrent map element access for shared state.

If you're parallelizing outside of process, you can get issues related to using shared OS resources such as file handles or folders.

It's just a guess at this point, I don't know if any of those are actual issues. We could try and see at some point.

pkg/tfgen/convert_cli.go Outdated Show resolved Hide resolved
if runtime.GOOS == "windows" {
// Currently there is a test issue in CI/test setup:
//
// convertViaPulumiCLI: failed to clean up temp bridge-examples.json file: The
Copy link
Member

Choose a reason for hiding this comment

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

Have we manually tested running the new convert workflow on windows? We do support authoring bridged providers on windows AFAIK.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, we haven't. It's not on the critical path. I can add it to the bottom of the epic.

Copy link
Member

Choose a reason for hiding this comment

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

It's necessary for removing the old convert logic, but shouldn't be blocking for us to use the new logic. We might want to add a warning for users who turn on the new converter on windows computers.

@iwahbe
Copy link
Member

iwahbe commented Oct 3, 2023

I would really like a plan for how this change will roll out.

@t0yv0
Copy link
Member Author

t0yv0 commented Oct 3, 2023

The plan is included in the epic #1280

@t0yv0 t0yv0 requested a review from iwahbe October 3, 2023 19:38
@github-actions
Copy link

github-actions bot commented Oct 3, 2023

Diff for pulumi-azuread with merge commit 60260d4

@github-actions
Copy link

github-actions bot commented Oct 3, 2023

Diff for pulumi-random with merge commit 60260d4

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.

LGTM

@t0yv0 t0yv0 merged commit e3aabd3 into master Oct 3, 2023
@t0yv0 t0yv0 deleted the t0yv0/convert-examples-via-cli branch October 3, 2023 22:03
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.

Decouple example conversion to avoid linking converters into providers
4 participants