Merge pull request #26907 from smarterclayton/tolerate_quantity

Automatic merge from submit-queue

Resource quantity must support leading and trailing whitespace in JSON for back-compat

For backwards compatibility reasons, we must continue to support leading or trailing whitespace on Quantity values when deserialized from JSON.  We must also support numbers serialized into yaml (`cpu: 1`) and JSON (`"cpu": 1`)

Fixes #26898
This commit is contained in:
k8s-merge-robot
2016-06-08 16:16:41 -07:00
2 changed files with 39 additions and 8 deletions

View File

@@ -260,7 +260,6 @@ Suffix:
switch str[i] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
default:
pos = i
break Suffix
}
}
@@ -620,6 +619,7 @@ func (q Quantity) MarshalJSON() ([]byte, error) {
}
// UnmarshalJSON implements the json.Unmarshaller interface.
// TODO: Remove support for leading/trailing whitespace
func (q *Quantity) UnmarshalJSON(value []byte) error {
l := len(value)
if l == 4 && bytes.Equal(value, []byte("null")) {
@@ -627,14 +627,11 @@ func (q *Quantity) UnmarshalJSON(value []byte) error {
q.i = int64Amount{}
return nil
}
if l < 2 {
return ErrFormatWrong
}
if value[0] == '"' && value[l-1] == '"' {
if l >= 2 && value[0] == '"' && value[l-1] == '"' {
value = value[1 : l-1]
}
parsed, err := ParseQuantity(string(value))
parsed, err := ParseQuantity(strings.TrimSpace(string(value)))
if err != nil {
return err
}