From 1478cddfb2080bdac0b5a38c65cbef54ca967bc9 Mon Sep 17 00:00:00 2001 From: Zheng Xi Zhou Date: Mon, 27 Jun 2022 11:33:16 +0800 Subject: [PATCH] Fix: Terraform state is being overwritten each time (#326) * Fix: Terraform state is being overwritten each time When using Terraform component to create applications in different namespaces, Terraform state is being overwritten each time Fix https://github.com/kubevela/kubevela/issues/4215 Signed-off-by: Zheng Xi Zhou * fix UT Signed-off-by: Zheng Xi Zhou --- controllers/configuration/backend/backend.go | 2 +- .../configuration/backend/backend_test.go | 16 +++++----- .../configuration/backend/kubernetes.go | 11 ++----- .../configuration/configuration_test.go | 29 +++++++++---------- go.mod | 2 +- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/controllers/configuration/backend/backend.go b/controllers/configuration/backend/backend.go index 2812aee7..6c8b1258 100644 --- a/controllers/configuration/backend/backend.go +++ b/controllers/configuration/backend/backend.go @@ -104,7 +104,7 @@ func handleDefaultBackend(configuration *v1beta2.Configuration, k8sClient client InClusterConfig: true, } } - return newDefaultK8SBackend(configuration.Spec.Backend.SecretSuffix, k8sClient), nil + return newDefaultK8SBackend(configuration.Spec.Backend.SecretSuffix, k8sClient, configuration.Namespace), nil } func handleInlineBackendHCL(hclCode string) (string, interface{}, error) { diff --git a/controllers/configuration/backend/backend_test.go b/controllers/configuration/backend/backend_test.go index e5ea427e..444bb910 100644 --- a/controllers/configuration/backend/backend_test.go +++ b/controllers/configuration/backend/backend_test.go @@ -64,12 +64,12 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "" } } `, SecretSuffix: "", - SecretNS: "vela-system", + SecretNS: "", }, }, }, @@ -90,12 +90,12 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "" } } `, SecretSuffix: "", - SecretNS: "vela-system", + SecretNS: "", }, }, }, @@ -225,12 +225,12 @@ terraform { backend "kubernetes" { secret_suffix = "suffix" in_cluster_config = true - namespace = "vela-system" + namespace = "" } } `, SecretSuffix: "suffix", - SecretNS: "vela-system", + SecretNS: "", }, }, }, @@ -257,12 +257,12 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "a" } } `, SecretSuffix: "", - SecretNS: "vela-system", + SecretNS: "a", }, }, }, diff --git a/controllers/configuration/backend/kubernetes.go b/controllers/configuration/backend/kubernetes.go index 27091f6d..17505d95 100644 --- a/controllers/configuration/backend/kubernetes.go +++ b/controllers/configuration/backend/kubernetes.go @@ -49,16 +49,11 @@ type K8SBackend struct { SecretNS string } -func getDefaultK8sBackendSecretNS() string { +func newDefaultK8SBackend(suffix string, client client.Client, namespace string) *K8SBackend { ns := os.Getenv("TERRAFORM_BACKEND_NAMESPACE") if ns == "" { - ns = "vela-system" + ns = namespace } - return ns -} - -func newDefaultK8SBackend(suffix string, client client.Client) *K8SBackend { - ns := getDefaultK8sBackendSecretNS() hcl := renderK8SBackendHCL(suffix, ns) return &K8SBackend{ Client: client, @@ -77,7 +72,7 @@ func newK8SBackend(k8sClient client.Client, backendConf interface{}, _ map[strin if conf.Namespace != nil { ns = *conf.Namespace } else { - ns = getDefaultK8sBackendSecretNS() + ns = os.Getenv("TERRAFORM_BACKEND_NAMESPACE") } return &K8SBackend{ Client: k8sClient, diff --git a/controllers/configuration/configuration_test.go b/controllers/configuration/configuration_test.go index fbb0f0c0..f1734790 100644 --- a/controllers/configuration/configuration_test.go +++ b/controllers/configuration/configuration_test.go @@ -106,7 +106,6 @@ func TestValidConfigurationObject(t *testing.T) { func TestRenderConfiguration(t *testing.T) { type args struct { configuration *v1beta2.Configuration - ns string configurationType types.ConfigurationType credentials map[string]string } @@ -127,12 +126,14 @@ func TestRenderConfiguration(t *testing.T) { name: "backend is not nil, configuration is hcl", args: args{ configuration: &v1beta2.Configuration{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "n1", + }, Spec: v1beta2.ConfigurationSpec{ Backend: &v1beta2.Backend{}, HCL: "image_id=123", }, }, - ns: "vela-system", configurationType: types.ConfigurationHCL, }, want: want{ @@ -142,7 +143,7 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "n1" } } `, @@ -153,12 +154,12 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "n1" } } `, SecretSuffix: "", - SecretNS: "vela-system", + SecretNS: "n1", }, }, }, @@ -166,11 +167,13 @@ terraform { name: "backend is nil, configuration is remote", args: args{ configuration: &v1beta2.Configuration{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "n2", + }, Spec: v1beta2.ConfigurationSpec{ Remote: "https://github.com/a/b.git", }, }, - ns: "vela-system", configurationType: types.ConfigurationRemote, }, want: want{ @@ -179,7 +182,7 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "n2" } } `, @@ -190,12 +193,12 @@ terraform { backend "kubernetes" { secret_suffix = "" in_cluster_config = true - namespace = "vela-system" + namespace = "n2" } } `, SecretSuffix: "", - SecretNS: "vela-system", + SecretNS: "n2", }, }, }, @@ -205,7 +208,6 @@ terraform { configuration: &v1beta2.Configuration{ Spec: v1beta2.ConfigurationSpec{}, }, - ns: "vela-system", }, want: want{ errMsg: "Unsupported Configuration Type", @@ -220,13 +222,10 @@ terraform { t.Errorf("ValidConfigurationObject() error = %v, wantErr %v", err, tc.want.errMsg) return } - if got != tc.want.cfg { - t.Errorf("ValidConfigurationObject() = %v, want %v", got, tc.want.cfg) - return - } + assert.Equal(t, tc.want.cfg, got) if !reflect.DeepEqual(tc.want.backendInterface, backendConf) { - t.Errorf("ValidBackendSecretList() = %#v, want %#v", backendConf, tc.want.backendInterface) + t.Errorf("backendInterface is not equal.\n got %#v\n, want %#v", backendConf, tc.want.backendInterface) } }) } diff --git a/go.mod b/go.mod index 47fcefea..73c64d92 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/onsi/gomega v1.14.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.7.0 - github.com/zclconf/go-cty v1.10.0 golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd gopkg.in/yaml.v2 v2.4.0 gotest.tools v2.2.0+incompatible @@ -60,6 +59,7 @@ require ( github.com/prometheus/common v0.26.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/zclconf/go-cty v1.10.0 // indirect golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect