Skip to content

Commit

Permalink
Added a call for restore logic
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Dec 30, 2024
1 parent 40fb6bf commit f008bbd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hack/runtime-migrator/cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
24 changes: 22 additions & 2 deletions hack/runtime-migrator/cmd/restore/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation"
"log/slog"
Expand All @@ -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)
Expand All @@ -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)
}
}
22 changes: 18 additions & 4 deletions hack/runtime-migrator/cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions hack/runtime-migrator/internal/backup/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions hack/runtime-migrator/internal/restore/output.go
Original file line number Diff line number Diff line change
@@ -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)
}
52 changes: 52 additions & 0 deletions hack/runtime-migrator/internal/restore/results.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f008bbd

Please sign in to comment.