@@ -13,12 +13,14 @@ import (
1313 "os"
1414 "strconv"
1515 "strings"
16+ "time"
1617
1718 "github.com/MakeNowJust/heredoc"
1819 "github.com/dustin/go-humanize"
1920 "github.com/spf13/cobra"
2021
2122 kraftcloud "sdk.kraft.cloud"
23+ ukcinstances "sdk.kraft.cloud/instances"
2224
2325 "kraftkit.sh/cmdfactory"
2426 "kraftkit.sh/config"
@@ -152,13 +154,14 @@ func importVolumeData(ctx context.Context, opts *ImportOptions) (retErr error) {
152154
153155 var authStr string
154156 var instFQDN string
157+ var instID string
155158
156159 paramodel , err = processTree (ctx , "Spawning temporary volume data import instance" ,
157160 func (ctx context.Context ) error {
158161 if authStr , err = utils .GenRandAuth (); err != nil {
159162 return fmt .Errorf ("generating random authentication string: %w" , err )
160163 }
161- _ , instFQDN , err = runVolimport (ctx , icli , opts .VolimportImage , volUUID , authStr , opts .Timeout )
164+ instID , instFQDN , err = runVolimport (ctx , icli , opts .VolimportImage , volUUID , authStr , opts .Timeout )
162165 return err
163166 },
164167 )
@@ -240,7 +243,30 @@ func importVolumeData(ctx context.Context, opts *ImportOptions) (retErr error) {
240243 Info ("Import complete" )
241244 }
242245
243- return nil
246+ // Stopping time can be anywhere between 1-1000ms, so we set a 1100ms timeout
247+ waitResp , err := cli .Instances ().WithMetro (opts .Metro ).Wait (ctx , ukcinstances .StateStopped , 1100 , instID )
248+ if err != nil {
249+ return fmt .Errorf ("waiting for volume data import instance to stop: %w" , err )
250+ }
251+
252+ if w , err := waitResp .FirstOrErr (); err == nil {
253+ if ukcinstances .State (w .State ) == ukcinstances .StateRunning {
254+ return fmt .Errorf ("volume data import instance did not stop yet: %s" , w .State )
255+ } else {
256+ // Wait a bit for the instance to be deleted
257+ // NOTE(craciunoiuc): this should never be reached, but it's a safety net
258+ time .Sleep (100 * time .Millisecond )
259+ return nil
260+ }
261+ } else {
262+ if strings .Contains (err .Error (), "No instance" ) {
263+ return nil
264+ } else if strings .Contains (err .Error (), "Operation timed out" ) {
265+ return fmt .Errorf ("timed out waiting for volume data import instance to stop: %w" , err )
266+ } else {
267+ return fmt .Errorf ("waiting for volume data import instance to stop: %w" , err )
268+ }
269+ }
244270}
245271
246272// processTree returns a TUI ProcessTree configured to run the given function
0 commit comments