Skip to content

Commit

Permalink
Removed creating client before getting GardenerClient
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Nov 23, 2024
1 parent 476e53b commit 489b630
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
16 changes: 8 additions & 8 deletions hack/runtime-migrator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion hack/runtime-migrator/cmd/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 0 additions & 16 deletions hack/runtime-migrator/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 6 additions & 11 deletions hack/runtime-migrator/internal/runtime/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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"]
Expand All @@ -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)
Expand All @@ -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"
Expand Down

0 comments on commit 489b630

Please sign in to comment.