Skip to content

Commit

Permalink
dynamically configure provider of the framework server
Browse files Browse the repository at this point in the history
Signed-off-by: Erhan Cagirici <erhan@upbound.io>
  • Loading branch information
erhancagirici committed Jan 9, 2024
1 parent 719470d commit 81472f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type Provider struct {
// TerraformProvider is the Terraform schema of the provider.
TerraformProvider *schema.Provider

TerraformPluginFrameworkProvider *fwprovider.Provider
TerraformPluginFrameworkProvider fwprovider.Provider

// refInjectors is an ordered list of `ReferenceInjector`s for
// injecting references across this Provider's resources.
Expand Down Expand Up @@ -197,7 +197,7 @@ func WithTerraformProvider(tp *schema.Provider) ProviderOption {
}
}

func WithTerraformPluginFrameworkProvider(tp *fwprovider.Provider) ProviderOption {
func WithTerraformPluginFrameworkProvider(tp fwprovider.Provider) ProviderOption {
return func(p *Provider) {
p.TerraformPluginFrameworkProvider = tp
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func NewProvider(ctx context.Context, schema []byte, prefix string, modulePath s

// 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)
terraformPluginFrameworkResourceFunctions := p.TerraformPluginFrameworkProvider.Resources(ctx)
for _, resourceFunc := range terraformPluginFrameworkResourceFunctions {
resource := resourceFunc()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewTerraformPluginFrameworkAsyncConnector(kube client.Client,
ots *OperationTrackerStore,
sf terraform.SetupFn,
cfg *config.Resource,
provider *provider.Provider,
provider provider.Provider,
opts ...TerraformPluginFrameworkAsyncOption) *TerraformPluginFrameworkAsyncConnector {
nfac := &TerraformPluginFrameworkAsyncConnector{
TerraformPluginFrameworkConnector: NewTerraformPluginFrameworkConnector(kube, sf, cfg, ots, provider),
Expand Down
24 changes: 15 additions & 9 deletions pkg/controller/external_terraform_plugin_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type TerraformPluginFrameworkConnector struct {
metricRecorder *metrics.MetricRecorder
operationTrackerStore *OperationTrackerStore
isManagementPoliciesEnabled bool
terraformPluginFrameworkProvider *fwprovider.Provider
terraformPluginFrameworkProvider fwprovider.Provider
}

// TerraformPluginFrameworkConnectorOption allows you to configure TerraformPluginFrameworkConnector.
Expand Down Expand Up @@ -72,7 +72,7 @@ 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, terraformPluginFrameworkProvider fwprovider.Provider, opts ...TerraformPluginFrameworkConnectorOption) *TerraformPluginFrameworkConnector {
connector := &TerraformPluginFrameworkConnector{
getTerraformSetup: sf,
kube: kube,
Expand Down Expand Up @@ -118,7 +118,17 @@ func (c *TerraformPluginFrameworkConnector) Connect(ctx context.Context, mg xpre
return nil, errors.Wrapf(err, "failed to get the extended parameters for resource %q", mg.GetName())
}

if !opTracker.HasFrameworkTFState() {
resourceSchema, err := c.getResourceSchema(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve resource schema")
}
hasState := false
if opTracker.HasFrameworkTFState() {
tfStateValue, err := opTracker.GetFrameworkTFState().Unmarshal(resourceSchema.Type().TerraformType(ctx))
hasState = err == nil && !tfStateValue.IsNull()
}

if !hasState {
logger.Debug("Instance state not found in cache, reconstructing...")
tfState, err := tr.GetObservation()
if err != nil {
Expand Down Expand Up @@ -150,11 +160,6 @@ func (c *TerraformPluginFrameworkConnector) Connect(ctx context.Context, mg xpre
return nil, errors.Wrap(err, "could not configure provider server")
}

resourceSchema, err := c.getResourceSchema(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve resource schema")
}

return &terraformPluginFrameworkExternalClient{
ts: ts,
config: c.config,
Expand All @@ -180,7 +185,7 @@ func (c *TerraformPluginFrameworkConnector) getResourceSchema(ctx context.Contex
}

func (c *TerraformPluginFrameworkConnector) configureProvider(ctx context.Context, ts terraform.Setup) (tfprotov5.ProviderServer, error) {
providerServer := providerserver.NewProtocol5(*c.terraformPluginFrameworkProvider)()
providerServer := providerserver.NewProtocol5(ts.FrameworkProvider)()
tsBytes, err := json.Marshal(ts.Configuration)
if err != nil {
return nil, errors.Wrap(err, "cannot marshal ts config")
Expand Down Expand Up @@ -245,6 +250,7 @@ func (n *terraformPluginFrameworkExternalClient) getDiffPlan(ctx context.Context

prcReq := &tfprotov5.PlanResourceChangeRequest{
TypeName: n.config.Name,
PriorState: n.opTracker.GetFrameworkTFState(),
Config: &tfConfig,
ProposedNewState: &tfPlannedState,
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/terraform/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/mitchellh/go-ps"
"github.com/pkg/errors"
"github.com/spf13/afero"
Expand Down Expand Up @@ -122,6 +123,8 @@ type Setup struct {
Scheduler ProviderScheduler

Meta any

FrameworkProvider fwprovider.Provider
}

// Map returns the Setup object in map form. The initial reason was so that
Expand Down

0 comments on commit 81472f0

Please sign in to comment.