From 10d267ed8b126c6425233950145f824205b9902b Mon Sep 17 00:00:00 2001 From: Althaf M Date: Fri, 13 Dec 2024 22:22:59 +0000 Subject: [PATCH 1/3] fix: (toToml) renders int as float This commit fixes the issue where the yaml.Unmarshaller converts all int values into float64, this passes in option to decoder, which enables conversion of int into . Signed-off-by: Althaf M --- cmd/helm/template_test.go | 12 ++++++++++ cmd/helm/testdata/output/issue-totoml.txt | 8 +++++++ .../testcharts/issue-totoml/Chart.yaml | 3 +++ .../issue-totoml/templates/configmap.yaml | 6 +++++ .../testcharts/issue-totoml/values.yaml | 2 ++ pkg/chart/loader/load.go | 11 ++++++---- pkg/chartutil/dependencies_test.go | 22 +++++++++++++++++++ pkg/chartutil/values.go | 11 ++++++---- 8 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 cmd/helm/testdata/output/issue-totoml.txt create mode 100644 cmd/helm/testdata/testcharts/issue-totoml/Chart.yaml create mode 100644 cmd/helm/testdata/testcharts/issue-totoml/templates/configmap.yaml create mode 100644 cmd/helm/testdata/testcharts/issue-totoml/values.yaml diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index e5b939879..28e24ce63 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -22,6 +22,18 @@ import ( "testing" ) +func TestTemplateCmdWithToml(t *testing.T) { + + tests := []cmdTestCase{ + { + name: "check toToml function rendering", + cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/issue-totoml"), + golden: "output/issue-totoml.txt", + }, + } + runTestCmd(t, tests) +} + var chartPath = "testdata/testcharts/subchart" func TestTemplateCmd(t *testing.T) { diff --git a/cmd/helm/testdata/output/issue-totoml.txt b/cmd/helm/testdata/output/issue-totoml.txt new file mode 100644 index 000000000..06cf4bb8d --- /dev/null +++ b/cmd/helm/testdata/output/issue-totoml.txt @@ -0,0 +1,8 @@ +--- +# Source: issue-totoml/templates/configmap.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: issue-totoml +data: | + key = 13 diff --git a/cmd/helm/testdata/testcharts/issue-totoml/Chart.yaml b/cmd/helm/testdata/testcharts/issue-totoml/Chart.yaml new file mode 100644 index 000000000..f4be7a213 --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-totoml/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v2 +name: issue-totoml +version: 0.1.0 diff --git a/cmd/helm/testdata/testcharts/issue-totoml/templates/configmap.yaml b/cmd/helm/testdata/testcharts/issue-totoml/templates/configmap.yaml new file mode 100644 index 000000000..621e70d48 --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-totoml/templates/configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: issue-totoml +data: | + {{ .Values.global | toToml }} diff --git a/cmd/helm/testdata/testcharts/issue-totoml/values.yaml b/cmd/helm/testdata/testcharts/issue-totoml/values.yaml new file mode 100644 index 000000000..dd0140449 --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-totoml/values.yaml @@ -0,0 +1,2 @@ +global: + key: 13 \ No newline at end of file diff --git a/pkg/chart/loader/load.go b/pkg/chart/loader/load.go index 67f09c2bb..6ef83cb5e 100644 --- a/pkg/chart/loader/load.go +++ b/pkg/chart/loader/load.go @@ -18,13 +18,13 @@ package loader import ( "bytes" + "encoding/json" + "github.com/pkg/errors" "log" "os" "path/filepath" - "strings" - - "github.com/pkg/errors" "sigs.k8s.io/yaml" + "strings" "helm.sh/helm/v4/pkg/chart" ) @@ -104,7 +104,10 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) { } case f.Name == "values.yaml": c.Values = make(map[string]interface{}) - if err := yaml.Unmarshal(f.Data, &c.Values); err != nil { + if err := yaml.Unmarshal(f.Data, &c.Values, func(d *json.Decoder) *json.Decoder { + d.UseNumber() + return d + }); err != nil { return c, errors.Wrap(err, "cannot load values.yaml") } case f.Name == "values.schema.json": diff --git a/pkg/chartutil/dependencies_test.go b/pkg/chartutil/dependencies_test.go index af34a3f6b..516239d27 100644 --- a/pkg/chartutil/dependencies_test.go +++ b/pkg/chartutil/dependencies_test.go @@ -15,8 +15,10 @@ limitations under the License. package chartutil import ( + "encoding/json" "os" "path/filepath" + "reflect" "sort" "strconv" "testing" @@ -237,9 +239,24 @@ func TestProcessDependencyImportValues(t *testing.T) { if b := strconv.FormatBool(pv); b != vv { t.Errorf("failed to match imported bool value %v with expected %v for key %q", b, vv, kk) } + case json.Number: + if fv, err := pv.Float64(); err == nil { + if sfv := strconv.FormatFloat(fv, 'f', -1, 64); sfv != vv { + t.Errorf("failed to match imported float value %v with expected %v for key %q", sfv, vv, kk) + } + } + if iv, err := pv.Int64(); err == nil { + if siv := strconv.FormatInt(iv, 10); siv != vv { + t.Errorf("failed to match imported int value %v with expected %v for key %q", siv, vv, kk) + } + } + if pv.String() != vv { + t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk) + } default: if pv != vv { t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk) + t.Error(reflect.TypeOf(pv)) } } } @@ -309,9 +326,14 @@ func TestProcessDependencyImportValuesMultiLevelPrecedence(t *testing.T) { if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv { t.Errorf("failed to match imported float value %v with expected %v", s, vv) } + case json.Number: + if pv.String() != vv { + t.Errorf("failed to match imported string value %q with expected %q", pv, vv) + } default: if pv != vv { t.Errorf("failed to match imported string value %q with expected %q", pv, vv) + t.Error(reflect.TypeOf(pv)) } } } diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index d41df6e7f..79125083a 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -17,13 +17,13 @@ limitations under the License. package chartutil import ( + "encoding/json" "fmt" + "github.com/pkg/errors" "io" "os" - "strings" - - "github.com/pkg/errors" "sigs.k8s.io/yaml" + "strings" "helm.sh/helm/v4/pkg/chart" ) @@ -105,7 +105,10 @@ func tableLookup(v Values, simple string) (Values, error) { // ReadValues will parse YAML byte data into a Values. 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 { vals = Values{} } From 57d85993cbbb823f2145f75b0a7a27e40d9aab22 Mon Sep 17 00:00:00 2001 From: Althaf M Date: Sat, 14 Dec 2024 23:22:28 +0000 Subject: [PATCH 2/3] remove debug statements Signed-off-by: Althaf M --- pkg/chartutil/dependencies_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/chartutil/dependencies_test.go b/pkg/chartutil/dependencies_test.go index 516239d27..ac8e4d76e 100644 --- a/pkg/chartutil/dependencies_test.go +++ b/pkg/chartutil/dependencies_test.go @@ -18,7 +18,6 @@ import ( "encoding/json" "os" "path/filepath" - "reflect" "sort" "strconv" "testing" @@ -256,7 +255,6 @@ func TestProcessDependencyImportValues(t *testing.T) { default: if pv != vv { t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk) - t.Error(reflect.TypeOf(pv)) } } } @@ -333,7 +331,6 @@ func TestProcessDependencyImportValuesMultiLevelPrecedence(t *testing.T) { default: if pv != vv { t.Errorf("failed to match imported string value %q with expected %q", pv, vv) - t.Error(reflect.TypeOf(pv)) } } } From 88f7dc5329a9cf17fdcdbef3f35e4782817cbd5a Mon Sep 17 00:00:00 2001 From: Althaf M Date: Wed, 22 Jan 2025 09:27:51 +0000 Subject: [PATCH 3/3] merge: fixing merge conflicts Signed-off-by: Althaf M althafm@outlook.com Signed-off-by: Althaf M --- pkg/chart/loader/load.go | 5 +++-- pkg/chartutil/values.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/chart/loader/load.go b/pkg/chart/loader/load.go index 6ef83cb5e..7645ba96c 100644 --- a/pkg/chart/loader/load.go +++ b/pkg/chart/loader/load.go @@ -19,13 +19,14 @@ package loader import ( "bytes" "encoding/json" - "github.com/pkg/errors" "log" "os" "path/filepath" - "sigs.k8s.io/yaml" "strings" + "github.com/pkg/errors" + "sigs.k8s.io/yaml" + "helm.sh/helm/v4/pkg/chart" ) diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index 79125083a..706c44d48 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -19,12 +19,13 @@ package chartutil import ( "encoding/json" "fmt" - "github.com/pkg/errors" "io" "os" - "sigs.k8s.io/yaml" "strings" + "github.com/pkg/errors" + "sigs.k8s.io/yaml" + "helm.sh/helm/v4/pkg/chart" )