From 2881dbdb3593aa4f6ac4213c67ced9bf3afc9852 Mon Sep 17 00:00:00 2001 From: loheagn Date: Fri, 5 Aug 2022 09:50:36 +0800 Subject: [PATCH 1/4] Fix some bugs of `backup_restore` tool Signed-off-by: loheagn --- hack/tool/backup_restore/cmd/backup.go | 12 ++++-- hack/tool/backup_restore/cmd/restore.go | 37 ++++++++++++++----- .../internal/app/configuration.go | 5 +++ hack/tool/backup_restore/internal/app/env.go | 22 +++++++++++ 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/hack/tool/backup_restore/cmd/backup.go b/hack/tool/backup_restore/cmd/backup.go index b034fa03..03ca7b21 100644 --- a/hack/tool/backup_restore/cmd/backup.go +++ b/hack/tool/backup_restore/cmd/backup.go @@ -39,11 +39,15 @@ var ( func newBackupCmd(kubeFlags *genericclioptions.ConfigFlags) *cobra.Command { backupCmd := &cobra.Command{ Use: "backup", - PreRunE: func(cmd *cobra.Command, args []string) error { - return app.BuildK8SClient(kubeFlags) + PreRun: func(cmd *cobra.Command, args []string) { + if err := app.BuildK8SClient(kubeFlags); err != nil { + log.Fatal(err) + } }, - RunE: func(cmd *cobra.Command, args []string) error { - return backup(context.Background()) + Run: func(cmd *cobra.Command, args []string) { + if err := backup(context.Background()); err != nil { + log.Fatal(err) + } }, } backupCmd.Flags().StringArrayVar(&configurationNameList, "configuration", []string{}, "the name of the configurations which need to be backed up") diff --git a/hack/tool/backup_restore/cmd/restore.go b/hack/tool/backup_restore/cmd/restore.go index 9df4e496..23f86293 100644 --- a/hack/tool/backup_restore/cmd/restore.go +++ b/hack/tool/backup_restore/cmd/restore.go @@ -38,14 +38,10 @@ var ( func newRestoreCmd(kubeFlags *genericclioptions.ConfigFlags) *cobra.Command { restoreCmd := &cobra.Command{ Use: "restore", - PreRunE: func(cmd *cobra.Command, args []string) error { - err := app.BuildK8SClient(kubeFlags) - if err != nil { - return err - } + PreRun: func(cmd *cobra.Command, args []string) { pwd, err := os.Getwd() if err != nil { - return err + log.Fatal(err) } if stateJSONPath == "" { log.Fatal("`--state` should not be empty") @@ -65,10 +61,15 @@ func newRestoreCmd(kubeFlags *genericclioptions.ConfigFlags) *cobra.Command { log.Print("WARN: `--component` is empty. Will take the first component of the Application as the cloud resource which should be restored") } } - return nil + if err := app.BuildK8SClient(kubeFlags); err != nil { + log.Fatal(err) + } + presetTFBackendNS() }, - RunE: func(cmd *cobra.Command, args []string) error { - return restore(context.Background()) + Run: func(cmd *cobra.Command, args []string) { + if err := restore(context.Background()); err != nil { + log.Fatal(err) + } }, } restoreCmd.Flags().StringVar(&stateJSONPath, "state", "state.json", "the path of the backed up Terraform state file") @@ -109,3 +110,21 @@ func restore(ctx context.Context) error { return app.WaitConfiguration(ctx, resourceOwner.GetConfigurationNamespacedName()) } + +// presetTFBackendNS try to set the "TERRAFORM_BACKEND_NAMESPACE" environment variable +func presetTFBackendNS() { + backendNS := os.Getenv(app.TFBackendNS) + if backendNS != "" { + goto end + } + + // if user don't set the "TERRAFORM_BACKEND_NAMESPACE" environment variable, + // we try to fetch the environment variable from the terraform-controller deployment + // and set it in the local environment to make sure the consistency + backendNS = app.GetTFBackendNSFromDeployment() + if backendNS != "" { + _ = os.Setenv(app.TFBackendNS, backendNS) + } +end: + log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS) +} diff --git a/hack/tool/backup_restore/internal/app/configuration.go b/hack/tool/backup_restore/internal/app/configuration.go index a16e9e8e..365448ef 100644 --- a/hack/tool/backup_restore/internal/app/configuration.go +++ b/hack/tool/backup_restore/internal/app/configuration.go @@ -113,6 +113,11 @@ func WaitConfiguration(ctx context.Context, namespacedName *crossplane.Reference gotConf := &v1beta2.Configuration{} for { if err := K8SClient.Get(ctx, client.ObjectKey{Name: namespacedName.Name, Namespace: namespacedName.Namespace}, gotConf); err != nil { + if kerrors.IsNotFound(err) { + log.Printf("can not find the configuration({Name: %s, Namespace: %s}), waiting......", namespacedName.Name, namespacedName.Namespace) + time.Sleep(500 * time.Millisecond) + continue + } errCh <- err return } diff --git a/hack/tool/backup_restore/internal/app/env.go b/hack/tool/backup_restore/internal/app/env.go index 49634fd7..1cbf7a85 100644 --- a/hack/tool/backup_restore/internal/app/env.go +++ b/hack/tool/backup_restore/internal/app/env.go @@ -17,10 +17,17 @@ limitations under the License. package app import ( + "context" + "log" "os" "strings" + + appsv1 "k8s.io/api/apps/v1" + "sigs.k8s.io/controller-runtime/pkg/client" ) +const TFBackendNS = "TERRAFORM_BACKEND_NAMESPACE" + func GetAllENVs() map[string]string { envs := make(map[string]string) for _, envStr := range os.Environ() { @@ -29,3 +36,18 @@ func GetAllENVs() map[string]string { } return envs } + +func GetTFBackendNSFromDeployment() string { + deployment := appsv1.Deployment{} + if err := K8SClient.Get(context.Background(), client.ObjectKey{Name: "terraform-controller", Namespace: "vela-system"}, &deployment); err != nil { + log.Printf("WARN: get terraform-controller deployment in the vela-system namesapce failed, %v", err) + return "" + } + envs := deployment.Spec.Template.Spec.Containers[0].Env + for _, env := range envs { + if env.Name == "TERRAFORM_BACKEND_NAMESPACE" { + return env.Value + } + } + return "" +} From 7b0e38653d9622d80d35ae19b7f00bb38e62f252 Mon Sep 17 00:00:00 2001 From: loheagn Date: Tue, 9 Aug 2022 11:25:18 +0800 Subject: [PATCH 2/4] remove `goto` Signed-off-by: loheagn --- hack/tool/backup_restore/cmd/restore.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hack/tool/backup_restore/cmd/restore.go b/hack/tool/backup_restore/cmd/restore.go index 23f86293..d2d29d7d 100644 --- a/hack/tool/backup_restore/cmd/restore.go +++ b/hack/tool/backup_restore/cmd/restore.go @@ -115,16 +115,15 @@ func restore(ctx context.Context) error { func presetTFBackendNS() { backendNS := os.Getenv(app.TFBackendNS) if backendNS != "" { - goto end + log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS) } - // if user don't set the "TERRAFORM_BACKEND_NAMESPACE" environment variable, + // if user doesn't set the "TERRAFORM_BACKEND_NAMESPACE" environment variable, // we try to fetch the environment variable from the terraform-controller deployment // and set it in the local environment to make sure the consistency backendNS = app.GetTFBackendNSFromDeployment() if backendNS != "" { _ = os.Setenv(app.TFBackendNS, backendNS) } -end: log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS) } From d1976ed252c2440a0ae76eca67251b44c78e10fc Mon Sep 17 00:00:00 2001 From: loheagn Date: Tue, 9 Aug 2022 11:26:26 +0800 Subject: [PATCH 3/4] check the container name when try to fetch the `TERRAFORM_BACKEND_NAMESPACE` env from terraform-controller deployment Signed-off-by: loheagn --- hack/tool/backup_restore/internal/app/env.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/hack/tool/backup_restore/internal/app/env.go b/hack/tool/backup_restore/internal/app/env.go index 1cbf7a85..9ddf689b 100644 --- a/hack/tool/backup_restore/internal/app/env.go +++ b/hack/tool/backup_restore/internal/app/env.go @@ -23,6 +23,7 @@ import ( "strings" appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -43,8 +44,19 @@ func GetTFBackendNSFromDeployment() string { log.Printf("WARN: get terraform-controller deployment in the vela-system namesapce failed, %v", err) return "" } - envs := deployment.Spec.Template.Spec.Containers[0].Env - for _, env := range envs { + var tfContainer *corev1.Container + containers := deployment.Spec.Template.Spec.Containers + for _, container := range containers { + if container.Name == "terraform-controller" { + tfContainer = &container + break + } + } + if tfContainer == nil { + log.Println("WARN: terraform-controller container not found in the terraform-controller deployment") + return "" + } + for _, env := range tfContainer.Env { if env.Name == "TERRAFORM_BACKEND_NAMESPACE" { return env.Value } From 5d81c7b4095e9b5ddfa0bc5d07ef81a7c54eb894 Mon Sep 17 00:00:00 2001 From: loheagn Date: Tue, 9 Aug 2022 11:33:04 +0800 Subject: [PATCH 4/4] fix for "remove `goto`" Signed-off-by: loheagn --- hack/tool/backup_restore/cmd/restore.go | 1 + 1 file changed, 1 insertion(+) diff --git a/hack/tool/backup_restore/cmd/restore.go b/hack/tool/backup_restore/cmd/restore.go index d2d29d7d..e50a7ea6 100644 --- a/hack/tool/backup_restore/cmd/restore.go +++ b/hack/tool/backup_restore/cmd/restore.go @@ -116,6 +116,7 @@ func presetTFBackendNS() { backendNS := os.Getenv(app.TFBackendNS) if backendNS != "" { log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS) + return } // if user doesn't set the "TERRAFORM_BACKEND_NAMESPACE" environment variable,