Skip to content

Commit

Permalink
Fix shims (#2511)
Browse files Browse the repository at this point in the history
## Problem
When running `go list -m` in a provider upgraded to use bridge v3.93.1
you will get this error:

```
go: github.com/hashicorp/terraform-provider-tls@v0.0.0: invalid version: unknown revision v0.0.0
go: github.com/hashicorp/terraform-provider-tls/shim@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
go: github.com/terraform-providers/terraform-provider-random/randomshim@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
```

Ian called this out in
#2473 (comment).
My response there is true in the sense that providers _do still build_
just fine -- `go build`, `go test` etc. don't touch these unused
modules. The caveat I overlooked is that some IDE integrations (or more
specifically any tooling built around `go list`) _will_ try to resolve
all of these direct and indirect dependencies.

When we consolidated modules, we moved `replace` directives for these
shims out of the test module and into the root module.

## Option 0: Bring back all the modules

I beg you not to do this but I do want to call it out as an option if
the team would prefer it. I outline two more "idiomatic" options below.

## Option 1: Handle replacements in the consumer

As a reminder, the `replace` directive only applies to the module where
it's defined -- primarily to keep Go's minimal version selection
algorithm simple and fast, but also to give downstream modules complete
control of their builds. Hence `replace` is best suited for temporary
overrides, for example to point at a local branch.

Permanent `replace` directives do happen and we use them quite a lot,
though. If a module truly needs a permanent `replace` to work properly
(which, to be clear, should be avoided!), its consumers need to mirror
the same `replace`.

In other words, Option 1 is to not make any changes. Downstream projects
can resolve this issue by copying these lines into `go.mod`:

```
replace github.com/hashicorp/terraform-provider-tls/shim => github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/tlsshim v0.0.0-20241025174446-f865647b85ed

replace github.com/hashicorp/terraform-provider-tls => github.com/hashicorp/terraform-provider-tls v1.2.1-0.20210201152640-49d7590ccf3f

replace github.com/terraform-providers/terraform-provider-random/randomshim => github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/randomshim v0.0.0-20241025174446-f865647b85ed
```

Clearly this is burdensome. For a real-world data point, this is the
same strategy adopted by the k8s ecosystem and it has made dependency
management with k8s notoriously painful.

We should not do this.

## Option 2: Fork instead of abusing modules

`tlsshim` and `randomshim` are their own modules because they
intentionally trick the compiler into exposing internal terraform
packages. This `internal` import is only legal if your module is
identified as
`github.com/terraform-providers/terraform-provider-random`:


https://github.com/pulumi/pulumi-terraform-bridge/blob/7dfbcd601c7392fbf04bdec4c83a56b1ef115bc6/pkg/pf/tests/internal/randomshim/shim.go#L17-L24

This is hacky but it matches the guidance we gives to users, and it is
_workable_ as long as the module consuming these shims doesn't have
downstream consumers of its own. This is the case with most providers,
but tfbridge is different because it _is_ meant to be consumed as a
library.

A more typical remedy for consuming private types is to fork the
dependency and expose what you need. We could fork these two
repositories under the `pulumi` org, but new repos come with their own
overhead.

Instead, I opted to simply check-in the upstream random and tls
providers. I preserved the commits we were previously pinned to
([c6e90de46687](hashicorp/terraform-provider-random@c6e90de)
and
[afdd54107aba](hashicorp/terraform-provider-tls@afdd541),
respectively) so licensing is unaffected.

This way our `go.mod` will reflect a dependency on a public module that
is still consumable downstream (e.g. `terraform-provider-random`), and
we will use forks that expose the internals when testing.

This PR implements Option 2.
  • Loading branch information
blampe authored Oct 28, 2024
1 parent 66ab90d commit ce626b6
Show file tree
Hide file tree
Showing 102 changed files with 24,669 additions and 286 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Fixes a false positive for gofmt on Windows.
# For more details,see: https://github.com/golangci/golangci-lint/issues/580
*.go text eol=lf
*.go text eol=lf
**/vendor/** linguist-vendored
12 changes: 4 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/hashicorp/terraform-plugin-mux v0.16.0
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hashicorp/terraform-provider-tls/shim v0.0.0-00010101000000-000000000000
github.com/hashicorp/terraform-provider-tls v1.2.1-0.20230117062332-afdd54107aba
github.com/hashicorp/terraform-svchost v0.1.1
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
Expand All @@ -51,7 +51,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/teekennedy/goldmark-markdown v0.3.0
github.com/terraform-providers/terraform-provider-random/randomshim v0.0.0-00010101000000-000000000000
github.com/terraform-providers/terraform-provider-random v1.3.2-0.20231204135814-c6e90de46687
github.com/yuin/goldmark v1.7.4
github.com/zclconf/go-cty v1.14.2
golang.org/x/crypto v0.25.0
Expand Down Expand Up @@ -96,7 +96,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/hashicorp/terraform-provider-tls v0.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -111,7 +110,6 @@ require (
github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 // indirect
github.com/pulumi/esc v0.10.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/terraform-providers/terraform-provider-random v1.3.2-0.20231204135814-c6e90de46687 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
Expand Down Expand Up @@ -261,8 +259,6 @@ require (

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240520223432-0c0bf0d65f10

replace github.com/hashicorp/terraform-provider-tls => github.com/hashicorp/terraform-provider-tls v1.2.1-0.20230117062332-afdd54107aba
replace github.com/hashicorp/terraform-provider-tls => ./pkg/pf/tests/internal/tlsshim/vendor/github.com/hashicorp/terraform-provider-tls

replace github.com/hashicorp/terraform-provider-tls/shim => ./pkg/pf/tests/internal/tlsshim

replace github.com/terraform-providers/terraform-provider-random/randomshim => ./pkg/pf/tests/internal/randomshim
replace github.com/terraform-providers/terraform-provider-random => ./pkg/pf/tests/internal/randomshim/vendor/github.com/hashicorp/terraform-provider-random
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,6 @@ github.com/hashicorp/terraform-plugin-sdk v1.7.0/go.mod h1:OjgQmey5VxnPej/buEhe+
github.com/hashicorp/terraform-plugin-test v1.2.0/go.mod h1:QIJHYz8j+xJtdtLrFTlzQVC0ocr3rf/OjIpgZLK56Hs=
github.com/hashicorp/terraform-plugin-testing v1.5.1 h1:T4aQh9JAhmWo4+t1A7x+rnxAJHCDIYW9kXyo4sVO92c=
github.com/hashicorp/terraform-plugin-testing v1.5.1/go.mod h1:dg8clO6K59rZ8w9EshBmDp1CxTIPu3yA4iaDpX1h5u0=
github.com/hashicorp/terraform-provider-tls v1.2.1-0.20230117062332-afdd54107aba h1:xoUp/RqxaIf20SzMbRTbdM/ORPoJ6CBGuw9PoxyGWqk=
github.com/hashicorp/terraform-provider-tls v1.2.1-0.20230117062332-afdd54107aba/go.mod h1:TBjYP1eCMBy/8zMvovtWNHC9/HT/qaRNqN8HdxeIWaE=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
Expand Down Expand Up @@ -2031,8 +2029,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/teekennedy/goldmark-markdown v0.3.0 h1:ik9/biVGCwGWFg8dQ3KVm2pQ/wiiG0whYiUcz9xH0W8=
github.com/teekennedy/goldmark-markdown v0.3.0/go.mod h1:kMhDz8La77A9UHvJGsxejd0QUflN9sS+QXCqnhmxmNo=
github.com/terraform-providers/terraform-provider-random v1.3.2-0.20231204135814-c6e90de46687 h1:4gT3jbJFnnGV2Liq4jziT7C4w/OOM+pkuwBGVDagRgc=
github.com/terraform-providers/terraform-provider-random v1.3.2-0.20231204135814-c6e90de46687/go.mod h1:nH2NdrX91QoiiaPJsTZ9vKoezUq5IgHJ8nijpD6lT5o=
github.com/texttheater/golang-levenshtein v1.0.1 h1:+cRNoVrfiwufQPhoMzB6N0Yf/Mqajr6t1lOv8GyGE2U=
github.com/texttheater/golang-levenshtein v1.0.1/go.mod h1:PYAKrbF5sAiq9wd+H82hs7gNaen0CplQ9uvm6+enD/8=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
Expand Down
3 changes: 1 addition & 2 deletions pkg/pf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ gen.testdata::
tidy::
go mod tidy
cd tests && go mod tidy
cd tests/internal/randomshim && go mod tidy
cd tests/internal/tlsshim && go mod tidy
cd dynamic && go mod tidy
cd tests/testdatagen/genrandom && go mod tidy

.PHONY: todos
4 changes: 2 additions & 2 deletions pkg/pf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Follow these steps to bridge a Terraform Provider to Pulumi.
[exposes](https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/provider.go#L13) a `func
New() provider.Provider` call. Since this definition lives in an `internal` package it cannot easily be referenced in
an external Go project, but it is still possible to reference it using Go linker tricks. See
`tests/internal/randomshim/shim.go` for a full example.
[pulumi-random](https://github.com/pulumi/pulumi-random/tree/48c0b3014aeaa0cb95fd6419d631cb2555ce89ac/provider/shim) for a full example.

2. Populate a `ProviderInfo` struct, mapping Terraform resource names to Pulumi tokens. Replace `myprovider` with your
provider name.
Expand Down Expand Up @@ -137,7 +137,7 @@ to the Plugin Framework.
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-tls/internal/provider"
"github.com/hashicorp/terraform-provider-tls/internal/provider"
)
func NewProvider() *schema.Provider {
Expand Down
38 changes: 0 additions & 38 deletions pkg/pf/tests/internal/randomshim/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/pf/tests/internal/randomshim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package randomshim

import (
tfpf "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/terraform-providers/terraform-provider-random/internal/provider"
"github.com/terraform-providers/terraform-provider-random/provider"
)

func NewProvider() tfpf.Provider {
Expand Down
Loading

0 comments on commit ce626b6

Please sign in to comment.