Skip to content

Support numeric CPU values #413

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

Merged
merged 2 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions docs/deployments/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Serve models at scale.
max_replicas: <int> # maximum number of replicas (default: 100)
init_replicas: <int> # initial number of replicas (default: <min_replicas>)
target_cpu_utilization: <int> # CPU utilization threshold (as a percentage) to trigger scaling (default: 80)
cpu: <string> # CPU request per replica (default: 200m)
gpu: <string> # gpu request per replica (default: 0)
cpu: <string | int | float> # CPU request per replica (default: 200m)
gpu: <int> # gpu request per replica (default: 0)
mem: <string> # memory request per replica (default: Null)
```

Expand Down
21 changes: 6 additions & 15 deletions pkg/lib/cast/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,30 +733,21 @@ func IsFloatType(in interface{}) bool {
return false
}

func IsFloatOrIntType(in interface{}) bool {
func IsNumericType(in interface{}) bool {
return IsIntType(in) || IsFloatType(in)
}

func IsScalarType(in interface{}) bool {
switch in.(type) {
case int8:
return true
case int16:
return true
case int32:
return true
case int64:
return true
case int:
return true
case float32:
return true
case float64:
if IsNumericType(in) {
return true
}

switch in.(type) {
case string:
return true
case bool:
return true
}

return false
}
18 changes: 17 additions & 1 deletion pkg/lib/configreader/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"io/ioutil"
"strings"

"github.com/cortexlabs/cortex/pkg/lib/cast"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/regex"
"github.com/cortexlabs/cortex/pkg/lib/slices"
s "github.com/cortexlabs/cortex/pkg/lib/strings"
"github.com/cortexlabs/cortex/pkg/lib/urls"
)

Expand All @@ -38,6 +40,8 @@ type StringValidation struct {
AlphaNumericDashUnderscore bool
DNS1035 bool
DNS1123 bool
CastNumeric bool
CastScalar bool
AllowCortexResources bool
RequireCortexResources bool
Validator func(string) (string, error)
Expand All @@ -53,7 +57,19 @@ func String(inter interface{}, v *StringValidation) (string, error) {
}
casted, castOk := inter.(string)
if !castOk {
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString)
if v.CastScalar {
if !cast.IsScalarType(inter) {
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat, PrimTypeBool)
}
casted = s.ObjFlatNoQuotes(inter)
} else if v.CastNumeric {
if !cast.IsNumericType(inter) {
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat)
}
casted = s.ObjFlatNoQuotes(inter)
} else {
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString)
}
}
return ValidateString(casted, v)
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/lib/configreader/string_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package configreader
import (
"io/ioutil"

"github.com/cortexlabs/cortex/pkg/lib/cast"
"github.com/cortexlabs/cortex/pkg/lib/errors"
s "github.com/cortexlabs/cortex/pkg/lib/strings"
)

type StringPtrValidation struct {
Expand All @@ -33,6 +35,8 @@ type StringPtrValidation struct {
AlphaNumericDashUnderscore bool
DNS1035 bool
DNS1123 bool
CastScalar bool
CastNumeric bool
AllowCortexResources bool
RequireCortexResources bool
Validator func(string) (string, error)
Expand All @@ -47,6 +51,8 @@ func makeStringValValidation(v *StringPtrValidation) *StringValidation {
AlphaNumericDashUnderscore: v.AlphaNumericDashUnderscore,
DNS1035: v.DNS1035,
DNS1123: v.DNS1123,
CastScalar: v.CastScalar,
CastNumeric: v.CastNumeric,
AllowCortexResources: v.AllowCortexResources,
RequireCortexResources: v.RequireCortexResources,
}
Expand All @@ -58,8 +64,21 @@ func StringPtr(inter interface{}, v *StringPtrValidation) (*string, error) {
}
casted, castOk := inter.(string)
if !castOk {
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString)
if v.CastScalar {
if !cast.IsScalarType(inter) {
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat, PrimTypeBool)
}
casted = s.ObjFlatNoQuotes(inter)
} else if v.CastNumeric {
if !cast.IsNumericType(inter) {
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat)
}
casted = s.ObjFlatNoQuotes(inter)
} else {
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString)
}
}

return ValidateStringPtrProvided(&casted, v)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/operator/api/userconfig/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var apiComputeFieldValidation = &cr.StructFieldValidation{
{
StructField: "CPU",
StringValidation: &cr.StringValidation{
Default: "200m",
Default: "200m",
CastNumeric: true,
},
Parser: QuantityParser(&QuantityValidation{
GreaterThan: k8sQuantityPtr(kresource.MustParse("0")),
Expand Down