From f008bbdf02171089316de87356b47ab30d781022 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 12:39:28 +0100 Subject: [PATCH] Added a call for restore logic --- hack/runtime-migrator/cmd/backup/main.go | 2 +- hack/runtime-migrator/cmd/restore/main.go | 24 ++++++++- hack/runtime-migrator/cmd/restore/restore.go | 22 ++++++-- .../internal/backup/results.go | 9 ++-- .../internal/restore/output.go | 41 +++++++++++++++ .../internal/restore/results.go | 52 +++++++++++++++++++ 6 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 hack/runtime-migrator/internal/restore/output.go create mode 100644 hack/runtime-migrator/internal/restore/results.go diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index 24727982..ac230755 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -51,7 +51,7 @@ func main() { err = backup.Do(context.Background(), runtimeIds) if err != nil { - slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) + slog.Error("Failed to backup runtimes", slog.Any("error", err)) os.Exit(1) } } diff --git a/hack/runtime-migrator/cmd/restore/main.go b/hack/runtime-migrator/cmd/restore/main.go index d063617c..a7ba440c 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "log/slog" @@ -23,7 +24,7 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - _, err := initialisation.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := initialisation.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) @@ -35,9 +36,28 @@ func main() { os.Exit(1) } - _, err = initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + shootClient, err := initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) } + + restore, err := NewRestore(cfg, kubeconfigProvider, shootClient) + if err != nil { + slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) + os.Exit(1) + } + + slog.Info("Reading runtimeIds from input file") + runtimeIds, err := initialisation.GetRuntimeIDsFromInputFile(cfg.Config) + if err != nil { + slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) + os.Exit(1) + } + + err = restore.Do(context.Background(), runtimeIds) + if err != nil { + slog.Error("Failed to restore runtimes", slog.Any("error", err)) + os.Exit(1) + } } diff --git a/hack/runtime-migrator/cmd/restore/restore.go b/hack/runtime-migrator/cmd/restore/restore.go index e1871ef4..5f134009 100644 --- a/hack/runtime-migrator/cmd/restore/restore.go +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -4,7 +4,6 @@ import ( "context" "fmt" gardener_types "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/backup" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/restore" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" @@ -22,9 +21,24 @@ const ( type Restore struct { shootClient gardener_types.ShootInterface kubeconfigProvider kubeconfig.Provider - outputWriter backup.OutputWriter - results backup.Results - cfg initialisation.Config + outputWriter restore.OutputWriter + results restore.Results + cfg initialisation.RestoreConfig +} + +func NewRestore(cfg initialisation.RestoreConfig, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Restore, error) { + outputWriter, err := restore.NewOutputWriter(cfg.OutputPath) + if err != nil { + return Restore{}, err + } + + return Restore{ + shootClient: shootClient, + kubeconfigProvider: kubeconfigProvider, + outputWriter: outputWriter, + results: restore.NewRestoreResults(outputWriter.NewResultsDir), + cfg: cfg, + }, err } func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { diff --git a/hack/runtime-migrator/internal/backup/results.go b/hack/runtime-migrator/internal/backup/results.go index be966c76..46489c27 100644 --- a/hack/runtime-migrator/internal/backup/results.go +++ b/hack/runtime-migrator/internal/backup/results.go @@ -20,11 +20,10 @@ type RuntimeResult struct { } type Results struct { - Results []RuntimeResult - Succeeded int - Failed int - DifferenceDetected int - OutputDirectory string + Results []RuntimeResult + Succeeded int + Failed int + OutputDirectory string } func NewBackupResults(outputDirectory string) Results { diff --git a/hack/runtime-migrator/internal/restore/output.go b/hack/runtime-migrator/internal/restore/output.go new file mode 100644 index 00000000..93bdf0a1 --- /dev/null +++ b/hack/runtime-migrator/internal/restore/output.go @@ -0,0 +1,41 @@ +package restore + +import ( + "encoding/json" + "fmt" + "os" + "path" + "time" +) + +type OutputWriter struct { + NewResultsDir string +} + +func NewOutputWriter(outputDir string) (OutputWriter, error) { + newResultsDir := path.Join(outputDir, fmt.Sprintf("backup-%s", time.Now().Format(time.RFC3339))) + + err := os.MkdirAll(newResultsDir, os.ModePerm) + if err != nil { + return OutputWriter{}, fmt.Errorf("failed to create results directory: %v", err) + } + + return OutputWriter{ + NewResultsDir: newResultsDir, + }, nil +} + +func (ow OutputWriter) SaveRestoreResults(results Results) (string, error) { + resultFile, err := json.Marshal(results.Results) + if err != nil { + return "", err + } + + fileName := fmt.Sprintf("%s/backup-results.json", ow.NewResultsDir) + return fileName, writeFile(fileName, resultFile) +} + +func writeFile(filePath string, content []byte) error { + const writePermissions = 0644 + return os.WriteFile(filePath, content, writePermissions) +} diff --git a/hack/runtime-migrator/internal/restore/results.go b/hack/runtime-migrator/internal/restore/results.go new file mode 100644 index 00000000..347fa569 --- /dev/null +++ b/hack/runtime-migrator/internal/restore/results.go @@ -0,0 +1,52 @@ +package restore + +type StatusType string + +const ( + StatusSuccess StatusType = "Success" + StatusError StatusType = "Error" +) + +type RuntimeResult struct { + RuntimeID string `json:"runtimeId"` + ShootName string `json:"shootName"` + Status StatusType `json:"status"` + ErrorMessage string `json:"errorMessage,omitempty"` +} + +type Results struct { + Results []RuntimeResult + Succeeded int + Failed int + OutputDirectory string +} + +func NewRestoreResults(outputDirectory string) Results { + return Results{ + Results: make([]RuntimeResult, 0), + OutputDirectory: outputDirectory, + } +} + +func (br *Results) ErrorOccurred(runtimeID, shootName string, errorMsg string) { + result := RuntimeResult{ + RuntimeID: runtimeID, + ShootName: shootName, + Status: StatusError, + ErrorMessage: errorMsg, + } + + br.Failed++ + br.Results = append(br.Results, result) +} + +func (br *Results) OperationSucceeded(runtimeID string, shootName string) { + result := RuntimeResult{ + RuntimeID: runtimeID, + ShootName: shootName, + Status: StatusSuccess, + } + + br.Succeeded++ + br.Results = append(br.Results, result) +}