From dc11e8ebe3ec3d5610feeaca2409b6c310c4fb4a Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Fri, 27 Dec 2024 16:35:23 +0100 Subject: [PATCH 01/11] Refactor --- hack/runtime-migrator/cmd/backup/backup.go | 6 +++--- hack/runtime-migrator/cmd/backup/main.go | 10 +++++----- hack/runtime-migrator/cmd/migration/main.go | 12 ++++++------ hack/runtime-migrator/cmd/migration/migration.go | 2 +- hack/runtime-migrator/cmd/restore/main.go | 13 +++++-------- hack/runtime-migrator/internal/backup/backup.go | 4 ++-- .../internal/{config => input}/client.go | 2 +- .../{config/input.go => input/migrationparams.go} | 2 +- .../runtime-migrator/internal/migration/migrator.go | 2 +- hack/runtime-migrator/internal/shoot/util.go | 4 ++-- 10 files changed, 27 insertions(+), 30 deletions(-) rename hack/runtime-migrator/internal/{config => input}/client.go (99%) rename hack/runtime-migrator/internal/{config/input.go => input/migrationparams.go} (99%) diff --git a/hack/runtime-migrator/cmd/backup/backup.go b/hack/runtime-migrator/cmd/backup/backup.go index a881a13f..bab3eaa5 100644 --- a/hack/runtime-migrator/cmd/backup/backup.go +++ b/hack/runtime-migrator/cmd/backup/backup.go @@ -5,7 +5,7 @@ import ( "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/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -23,10 +23,10 @@ type Backup struct { kubeconfigProvider kubeconfig.Provider outputWriter backup.OutputWriter results backup.Results - cfg config.Config + cfg input.Config } -func NewBackup(cfg config.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { +func NewBackup(cfg input.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { outputWriter, err := backup.NewOutputWriter(cfg.OutputPath) if err != nil { return Backup{}, err diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index 58cb44bf..533cae47 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -3,7 +3,7 @@ package main import ( "context" "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -12,7 +12,7 @@ import ( func main() { slog.Info("Starting runtime-backuper") - cfg := config.NewConfig() + cfg := input.NewConfig() opts := zap.Options{ Development: true, @@ -22,13 +22,13 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := config.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - shootClient, err := config.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + shootClient, err := input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -41,7 +41,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := config.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := input.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/main.go b/hack/runtime-migrator/cmd/migration/main.go index 27539f06..64f44c9a 100644 --- a/hack/runtime-migrator/cmd/migration/main.go +++ b/hack/runtime-migrator/cmd/migration/main.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" kimConfig "github.com/kyma-project/infrastructure-manager/pkg/config" v12 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -27,7 +27,7 @@ const ( func main() { slog.Info("Starting runtime-migrator") - cfg := config.NewConfig() + cfg := input.NewConfig() opts := zap.Options{ Development: true, @@ -37,19 +37,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := config.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - kcpClient, err := config.CreateKcpClient(&cfg) + kcpClient, err := input.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - gardenerShootClient, err := config.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + gardenerShootClient, err := input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -75,7 +75,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := config.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := input.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/migration.go b/hack/runtime-migrator/cmd/migration/migration.go index 3107f49b..ba6b0b89 100644 --- a/hack/runtime-migrator/cmd/migration/migration.go +++ b/hack/runtime-migrator/cmd/migration/migration.go @@ -8,7 +8,7 @@ import ( gardener_types "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" runtimev1 "github.com/kyma-project/infrastructure-manager/api/v1" - config2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + config2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/migration" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" diff --git a/hack/runtime-migrator/cmd/restore/main.go b/hack/runtime-migrator/cmd/restore/main.go index 8826e225..88c1a6d1 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -2,19 +2,16 @@ package main import ( "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "time" ) -const expirationTime = 60 * time.Minute - func main() { slog.Info("Starting runtime-restorer") - cfg := config.NewConfig() + cfg := input.NewConfig() opts := zap.Options{ Development: true, @@ -24,19 +21,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - _, err := config.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + _, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - _, err = config.CreateKcpClient(&cfg) + _, err = input.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - _, err = config.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + _, err = input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/internal/backup/backup.go b/hack/runtime-migrator/internal/backup/backup.go index 805c9160..505769bf 100644 --- a/hack/runtime-migrator/internal/backup/backup.go +++ b/hack/runtime-migrator/internal/backup/backup.go @@ -4,14 +4,14 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" authenticationv1alpha1 "github.com/gardener/oidc-webhook-authenticator/apis/authentication/v1alpha1" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" rbacv1 "k8s.io/api/rbac/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type Backuper struct { - cfg config.Config + cfg input.Config isDryRun bool kubeconfigProvider kubeconfig.Provider } diff --git a/hack/runtime-migrator/internal/config/client.go b/hack/runtime-migrator/internal/input/client.go similarity index 99% rename from hack/runtime-migrator/internal/config/client.go rename to hack/runtime-migrator/internal/input/client.go index 948251c7..98236649 100644 --- a/hack/runtime-migrator/internal/config/client.go +++ b/hack/runtime-migrator/internal/input/client.go @@ -1,4 +1,4 @@ -package config +package input import ( "fmt" diff --git a/hack/runtime-migrator/internal/config/input.go b/hack/runtime-migrator/internal/input/migrationparams.go similarity index 99% rename from hack/runtime-migrator/internal/config/input.go rename to hack/runtime-migrator/internal/input/migrationparams.go index 9b6181f0..ea3a7c0d 100644 --- a/hack/runtime-migrator/internal/config/input.go +++ b/hack/runtime-migrator/internal/input/migrationparams.go @@ -1,4 +1,4 @@ -package config +package input import ( "bufio" diff --git a/hack/runtime-migrator/internal/migration/migrator.go b/hack/runtime-migrator/internal/migration/migrator.go index c457c76b..221693fb 100644 --- a/hack/runtime-migrator/internal/migration/migrator.go +++ b/hack/runtime-migrator/internal/migration/migrator.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/gardener/gardener/pkg/apis/core/v1beta1" v1 "github.com/kyma-project/infrastructure-manager/api/v1" - migrator "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/config" + migrator "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" "github.com/pkg/errors" diff --git a/hack/runtime-migrator/internal/shoot/util.go b/hack/runtime-migrator/internal/shoot/util.go index ed736c9d..291a6f93 100644 --- a/hack/runtime-migrator/internal/shoot/util.go +++ b/hack/runtime-migrator/internal/shoot/util.go @@ -4,7 +4,7 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" 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/config" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -18,7 +18,7 @@ func Fetch(ctx context.Context, shootList *v1beta1.ShootList, shootClient garden return nil, errors.New("shoot was deleted or the runtimeID is incorrect") } - getCtx, cancel := context.WithTimeout(ctx, config.TimeoutK8sOperation) + getCtx, cancel := context.WithTimeout(ctx, input.TimeoutK8sOperation) defer cancel() // We are fetching the shoot from the gardener to make sure the runtime didn't get deleted during the migration process From b57e3993a0bae8c54fe29c03f86358c7703cfc82 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Fri, 27 Dec 2024 16:44:14 +0100 Subject: [PATCH 02/11] Refactor --- hack/runtime-migrator/cmd/backup/backup.go | 6 +++--- hack/runtime-migrator/cmd/backup/main.go | 10 +++++----- hack/runtime-migrator/cmd/migration/main.go | 12 ++++++------ hack/runtime-migrator/cmd/migration/migration.go | 2 +- hack/runtime-migrator/cmd/restore/main.go | 10 +++++----- hack/runtime-migrator/internal/backup/backup.go | 4 ++-- .../internal/{input => init}/client.go | 2 +- .../internal/{input => init}/migrationparams.go | 2 +- hack/runtime-migrator/internal/migration/migrator.go | 2 +- hack/runtime-migrator/internal/shoot/util.go | 4 ++-- 10 files changed, 27 insertions(+), 27 deletions(-) rename hack/runtime-migrator/internal/{input => init}/client.go (99%) rename hack/runtime-migrator/internal/{input => init}/migrationparams.go (99%) diff --git a/hack/runtime-migrator/cmd/backup/backup.go b/hack/runtime-migrator/cmd/backup/backup.go index bab3eaa5..917617bb 100644 --- a/hack/runtime-migrator/cmd/backup/backup.go +++ b/hack/runtime-migrator/cmd/backup/backup.go @@ -5,7 +5,7 @@ import ( "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/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -23,10 +23,10 @@ type Backup struct { kubeconfigProvider kubeconfig.Provider outputWriter backup.OutputWriter results backup.Results - cfg input.Config + cfg init.Config } -func NewBackup(cfg input.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { +func NewBackup(cfg init.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { outputWriter, err := backup.NewOutputWriter(cfg.OutputPath) if err != nil { return Backup{}, err diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index 533cae47..539e4ce0 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -3,7 +3,7 @@ package main import ( "context" "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -12,7 +12,7 @@ import ( func main() { slog.Info("Starting runtime-backuper") - cfg := input.NewConfig() + cfg := init.NewConfig() opts := zap.Options{ Development: true, @@ -22,13 +22,13 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := init.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - shootClient, err := input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + shootClient, err := init.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -41,7 +41,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := input.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := init.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/main.go b/hack/runtime-migrator/cmd/migration/main.go index 64f44c9a..961b812d 100644 --- a/hack/runtime-migrator/cmd/migration/main.go +++ b/hack/runtime-migrator/cmd/migration/main.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" kimConfig "github.com/kyma-project/infrastructure-manager/pkg/config" v12 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -27,7 +27,7 @@ const ( func main() { slog.Info("Starting runtime-migrator") - cfg := input.NewConfig() + cfg := init.NewConfig() opts := zap.Options{ Development: true, @@ -37,19 +37,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := init.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - kcpClient, err := input.CreateKcpClient(&cfg) + kcpClient, err := init.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - gardenerShootClient, err := input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + gardenerShootClient, err := init.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -75,7 +75,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := input.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := init.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/migration.go b/hack/runtime-migrator/cmd/migration/migration.go index ba6b0b89..39895d19 100644 --- a/hack/runtime-migrator/cmd/migration/migration.go +++ b/hack/runtime-migrator/cmd/migration/migration.go @@ -8,7 +8,7 @@ import ( gardener_types "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" runtimev1 "github.com/kyma-project/infrastructure-manager/api/v1" - config2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + config2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/migration" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" diff --git a/hack/runtime-migrator/cmd/restore/main.go b/hack/runtime-migrator/cmd/restore/main.go index 88c1a6d1..cba8fdaa 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -11,7 +11,7 @@ import ( func main() { slog.Info("Starting runtime-restorer") - cfg := input.NewConfig() + cfg := init.NewConfig() opts := zap.Options{ Development: true, @@ -21,19 +21,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - _, err := input.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + _, err := init.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - _, err = input.CreateKcpClient(&cfg) + _, err = init.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - _, err = input.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + _, err = init.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/internal/backup/backup.go b/hack/runtime-migrator/internal/backup/backup.go index 505769bf..f9fdfd36 100644 --- a/hack/runtime-migrator/internal/backup/backup.go +++ b/hack/runtime-migrator/internal/backup/backup.go @@ -4,14 +4,14 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" authenticationv1alpha1 "github.com/gardener/oidc-webhook-authenticator/apis/authentication/v1alpha1" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" rbacv1 "k8s.io/api/rbac/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type Backuper struct { - cfg input.Config + cfg init.Config isDryRun bool kubeconfigProvider kubeconfig.Provider } diff --git a/hack/runtime-migrator/internal/input/client.go b/hack/runtime-migrator/internal/init/client.go similarity index 99% rename from hack/runtime-migrator/internal/input/client.go rename to hack/runtime-migrator/internal/init/client.go index 98236649..eb83baf3 100644 --- a/hack/runtime-migrator/internal/input/client.go +++ b/hack/runtime-migrator/internal/init/client.go @@ -1,4 +1,4 @@ -package input +package init import ( "fmt" diff --git a/hack/runtime-migrator/internal/input/migrationparams.go b/hack/runtime-migrator/internal/init/migrationparams.go similarity index 99% rename from hack/runtime-migrator/internal/input/migrationparams.go rename to hack/runtime-migrator/internal/init/migrationparams.go index ea3a7c0d..6045c2c8 100644 --- a/hack/runtime-migrator/internal/input/migrationparams.go +++ b/hack/runtime-migrator/internal/init/migrationparams.go @@ -1,4 +1,4 @@ -package input +package init import ( "bufio" diff --git a/hack/runtime-migrator/internal/migration/migrator.go b/hack/runtime-migrator/internal/migration/migrator.go index 221693fb..fd0b9254 100644 --- a/hack/runtime-migrator/internal/migration/migrator.go +++ b/hack/runtime-migrator/internal/migration/migrator.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/gardener/gardener/pkg/apis/core/v1beta1" v1 "github.com/kyma-project/infrastructure-manager/api/v1" - migrator "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/input" + migrator "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" "github.com/pkg/errors" diff --git a/hack/runtime-migrator/internal/shoot/util.go b/hack/runtime-migrator/internal/shoot/util.go index 291a6f93..3e79c85a 100644 --- a/hack/runtime-migrator/internal/shoot/util.go +++ b/hack/runtime-migrator/internal/shoot/util.go @@ -4,7 +4,7 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" 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/input" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -18,7 +18,7 @@ func Fetch(ctx context.Context, shootList *v1beta1.ShootList, shootClient garden return nil, errors.New("shoot was deleted or the runtimeID is incorrect") } - getCtx, cancel := context.WithTimeout(ctx, input.TimeoutK8sOperation) + getCtx, cancel := context.WithTimeout(ctx, init.TimeoutK8sOperation) defer cancel() // We are fetching the shoot from the gardener to make sure the runtime didn't get deleted during the migration process From 64d8748ab93a1066316c9d59a944fe7c8f336e8e Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Fri, 27 Dec 2024 17:50:14 +0100 Subject: [PATCH 03/11] Refactor --- hack/runtime-migrator/cmd/backup/main.go | 2 + hack/runtime-migrator/cmd/migration/main.go | 14 ++-- hack/runtime-migrator/cmd/restore/main.go | 12 ++-- hack/runtime-migrator/cmd/restore/restore.go | 71 +++++++++++++++++++ .../internal/backup/backup.go | 4 +- .../init/{migrationparams.go => params.go} | 25 ++++++- .../internal/restore/restorer.go | 19 +++++ hack/runtime-migrator/internal/shoot/util.go | 4 +- 8 files changed, 133 insertions(+), 18 deletions(-) create mode 100644 hack/runtime-migrator/cmd/restore/restore.go rename hack/runtime-migrator/internal/init/{migrationparams.go => params.go} (85%) create mode 100644 hack/runtime-migrator/internal/restore/restorer.go diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index 539e4ce0..2b07fffa 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -14,6 +14,8 @@ func main() { slog.Info("Starting runtime-backuper") cfg := init.NewConfig() + init.PrintConfig(cfg) + opts := zap.Options{ Development: true, } diff --git a/hack/runtime-migrator/cmd/migration/main.go b/hack/runtime-migrator/cmd/migration/main.go index 961b812d..86e98a84 100644 --- a/hack/runtime-migrator/cmd/migration/main.go +++ b/hack/runtime-migrator/cmd/migration/main.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" kimConfig "github.com/kyma-project/infrastructure-manager/pkg/config" v12 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -27,7 +27,9 @@ const ( func main() { slog.Info("Starting runtime-migrator") - cfg := init.NewConfig() + cfg := init2.NewConfig() + + init2.PrintConfig(cfg) opts := zap.Options{ Development: true, @@ -37,19 +39,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := init.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + kubeconfigProvider, err := init2.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - kcpClient, err := init.CreateKcpClient(&cfg) + kcpClient, err := init2.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - gardenerShootClient, err := init.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + gardenerShootClient, err := init2.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -75,7 +77,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := init.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := init2.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", 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 cba8fdaa..69d7b663 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -11,7 +11,9 @@ import ( func main() { slog.Info("Starting runtime-restorer") - cfg := init.NewConfig() + cfg := init2.NewRestoreConfig() + + init2.PrintRestoreConfig(cfg) opts := zap.Options{ Development: true, @@ -21,19 +23,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - _, err := init.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + _, err := init2.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - _, err = init.CreateKcpClient(&cfg) + _, err = init2.CreateKcpClient(&cfg.Config) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - _, err = init.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + _, err = init2.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", 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 new file mode 100644 index 00000000..b33b48f3 --- /dev/null +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -0,0 +1,71 @@ +package main + +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" + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/restore" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" + "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "log/slog" + "time" +) + +const ( + timeoutK8sOperation = 20 * time.Second + expirationTime = 60 * time.Minute +) + +type Restore struct { + shootClient gardener_types.ShootInterface + kubeconfigProvider kubeconfig.Provider + outputWriter backup.OutputWriter + results backup.Results + cfg init2.Config +} + +func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { + listCtx, cancel := context.WithTimeout(ctx, timeoutK8sOperation) + defer cancel() + + shootList, err := r.shootClient.List(listCtx, v1.ListOptions{}) + if err != nil { + return err + } + + _ = restore.NewRestorer(r.cfg.IsDryRun, r.kubeconfigProvider) + + for _, runtimeID := range runtimeIDs { + shootToBackup, err := shoot.Fetch(ctx, shootList, r.shootClient, runtimeID) + if err != nil { + errMsg := fmt.Sprintf("Failed to fetch shoot: %v", err) + r.results.ErrorOccurred(runtimeID, "", errMsg) + slog.Error(errMsg, "runtimeID", runtimeID) + + continue + } + + if shoot.IsBeingDeleted(shootToBackup) { + errMsg := fmt.Sprintf("Shoot is being deleted: %v", err) + r.results.ErrorOccurred(runtimeID, shootToBackup.Name, errMsg) + slog.Error(errMsg, "runtimeID", runtimeID) + continue + } + + // + + if r.cfg.IsDryRun { + slog.Info("Runtime processed successfully (dry-run)", "runtimeID", runtimeID) + + continue + } + + slog.Info("Runtime backup created successfully successfully", "runtimeID", runtimeID) + r.results.OperationSucceeded(runtimeID, shootToBackup.Name) + } + + return nil +} diff --git a/hack/runtime-migrator/internal/backup/backup.go b/hack/runtime-migrator/internal/backup/backup.go index f9fdfd36..c7e6b461 100644 --- a/hack/runtime-migrator/internal/backup/backup.go +++ b/hack/runtime-migrator/internal/backup/backup.go @@ -4,14 +4,14 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" authenticationv1alpha1 "github.com/gardener/oidc-webhook-authenticator/apis/authentication/v1alpha1" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" rbacv1 "k8s.io/api/rbac/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type Backuper struct { - cfg init.Config + cfg init2.Config isDryRun bool kubeconfigProvider kubeconfig.Provider } diff --git a/hack/runtime-migrator/internal/init/migrationparams.go b/hack/runtime-migrator/internal/init/params.go similarity index 85% rename from hack/runtime-migrator/internal/init/migrationparams.go rename to hack/runtime-migrator/internal/init/params.go index 6045c2c8..652bbebb 100644 --- a/hack/runtime-migrator/internal/init/migrationparams.go +++ b/hack/runtime-migrator/internal/init/params.go @@ -26,7 +26,7 @@ const ( TimeoutK8sOperation = 20 * time.Second ) -func printConfig(cfg Config) { +func PrintConfig(cfg Config) { log.Println("gardener-kubeconfig-path:", cfg.GardenerKubeconfigPath) log.Println("kcp-kubeconfig-path:", cfg.KcpKubeconfigPath) log.Println("gardener-project-name:", cfg.GardenerProjectName) @@ -50,8 +50,6 @@ func NewConfig() Config { flag.Parse() - printConfig(result) - return result } @@ -81,3 +79,24 @@ func GetRuntimeIDsFromInputFile(cfg Config) ([]string, error) { } return runtimeIDs, err } + +type RestoreConfig struct { + Config + BackupDir string +} + +func NewRestoreConfig() RestoreConfig { + restoreConfig := RestoreConfig{ + Config: NewConfig(), + } + + flag.StringVar(&restoreConfig.BackupDir, "backup-path", "/path/to/backup/dir", "Path to the directory containing backup.") + flag.Parse() + + return restoreConfig +} + +func PrintRestoreConfig(cfg RestoreConfig) { + log.Println("backup-path:", cfg.BackupDir) + PrintConfig(cfg.Config) +} diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go new file mode 100644 index 00000000..81638003 --- /dev/null +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -0,0 +1,19 @@ +package restore + +import ( + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" +) + +type Restorer struct { + cfg init2.Config + isDryRun bool + kubeconfigProvider kubeconfig.Provider +} + +func NewRestorer(isDryRun bool, kubeconfigProvider kubeconfig.Provider) Restorer { + return Restorer{ + isDryRun: isDryRun, + kubeconfigProvider: kubeconfigProvider, + } +} diff --git a/hack/runtime-migrator/internal/shoot/util.go b/hack/runtime-migrator/internal/shoot/util.go index 3e79c85a..f1af003b 100644 --- a/hack/runtime-migrator/internal/shoot/util.go +++ b/hack/runtime-migrator/internal/shoot/util.go @@ -4,7 +4,7 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" 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/init" + init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -18,7 +18,7 @@ func Fetch(ctx context.Context, shootList *v1beta1.ShootList, shootClient garden return nil, errors.New("shoot was deleted or the runtimeID is incorrect") } - getCtx, cancel := context.WithTimeout(ctx, init.TimeoutK8sOperation) + getCtx, cancel := context.WithTimeout(ctx, init2.TimeoutK8sOperation) defer cancel() // We are fetching the shoot from the gardener to make sure the runtime didn't get deleted during the migration process From b70dde6a8ca411611df331d730a7711d9acfe2c8 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 09:40:17 +0100 Subject: [PATCH 04/11] Refactor package name --- hack/runtime-migrator/cmd/backup/backup.go | 6 +++--- hack/runtime-migrator/cmd/backup/main.go | 12 ++++++------ hack/runtime-migrator/cmd/migration/main.go | 14 +++++++------- hack/runtime-migrator/cmd/migration/migration.go | 8 ++++---- hack/runtime-migrator/cmd/restore/main.go | 12 ++++++------ hack/runtime-migrator/cmd/restore/restore.go | 4 ++-- hack/runtime-migrator/internal/backup/backup.go | 4 ++-- .../internal/{init => initialisation}/client.go | 2 +- .../internal/{init => initialisation}/params.go | 2 +- hack/runtime-migrator/internal/restore/restorer.go | 4 ++-- hack/runtime-migrator/internal/shoot/util.go | 4 ++-- 11 files changed, 36 insertions(+), 36 deletions(-) rename hack/runtime-migrator/internal/{init => initialisation}/client.go (99%) rename hack/runtime-migrator/internal/{init => initialisation}/params.go (99%) diff --git a/hack/runtime-migrator/cmd/backup/backup.go b/hack/runtime-migrator/cmd/backup/backup.go index 917617bb..f4fa8d9d 100644 --- a/hack/runtime-migrator/cmd/backup/backup.go +++ b/hack/runtime-migrator/cmd/backup/backup.go @@ -5,7 +5,7 @@ import ( "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/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -23,10 +23,10 @@ type Backup struct { kubeconfigProvider kubeconfig.Provider outputWriter backup.OutputWriter results backup.Results - cfg init.Config + cfg initialisation.Config } -func NewBackup(cfg init.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { +func NewBackup(cfg initialisation.Config, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Backup, error) { outputWriter, err := backup.NewOutputWriter(cfg.OutputPath) if err != nil { return Backup{}, err diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index 2b07fffa..24727982 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -3,7 +3,7 @@ package main import ( "context" "fmt" - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -12,9 +12,9 @@ import ( func main() { slog.Info("Starting runtime-backuper") - cfg := init.NewConfig() + cfg := initialisation.NewConfig() - init.PrintConfig(cfg) + initialisation.PrintConfig(cfg) opts := zap.Options{ Development: true, @@ -24,13 +24,13 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := init.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) } - shootClient, err := init.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) @@ -43,7 +43,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := init.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := initialisation.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/main.go b/hack/runtime-migrator/cmd/migration/main.go index 86e98a84..baf783e9 100644 --- a/hack/runtime-migrator/cmd/migration/main.go +++ b/hack/runtime-migrator/cmd/migration/main.go @@ -12,7 +12,7 @@ import ( "strings" "time" - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" kimConfig "github.com/kyma-project/infrastructure-manager/pkg/config" v12 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -27,9 +27,9 @@ const ( func main() { slog.Info("Starting runtime-migrator") - cfg := init2.NewConfig() + cfg := initialisation.NewConfig() - init2.PrintConfig(cfg) + initialisation.PrintConfig(cfg) opts := zap.Options{ Development: true, @@ -39,19 +39,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - kubeconfigProvider, err := init2.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) } - kcpClient, err := init2.CreateKcpClient(&cfg) + kcpClient, err := initialisation.CreateKcpClient(&cfg) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - gardenerShootClient, err := init2.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + gardenerShootClient, err := initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) @@ -77,7 +77,7 @@ func main() { } slog.Info("Reading runtimeIds from input file") - runtimeIds, err := init2.GetRuntimeIDsFromInputFile(cfg) + runtimeIds, err := initialisation.GetRuntimeIDsFromInputFile(cfg) if err != nil { slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/migration.go b/hack/runtime-migrator/cmd/migration/migration.go index 39895d19..9f6914e7 100644 --- a/hack/runtime-migrator/cmd/migration/migration.go +++ b/hack/runtime-migrator/cmd/migration/migration.go @@ -8,7 +8,7 @@ import ( gardener_types "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" runtimev1 "github.com/kyma-project/infrastructure-manager/api/v1" - config2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/migration" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" @@ -26,7 +26,7 @@ type Migration struct { isDryRun bool } -func NewMigration(migratorConfig config2.Config, converterConfig config.ConverterConfig, auditLogConfig auditlogs.Configuration, kubeconfigProvider kubeconfig.Provider, kcpClient client.Client, shootClient gardener_types.ShootInterface) (Migration, error) { +func NewMigration(migratorConfig initialisation.Config, converterConfig config.ConverterConfig, auditLogConfig auditlogs.Configuration, kubeconfigProvider kubeconfig.Provider, kcpClient client.Client, shootClient gardener_types.ShootInterface) (Migration, error) { outputWriter, err := migration.NewOutputWriter(migratorConfig.OutputPath) if err != nil { @@ -44,7 +44,7 @@ func NewMigration(migratorConfig config2.Config, converterConfig config.Converte } func (m Migration) Do(ctx context.Context, runtimeIDs []string) error { - listCtx, cancel := context.WithTimeout(ctx, config2.TimeoutK8sOperation) + listCtx, cancel := context.WithTimeout(ctx, initialisation.TimeoutK8sOperation) defer cancel() shootList, err := m.shootClient.List(listCtx, v1.ListOptions{}) @@ -92,7 +92,7 @@ func (m Migration) Do(ctx context.Context, runtimeIDs []string) error { return } - migrationCtx, cancel := context.WithTimeout(ctx, config2.TimeoutK8sOperation) + migrationCtx, cancel := context.WithTimeout(ctx, initialisation.TimeoutK8sOperation) defer cancel() runtime, err := m.runtimeMigrator.Do(migrationCtx, *shootToMigrate) diff --git a/hack/runtime-migrator/cmd/restore/main.go b/hack/runtime-migrator/cmd/restore/main.go index 69d7b663..d063617c 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "log/slog" "os" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -11,9 +11,9 @@ import ( func main() { slog.Info("Starting runtime-restorer") - cfg := init2.NewRestoreConfig() + cfg := initialisation.NewRestoreConfig() - init2.PrintRestoreConfig(cfg) + initialisation.PrintRestoreConfig(cfg) opts := zap.Options{ Development: true, @@ -23,19 +23,19 @@ func main() { gardenerNamespace := fmt.Sprintf("garden-%s", cfg.GardenerProjectName) - _, err := init2.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) + _, err := initialisation.SetupKubernetesKubeconfigProvider(cfg.GardenerKubeconfigPath, gardenerNamespace, expirationTime) if err != nil { slog.Error(fmt.Sprintf("Failed to create kubeconfig provider: %v", err)) os.Exit(1) } - _, err = init2.CreateKcpClient(&cfg.Config) + _, err = initialisation.CreateKcpClient(&cfg.Config) if err != nil { slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } - _, err = init2.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + _, err = initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", 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 b33b48f3..e1871ef4 100644 --- a/hack/runtime-migrator/cmd/restore/restore.go +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -5,7 +5,7 @@ import ( "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" - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "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" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" @@ -24,7 +24,7 @@ type Restore struct { kubeconfigProvider kubeconfig.Provider outputWriter backup.OutputWriter results backup.Results - cfg init2.Config + cfg initialisation.Config } func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { diff --git a/hack/runtime-migrator/internal/backup/backup.go b/hack/runtime-migrator/internal/backup/backup.go index c7e6b461..c94b1779 100644 --- a/hack/runtime-migrator/internal/backup/backup.go +++ b/hack/runtime-migrator/internal/backup/backup.go @@ -4,14 +4,14 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" authenticationv1alpha1 "github.com/gardener/oidc-webhook-authenticator/apis/authentication/v1alpha1" - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" rbacv1 "k8s.io/api/rbac/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type Backuper struct { - cfg init2.Config + cfg initialisation.Config isDryRun bool kubeconfigProvider kubeconfig.Provider } diff --git a/hack/runtime-migrator/internal/init/client.go b/hack/runtime-migrator/internal/initialisation/client.go similarity index 99% rename from hack/runtime-migrator/internal/init/client.go rename to hack/runtime-migrator/internal/initialisation/client.go index eb83baf3..9e5d1283 100644 --- a/hack/runtime-migrator/internal/init/client.go +++ b/hack/runtime-migrator/internal/initialisation/client.go @@ -1,4 +1,4 @@ -package init +package initialisation import ( "fmt" diff --git a/hack/runtime-migrator/internal/init/params.go b/hack/runtime-migrator/internal/initialisation/params.go similarity index 99% rename from hack/runtime-migrator/internal/init/params.go rename to hack/runtime-migrator/internal/initialisation/params.go index 652bbebb..83d208ff 100644 --- a/hack/runtime-migrator/internal/init/params.go +++ b/hack/runtime-migrator/internal/initialisation/params.go @@ -1,4 +1,4 @@ -package init +package initialisation import ( "bufio" diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go index 81638003..3f4dbd78 100644 --- a/hack/runtime-migrator/internal/restore/restorer.go +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -1,12 +1,12 @@ package restore import ( - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" ) type Restorer struct { - cfg init2.Config + cfg initialisation.Config isDryRun bool kubeconfigProvider kubeconfig.Provider } diff --git a/hack/runtime-migrator/internal/shoot/util.go b/hack/runtime-migrator/internal/shoot/util.go index f1af003b..48636880 100644 --- a/hack/runtime-migrator/internal/shoot/util.go +++ b/hack/runtime-migrator/internal/shoot/util.go @@ -4,7 +4,7 @@ import ( "context" "github.com/gardener/gardener/pkg/apis/core/v1beta1" gardener_types "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1" - init2 "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/pkg/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -18,7 +18,7 @@ func Fetch(ctx context.Context, shootList *v1beta1.ShootList, shootClient garden return nil, errors.New("shoot was deleted or the runtimeID is incorrect") } - getCtx, cancel := context.WithTimeout(ctx, init2.TimeoutK8sOperation) + getCtx, cancel := context.WithTimeout(ctx, initialisation.TimeoutK8sOperation) defer cancel() // We are fetching the shoot from the gardener to make sure the runtime didn't get deleted during the migration process From 40fb6bfa7efe1fc291cc50fd4a7a618bfff13f32 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 11:12:40 +0100 Subject: [PATCH 05/11] Refactor package name --- hack/runtime-migrator/internal/migration/migrator.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hack/runtime-migrator/internal/migration/migrator.go b/hack/runtime-migrator/internal/migration/migrator.go index fd0b9254..8c5d3f94 100644 --- a/hack/runtime-migrator/internal/migration/migrator.go +++ b/hack/runtime-migrator/internal/migration/migrator.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/gardener/gardener/pkg/apis/core/v1beta1" v1 "github.com/kyma-project/infrastructure-manager/api/v1" - migrator "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/init" + "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" "github.com/kyma-project/infrastructure-manager/pkg/config" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" "github.com/pkg/errors" @@ -25,13 +25,13 @@ const ( ) type Migrator struct { - cfg migrator.Config + cfg initialisation.Config converterConfig config.ConverterConfig kubeconfigProvider kubeconfig.Provider kcpClient client.Client } -func NewMigrator(cfg migrator.Config, kubeconfigProvider kubeconfig.Provider, kcpClient client.Client) Migrator { +func NewMigrator(cfg initialisation.Config, kubeconfigProvider kubeconfig.Provider, kcpClient client.Client) Migrator { return Migrator{ cfg: cfg, kubeconfigProvider: kubeconfigProvider, From f008bbdf02171089316de87356b47ab30d781022 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 12:39:28 +0100 Subject: [PATCH 06/11] 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) +} From 9e4df407e550acfaacba4920321734de6a42b8d5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 15:06:27 +0100 Subject: [PATCH 07/11] First version of restore implemented --- hack/runtime-migrator/cmd/backup/main.go | 2 +- hack/runtime-migrator/cmd/migration/main.go | 2 +- hack/runtime-migrator/cmd/restore/main.go | 4 +- hack/runtime-migrator/cmd/restore/restore.go | 58 +++++++++++++------ .../internal/initialisation/client.go | 19 ++++-- .../internal/restore/restorer.go | 35 ++++++++--- 6 files changed, 85 insertions(+), 35 deletions(-) diff --git a/hack/runtime-migrator/cmd/backup/main.go b/hack/runtime-migrator/cmd/backup/main.go index ac230755..6ffdbb4d 100644 --- a/hack/runtime-migrator/cmd/backup/main.go +++ b/hack/runtime-migrator/cmd/backup/main.go @@ -30,7 +30,7 @@ func main() { os.Exit(1) } - shootClient, err := initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + shootClient, _, err := initialisation.SetupGardenerShootClients(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) diff --git a/hack/runtime-migrator/cmd/migration/main.go b/hack/runtime-migrator/cmd/migration/main.go index baf783e9..0a7b3df8 100644 --- a/hack/runtime-migrator/cmd/migration/main.go +++ b/hack/runtime-migrator/cmd/migration/main.go @@ -51,7 +51,7 @@ func main() { os.Exit(1) } - gardenerShootClient, err := initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + gardenerShootClient, _, err := initialisation.SetupGardenerShootClients(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { slog.Error("Failed to setup Gardener shoot client", 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 a7ba440c..0dd9cb63 100644 --- a/hack/runtime-migrator/cmd/restore/main.go +++ b/hack/runtime-migrator/cmd/restore/main.go @@ -36,13 +36,13 @@ func main() { os.Exit(1) } - shootClient, err := initialisation.SetupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) + shootClient, dynamicGardenerClient, err := initialisation.SetupGardenerShootClients(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) + restore, err := NewRestore(cfg, kubeconfigProvider, shootClient, dynamicGardenerClient) if err != nil { slog.Error("Failed to setup Gardener shoot client", 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 5f134009..c4326f3d 100644 --- a/hack/runtime-migrator/cmd/restore/restore.go +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -9,7 +9,9 @@ import ( "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/shoot" "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" "log/slog" + "sigs.k8s.io/controller-runtime/pkg/client" "time" ) @@ -19,25 +21,29 @@ const ( ) type Restore struct { - shootClient gardener_types.ShootInterface - kubeconfigProvider kubeconfig.Provider - outputWriter restore.OutputWriter - results restore.Results - cfg initialisation.RestoreConfig + shootClient gardener_types.ShootInterface + dynamicGardenerClient client.Client + kubeconfigProvider kubeconfig.Provider + outputWriter restore.OutputWriter + results restore.Results + cfg initialisation.RestoreConfig } -func NewRestore(cfg initialisation.RestoreConfig, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface) (Restore, error) { +const fieldManagerName = "kim" + +func NewRestore(cfg initialisation.RestoreConfig, kubeconfigProvider kubeconfig.Provider, shootClient gardener_types.ShootInterface, dynamicGardenerClient client.Client) (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, + shootClient: shootClient, + dynamicGardenerClient: dynamicGardenerClient, + kubeconfigProvider: kubeconfigProvider, + outputWriter: outputWriter, + results: restore.NewRestoreResults(outputWriter.NewResultsDir), + cfg: cfg, }, err } @@ -50,10 +56,10 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { return err } - _ = restore.NewRestorer(r.cfg.IsDryRun, r.kubeconfigProvider) + restorer := restore.NewRestorer(r.cfg.BackupDir) for _, runtimeID := range runtimeIDs { - shootToBackup, err := shoot.Fetch(ctx, shootList, r.shootClient, runtimeID) + currentShoot, err := shoot.Fetch(ctx, shootList, r.shootClient, runtimeID) if err != nil { errMsg := fmt.Sprintf("Failed to fetch shoot: %v", err) r.results.ErrorOccurred(runtimeID, "", errMsg) @@ -62,14 +68,20 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { continue } - if shoot.IsBeingDeleted(shootToBackup) { + if shoot.IsBeingDeleted(currentShoot) { errMsg := fmt.Sprintf("Shoot is being deleted: %v", err) - r.results.ErrorOccurred(runtimeID, shootToBackup.Name, errMsg) + r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) slog.Error(errMsg, "runtimeID", runtimeID) continue } - // + shootToRestore, err := restorer.Do(context.Background(), runtimeID, currentShoot.Name) + if err != nil { + errMsg := fmt.Sprintf("Failed to restore runtime: %v", err) + r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) + slog.Error(errMsg, "runtimeID", runtimeID) + continue + } if r.cfg.IsDryRun { slog.Info("Runtime processed successfully (dry-run)", "runtimeID", runtimeID) @@ -77,8 +89,20 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { continue } + err = r.dynamicGardenerClient.Patch(ctx, &shootToRestore, client.Apply, &client.PatchOptions{ + FieldManager: fieldManagerName, + Force: ptr.To(true), + }) + + if err != nil { + errMsg := fmt.Sprintf("Failed to restore runtime: %v", err) + r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) + slog.Error(errMsg, "runtimeID", runtimeID) + continue + } + slog.Info("Runtime backup created successfully successfully", "runtimeID", runtimeID) - r.results.OperationSucceeded(runtimeID, shootToBackup.Name) + r.results.OperationSucceeded(runtimeID, currentShoot.Name) } return nil diff --git a/hack/runtime-migrator/internal/initialisation/client.go b/hack/runtime-migrator/internal/initialisation/client.go index 9e5d1283..e2d54113 100644 --- a/hack/runtime-migrator/internal/initialisation/client.go +++ b/hack/runtime-migrator/internal/initialisation/client.go @@ -27,8 +27,6 @@ func addToScheme(s *runtime.Scheme) error { return nil } -type GetClient = func() (client.Client, error) - func CreateKcpClient(cfg *Config) (client.Client, error) { restCfg, err := clientcmd.BuildConfigFromFlags("", cfg.KcpKubeconfigPath) if err != nil { @@ -77,18 +75,27 @@ func SetupKubernetesKubeconfigProvider(kubeconfigPath string, namespace string, int64(expirationTime.Seconds())), nil } -func SetupGardenerShootClient(kubeconfigPath, gardenerNamespace string) (gardener_types.ShootInterface, error) { +func SetupGardenerShootClients(kubeconfigPath, gardenerNamespace string) (gardener_types.ShootInterface, client.Client, error) { restConfig, err := gardener.NewRestConfigFromFile(kubeconfigPath) if err != nil { - return nil, err + return nil, nil, err } gardenerClientSet, err := gardener_types.NewForConfig(restConfig) if err != nil { - return nil, err + return nil, nil, err } shootClient := gardenerClientSet.Shoots(gardenerNamespace) - return shootClient, nil + scheme := runtime.NewScheme() + if err := addToScheme(scheme); err != nil { + return nil, nil, err + } + + var dynamicClient, _ = client.New(restConfig, client.Options{ + Scheme: scheme, + }) + + return shootClient, dynamicClient, nil } diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go index 3f4dbd78..e1e27512 100644 --- a/hack/runtime-migrator/internal/restore/restorer.go +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -1,19 +1,38 @@ package restore import ( - "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/initialisation" - "github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig" + "context" + "fmt" + "github.com/gardener/gardener/pkg/apis/core/v1beta1" + "os" + "path" + "sigs.k8s.io/yaml" ) type Restorer struct { - cfg initialisation.Config - isDryRun bool - kubeconfigProvider kubeconfig.Provider + backupDir string } -func NewRestorer(isDryRun bool, kubeconfigProvider kubeconfig.Provider) Restorer { +func NewRestorer(backupDir string) Restorer { return Restorer{ - isDryRun: isDryRun, - kubeconfigProvider: kubeconfigProvider, + backupDir: backupDir, } } + +func (r Restorer) Do(_ context.Context, runtimeID string, shootName string) (v1beta1.Shoot, error) { + filePath := path.Join(r.backupDir, fmt.Sprintf("%s/%s.yaml", runtimeID, shootName)) + + fileBytes, err := os.ReadFile(filePath) + if err != nil { + return v1beta1.Shoot{}, err + } + + var shoot v1beta1.Shoot + + err = yaml.Unmarshal(fileBytes, &shoot) + if err != nil { + return v1beta1.Shoot{}, err + } + + return shoot, nil +} From 8b260d4d8eb7f651273d6722933668b7091d5507 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Mon, 30 Dec 2024 17:27:32 +0100 Subject: [PATCH 08/11] Restore dry-run works --- hack/runtime-migrator/cmd/restore/restore.go | 11 ++++++++++- .../internal/initialisation/client.go | 4 ++-- .../internal/initialisation/params.go | 11 ++++++++--- hack/runtime-migrator/internal/restore/output.go | 4 ++-- hack/runtime-migrator/internal/restore/restorer.go | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/hack/runtime-migrator/cmd/restore/restore.go b/hack/runtime-migrator/cmd/restore/restore.go index c4326f3d..cea1eaba 100644 --- a/hack/runtime-migrator/cmd/restore/restore.go +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -85,6 +85,7 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { if r.cfg.IsDryRun { slog.Info("Runtime processed successfully (dry-run)", "runtimeID", runtimeID) + r.results.OperationSucceeded(runtimeID, currentShoot.Name) continue } @@ -101,9 +102,17 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { continue } - slog.Info("Runtime backup created successfully successfully", "runtimeID", runtimeID) + slog.Info("Runtime restore performed successfully", "runtimeID", runtimeID) r.results.OperationSucceeded(runtimeID, currentShoot.Name) } + resultsFile, err := r.outputWriter.SaveRestoreResults(r.results) + if err != nil { + return err + } + + slog.Info(fmt.Sprintf("Restore completed. Successfully restored backups: %d, Failed operations: %d", r.results.Succeeded, r.results.Failed)) + slog.Info(fmt.Sprintf("Restore results saved in: %s", resultsFile)) + return nil } diff --git a/hack/runtime-migrator/internal/initialisation/client.go b/hack/runtime-migrator/internal/initialisation/client.go index e2d54113..7eb99d4d 100644 --- a/hack/runtime-migrator/internal/initialisation/client.go +++ b/hack/runtime-migrator/internal/initialisation/client.go @@ -93,9 +93,9 @@ func SetupGardenerShootClients(kubeconfigPath, gardenerNamespace string) (garden return nil, nil, err } - var dynamicClient, _ = client.New(restConfig, client.Options{ + dynamicClient, err := client.New(restConfig, client.Options{ Scheme: scheme, }) - return shootClient, dynamicClient, nil + return shootClient, dynamicClient, err } diff --git a/hack/runtime-migrator/internal/initialisation/params.go b/hack/runtime-migrator/internal/initialisation/params.go index 83d208ff..b03d0468 100644 --- a/hack/runtime-migrator/internal/initialisation/params.go +++ b/hack/runtime-migrator/internal/initialisation/params.go @@ -86,10 +86,15 @@ type RestoreConfig struct { } func NewRestoreConfig() RestoreConfig { - restoreConfig := RestoreConfig{ - Config: NewConfig(), - } + restoreConfig := RestoreConfig{} + flag.StringVar(&restoreConfig.KcpKubeconfigPath, "kcp-kubeconfig-path", "/path/to/kcp/kubeconfig", "Path to the Kubeconfig file of KCP cluster.") + flag.StringVar(&restoreConfig.GardenerKubeconfigPath, "gardener-kubeconfig-path", "/path/to/gardener/kubeconfig", "Kubeconfig file for Gardener cluster.") + flag.StringVar(&restoreConfig.GardenerProjectName, "gardener-project-name", "gardener-project-name", "Name of the Gardener project.") + flag.StringVar(&restoreConfig.OutputPath, "output-path", "/tmp/", "Path where generated yamls will be saved. Directory has to exist.") + flag.BoolVar(&restoreConfig.IsDryRun, "dry-run", true, "Dry-run flag. Has to be set to 'false' otherwise it will not apply the Custom Resources on the KCP cluster.") + flag.StringVar(&restoreConfig.InputType, "input-type", InputTypeJSON, "Type of input to be used. Possible values: **txt** (see the example hack/runtime-migrator/input/runtimeids_sample.txt), and **json** (see the example hack/runtime-migrator/input/runtimeids_sample.json).") + flag.StringVar(&restoreConfig.InputFilePath, "input-file-path", "/path/to/input/file", "Path to the input file containing RuntimeCRs to be migrated.") flag.StringVar(&restoreConfig.BackupDir, "backup-path", "/path/to/backup/dir", "Path to the directory containing backup.") flag.Parse() diff --git a/hack/runtime-migrator/internal/restore/output.go b/hack/runtime-migrator/internal/restore/output.go index 93bdf0a1..9acc0b40 100644 --- a/hack/runtime-migrator/internal/restore/output.go +++ b/hack/runtime-migrator/internal/restore/output.go @@ -13,7 +13,7 @@ type OutputWriter struct { } func NewOutputWriter(outputDir string) (OutputWriter, error) { - newResultsDir := path.Join(outputDir, fmt.Sprintf("backup-%s", time.Now().Format(time.RFC3339))) + newResultsDir := path.Join(outputDir, fmt.Sprintf("restore-%s", time.Now().Format(time.RFC3339))) err := os.MkdirAll(newResultsDir, os.ModePerm) if err != nil { @@ -31,7 +31,7 @@ func (ow OutputWriter) SaveRestoreResults(results Results) (string, error) { return "", err } - fileName := fmt.Sprintf("%s/backup-results.json", ow.NewResultsDir) + fileName := fmt.Sprintf("%s/restore-results.json", ow.NewResultsDir) return fileName, writeFile(fileName, resultFile) } diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go index e1e27512..d8c22620 100644 --- a/hack/runtime-migrator/internal/restore/restorer.go +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -20,7 +20,7 @@ func NewRestorer(backupDir string) Restorer { } func (r Restorer) Do(_ context.Context, runtimeID string, shootName string) (v1beta1.Shoot, error) { - filePath := path.Join(r.backupDir, fmt.Sprintf("%s/%s.yaml", runtimeID, shootName)) + filePath := path.Join(r.backupDir, fmt.Sprintf("backup/%s/%s-to-restore.yaml", runtimeID, shootName)) fileBytes, err := os.ReadFile(filePath) if err != nil { From b058e973c7a9ecd92db6afed0242d83009d68277 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Tue, 31 Dec 2024 09:38:44 +0100 Subject: [PATCH 09/11] Restore works --- hack/runtime-migrator/internal/initialisation/client.go | 5 +++++ hack/runtime-migrator/internal/restore/restorer.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/hack/runtime-migrator/internal/initialisation/client.go b/hack/runtime-migrator/internal/initialisation/client.go index 7eb99d4d..dfa72749 100644 --- a/hack/runtime-migrator/internal/initialisation/client.go +++ b/hack/runtime-migrator/internal/initialisation/client.go @@ -97,5 +97,10 @@ func SetupGardenerShootClients(kubeconfigPath, gardenerNamespace string) (garden Scheme: scheme, }) + err = v1beta1.AddToScheme(dynamicClient.Scheme()) + if err != nil { + return nil, nil, err + } + return shootClient, dynamicClient, err } diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go index d8c22620..61fdb4bf 100644 --- a/hack/runtime-migrator/internal/restore/restorer.go +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -34,5 +34,8 @@ func (r Restorer) Do(_ context.Context, runtimeID string, shootName string) (v1b return v1beta1.Shoot{}, err } + shoot.Kind = "Shoot" + shoot.APIVersion = "core.gardener.cloud/v1beta1" + return shoot, nil } From 2d4e9012d9fad555cfa3e102719ea3ca14f8e40f Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Tue, 31 Dec 2024 10:25:33 +0100 Subject: [PATCH 10/11] Corrected context handling --- hack/runtime-migrator/cmd/restore/restore.go | 22 ++++++++++++++----- .../internal/restore/restorer.go | 3 +-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/hack/runtime-migrator/cmd/restore/restore.go b/hack/runtime-migrator/cmd/restore/restore.go index cea1eaba..553aa675 100644 --- a/hack/runtime-migrator/cmd/restore/restore.go +++ b/hack/runtime-migrator/cmd/restore/restore.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "github.com/gardener/gardener/pkg/apis/core/v1beta1" 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/initialisation" "github.com/kyma-project/infrastructure-manager/hack/runtime-migrator-app/internal/restore" @@ -72,14 +73,16 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { errMsg := fmt.Sprintf("Shoot is being deleted: %v", err) r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) slog.Error(errMsg, "runtimeID", runtimeID) + continue } - shootToRestore, err := restorer.Do(context.Background(), runtimeID, currentShoot.Name) + shootToRestore, err := restorer.Do(runtimeID, currentShoot.Name) if err != nil { errMsg := fmt.Sprintf("Failed to restore runtime: %v", err) r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) slog.Error(errMsg, "runtimeID", runtimeID) + continue } @@ -90,15 +93,12 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { continue } - err = r.dynamicGardenerClient.Patch(ctx, &shootToRestore, client.Apply, &client.PatchOptions{ - FieldManager: fieldManagerName, - Force: ptr.To(true), - }) - + err = r.applyResources(ctx, shootToRestore) if err != nil { errMsg := fmt.Sprintf("Failed to restore runtime: %v", err) r.results.ErrorOccurred(runtimeID, currentShoot.Name, errMsg) slog.Error(errMsg, "runtimeID", runtimeID) + continue } @@ -116,3 +116,13 @@ func (r Restore) Do(ctx context.Context, runtimeIDs []string) error { return nil } + +func (r Restore) applyResources(ctx context.Context, shootToRestore v1beta1.Shoot) error { + patchCtx, cancel := context.WithTimeout(ctx, timeoutK8sOperation) + defer cancel() + + return r.dynamicGardenerClient.Patch(patchCtx, &shootToRestore, client.Apply, &client.PatchOptions{ + FieldManager: fieldManagerName, + Force: ptr.To(true), + }) +} diff --git a/hack/runtime-migrator/internal/restore/restorer.go b/hack/runtime-migrator/internal/restore/restorer.go index 61fdb4bf..6ada323e 100644 --- a/hack/runtime-migrator/internal/restore/restorer.go +++ b/hack/runtime-migrator/internal/restore/restorer.go @@ -1,7 +1,6 @@ package restore import ( - "context" "fmt" "github.com/gardener/gardener/pkg/apis/core/v1beta1" "os" @@ -19,7 +18,7 @@ func NewRestorer(backupDir string) Restorer { } } -func (r Restorer) Do(_ context.Context, runtimeID string, shootName string) (v1beta1.Shoot, error) { +func (r Restorer) Do(runtimeID string, shootName string) (v1beta1.Shoot, error) { filePath := path.Join(r.backupDir, fmt.Sprintf("backup/%s/%s-to-restore.yaml", runtimeID, shootName)) fileBytes, err := os.ReadFile(filePath) From 9093175703b55412ee3f618aed13019f5e9d96f3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Tue, 31 Dec 2024 11:49:03 +0100 Subject: [PATCH 11/11] Error handling fixed in CreateKcpClient --- hack/runtime-migrator/internal/initialisation/client.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hack/runtime-migrator/internal/initialisation/client.go b/hack/runtime-migrator/internal/initialisation/client.go index dfa72749..2868f1b5 100644 --- a/hack/runtime-migrator/internal/initialisation/client.go +++ b/hack/runtime-migrator/internal/initialisation/client.go @@ -38,11 +38,9 @@ func CreateKcpClient(cfg *Config) (client.Client, error) { return nil, err } - var k8sClient, _ = client.New(restCfg, client.Options{ + return client.New(restCfg, client.Options{ Scheme: scheme, }) - - return k8sClient, nil } func SetupKubernetesKubeconfigProvider(kubeconfigPath string, namespace string, expirationTime time.Duration) (kubeconfig.Provider, error) {