Skip to content

Commit

Permalink
WIP: fix provider definition
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Jul 22, 2024
1 parent ff7804b commit 48225c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
6 changes: 1 addition & 5 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/statemetrics"
upcontroller "github.com/crossplane/upjet/pkg/controller"
"github.com/crossplane/upjet/pkg/terraform"
"gopkg.in/alecthomas/kingpin.v2"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -122,10 +121,7 @@ func main() {
Features: &feature.Flags{},
MetricOptions: &mo,
},
Provider: provider,
// use the following WorkspaceStoreOption to enable the shared gRPC mode
// terraform.WithProviderRunner(terraform.NewSharedProvider(log, os.Getenv("TERRAFORM_NATIVE_PROVIDER_PATH"), terraform.WithNativeProviderArgs("-debuggable")))
WorkspaceStore: terraform.NewWorkspaceStore(log),
Provider: provider,
SetupFn: clients.TerraformSetupBuilder(provider.TerraformProvider),
PollJitter: pollJitter,
OperationTrackerStore: upcontroller.NewOperationStore(log),
Expand Down
37 changes: 33 additions & 4 deletions internal/clients/equinix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
"encoding/json"

"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/upjet/pkg/terraform"
equinixprovider "github.com/equinix/terraform-provider-equinix/equinix/provider"
"github.com/equinix/terraform-provider-equinix/version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
terraformsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane/upjet/pkg/terraform"

"github.com/crossplane-contrib/provider-jet-equinix/apis/v1alpha1"
)

Expand Down Expand Up @@ -87,7 +89,15 @@ func prepareTerraformProviderConfiguration(creds map[string]string, pc v1alpha1.
// returns Terraform provider setup configuration
func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {
return func(ctx context.Context, client client.Client, mg resource.Managed) (terraform.Setup, error) {
ps := terraform.Setup{}
ps := terraform.Setup{
/*
Version: version,
Requirement: terraform.ProviderRequirement{
Source: providerSource,
Version: providerVersion,
},
*/
}

configRef := mg.GetProviderConfigReference()
if configRef == nil {
Expand All @@ -113,6 +123,25 @@ func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {
}

ps.Configuration = prepareTerraformProviderConfiguration(equinixCreds, pc.Spec.Configuration)
return ps, nil
return ps, errors.Wrap(configureNoForkEquinixClient(ctx, &ps, *tfProvider), "failed to configure the no-fork equinix client")
}
}

func configureNoForkEquinixClient(ctx context.Context, ps *terraform.Setup, p schema.Provider) error {
// Please be aware that this implementation relies on the schema.Provider
// parameter `p` being a non-pointer. This is because normally
// the Terraform plugin SDK normally configures the provider
// only once and using a pointer argument here will cause
// race conditions between resources referring to different
// ProviderConfigs.
diag := p.Configure(context.WithoutCancel(ctx), &terraformsdk.ResourceConfig{
Config: ps.Configuration,
})
if diag != nil && diag.HasError() {
return errors.Errorf("failed to configure the provider: %v", diag)
}

fwProvider := equinixprovider.CreateFrameworkProvider(version.ProviderVersion)
ps.FrameworkProvider = fwProvider
return nil
}

0 comments on commit 48225c8

Please sign in to comment.