Skip to content

Commit a712745

Browse files
committed
resolve review
1 parent 2b1a268 commit a712745

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

pkg/controller/worker/machines.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
shouldMigrateMachineAnnotation = "stackit.cloud/machine-should-be-migrated"
4343
migratedMachineAnnotation = "stackit.cloud/migrated-machine"
4444
workerMigratedAnnotation = "stackit.cloud/machine-controller-manager-migrated"
45+
46+
stackitProviderID = "stackit://"
4547
)
4648

4749
// MachineClassKind yields the name of the machine class kind used by OpenStack provider.
@@ -432,12 +434,6 @@ func (w *workerDelegate) migrateMachines(ctx context.Context) error {
432434
allMachines machinev1alpha1.MachineList
433435
migrateMachines []machinev1alpha1.Machine
434436
)
435-
var (
436-
openStackProviderIDPattern = regexp.MustCompile(`^openstack:///[^/]+/([^/]+)$`)
437-
stackitProviderIDPattern = regexp.MustCompile(`^stackit://[^/]+/([^/]+)$`)
438-
)
439-
440-
const stackitProviderID = "stackit://"
441437

442438
err := w.seedClient.List(ctx, &allMachines, &client.ListOptions{Namespace: w.worker.Namespace})
443439
if err != nil {
@@ -470,20 +466,10 @@ func (w *workerDelegate) migrateMachines(ctx context.Context) error {
470466
}
471467

472468
if m.Spec.ProviderID != "" {
473-
matches := openStackProviderIDPattern.FindStringSubmatch(m.Spec.ProviderID)
474-
if len(matches) != 2 {
475-
// A retry can resume, after the provider ID was already converted but
476-
// before updating the server labels completed.
477-
matches = stackitProviderIDPattern.FindStringSubmatch(m.Spec.ProviderID)
478-
}
479-
if len(matches) != 2 {
480-
return fmt.Errorf(
481-
"migrateMachines: malformed machine provider ID: %s",
482-
m.Spec.ProviderID,
483-
)
469+
serverID, err := serverIDFromProviderID(m.Spec.ProviderID)
470+
if err != nil {
471+
return fmt.Errorf("migrateMachines: %w", err)
484472
}
485-
// capture server ID from provider ID
486-
serverID := matches[1]
487473

488474
patch := client.MergeFrom(m.DeepCopy())
489475
m.Spec.ProviderID = fmt.Sprintf("%s%s/%s", stackitProviderID, w.iaaSClient.ProjectID(), serverID)
@@ -494,7 +480,7 @@ func (w *workerDelegate) migrateMachines(ctx context.Context) error {
494480

495481
_, err = w.iaaSClient.UpdateServer(ctx, serverID, iaas.UpdateServerPayload{
496482
Labels: map[string]any{
497-
// // TODO refine labels
483+
// TODO refine labels
498484
"mcm.gardener.cloud/machine": m.Name,
499485
"mcm.gardener.cloud/machineclass": m.Spec.Class.Name,
500486
"mcm.gardener.cloud/role": "node",
@@ -527,3 +513,19 @@ func (w *workerDelegate) markWorkerAsMigrated(ctx context.Context) error {
527513

528514
return w.seedClient.Patch(ctx, w.worker, patchWorker)
529515
}
516+
517+
func serverIDFromProviderID(providerID string) (string, error) {
518+
patterns := []*regexp.Regexp{
519+
regexp.MustCompile(`^openstack:///[^/]+/([^/]+)$`),
520+
regexp.MustCompile(`^stackit://[^/]+/([^/]+)$`),
521+
}
522+
523+
for _, pattern := range patterns {
524+
matches := pattern.FindStringSubmatch(providerID)
525+
if len(matches) == 2 {
526+
return matches[1], nil
527+
}
528+
}
529+
530+
return "", fmt.Errorf("malformed machine provider ID: %s", providerID)
531+
}

0 commit comments

Comments
 (0)