Skip to content

Commit

Permalink
Migration: Forward migration errors (from Incus) (canonical#13966)
Browse files Browse the repository at this point in the history
This PR adds functionality to report source errors during copy. This
helps to solve the issue of the incorrect error being reported when a
live migration is attempted while `migration.stateful` is missing or set
to false.

(Also rectifies a few unchecked type assertions in
`client/lxd_instances.go` since the linter complained)

Closes canonical#11948.
  • Loading branch information
tomponline authored and hamistao committed Aug 30, 2024
2 parents 7d475cf + 398d6ae commit c0108aa
Showing 1 changed file with 99 additions and 18 deletions.
117 changes: 99 additions & 18 deletions client/lxd_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ func (r *ProtocolLXD) tryCreateInstance(req api.InstancesPost, urls []string, op
operation := req.Source.Operation

// Forward targetOp to remote op
chConnect := make(chan error, 1)
chWait := make(chan error, 1)

go func() {
success := false
var errors []remoteOperationResult
Expand Down Expand Up @@ -762,13 +765,35 @@ func (r *ProtocolLXD) tryCreateInstance(req api.InstancesPost, urls []string, op
break
}

if !success {
rop.err = remoteOperationError("Failed instance creation", errors)
if success {
chConnect <- nil
close(chConnect)
} else {
chConnect <- remoteOperationError("Failed instance creation", errors)
close(chConnect)

if op != nil {
_ = op.Cancel()
}
}
}()

if op != nil {
go func() {
chWait <- op.Wait()
close(chWait)
}()
}

go func() {
var err error

select {
case err = <-chConnect:
case err = <-chWait:
}

rop.err = err
close(rop.chDone)
}()

Expand Down Expand Up @@ -953,7 +978,12 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,

targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

targetSecrets[k] = vStr
}

// Prepare the source request
Expand Down Expand Up @@ -981,7 +1011,12 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,

sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

sourceSecrets[k] = vStr
}

// Relay mode migration
Expand All @@ -1001,7 +1036,12 @@ func (r *ProtocolLXD) CopyInstance(source InstanceServer, instance api.Instance,
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

targetSecrets[k] = vStr
}

// Launch the relay
Expand Down Expand Up @@ -1243,9 +1283,16 @@ func (r *ProtocolLXD) ExecInstance(instanceName string, exec api.InstanceExecPos

value, ok := opAPI.Metadata["fds"]
if ok {
values := value.(map[string]any)
for k, v := range values {
fds[k] = v.(string)
values, ok := value.(map[string]any)
if ok {
for k, v := range values {
vStr, ok := v.(string)
if !ok {
continue
}

fds[k] = vStr
}
}
}

Expand All @@ -1260,7 +1307,12 @@ func (r *ProtocolLXD) ExecInstance(instanceName string, exec api.InstanceExecPos
outputs, ok := opAPI.Metadata["output"].(map[string]any)
if ok {
for k, v := range outputs {
outputFiles[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

outputFiles[k] = vStr
}
}

Expand Down Expand Up @@ -1992,7 +2044,12 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s

targetSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
targetSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

targetSecrets[k] = vStr
}

// Prepare the source request
Expand Down Expand Up @@ -2020,7 +2077,12 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s

sourceSecrets := map[string]string{}
for k, v := range opAPI.Metadata {
sourceSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

sourceSecrets[k] = vStr
}

// Relay mode migration
Expand All @@ -2040,7 +2102,12 @@ func (r *ProtocolLXD) CopyInstanceSnapshot(source InstanceServer, instanceName s
// Extract the websockets
targetSecrets := map[string]string{}
for k, v := range targetOpAPI.Metadata {
targetSecrets[k] = v.(string)
vStr, ok := v.(string)
if !ok {
continue
}

targetSecrets[k] = vStr
}

// Launch the relay
Expand Down Expand Up @@ -2596,9 +2663,16 @@ func (r *ProtocolLXD) ConsoleInstance(instanceName string, console api.InstanceC

value, ok := opAPI.Metadata["fds"]
if ok {
values := value.(map[string]any)
for k, v := range values {
fds[k] = v.(string)
values, ok := value.(map[string]any)
if ok {
for k, v := range values {
vStr, ok := v.(string)
if !ok {
continue
}

fds[k] = vStr
}
}
}

Expand Down Expand Up @@ -2688,9 +2762,16 @@ func (r *ProtocolLXD) ConsoleInstanceDynamic(instanceName string, console api.In

value, ok := opAPI.Metadata["fds"]
if ok {
values := value.(map[string]any)
for k, v := range values {
fds[k] = v.(string)
values, ok := value.(map[string]any)
if ok {
for k, v := range values {
vStr, ok := v.(string)
if !ok {
continue
}

fds[k] = vStr
}
}
}

Expand Down

0 comments on commit c0108aa

Please sign in to comment.