Skip to content

Commit ef63101

Browse files
authored
Support numeric CPU values (#413)
1 parent bfd57c6 commit ef63101

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

docs/deployments/apis.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Serve models at scale.
1818
max_replicas: <int> # maximum number of replicas (default: 100)
1919
init_replicas: <int> # initial number of replicas (default: <min_replicas>)
2020
target_cpu_utilization: <int> # CPU utilization threshold (as a percentage) to trigger scaling (default: 80)
21-
cpu: <string> # CPU request per replica (default: 200m)
22-
gpu: <string> # gpu request per replica (default: 0)
21+
cpu: <string | int | float> # CPU request per replica (default: 200m)
22+
gpu: <int> # gpu request per replica (default: 0)
2323
mem: <string> # memory request per replica (default: Null)
2424
```
2525

pkg/lib/cast/interface.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -733,30 +733,21 @@ func IsFloatType(in interface{}) bool {
733733
return false
734734
}
735735

736-
func IsFloatOrIntType(in interface{}) bool {
736+
func IsNumericType(in interface{}) bool {
737737
return IsIntType(in) || IsFloatType(in)
738738
}
739739

740740
func IsScalarType(in interface{}) bool {
741-
switch in.(type) {
742-
case int8:
743-
return true
744-
case int16:
745-
return true
746-
case int32:
747-
return true
748-
case int64:
749-
return true
750-
case int:
751-
return true
752-
case float32:
753-
return true
754-
case float64:
741+
if IsNumericType(in) {
755742
return true
743+
}
744+
745+
switch in.(type) {
756746
case string:
757747
return true
758748
case bool:
759749
return true
760750
}
751+
761752
return false
762753
}

pkg/lib/configreader/string.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"io/ioutil"
2222
"strings"
2323

24+
"github.com/cortexlabs/cortex/pkg/lib/cast"
2425
"github.com/cortexlabs/cortex/pkg/lib/errors"
2526
"github.com/cortexlabs/cortex/pkg/lib/regex"
2627
"github.com/cortexlabs/cortex/pkg/lib/slices"
28+
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2729
"github.com/cortexlabs/cortex/pkg/lib/urls"
2830
)
2931

@@ -38,6 +40,8 @@ type StringValidation struct {
3840
AlphaNumericDashUnderscore bool
3941
DNS1035 bool
4042
DNS1123 bool
43+
CastNumeric bool
44+
CastScalar bool
4145
AllowCortexResources bool
4246
RequireCortexResources bool
4347
Validator func(string) (string, error)
@@ -53,7 +57,19 @@ func String(inter interface{}, v *StringValidation) (string, error) {
5357
}
5458
casted, castOk := inter.(string)
5559
if !castOk {
56-
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString)
60+
if v.CastScalar {
61+
if !cast.IsScalarType(inter) {
62+
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat, PrimTypeBool)
63+
}
64+
casted = s.ObjFlatNoQuotes(inter)
65+
} else if v.CastNumeric {
66+
if !cast.IsNumericType(inter) {
67+
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat)
68+
}
69+
casted = s.ObjFlatNoQuotes(inter)
70+
} else {
71+
return "", ErrorInvalidPrimitiveType(inter, PrimTypeString)
72+
}
5773
}
5874
return ValidateString(casted, v)
5975
}

pkg/lib/configreader/string_ptr.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package configreader
1919
import (
2020
"io/ioutil"
2121

22+
"github.com/cortexlabs/cortex/pkg/lib/cast"
2223
"github.com/cortexlabs/cortex/pkg/lib/errors"
24+
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2325
)
2426

2527
type StringPtrValidation struct {
@@ -33,6 +35,8 @@ type StringPtrValidation struct {
3335
AlphaNumericDashUnderscore bool
3436
DNS1035 bool
3537
DNS1123 bool
38+
CastScalar bool
39+
CastNumeric bool
3640
AllowCortexResources bool
3741
RequireCortexResources bool
3842
Validator func(string) (string, error)
@@ -47,6 +51,8 @@ func makeStringValValidation(v *StringPtrValidation) *StringValidation {
4751
AlphaNumericDashUnderscore: v.AlphaNumericDashUnderscore,
4852
DNS1035: v.DNS1035,
4953
DNS1123: v.DNS1123,
54+
CastScalar: v.CastScalar,
55+
CastNumeric: v.CastNumeric,
5056
AllowCortexResources: v.AllowCortexResources,
5157
RequireCortexResources: v.RequireCortexResources,
5258
}
@@ -58,8 +64,21 @@ func StringPtr(inter interface{}, v *StringPtrValidation) (*string, error) {
5864
}
5965
casted, castOk := inter.(string)
6066
if !castOk {
61-
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString)
67+
if v.CastScalar {
68+
if !cast.IsScalarType(inter) {
69+
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat, PrimTypeBool)
70+
}
71+
casted = s.ObjFlatNoQuotes(inter)
72+
} else if v.CastNumeric {
73+
if !cast.IsNumericType(inter) {
74+
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString, PrimTypeInt, PrimTypeFloat)
75+
}
76+
casted = s.ObjFlatNoQuotes(inter)
77+
} else {
78+
return nil, ErrorInvalidPrimitiveType(inter, PrimTypeString)
79+
}
6280
}
81+
6382
return ValidateStringPtrProvided(&casted, v)
6483
}
6584

pkg/operator/api/userconfig/compute.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ var apiComputeFieldValidation = &cr.StructFieldValidation{
8282
{
8383
StructField: "CPU",
8484
StringValidation: &cr.StringValidation{
85-
Default: "200m",
85+
Default: "200m",
86+
CastNumeric: true,
8687
},
8788
Parser: QuantityParser(&QuantityValidation{
8889
GreaterThan: k8sQuantityPtr(kresource.MustParse("0")),

0 commit comments

Comments
 (0)