From 48225c8abe805144761ef3a0c82bc26d57ac3df9 Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Mon, 22 Jul 2024 09:40:58 -0400 Subject: [PATCH] WIP: fix provider definition Signed-off-by: Marques Johansson --- cmd/provider/main.go | 6 +----- internal/clients/equinix.go | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 381a735..df5c965 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -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" @@ -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), diff --git a/internal/clients/equinix.go b/internal/clients/equinix.go index bda40e0..7ed29f3 100644 --- a/internal/clients/equinix.go +++ b/internal/clients/equinix.go @@ -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" ) @@ -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 { @@ -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 +}