From 0340737915b657f70435bab1e84cc4ecdc530b5a Mon Sep 17 00:00:00 2001 From: Lachezar Tsonov Date: Thu, 14 Nov 2024 10:54:45 +0200 Subject: [PATCH] Save generated credentialsID from update response in state before re-reading from server to avoid perpetual drift --- castai/resource_aks_cluster.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/castai/resource_aks_cluster.go b/castai/resource_aks_cluster.go index fb69dfb8..0bf661ac 100644 --- a/castai/resource_aks_cluster.go +++ b/castai/resource_aks_cluster.go @@ -226,6 +226,7 @@ func updateAKSClusterSettings(ctx context.Context, data *schema.ResourceData, cl // Retries are required for newly created IAM resources to initialise on Azure side. b := backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(10*time.Second), 30), ctx) var lastErr error + var credentialsID string if err = backoff.RetryNotify(func() error { response, err := client.ExternalClusterAPIUpdateClusterWithResponse(ctx, data.Id(), req) if err != nil { @@ -247,13 +248,7 @@ func updateAKSClusterSettings(ctx context.Context, data *schema.ResourceData, cl return fmt.Errorf("error in update cluster response: %w", err) } - //if err == nil { - // log.Printf("======after updating in API the credentials are (%v), existing (%v)", *response.JSON200.CredentialsId, data.Get(FieldClusterCredentialsId)) - // err = data.Set(FieldClusterCredentialsId, *response.JSON200.CredentialsId) - // if err != nil { - // panic(err) // TODO - // } - //} + credentialsID = *response.JSON200.CredentialsId return nil }, b, func(err error, _ time.Duration) { // Only store non-context errors so we can surface the last "real" error to the user at the end @@ -278,5 +273,9 @@ func updateAKSClusterSettings(ctx context.Context, data *schema.ResourceData, cl return fmt.Errorf("updating cluster configuration: %w", err) } + // In case the update succeeded, we must update the state with the *generated* credentials_id before re-reading. + // This is because on update, the credentials_id always changes => read drift detection would see that and trigger infinite drift + err = data.Set(FieldClusterCredentialsId, credentialsID) + return nil }