From acb59402d6f9619e7f6f4f870dcc69c322ea5e31 Mon Sep 17 00:00:00 2001 From: Cem Mergenci Date: Wed, 21 Feb 2024 09:58:09 +0300 Subject: [PATCH] Check LateInitialize management policy in Plugin Framework client. Signed-off-by: Cem Mergenci (cherry picked from commit af31423729601cfee5825b1d59f0d9c881f87078) --- pkg/controller/external_tfpluginfw.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/pkg/controller/external_tfpluginfw.go b/pkg/controller/external_tfpluginfw.go index 3f4119de..61785af0 100644 --- a/pkg/controller/external_tfpluginfw.go +++ b/pkg/controller/external_tfpluginfw.go @@ -27,6 +27,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane/upjet/pkg/config" @@ -270,13 +271,15 @@ func (n *terraformPluginFrameworkExternalClient) getDiffPlanResponse(ctx context return nil, false, errors.Wrap(err, "cannot compare prior state and plan") } - // filter diffs that has unknown plan value which corresponds to - // computed values. These cause unnecessary diff detection when only computed - // attribute diffs exist in the raw diff and no actual diff exists in the - // parametrizable attributes + // Filter diffs that have unknown plan values, which correspond to + // computed fields, and null plan values, which correspond to + // not-specified fields. Such cases cause unnecessary diff detection + // when only computed attributes or not-specified argument diffs + // exist in the raw diff and no actual diff exists in the + // parametrizable attributes. filteredDiff := make([]tftypes.ValueDiff, 0) for _, diff := range rawDiff { - if diff.Value1.IsKnown() { + if diff.Value1.IsKnown() && !diff.Value1.IsNull() { filteredDiff = append(filteredDiff, diff) } } @@ -348,10 +351,16 @@ func (n *terraformPluginFrameworkExternalClient) Observe(ctx context.Context, mg if err != nil { return managed.ExternalObservation{}, errors.Wrap(err, "cannot marshal the attributes of the new state for late-initialization") } - specUpdateRequired, err = mg.(resource.Terraformed).LateInitialize(buff) - if err != nil { - return managed.ExternalObservation{}, errors.Wrap(err, "cannot late-initialize the managed resource") + + 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) + if err != nil { + return managed.ExternalObservation{}, errors.Wrap(err, "cannot late-initialize the managed resource") + } } + err = mg.(resource.Terraformed).SetObservation(stateValueMap) if err != nil { return managed.ExternalObservation{}, errors.Errorf("could not set observation: %v", err)