Skip to content

Commit

Permalink
Added code for storing backup results
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Dec 18, 2024
1 parent 79c8bf0 commit 2880523
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 11 deletions.
16 changes: 8 additions & 8 deletions hack/runtime-migrator/cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/backup"
"github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config"
"github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"log/slog"
"time"
)

Expand Down Expand Up @@ -54,20 +54,20 @@ func (b Backup) Do(ctx context.Context, runtimeIDs []string) error {
for _, runtimeID := range runtimeIDs {
shoot, err := b.fetchShoot(ctx, shootList, runtimeID)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to fetch shoot for runtimeID=%s", runtimeID))
}

if shoot == nil {
slog.Error(fmt.Sprintf("Failed to fetch runtime: %v", err), "runtimeID", runtimeID)
continue
}

runtimeBackup, err := backuper.Do(ctx, *shoot)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to backup runtimeID=%s", runtimeID))
slog.Error(fmt.Sprintf("Failed to backup runtime: %v", err), "runtimeID", runtimeID)
continue
}

if err := b.outputWriter.Save(runtimeBackup); err != nil {
return err
if !b.cfg.IsDryRun {
if err := b.outputWriter.Save(runtimeBackup); err != nil {
slog.Error(fmt.Sprintf("Failed to store backup: %v", err), "runtimeID", runtimeID)
}
}
}

Expand Down
61 changes: 58 additions & 3 deletions hack/runtime-migrator/internal/backup/output.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package backup

import (
"encoding/json"
"fmt"
"os"
"path"
"sigs.k8s.io/yaml"
"time"
)

type OutputWriter struct {
NewResultsDir string
BackupDir string
Expand All @@ -10,9 +19,55 @@ const (
)

func NewOutputWriter(outputDir string) (OutputWriter, error) {
return OutputWriter{}, nil
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)
}

backupDir := path.Join(newResultsDir, backupFolderName)

err = os.MkdirAll(backupDir, os.ModePerm)
if err != nil {
return OutputWriter{}, fmt.Errorf("failed to create backup directory: %v", err)
}

return OutputWriter{
NewResultsDir: newResultsDir,
BackupDir: backupDir,
}, nil
}

func (ow OutputWriter) Save(runtimeID string, runtimeBackup RuntimeBackup) error {
err := os.MkdirAll(fmt.Sprintf("%s/%s", ow.BackupDir, runtimeID), os.ModePerm)
if err != nil {
return err
}

return saveYaml(runtimeBackup.Shoot, fmt.Sprintf("%s/%s/%s.yaml", ow.BackupDir, runtimeID, runtimeBackup.Shoot.Name))
}

func (ow OutputWriter) SaveBackupResults(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 saveYaml[T any](object T, path string) error {
yamlBytes, err := yaml.Marshal(object)
if err != nil {
return err
}

return writeFile(path, yamlBytes)
}

func (ow OutputWriter) Save(runtimeBackup RuntimeBackup) error {
return nil
func writeFile(filePath string, content []byte) error {
const writePermissions = 0644
return os.WriteFile(filePath, content, writePermissions)
}
63 changes: 63 additions & 0 deletions hack/runtime-migrator/internal/backup/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package backup

import (
"fmt"
)

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"`
BackupDirPath string `json:"backupDirPath,omitempty"`
}

type Results struct {
Results []RuntimeResult
Succeeded int
Failed int
DifferenceDetected int
OutputDirectory string
}

func NewBackupResults(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,
BackupDirPath: br.getBackupDirPath(runtimeID),
}

br.Succeeded++
br.Results = append(br.Results, result)
}

func (br *Results) getBackupDirPath(runtimeID string) string {
return fmt.Sprintf("%s/%s/%s.yaml", br.OutputDirectory, backupFolderName, runtimeID)
}

0 comments on commit 2880523

Please sign in to comment.