Skip to content

Commit

Permalink
Merge pull request #333 from loheagn/backup_restore_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chivalryq committed Aug 9, 2022
2 parents 337610e + 5d81c7b commit 8620dbb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
12 changes: 8 additions & 4 deletions hack/tool/backup_restore/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 28 additions & 9 deletions hack/tool/backup_restore/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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 != "" {
log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS)
return
}

// 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)
}
log.Printf("use the `TERRAFORM_BACKEND_NAMESPACE` environment variable: %s", backendNS)
}
5 changes: 5 additions & 0 deletions hack/tool/backup_restore/internal/app/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 34 additions & 0 deletions hack/tool/backup_restore/internal/app/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ limitations under the License.
package app

import (
"context"
"log"
"os"
"strings"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/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() {
Expand All @@ -29,3 +37,29 @@ 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 ""
}
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
}
}
return ""
}

0 comments on commit 8620dbb

Please sign in to comment.