From 489b630ba3d7b7fe8aad8ab5b054ba85d98ef8da Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Sat, 23 Nov 2024 18:31:15 +0100 Subject: [PATCH] Removed creating client before getting GardenerClient --- hack/runtime-migrator/cmd/main.go | 16 ++++++++-------- hack/runtime-migrator/cmd/migration.go | 2 +- hack/runtime-migrator/internal/config/config.go | 16 ---------------- .../internal/runtime/migrator.go | 17 ++++++----------- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/hack/runtime-migrator/cmd/main.go b/hack/runtime-migrator/cmd/main.go index eb00d060..747eed41 100644 --- a/hack/runtime-migrator/cmd/main.go +++ b/hack/runtime-migrator/cmd/main.go @@ -29,7 +29,7 @@ import ( ) const ( - timeoutK8sOperation = 10 * time.Second + timeoutK8sOperation = 15 * time.Second expirationTime = 60 * time.Minute runtimeIDAnnotation = "kcp.provisioner.kyma-project.io/runtime-id" ) @@ -54,45 +54,45 @@ func main() { kcpClient, err := config.CreateKcpClient(&cfg) if err != nil { - slog.Error("Failed to create kcp client: %v ", slog.Any("error", err)) + slog.Error("Failed to create kcp client", slog.Any("error", err)) os.Exit(1) } gardenerShootClient, err := setupGardenerShootClient(cfg.GardenerKubeconfigPath, gardenerNamespace) if err != nil { - slog.Error("Failed to setup Gardener shoot client: %v", slog.Any("error", err)) + slog.Error("Failed to setup Gardener shoot client", slog.Any("error", err)) os.Exit(1) } auditLogConfig, err := getAuditLogConfig(kcpClient) if err != nil { - slog.Error("Failed to get audit log config: %v", slog.Any("error", err)) + slog.Error("Failed to get audit log config", slog.Any("error", err)) os.Exit(1) } converterConfig, err := getConverterConfig(kcpClient) if err != nil { - slog.Error("Failed to get converter config: %v", slog.Any("error", err)) + slog.Error("Failed to get converter config", slog.Any("error", err)) os.Exit(1) } slog.Info("Migrating runtimes") migrator, err := NewMigration(cfg, converterConfig, auditLogConfig, kubeconfigProvider, kcpClient, gardenerShootClient) if err != nil { - slog.Error("Failed to create migrator: %v", slog.Any("error", err)) + slog.Error("Failed to create migrator", slog.Any("error", err)) os.Exit(1) } slog.Info("Reading runtimeIds from input file") runtimeIds, err := getRuntimeIDsFromInputFile(cfg) if err != nil { - slog.Error("Failed to read runtime Ids from input: %v", slog.Any("error", err)) + slog.Error("Failed to read runtime Ids from input", slog.Any("error", err)) os.Exit(1) } err = migrator.Do(context.Background(), runtimeIds) if err != nil { - slog.Error(fmt.Sprintf("Failed to migrate runtimes: %v", slog.Any("error", err))) + slog.Error("Failed to migrate runtimes", slog.Any("error", err)) os.Exit(1) } } diff --git a/hack/runtime-migrator/cmd/migration.go b/hack/runtime-migrator/cmd/migration.go index 224eb12a..f5d7ea7c 100644 --- a/hack/runtime-migrator/cmd/migration.go +++ b/hack/runtime-migrator/cmd/migration.go @@ -37,7 +37,7 @@ func NewMigration(migratorConfig config2.Config, converterConfig config.Converte } return Migration{ - runtimeMigrator: runtime.NewMigrator(migratorConfig, kubeconfigProvider), + runtimeMigrator: runtime.NewMigrator(migratorConfig, kubeconfigProvider, kcpClient), runtimeVerifier: runtime.NewVerifier(converterConfig, auditLogConfig), kcpClient: kcpClient, shootClient: shootClient, diff --git a/hack/runtime-migrator/internal/config/config.go b/hack/runtime-migrator/internal/config/config.go index 451968a9..edbcdf2f 100644 --- a/hack/runtime-migrator/internal/config/config.go +++ b/hack/runtime-migrator/internal/config/config.go @@ -69,22 +69,6 @@ func addToScheme(s *runtime.Scheme) error { type GetClient = func() (client.Client, error) -func (cfg *Config) Client() (client.Client, error) { - restCfg, err := clientcmd.BuildConfigFromFlags("", cfg.KcpKubeconfigPath) - if err != nil { - return nil, fmt.Errorf("unable to fetch rest config: %w", err) - } - - scheme := runtime.NewScheme() - if err := addToScheme(scheme); err != nil { - return nil, err - } - - return client.New(restCfg, client.Options{ - Scheme: scheme, - }) -} - func CreateKcpClient(cfg *Config) (client.Client, error) { restCfg, err := clientcmd.BuildConfigFromFlags("", cfg.KcpKubeconfigPath) if err != nil { diff --git a/hack/runtime-migrator/internal/runtime/migrator.go b/hack/runtime-migrator/internal/runtime/migrator.go index d63aa723..887d6247 100644 --- a/hack/runtime-migrator/internal/runtime/migrator.go +++ b/hack/runtime-migrator/internal/runtime/migrator.go @@ -28,10 +28,11 @@ type Migrator struct { kcpClient client.Client } -func NewMigrator(cfg migrator.Config, kubeconfigProvider kubeconfig.Provider) Migrator { +func NewMigrator(cfg migrator.Config, kubeconfigProvider kubeconfig.Provider, kcpClient client.Client) Migrator { return Migrator{ cfg: cfg, kubeconfigProvider: kubeconfigProvider, + kcpClient: kcpClient, } } @@ -44,7 +45,7 @@ func (m Migrator) Do(ctx context.Context, shoot v1beta1.Shoot) (v1.Runtime, erro var oidcConfig = getOidcConfig(shoot) var licenceType = shoot.Annotations["kcp.provisioner.kyma-project.io/licence-type"] - labels, err := getAllRuntimeLabels(ctx, shoot, m.cfg.Client) + labels, err := getAllRuntimeLabels(ctx, shoot, m.kcpClient) if err != nil { return v1.Runtime{}, err } @@ -170,16 +171,10 @@ func getOidcConfig(shoot v1beta1.Shoot) v1beta1.OIDCConfig { return oidcConfig } -func getAllRuntimeLabels(ctx context.Context, shoot v1beta1.Shoot, getClient migrator.GetClient) (map[string]string, error) { +func getAllRuntimeLabels(ctx context.Context, shoot v1beta1.Shoot, kcpClient client.Client) (map[string]string, error) { enrichedRuntimeLabels := map[string]string{} var err error - // add agreed labels from the GardenerCluster CR - k8sClient, clientErr := getClient() - - if clientErr != nil { - return map[string]string{}, errors.Wrap(clientErr, fmt.Sprintf("Failed to get GardenerClient for shoot %s - %s\n", shoot.Name, clientErr)) - } gardenerCluster := v1.GardenerCluster{} kymaID, found := shoot.Annotations["kcp.provisioner.kyma-project.io/runtime-id"] @@ -188,7 +183,7 @@ func getAllRuntimeLabels(ctx context.Context, shoot v1beta1.Shoot, getClient mig } gardenerCRKey := types.NamespacedName{Name: kymaID, Namespace: "kcp-system"} - getGardenerCRerr := k8sClient.Get(ctx, gardenerCRKey, &gardenerCluster) + getGardenerCRerr := kcpClient.Get(ctx, gardenerCRKey, &gardenerCluster) if getGardenerCRerr != nil { var errMsg = fmt.Sprintf("Failed to retrieve GardenerCluster CR for shoot %s\n", shoot.Name) return map[string]string{}, errors.Wrap(getGardenerCRerr, errMsg) @@ -202,7 +197,7 @@ func getAllRuntimeLabels(ctx context.Context, shoot v1beta1.Shoot, getClient mig enrichedRuntimeLabels["kyma-project.io/region"] = gardenerCluster.Labels["kyma-project.io/region"] enrichedRuntimeLabels["kyma-project.io/shoot-name"] = gardenerCluster.Labels["kyma-project.io/shoot-name"] enrichedRuntimeLabels["operator.kyma-project.io/kyma-name"] = gardenerCluster.Labels["operator.kyma-project.io/kyma-name"] - // The runtime CR should be controlled by the KIM in dry-run mode + // The runtime CR should be controlled by the Provisioner enrichedRuntimeLabels["kyma-project.io/controlled-by-provisioner"] = "true" // add custom label for the migrator enrichedRuntimeLabels[migratorLabel] = "true"