diff --git a/docs/deployments/apis.md b/docs/deployments/apis.md index 7c3f4b6ddc..47b90ef8e5 100644 --- a/docs/deployments/apis.md +++ b/docs/deployments/apis.md @@ -18,8 +18,8 @@ Serve models at scale. max_replicas: # maximum number of replicas (default: 100) init_replicas: # initial number of replicas (default: ) target_cpu_utilization: # CPU utilization threshold (as a percentage) to trigger scaling (default: 80) - cpu: # CPU request per replica (default: 200m) - gpu: # gpu request per replica (default: 0) + cpu: # CPU request per replica (default: 200m) + gpu: # gpu request per replica (default: 0) mem: # memory request per replica (default: Null) ``` diff --git a/pkg/lib/cast/interface.go b/pkg/lib/cast/interface.go index a38c27e2e3..4d981a8770 100644 --- a/pkg/lib/cast/interface.go +++ b/pkg/lib/cast/interface.go @@ -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 } diff --git a/pkg/lib/configreader/string.go b/pkg/lib/configreader/string.go index 003d15a332..afb9b01729 100644 --- a/pkg/lib/configreader/string.go +++ b/pkg/lib/configreader/string.go @@ -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" ) @@ -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) @@ -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) } diff --git a/pkg/lib/configreader/string_ptr.go b/pkg/lib/configreader/string_ptr.go index a18ce2f553..46088b1d57 100644 --- a/pkg/lib/configreader/string_ptr.go +++ b/pkg/lib/configreader/string_ptr.go @@ -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 { @@ -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) @@ -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, } @@ -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) } diff --git a/pkg/operator/api/userconfig/compute.go b/pkg/operator/api/userconfig/compute.go index d9fad95ac9..980e7c1ac8 100644 --- a/pkg/operator/api/userconfig/compute.go +++ b/pkg/operator/api/userconfig/compute.go @@ -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")),