@@ -313,6 +313,11 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
313313 serverReq .Volumes = sanitizeVolumeMap (serverReq .Name , volumes )
314314 }
315315
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+
316321 //
317322 // BootType.
318323 //
@@ -433,6 +438,46 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
433438 return server , nil
434439}
435440
441+ func addDefaultVolumes (serverType * instance.ServerType , volumes map [string ]* instance.VolumeServerTemplate ) map [string ]* instance.VolumeServerTemplate {
442+ needScratch := false
443+ hasScratch := false
444+ defaultVolumes := []* instance.VolumeServerTemplate (nil )
445+ if serverType .ScratchStorageMaxSize != nil && * serverType .ScratchStorageMaxSize > 0 {
446+ needScratch = true
447+ }
448+ for _ , volume := range volumes {
449+ if volume .VolumeType == instance .VolumeVolumeTypeScratch {
450+ hasScratch = true
451+ }
452+ }
453+
454+ if needScratch && ! hasScratch {
455+ defaultVolumes = append (defaultVolumes , & instance.VolumeServerTemplate {
456+ Name : scw .StringPtr ("default-cli-scratch-volume" ),
457+ Size : serverType .ScratchStorageMaxSize ,
458+ VolumeType : instance .VolumeVolumeTypeScratch ,
459+ })
460+ }
461+
462+ if defaultVolumes != nil {
463+ if volumes == nil {
464+ volumes = make (map [string ]* instance.VolumeServerTemplate )
465+ }
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
475+ }
476+ }
477+
478+ return volumes
479+ }
480+
436481// buildVolumes creates the initial volume map.
437482// It is not the definitive one, it will be mutated all along the process.
438483func buildVolumes (api * instance.API , zone scw.Zone , serverName , rootVolume string , additionalVolumes []string ) (map [string ]* instance.VolumeServerTemplate , error ) {
@@ -473,7 +518,7 @@ func buildVolumes(api *instance.API, zone scw.Zone, serverName, rootVolume strin
473518// Volumes definition must be through multiple arguments (eg: volumes.0="l:20GB" volumes.1="b:100GB")
474519//
475520// A valid volume format is either
476- // - a "creation" format: ^((local|l|block|b):)?\d+GB?$ (size is handled by go-humanize, so other sizes are supported)
521+ // - a "creation" format: ^((local|l|block|b|scratch|s ):)?\d+GB?$ (size is handled by go-humanize, so other sizes are supported)
477522// - a "creation" format with a snapshot id: l:<uuid> b:<uuid>
478523// - a UUID format
479524func buildVolumeTemplate (api * instance.API , zone scw.Zone , flagV string ) (* instance.VolumeServerTemplate , error ) {
@@ -488,6 +533,8 @@ func buildVolumeTemplate(api *instance.API, zone scw.Zone, flagV string) (*insta
488533 vt .VolumeType = instance .VolumeVolumeTypeLSSD
489534 case "b" , "block" :
490535 vt .VolumeType = instance .VolumeVolumeTypeBSSD
536+ case "s" , "scratch" :
537+ vt .VolumeType = instance .VolumeVolumeTypeScratch
491538 default :
492539 return nil , fmt .Errorf ("invalid volume type %s in %s volume" , parts [0 ], flagV )
493540 }
0 commit comments