Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove diff calculation in observe-only reconciliation #461

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/controller/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
if e.eventHandler != nil {
e.eventHandler.Forget(rateLimiterStatus, mg.GetName())
}

// TODO(cem): Consider skipping diff calculation (terraform plan) to
// avoid potential config validation errors in the import path. See
// https://github.com/crossplane/upjet/pull/461
plan, err := e.workspace.Plan(ctx)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errPlan)
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/external_tfpluginfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ func (n *terraformPluginFrameworkExternalClient) Observe(ctx context.Context, mg
}
}

// TODO(cem): Consider skipping diff calculation to avoid potential config
// validation errors in the import path. See
// https://github.com/crossplane/upjet/pull/461
planResponse, hasDiff, err := n.getDiffPlanResponse(ctx, tfStateValue)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot calculate diff")
Expand Down
65 changes: 37 additions & 28 deletions pkg/controller/external_tfpluginsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ type Resource interface {
}

type terraformPluginSDKExternal struct {
ts terraform.Setup
resourceSchema Resource
config *config.Resource
instanceDiff *tf.InstanceDiff
params map[string]any
rawConfig cty.Value
logger logging.Logger
metricRecorder *metrics.MetricRecorder
opTracker *AsyncTracker
ts terraform.Setup
resourceSchema Resource
config *config.Resource
instanceDiff *tf.InstanceDiff
params map[string]any
rawConfig cty.Value
logger logging.Logger
metricRecorder *metrics.MetricRecorder
opTracker *AsyncTracker
isManagementPoliciesEnabled bool
}

func getExtendedParameters(ctx context.Context, tr resource.Terraformed, externalName string, cfg *config.Resource, ts terraform.Setup, initParamsMerged bool, kube client.Client) (map[string]any, error) {
Expand Down Expand Up @@ -294,14 +295,15 @@ func (c *TerraformPluginSDKConnector) Connect(ctx context.Context, mg xpresource
}

return &terraformPluginSDKExternal{
ts: ts,
resourceSchema: c.config.TerraformResource,
config: c.config,
params: params,
rawConfig: rawConfig,
logger: logger,
metricRecorder: c.metricRecorder,
opTracker: opTracker,
ts: ts,
resourceSchema: c.config.TerraformResource,
config: c.config,
params: params,
rawConfig: rawConfig,
logger: logger,
metricRecorder: c.metricRecorder,
opTracker: opTracker,
isManagementPoliciesEnabled: c.isManagementPoliciesEnabled,
}, nil
}

Expand Down Expand Up @@ -460,6 +462,7 @@ func (n *terraformPluginSDKExternal) getResourceDataDiff(tr resource.Terraformed
}

func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource.Managed) (managed.ExternalObservation, error) { //nolint:gocyclo
var err error
n.logger.Debug("Observing the external resource")

if meta.WasDeleted(mg) && n.opTracker.IsDeleted() {
Expand Down Expand Up @@ -492,15 +495,22 @@ func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource.
diffState.Attributes = nil
diffState.ID = ""
}
instanceDiff, err := n.getResourceDataDiff(mg.(resource.Terraformed), ctx, diffState, resourceExists)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot compute the instance diff")

n.instanceDiff = nil
policySet := sets.New[xpv1.ManagementAction](mg.(resource.Terraformed).GetManagementPolicies()...)
observeOnlyPolicy := sets.New(xpv1.ManagementActionObserve)
isObserveOnlyPolicy := policySet.Equal(observeOnlyPolicy)
if !isObserveOnlyPolicy || !n.isManagementPoliciesEnabled {
n.instanceDiff, err = n.getResourceDataDiff(mg.(resource.Terraformed), ctx, diffState, resourceExists)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot compute the instance diff")
}
}
if instanceDiff == nil {
instanceDiff = tf.NewInstanceDiff()
if n.instanceDiff == nil {
n.instanceDiff = tf.NewInstanceDiff()
}
n.instanceDiff = instanceDiff
noDiff := instanceDiff.Empty()

hasDiff := !n.instanceDiff.Empty()

if !resourceExists && mg.GetDeletionTimestamp() != nil {
gvk := mg.GetObjectKind().GroupVersionKind()
Expand Down Expand Up @@ -533,7 +543,6 @@ func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource.
return managed.ExternalObservation{}, errors.Wrap(err, "cannot marshal the attributes of the new state for late-initialization")
}

policySet := sets.New[xpv1.ManagementAction](mg.(resource.Terraformed).GetManagementPolicies()...)
policyHasLateInit := policySet.HasAny(xpv1.ManagementActionLateInitialize, xpv1.ManagementActionAll)
if policyHasLateInit {
specUpdateRequired, err = mg.(resource.Terraformed).LateInitialize(buff)
Expand All @@ -547,11 +556,11 @@ func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource.
return managed.ExternalObservation{}, errors.Errorf("could not set observation: %v", err)
}

if noDiff {
if !hasDiff {
n.metricRecorder.SetReconcileTime(mg.GetName())
}
if !specUpdateRequired {
resource.SetUpToDateCondition(mg, noDiff)
resource.SetUpToDateCondition(mg, !hasDiff)
}
// check for an external-name change
if nameChanged, err := n.setExternalName(mg, stateValueMap); err != nil {
Expand All @@ -563,7 +572,7 @@ func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource.

return managed.ExternalObservation{
ResourceExists: resourceExists,
ResourceUpToDate: noDiff,
ResourceUpToDate: !hasDiff,
ConnectionDetails: connDetails,
ResourceLateInitialized: specUpdateRequired,
}, nil
Expand Down
Loading