From 1ed1df0d27f405d650f4304895c2b0b7d3a3f184 Mon Sep 17 00:00:00 2001 From: Cem Mergenci Date: Wed, 14 Feb 2024 01:24:51 +0300 Subject: [PATCH] Check LateInitialize management policy in SDKv2 external client. Signed-off-by: Cem Mergenci --- pkg/controller/external_tfpluginsdk.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/controller/external_tfpluginsdk.go b/pkg/controller/external_tfpluginsdk.go index 5b40fe0e..ae49b59c 100644 --- a/pkg/controller/external_tfpluginsdk.go +++ b/pkg/controller/external_tfpluginsdk.go @@ -22,6 +22,7 @@ import ( tf "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "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" @@ -522,9 +523,14 @@ func (n *terraformPluginSDKExternal) Observe(ctx context.Context, mg xpresource. 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)