From 5f84779a41ab254abd2f8257f3ba12fff95411ad Mon Sep 17 00:00:00 2001 From: Erhan Cagirici Date: Tue, 9 Jan 2024 17:01:59 +0300 Subject: [PATCH] refactor obsolete plugin framework provider variables Signed-off-by: Erhan Cagirici --- pkg/config/provider.go | 4 +--- .../external_async_terraform_plugin_framework.go | 4 +--- pkg/controller/external_terraform_plugin_framework.go | 11 +++++------ pkg/pipeline/templates/controller.go.tmpl | 4 ++-- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/config/provider.go b/pkg/config/provider.go index 558c3ba0..8239c3de 100644 --- a/pkg/config/provider.go +++ b/pkg/config/provider.go @@ -288,6 +288,7 @@ func NewProvider(ctx context.Context, schema []byte, prefix string, modulePath s } p.skippedResourceNames = make([]string, 0, len(resourceMap)) + terraformPluginFrameworkResourceFunctions := p.TerraformPluginFrameworkProvider.Resources(ctx) for name, terraformResource := range resourceMap { if len(terraformResource.Schema) == 0 { // There are resources with no schema, that we will address later. @@ -323,9 +324,6 @@ func NewProvider(ctx context.Context, schema []byte, prefix string, modulePath s "but either config.Provider.TerraformProvider is not configured or the Go schema does not exist for the resource", name)) } - // TODO(cem): Consider creating a new context here, rather than getting one as input to this function. - // TODO(cem): Currently, terraformPluginFrameworkResourceFunctions is calculated for each plugin framework resource. Doing so is wasteful, because the result is independent of the resource. It should be called once, outside the loop. - terraformPluginFrameworkResourceFunctions := p.TerraformPluginFrameworkProvider.Resources(ctx) for _, resourceFunc := range terraformPluginFrameworkResourceFunctions { resource := resourceFunc() diff --git a/pkg/controller/external_async_terraform_plugin_framework.go b/pkg/controller/external_async_terraform_plugin_framework.go index e638230b..1bc6a991 100644 --- a/pkg/controller/external_async_terraform_plugin_framework.go +++ b/pkg/controller/external_async_terraform_plugin_framework.go @@ -11,7 +11,6 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/pkg/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -35,10 +34,9 @@ func NewTerraformPluginFrameworkAsyncConnector(kube client.Client, ots *OperationTrackerStore, sf terraform.SetupFn, cfg *config.Resource, - provider provider.Provider, opts ...TerraformPluginFrameworkAsyncOption) *TerraformPluginFrameworkAsyncConnector { nfac := &TerraformPluginFrameworkAsyncConnector{ - TerraformPluginFrameworkConnector: NewTerraformPluginFrameworkConnector(kube, sf, cfg, ots, provider), + TerraformPluginFrameworkConnector: NewTerraformPluginFrameworkConnector(kube, sf, cfg, ots), } for _, f := range opts { f(nfac) diff --git a/pkg/controller/external_terraform_plugin_framework.go b/pkg/controller/external_terraform_plugin_framework.go index 575acbdf..da69adb2 100644 --- a/pkg/controller/external_terraform_plugin_framework.go +++ b/pkg/controller/external_terraform_plugin_framework.go @@ -72,13 +72,12 @@ func WithTerraformPluginFrameworkManagementPolicies(isManagementPoliciesEnabled } } -func NewTerraformPluginFrameworkConnector(kube client.Client, sf terraform.SetupFn, cfg *config.Resource, ots *OperationTrackerStore, terraformPluginFrameworkProvider fwprovider.Provider, opts ...TerraformPluginFrameworkConnectorOption) *TerraformPluginFrameworkConnector { +func NewTerraformPluginFrameworkConnector(kube client.Client, sf terraform.SetupFn, cfg *config.Resource, ots *OperationTrackerStore, opts ...TerraformPluginFrameworkConnectorOption) *TerraformPluginFrameworkConnector { connector := &TerraformPluginFrameworkConnector{ - getTerraformSetup: sf, - kube: kube, - config: cfg, - operationTrackerStore: ots, - terraformPluginFrameworkProvider: terraformPluginFrameworkProvider, + getTerraformSetup: sf, + kube: kube, + config: cfg, + operationTrackerStore: ots, } for _, f := range opts { f(connector) diff --git a/pkg/pipeline/templates/controller.go.tmpl b/pkg/pipeline/templates/controller.go.tmpl index 56c5efe9..611addb5 100644 --- a/pkg/pipeline/templates/controller.go.tmpl +++ b/pkg/pipeline/templates/controller.go.tmpl @@ -68,7 +68,7 @@ func Setup(mgr ctrl.Manager, o tjcontroller.Options) error { {{- end -}} {{- else if .UseTerraformPluginFrameworkClient -}} {{- if .UseAsync }} - tjcontroller.NewTerraformPluginFrameworkAsyncConnector(mgr.GetClient(), o.OperationTrackerStore, o.SetupFn, o.Provider.Resources["{{ .ResourceType }}"], o.Provider.TerraformPluginFrameworkProvider, + tjcontroller.NewTerraformPluginFrameworkAsyncConnector(mgr.GetClient(), o.OperationTrackerStore, o.SetupFn, o.Provider.Resources["{{ .ResourceType }}"], tjcontroller.WithTerraformPluginFrameworkAsyncLogger(o.Logger), tjcontroller.WithTerraformPluginFrameworkAsyncConnectorEventHandler(eventHandler), tjcontroller.WithTerraformPluginFrameworkAsyncCallbackProvider(ac), @@ -78,7 +78,7 @@ func Setup(mgr ctrl.Manager, o tjcontroller.Options) error { {{- end -}} ) {{- else }} - tjcontroller.NewTerraformPluginFrameworkConnector(mgr.GetClient(), o.SetupFn, o.Provider.Resources["{{ .ResourceType }}"], o.OperationTrackerStore, o.Provider.TerraformPluginFrameworkProvider, + tjcontroller.NewTerraformPluginFrameworkConnector(mgr.GetClient(), o.SetupFn, o.Provider.Resources["{{ .ResourceType }}"], o.OperationTrackerStore, tjcontroller.WithTerraformPluginFrameworkLogger(o.Logger), tjcontroller.WithTerraformPluginFrameworkMetricRecorder(metrics.NewMetricRecorder({{ .TypePackageAlias }}{{ .CRD.Kind }}_GroupVersionKind, mgr, o.PollInterval)), {{if .FeaturesPackageAlias -}}