Skip to content

Commit

Permalink
Merge pull request #13 from hathora/additional-validation
Browse files Browse the repository at this point in the history
Updating cpu and memory validation
  • Loading branch information
msaxon authored May 29, 2024
2 parents 53edbbc + f5dfc35 commit 70f4c5b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -6,12 +7,15 @@ github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 h1:R/ZjJpjQK
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731/go.mod h1:M9R1FoZ3y//hwwnJtO51ypFGwm8ZfpxPT/ZLtO1mcgQ=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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 @@ -24,7 +24,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
10 changes: 8 additions & 2 deletions internal/commands/altsrc/filesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,15 @@ func (fvs *fileValueSource) populateFileCache() error {

switch fvs.fileType {
case "json": // json is a subset of yaml
fvs.cache.populateYAML()
err := fvs.cache.populateYAML()
if err != nil {
return err
}
case "yaml":
fvs.cache.populateYAML()
err := fvs.cache.populateYAML()
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported file type %q", fvs.fileType)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/commands/altsrc/flaginit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ func Test_InitializeFlagSources(t *testing.T) {
},
}

cmd.Run(context.Background(), append([]string{"test"}, tt.args...))
err := cmd.Run(context.Background(), append([]string{"test"}, tt.args...))
if err != nil {
return
}

assert.Equal(t, fmt.Sprintf("%v", tt.expectValue), flagValue)
})
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 @@ -125,6 +126,12 @@ var Deployment = &cli.Command{
addlPorts := cmd.StringSlice(additionalContainerPortsFlag.Name)
envVars := cmd.StringSlice(envVarsFlag.Name)

if requestedMemory != (requestedCPU * 2048) {
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 @@ -230,13 +237,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

0 comments on commit 70f4c5b

Please sign in to comment.