Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating cpu and memory validation #13

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2/go.mod h1:Q79oyIY/z4jtzIrKEK6MUeWC7/szGr46x4QdOaOAIWc=
github.com/urfave/cli/v3 v3.0.0-alpha9 h1:P0RMy5fQm1AslQS+XCmy9UknDXctOmG/q/FZkUFnJSo=
github.com/urfave/cli/v3 v3.0.0-alpha9/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
Expand Down
3 changes: 2 additions & 1 deletion internal/archive/targz.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func getIgnoreMatchers(srcFolder string, filepaths ...string) ([]gitignore.Ignor
for _, path := range filepaths {
matcher, err := gitignore.NewGitIgnore(filepath.Join(srcFolder, path), ".")
if err != nil {
return nil, err
zap.L().Debug("Did not file a " + path + " file. Skipping.")
continue
}

matchers = append(matchers, matcher)
Expand Down
27 changes: 27 additions & 0 deletions internal/commands/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"slices"
"strconv"
"strings"
)

Expand All @@ -21,3 +22,29 @@ func requireIntInRange(value int64, lower int64, upper int64, flagName string) e

return nil
}

func requireFloatInRange(value float64, lower float64, upper float64, flagName string) error {
if value < lower || value > upper {
return fmt.Errorf("flag %s must be between %s and %s",
flagName,
strconv.FormatFloat(lower, 'f', -1, 64),
strconv.FormatFloat(upper, 'f', -1, 64))
}

return nil
}

func requireMaxDecimals(value float64, max int, flagName string) error {
var decimals int
stringValue := strconv.FormatFloat(value, 'f', -1, 64)
index := strings.IndexByte(stringValue, '.')
if index > -1 {
decimals = len(stringValue) - index - 1
}

if decimals > max {
return fmt.Errorf("flag %s must have a maximum of %d decimal point(s)", flagName, max)
}

return nil
}
22 changes: 22 additions & 0 deletions internal/commands/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strconv"

"github.com/hathora/ci/internal/sdk"
"github.com/hathora/ci/internal/sdk/models/shared"
Expand Down Expand Up @@ -122,6 +123,12 @@ var Deployment = &cli.Command{
addlPorts := cmd.StringSlice(additionalContainerPortsFlag.Name)
envVars := cmd.StringSlice(envVarsFlag.Name)

if requestedCPU != (requestedMemory / 2048) {
msaxon marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("invalid memory: %s and cpu: %s requested-memory-mb must be a 2048:1 ratio to requested-cpu",
strconv.FormatFloat(requestedMemory, 'f', -1, 64),
strconv.FormatFloat(requestedCPU, 'f', -1, 64))
}

additionalContainerPorts, err := parseContainerPorts(addlPorts)
if err != nil {
return fmt.Errorf("invalid additional container ports: %w", err)
Expand Down Expand Up @@ -227,13 +234,28 @@ var (
Sources: cli.EnvVars(deploymentEnvVar("REQUESTED_MEMORY_MB")),
Usage: "the amount of memory allocated to your process in MB",
Required: true,
Action: func(ctx context.Context, cmd *cli.Command, v float64) error {
return requireFloatInRange(v, 1024, 8192, "requested-memory-mb")
},
}

requestedCPUFlag = &cli.FloatFlag{
Name: "requested-cpu",
Sources: cli.EnvVars(deploymentEnvVar("REQUESTED_CPU")),
Usage: "the number of cores allocated to your process",
Required: true,
Action: func(ctx context.Context, cmd *cli.Command, v float64) error {
rangeErr := requireFloatInRange(v, 0.5, 4, "requested-cpu")
if rangeErr != nil {
return rangeErr
}
decimalErr := requireMaxDecimals(v, 1, "requested-cpu")
if decimalErr != nil {
return decimalErr
}

return nil
},
}
)

Expand Down