Skip to content

incusd/instances_post: Fix cluster internal migrations #1427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 47 additions & 31 deletions cmd/incusd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ func createFromMigration(ctx context.Context, s *state.State, r *http.Request, p
StoragePool: storagePool,
}

// Check if the pool is changing at all.
if r != nil && isClusterNotification(r) && inst != nil {
_, currentPool, _ := internalInstance.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
if currentPool["pool"] == storagePool {
migrationArgs.StoragePool = ""
}
}

sink, err := newMigrationSink(&migrationArgs)
if err != nil {
return response.InternalError(err)
Expand All @@ -391,45 +399,53 @@ func createFromMigration(ctx context.Context, s *state.State, r *http.Request, p

instOp.Done(nil) // Complete operation that was created earlier, to release lock.

// Update root device for instance.
err = s.DB.Cluster.Transaction(context.Background(), func(ctx context.Context, tx *db.ClusterTx) error {
devs := inst.LocalDevices().CloneNative()
rootDevKey, _, err := internalInstance.GetRootDiskDevice(devs)
if err != nil {
if !errors.Is(err, internalInstance.ErrNoRootDisk) {
return err
}
if migrationArgs.StoragePool != "" {
// Update root device for the instance.
err = s.DB.Cluster.Transaction(context.Background(), func(ctx context.Context, tx *db.ClusterTx) error {
devs := inst.LocalDevices().CloneNative()
rootDevKey, _, err := internalInstance.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
if err != nil {
if !errors.Is(err, internalInstance.ErrNoRootDisk) {
return err
}

rootDev := map[string]string{}
rootDev["type"] = "disk"
rootDev["path"] = "/"
rootDev["pool"] = storagePool
rootDev := map[string]string{}
rootDev["type"] = "disk"
rootDev["path"] = "/"
rootDev["pool"] = storagePool

devs["root"] = rootDev
} else {
// Apply the override.
devs[rootDevKey]["pool"] = storagePool
}
devs["root"] = rootDev
} else {
// Copy the device if not a local device.
_, ok := devs[rootDevKey]
if !ok {
devs[rootDevKey] = inst.ExpandedDevices().CloneNative()[rootDevKey]
}

devices, err := dbCluster.APIToDevices(devs)
if err != nil {
return err
}
// Apply the override.
devs[rootDevKey]["pool"] = storagePool
}

id, err := dbCluster.GetInstanceID(ctx, tx.Tx(), inst.Project().Name, inst.Name())
if err != nil {
return fmt.Errorf("Failed to get ID of moved instance: %w", err)
}
devices, err := dbCluster.APIToDevices(devs)
if err != nil {
return err
}

err = dbCluster.UpdateInstanceDevices(ctx, tx.Tx(), int64(id), devices)
id, err := dbCluster.GetInstanceID(ctx, tx.Tx(), inst.Project().Name, inst.Name())
if err != nil {
return fmt.Errorf("Failed to get ID of moved instance: %w", err)
}

err = dbCluster.UpdateInstanceDevices(ctx, tx.Tx(), int64(id), devices)
if err != nil {
return err
}

return nil
})
if err != nil {
return err
}

return nil
})
if err != nil {
return err
}

runRevert.Success()
Expand Down