Skip to content

Commit 6120e2c

Browse files
committed
Update kubernetes quantity parsing error message
1 parent 08ff96b commit 6120e2c

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

pkg/lib/k8s/errors.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2019 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package k8s
18+
19+
type ErrorKind int
20+
21+
const (
22+
ErrUnknown ErrorKind = iota
23+
ErrParseQuantity ErrorKind = iota
24+
)
25+
26+
var errorKinds = []string{
27+
"err_unknown",
28+
"err_parse_quantity",
29+
}
30+
31+
var _ = [1]int{}[int(ErrParseQuantity)-(len(errorKinds)-1)] // Ensure list length matches
32+
33+
func (t ErrorKind) String() string {
34+
return errorKinds[t]
35+
}
36+
37+
// MarshalText satisfies TextMarshaler
38+
func (t ErrorKind) MarshalText() ([]byte, error) {
39+
return []byte(t.String()), nil
40+
}
41+
42+
// UnmarshalText satisfies TextUnmarshaler
43+
func (t *ErrorKind) UnmarshalText(text []byte) error {
44+
enum := string(text)
45+
for i := 0; i < len(errorKinds); i++ {
46+
if enum == errorKinds[i] {
47+
*t = ErrorKind(i)
48+
return nil
49+
}
50+
}
51+
52+
*t = ErrUnknown
53+
return nil
54+
}
55+
56+
// UnmarshalBinary satisfies BinaryUnmarshaler
57+
// Needed for msgpack
58+
func (t *ErrorKind) UnmarshalBinary(data []byte) error {
59+
return t.UnmarshalText(data)
60+
}
61+
62+
// MarshalBinary satisfies BinaryMarshaler
63+
func (t ErrorKind) MarshalBinary() ([]byte, error) {
64+
return []byte(t.String()), nil
65+
}
66+
67+
type Error struct {
68+
Kind ErrorKind
69+
message string
70+
}
71+
72+
func (e Error) Error() string {
73+
return e.message
74+
}
75+
76+
func ErrorParseQuantity(qtyStr string) error {
77+
return Error{
78+
Kind: ErrParseQuantity,
79+
message: qtyStr + ": invalid kubernetes quantity, some valid examples are 1, 200m, 500Mi, 2G (see here for more information: https://docs.cortex.dev/deployments/compute)",
80+
}
81+
}

pkg/operator/api/userconfig/quantity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
kresource "k8s.io/apimachinery/pkg/api/resource"
2424

2525
"github.com/cortexlabs/cortex/pkg/lib/configreader"
26+
"github.com/cortexlabs/cortex/pkg/lib/k8s"
2627
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2728
)
2829

@@ -42,7 +43,7 @@ func QuantityParser(v *QuantityValidation) func(string) (interface{}, error) {
4243
return func(str string) (interface{}, error) {
4344
k8sQuantity, err := kresource.ParseQuantity(str)
4445
if err != nil {
45-
return Quantity{}, err
46+
return Quantity{}, k8s.ErrorParseQuantity(str)
4647
}
4748

4849
if v.GreaterThan != nil {

0 commit comments

Comments
 (0)