From 158257823e029327d56b6d8272d4b1b819b12742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=B0=B1=E4=B8=9A?= Date: Thu, 17 Aug 2023 19:37:18 +0800 Subject: [PATCH] fix: Due to an error in SetRegion during the destruction process, a non-specified backend was used (#372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴就业 --- controllers/configuration/configuration.go | 4 +++ .../configuration/configuration_test.go | 25 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/controllers/configuration/configuration.go b/controllers/configuration/configuration.go index 4d2c1529..b3443642 100644 --- a/controllers/configuration/configuration.go +++ b/controllers/configuration/configuration.go @@ -61,6 +61,10 @@ func SetRegion(ctx context.Context, k8sClient client.Client, namespace, name str return configuration.Spec.Region, nil } + if !configuration.DeletionTimestamp.IsZero() { + return providerObj.Spec.Region, nil + } + configuration.Spec.Region = providerObj.Spec.Region return providerObj.Spec.Region, Update(ctx, k8sClient, &configuration) } diff --git a/controllers/configuration/configuration_test.go b/controllers/configuration/configuration_test.go index 0f30a933..82ed4607 100644 --- a/controllers/configuration/configuration_test.go +++ b/controllers/configuration/configuration_test.go @@ -2,14 +2,13 @@ package configuration import ( "context" - "strings" - "testing" - "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" + "strings" + "testing" "github.com/oam-dev/terraform-controller/api/types" crossplane "github.com/oam-dev/terraform-controller/api/types/crossplane-runtime" @@ -334,6 +333,17 @@ func TestSetRegion(t *testing.T) { } assert.Nil(t, k8sClient.Create(ctx, &configuration2)) + deletionTime := metav1.Now() + configuration3 := v1beta2.Configuration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "del", + Namespace: "default", + DeletionTimestamp: &deletionTime, + }, + Spec: v1beta2.ConfigurationSpec{}, + } + assert.Nil(t, k8sClient.Create(ctx, &configuration3)) + provider := &v1beta1.Provider{ Spec: v1beta1.ProviderSpec{ Region: "yyy", @@ -381,6 +391,15 @@ func TestSetRegion(t *testing.T) { errMsg: "failed to get configuration", }, }, + "configuration has been deleted": { + args: args{ + namespace: "default", + name: "del", + }, + want: want{ + region: "yyy", + }, + }, } for name, tc := range testcases { t.Run(name, func(t *testing.T) {