Skip to content

Commit

Permalink
feat(internal): Allow for specifying optional measurement units
Browse files Browse the repository at this point in the history
The value is then converted to mebibytes by dividing
the integer by 1024*1024. If something like ~3.2Mi
are specified, the value will be truncated down to 3Mi.

Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Jun 4, 2024
1 parent 8a58900 commit 8de3a7d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/compose/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func Up(ctx context.Context, opts *UpOptions, args ...string) error {
Client: opts.Client,
Env: env,
Image: service.Image,
Memory: uint(memory),
Memory: fmt.Sprintf("%d", memory),
Metro: opts.Metro,
Name: name,
ServiceGroupNameOrUUID: serviceGroup,
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type DeployOptions struct {
Jobs int `long:"jobs" short:"j" usage:"Allow N jobs at once"`
KernelDbg bool `long:"dbg" usage:"Build the debuggable (symbolic) kernel image instead of the stripped image"`
Kraftfile string `local:"true" long:"kraftfile" short:"K" usage:"Set the Kraftfile to use"`
Memory int `local:"true" long:"memory" short:"M" usage:"Specify the amount of memory to allocate (MiB)"`
Memory string `local:"true" long:"memory" short:"M" usage:"Specify the amount of memory to allocate (MiB increments)"`
Metro string `noattribute:"true"`
Name string `local:"true" long:"name" short:"n" usage:"Name of the deployment"`
NoCache bool `long:"no-cache" short:"F" usage:"Force a rebuild even if existing intermediate artifacts already exist"`
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/kraft/cloud/deploy/deployer_image_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ func (deployer *deployerImageName) Deploy(ctx context.Context, opts *DeployOptio
"deploying",
"",
func(ctx context.Context) error {

insts, groups, err = instancecreate.Create(ctx, &instancecreate.CreateOptions{
Env: opts.Env,
Features: opts.Features,
Domain: opts.Domain,
Image: deployer.imageName,
Memory: uint(opts.Memory),
Memory: opts.Memory,
Metro: opts.Metro,
Name: opts.Name,
Ports: opts.Ports,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (deployer *deployerKraftfileRuntime) Deploy(ctx context.Context, opts *Depl
Env: opts.Env,
Domain: opts.Domain,
Image: packs[0].ID(),
Memory: uint(opts.Memory),
Memory: opts.Memory,
Metro: opts.Metro,
Name: opts.Name,
Ports: opts.Ports,
Expand Down
21 changes: 18 additions & 3 deletions internal/cli/kraft/cloud/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/resource"

kraftcloud "sdk.kraft.cloud"
kcclient "sdk.kraft.cloud/client"
Expand All @@ -37,7 +38,7 @@ type CreateOptions struct {
Features []string `local:"true" long:"feature" short:"f" usage:"List of features to enable"`
Domain []string `local:"true" long:"domain" short:"d" usage:"The domain names to use for the service"`
Image string `noattribute:"true"`
Memory uint `local:"true" long:"memory" short:"M" usage:"Specify the amount of memory to allocate (MiB)"`
Memory string `local:"true" long:"memory" short:"M" usage:"Specify the amount of memory to allocate (MiB increments)"`
Metro string `noattribute:"true"`
Name string `local:"true" long:"name" short:"n" usage:"Specify the name of the instance"`
Output string `local:"true" long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"table"`
Expand Down Expand Up @@ -173,8 +174,22 @@ func Create(ctx context.Context, opts *CreateOptions, args ...string) (*kcclient
if len(args) > 0 {
req.Args = args
}
if opts.Memory > 0 {
req.MemoryMB = ptr(int(opts.Memory))
if opts.Memory != "" {
if _, err := strconv.ParseUint(opts.Memory, 10, 64); err == nil {
opts.Memory = fmt.Sprintf("%sMi", opts.Memory)
}

qty, err := resource.ParseQuantity(opts.Memory)
if err != nil {
return nil, nil, fmt.Errorf("could not parse memory quantity: %w", err)
}

if qty.Value() < 1024*1024 {
return nil, nil, fmt.Errorf("memory must be at least 1Mi")
}

// Convert to MiB
req.MemoryMB = ptr(int(qty.Value() / (1024 * 1024)))
}
if opts.Replicas > 0 {
req.Replicas = ptr(int(opts.Replicas))
Expand Down
24 changes: 21 additions & 3 deletions internal/cli/kraft/cloud/volume/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ package create
import (
"context"
"fmt"
"strconv"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/resource"

kraftcloud "sdk.kraft.cloud"
kcvolumes "sdk.kraft.cloud/volumes"
Expand All @@ -26,7 +28,7 @@ type CreateOptions struct {
Client kcvolumes.VolumesService `noattribute:"true"`
Metro string `noattribute:"true"`
Name string `local:"true" size:"name" short:"n"`
SizeMB int `local:"true" long:"size" short:"s" usage:"Size in MB"`
Size string `local:"true" long:"size" short:"s" usage:"Size (MiB increments)"`
Token string `noattribute:"true"`
}

Expand All @@ -51,7 +53,23 @@ func Create(ctx context.Context, opts *CreateOptions) (*kcvolumes.CreateResponse
)
}

createResp, err := opts.Client.WithMetro(opts.Metro).Create(ctx, opts.Name, opts.SizeMB)
if _, err := strconv.ParseUint(opts.Size, 10, 64); err == nil {
opts.Size = fmt.Sprintf("%sMi", opts.Size)
}

qty, err := resource.ParseQuantity(opts.Size)
if err != nil {
return nil, fmt.Errorf("could not parse size quantity: %w", err)
}

if qty.Value() < 1024*1024 {
return nil, fmt.Errorf("size must be at least 1Mi")
}

// Convert to MiB
sizeMB := int(qty.Value() / (1024 * 1024))

createResp, err := opts.Client.WithMetro(opts.Metro).Create(ctx, opts.Name, sizeMB)
if err != nil {
return nil, fmt.Errorf("creating volume: %w", err)
}
Expand Down Expand Up @@ -88,7 +106,7 @@ func NewCmd() *cobra.Command {
}

func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
if opts.SizeMB == 0 {
if opts.Size == "" {
return fmt.Errorf("must specify --size flag")
}

Expand Down

0 comments on commit 8de3a7d

Please sign in to comment.