Skip to content

Commit

Permalink
Fix: Terraform state is being overwritten each time (#326)
Browse files Browse the repository at this point in the history
* 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 kubevela/kubevela#4215

Signed-off-by: Zheng Xi Zhou <[email protected]>

* fix UT

Signed-off-by: Zheng Xi Zhou <[email protected]>
  • Loading branch information
zzxwill authored Jun 27, 2022
1 parent 92c9496 commit 1478cdd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion controllers/configuration/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions controllers/configuration/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = ""
}
}
`,
SecretSuffix: "",
SecretNS: "vela-system",
SecretNS: "",
},
},
},
Expand All @@ -90,12 +90,12 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = ""
}
}
`,
SecretSuffix: "",
SecretNS: "vela-system",
SecretNS: "",
},
},
},
Expand Down Expand Up @@ -225,12 +225,12 @@ terraform {
backend "kubernetes" {
secret_suffix = "suffix"
in_cluster_config = true
namespace = "vela-system"
namespace = ""
}
}
`,
SecretSuffix: "suffix",
SecretNS: "vela-system",
SecretNS: "",
},
},
},
Expand All @@ -257,12 +257,12 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = "a"
}
}
`,
SecretSuffix: "",
SecretNS: "vela-system",
SecretNS: "a",
},
},
},
Expand Down
11 changes: 3 additions & 8 deletions controllers/configuration/backend/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
29 changes: 14 additions & 15 deletions controllers/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{
Expand All @@ -142,7 +143,7 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = "n1"
}
}
`,
Expand All @@ -153,24 +154,26 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = "n1"
}
}
`,
SecretSuffix: "",
SecretNS: "vela-system",
SecretNS: "n1",
},
},
},
{
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{
Expand All @@ -179,7 +182,7 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = "n2"
}
}
`,
Expand All @@ -190,12 +193,12 @@ terraform {
backend "kubernetes" {
secret_suffix = ""
in_cluster_config = true
namespace = "vela-system"
namespace = "n2"
}
}
`,
SecretSuffix: "",
SecretNS: "vela-system",
SecretNS: "n2",
},
},
},
Expand All @@ -205,7 +208,6 @@ terraform {
configuration: &v1beta2.Configuration{
Spec: v1beta2.ConfigurationSpec{},
},
ns: "vela-system",
},
want: want{
errMsg: "Unsupported Configuration Type",
Expand All @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1478cdd

Please sign in to comment.