Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Jan 8, 2025
1 parent 8711b87 commit f475a34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions hack/runtime-migrator/cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error {
return err
}

restorer := restore.NewRestorer(r.cfg.BackupDir, r.cfg.RestoreCRB, r.cfg.RestoreOIDC)
restorer := restore.NewBackupReader(r.cfg.BackupDir, r.cfg.RestoreCRB, r.cfg.RestoreOIDC)

for _, runtimeID := range runtimeIDs {
currentShoot, err := shoot.Fetch(ctx, shootList, r.shootClient, runtimeID)
Expand All @@ -82,7 +82,7 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error {

objectsToRestore, err := restorer.Do(runtimeID, currentShoot.Name)
if err != nil {
errMsg := fmt.Sprintf("Failed to restore runtime: %v", err)
errMsg := fmt.Sprintf("Failed to read runtime from backup directory: %v", err)
r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg)
slog.Error(errMsg, "runtimeID", runtimeID)

Expand All @@ -105,7 +105,7 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error {

appliedCRBs, appliedOIDC, err := r.applyResources(ctx, objectsToRestore, runtimeID)
if err != nil {
errMsg := fmt.Sprintf("Failed to restore runtime: %v", err)
errMsg := fmt.Sprintf("Failed to apply resources: %v", err)
r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg)
slog.Error(errMsg, "runtimeID", runtimeID)

Expand All @@ -128,7 +128,7 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error {
}

func (r Restore) applyResources(ctx context.Context, objectsToRestore backup.RuntimeBackup, runtimeID string) ([]v12.ClusterRoleBinding, []authenticationv1alpha1.OpenIDConnect, error) {
err := r.applyShoot(ctx, objectsToRestore.ShootToRestore)
err := r.applyShoot(ctx, objectsToRestore.ShootForPatch)
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions hack/runtime-migrator/internal/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewBackuper(isDryRun bool, kcpClient client.Client, timeoutK8sOperation tim

type RuntimeBackup struct {
OriginalShoot v1beta1.Shoot
ShootToRestore v1beta1.Shoot
ShootForPatch v1beta1.Shoot
ClusterRoleBindings []rbacv1.ClusterRoleBinding
OIDCConfig []authenticationv1alpha1.OpenIDConnect
}
Expand All @@ -54,14 +54,14 @@ func (b Backuper) Do(ctx context.Context, shoot v1beta1.Shoot, runtimeID string)
}

return RuntimeBackup{
ShootToRestore: b.getShootToRestore(shoot),
ShootForPatch: b.getShootForPatch(shoot),
OriginalShoot: shoot,
ClusterRoleBindings: crbs,
OIDCConfig: oidcConfig,
}, nil
}

func (b Backuper) getShootToRestore(shootFromGardener v1beta1.Shoot) v1beta1.Shoot {
func (b Backuper) getShootForPatch(shootFromGardener v1beta1.Shoot) v1beta1.Shoot {
return v1beta1.Shoot{
TypeMeta: v1.TypeMeta{
Kind: "Shoot",
Expand Down
2 changes: 1 addition & 1 deletion hack/runtime-migrator/internal/backup/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (ow OutputWriter) Save(runtimeID string, runtimeBackup RuntimeBackup) error
return err
}

err = saveYaml(runtimeBackup.ShootToRestore, fmt.Sprintf("%s/%s-to-restore.yaml", runtimeDir, runtimeBackup.ShootToRestore.Name))
err = saveYaml(runtimeBackup.ShootForPatch, fmt.Sprintf("%s/%s-to-restore.yaml", runtimeDir, runtimeBackup.ShootForPatch.Name))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import (
"sigs.k8s.io/yaml"
)

type Restorer struct {
type BackupReader struct {
backupDir string
restoreCRB bool
restoreOIDC bool
}

func NewRestorer(backupDir string, restoreCRB, restoreOIDC bool) Restorer {
return Restorer{
func NewBackupReader(backupDir string, restoreCRB, restoreOIDC bool) BackupReader {
return BackupReader{
backupDir: backupDir,
restoreCRB: restoreCRB,
restoreOIDC: restoreOIDC,
}
}

func (r Restorer) Do(runtimeID string, shootName string) (backup.RuntimeBackup, error) {
shootToRestore, err := r.getShootToRestore(runtimeID, fmt.Sprintf("%s-to-restore", shootName))
func (r BackupReader) Do(runtimeID string, shootName string) (backup.RuntimeBackup, error) {
shootForPatch, err := r.getShoot(runtimeID, fmt.Sprintf("%s-to-restore", shootName))
if err != nil {
return backup.RuntimeBackup{}, err
}

originalShoot, err := r.getShootToRestore(runtimeID, fmt.Sprintf("%s-original", shootName))
originalShoot, err := r.getShoot(runtimeID, fmt.Sprintf("%s-original", shootName))
if err != nil {
return backup.RuntimeBackup{}, err
}
Expand Down Expand Up @@ -67,14 +67,14 @@ func (r Restorer) Do(runtimeID string, shootName string) (backup.RuntimeBackup,
}

return backup.RuntimeBackup{
ShootToRestore: shootToRestore,
ShootForPatch: shootForPatch,
OriginalShoot: originalShoot,
ClusterRoleBindings: crbs,
OIDCConfig: oidcConfig,
}, nil
}

func (r Restorer) getShootToRestore(runtimeID string, shootName string) (v1beta1.Shoot, error) {
func (r BackupReader) getShoot(runtimeID string, shootName string) (v1beta1.Shoot, error) {
shootFilePath := path.Join(r.backupDir, fmt.Sprintf("backup/%s/%s.yaml", runtimeID, shootName))

shoot, err := readFromFile[v1beta1.Shoot](shootFilePath)
Expand Down

0 comments on commit f475a34

Please sign in to comment.