Commit 9014bd9c authored by Oleg Sidorov's avatar Oleg Sidorov

chartutil.ReadValues is forced to unmarshal numbers into json.Number

This change is an attempt to address the common problem of json number
unmarshalling where any number is converted into a float64 and
represented in a scientific notation on a marshall call. This behavior
breaks things like: chart versions and image tags if not converted to
yaml strings explicitly.

An example of this behavior: k8s failure to fetch an image tagged with a
big number like: $IMAGE:20190612073634 after a few steps of yaml
re-rendering turns into: $IMAGE:2.0190612073634e+13.

Example issue: https://github.com/helm/helm/issues/1707

This commit forces yaml parser to use JSON modifiers and explicitly
enables interface{} unmarshalling instead of float64. The change
introduced might be breaking so should be processed with an extra care.

Due to the fact helm mostly dals with human-produced data (charts), we
have a decent level of confidence this change looses no functionality
helm users rely upon (the scientific notation).

Relevant doc: https://golang.org/pkg/encoding/json/#Decoder.UseNumberSigned-off-by: 's avatarOleg Sidorov <oleg.sidorov@booking.com>
parent 4cbbd104
...@@ -15,6 +15,7 @@ limitations under the License. ...@@ -15,6 +15,7 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
...@@ -302,6 +303,10 @@ func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Confi ...@@ -302,6 +303,10 @@ func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Confi
} }
switch pv.(type) { switch pv.(type) {
case json.Number:
if s := pv.(json.Number).String(); s != vv {
t.Errorf("Failed to match imported number value %v with expected %v", s, vv)
}
case float64: case float64:
s := strconv.FormatFloat(pv.(float64), 'f', -1, 64) s := strconv.FormatFloat(pv.(float64), 'f', -1, 64)
if s != vv { if s != vv {
......
...@@ -10,3 +10,4 @@ water: ...@@ -10,3 +10,4 @@ water:
water: water:
where: "everywhere" where: "everywhere"
nor: "any drop to drink" nor: "any drop to drink"
temperature: 1234567890
...@@ -17,6 +17,7 @@ limitations under the License. ...@@ -17,6 +17,7 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
...@@ -132,7 +133,10 @@ func tableLookup(v Values, simple string) (Values, error) { ...@@ -132,7 +133,10 @@ func tableLookup(v Values, simple string) (Values, error) {
// ReadValues will parse YAML byte data into a Values. // ReadValues will parse YAML byte data into a Values.
func ReadValues(data []byte) (vals Values, err error) { func ReadValues(data []byte) (vals Values, err error) {
err = yaml.Unmarshal(data, &vals) err = yaml.Unmarshal(data, &vals, func(d *json.Decoder) *json.Decoder {
d.UseNumber()
return d
})
if len(vals) == 0 { if len(vals) == 0 {
vals = Values{} vals = Values{}
} }
......
...@@ -53,6 +53,7 @@ water: ...@@ -53,6 +53,7 @@ water:
water: water:
where: "everywhere" where: "everywhere"
nor: "any drop to drink" nor: "any drop to drink"
temperature: 1234567890
` `
data, err := ReadValues([]byte(doc)) data, err := ReadValues([]byte(doc))
...@@ -266,6 +267,12 @@ func matchValues(t *testing.T, data map[string]interface{}) { ...@@ -266,6 +267,12 @@ func matchValues(t *testing.T, data map[string]interface{}) {
} else if o != "everywhere" { } else if o != "everywhere" {
t.Errorf("Expected water water everywhere") t.Errorf("Expected water water everywhere")
} }
if o, err := ttpl("{{.water.water.temperature}}", data); err != nil {
t.Errorf(".water.water.temperature: %s", err)
} else if o != "1234567890" {
t.Errorf("Expected water water temperature: 1234567890, got: %s", o)
}
} }
func ttpl(tpl string, v map[string]interface{}) (string, error) { func ttpl(tpl string, v map[string]interface{}) (string, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment