diff --git a/pkg/controller/external_tfpluginsdk.go b/pkg/controller/external_tfpluginsdk.go index cb408d3d..43c3560a 100644 --- a/pkg/controller/external_tfpluginsdk.go +++ b/pkg/controller/external_tfpluginsdk.go @@ -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) { @@ -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 } @@ -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() { @@ -492,15 +495,23 @@ 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(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() + + noDiff := n.instanceDiff.Empty() if !resourceExists && mg.GetDeletionTimestamp() != nil { gvk := mg.GetObjectKind().GroupVersionKind() @@ -533,7 +544,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)