Skip to content

Commit f4b2e9a

Browse files
committed
fix default volumes to start at 1 and add test
1 parent e50bde5 commit f4b2e9a

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

internal/namespaces/instance/v1/custom_server_create.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
291291
return nil, err
292292
}
293293

294-
if serverType != nil {
295-
volumes = addDefaultVolumes(serverType, volumes)
296-
}
297-
298294
// Validate root volume type and size.
299295
if getImageResponse != nil {
300296
if err := validateRootVolume(getImageResponse.Image.RootVolume.Size, volumes["0"]); err != nil {
@@ -317,6 +313,11 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
317313
serverReq.Volumes = sanitizeVolumeMap(serverReq.Name, volumes)
318314
}
319315

316+
// Add default volumes to server, ex: scratch storage for GPU servers
317+
if serverType != nil {
318+
serverReq.Volumes = addDefaultVolumes(serverType, serverReq.Volumes)
319+
}
320+
320321
//
321322
// BootType.
322323
//
@@ -440,6 +441,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
440441
func addDefaultVolumes(serverType *instance.ServerType, volumes map[string]*instance.VolumeServerTemplate) map[string]*instance.VolumeServerTemplate {
441442
needScratch := false
442443
hasScratch := false
444+
defaultVolumes := []*instance.VolumeServerTemplate(nil)
443445
if serverType.ScratchStorageMaxSize != nil && *serverType.ScratchStorageMaxSize > 0 {
444446
needScratch = true
445447
}
@@ -450,11 +452,26 @@ func addDefaultVolumes(serverType *instance.ServerType, volumes map[string]*inst
450452
}
451453

452454
if needScratch && !hasScratch {
453-
volumeKey := strconv.Itoa(len(volumes))
454-
volumes[volumeKey] = &instance.VolumeServerTemplate{
455+
if volumes == nil {
456+
volumes = make(map[string]*instance.VolumeServerTemplate)
457+
}
458+
defaultVolumes = append(defaultVolumes, &instance.VolumeServerTemplate{
455459
Name: scw.StringPtr("default-cli-scratch-volume"),
456460
Size: serverType.ScratchStorageMaxSize,
457461
VolumeType: instance.VolumeVolumeTypeScratch,
462+
})
463+
}
464+
465+
if defaultVolumes != nil {
466+
maxKey := 1
467+
for k, _ := range volumes {
468+
key, err := strconv.Atoi(k)
469+
if err == nil && key > maxKey {
470+
maxKey = key
471+
}
472+
}
473+
for i, vol := range defaultVolumes {
474+
volumes[strconv.Itoa(maxKey+i)] = vol
458475
}
459476
}
460477

internal/namespaces/instance/v1/custom_server_create_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package instance
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/alecthomas/assert"
@@ -485,3 +486,29 @@ func Test_CreateServerErrors(t *testing.T) {
485486
),
486487
}))
487488
}
489+
490+
func Test_CreateServerScratchStorage(t *testing.T) {
491+
t.Run("Default scratch storage", core.Test(&core.TestConfig{
492+
Commands: GetCommands(),
493+
Cmd: "scw instance server create type=H100-1-80G image=ubuntu_jammy_gpu_os_12 --debug",
494+
Check: core.TestCheckCombine(
495+
core.TestCheckGolden(),
496+
func(t *testing.T, ctx *core.CheckFuncCtx) {
497+
fmt.Println(ctx.LogBuffer)
498+
},
499+
core.TestCheckExitCode(0),
500+
func(t *testing.T, ctx *core.CheckFuncCtx) {
501+
server, isServer := ctx.Result.(*instance.Server)
502+
if !isServer {
503+
t.Fatalf("Result is not a server")
504+
}
505+
additionalVolume, exist := server.Volumes["1"]
506+
if !exist {
507+
t.Fatalf("Expected an additional scratch volume, found none")
508+
}
509+
assert.Equal(t, additionalVolume.VolumeType, instance.VolumeServerVolumeTypeBSSD)
510+
},
511+
),
512+
DisableParallel: true,
513+
}))
514+
}

0 commit comments

Comments
 (0)