diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index 23bb6f309..92ad68cb1 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -32,7 +32,7 @@ type Chart struct { // Values are default config for this template. Values map[string]interface{} // Schema is an optional JSON schema for imposing structure on Values - Schema map[string]interface{} + Schema []byte // Files are miscellaneous files in a chart archive, // e.g. README, LICENSE, etc. Files []*File diff --git a/pkg/chart/loader/load.go b/pkg/chart/loader/load.go index ae8c89d19..f2c52e530 100644 --- a/pkg/chart/loader/load.go +++ b/pkg/chart/loader/load.go @@ -91,10 +91,7 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) { } c.RawValues = f.Data case f.Name == "values.schema.yaml": - c.Schema = make(map[string]interface{}) - if err := yaml.Unmarshal(f.Data, &c.Schema); err != nil { - return c, errors.Wrap(err, "cannot load values.schema.yaml") - } + c.Schema = f.Data case strings.HasPrefix(f.Name, "templates/"): c.Templates = append(c.Templates, &chart.File{Name: f.Name, Data: f.Data}) case strings.HasPrefix(f.Name, "charts/"): diff --git a/pkg/chart/loader/load_test.go b/pkg/chart/loader/load_test.go index 855921ef4..2b5235c7e 100644 --- a/pkg/chart/loader/load_test.go +++ b/pkg/chart/loader/load_test.go @@ -17,6 +17,7 @@ limitations under the License. package loader import ( + "bytes" "testing" "helm.sh/helm/pkg/chart" @@ -105,7 +106,7 @@ icon: https://example.com/64x64.png t.Error("Expected chart values to be populated with default values") } - if c.Schema["type"] != "Values" { + if !bytes.Equal(c.Schema, []byte("type: Values")) { t.Error("Expected chart schema to be populated with default values") } diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index 29acd57eb..9b6168597 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -46,9 +46,6 @@ const GlobalKey = "global" // Values represents a collection of chart values. type Values map[string]interface{} -// Schema represents the document structure to validate the values.yaml file against -type Schema map[string]interface{} - // YAML encodes the Values into a YAML string. func (v Values) YAML() (string, error) { b, err := yaml.Marshal(v) @@ -128,15 +125,6 @@ func ReadValues(data []byte) (vals Values, err error) { return vals, err } -// ReadSchema will parse YAML byte data into a Schema. -func ReadSchema(data []byte) (schema Schema, err error) { - err = yaml.Unmarshal(data, &schema) - if len(schema) == 0 { - schema = Schema{} - } - return schema, err -} - // ReadValuesFile will parse a YAML file into a map of values. func ReadValuesFile(filename string) (Values, error) { data, err := ioutil.ReadFile(filename) @@ -146,22 +134,13 @@ func ReadValuesFile(filename string) (Values, error) { return ReadValues(data) } -// ReadSchemaFile will parse a YAML file into a Schema. -func ReadSchemaFile(filename string) (Schema, error) { - data, err := ioutil.ReadFile(filename) - if err != nil { - return Schema{}, err - } - return ReadSchema(data) -} - // ValidateAgainstSchema checks that values does not violate the structure laid out in schema -func ValidateAgainstSchema(values Values, schema Schema) error { +func ValidateAgainstSchema(values Values, schema []byte) error { valuesJSON, err := convertYAMLToJSON(values) if err != nil { return err } - schemaJSON, err := convertYAMLToJSON(schema) + schemaJSON, err := yaml.YAMLToJSON(schema) if err != nil { return err } diff --git a/pkg/chartutil/values_test.go b/pkg/chartutil/values_test.go index 3ad9caebc..4ec1651d2 100644 --- a/pkg/chartutil/values_test.go +++ b/pkg/chartutil/values_test.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/ioutil" "testing" "text/template" @@ -160,20 +161,12 @@ func TestReadValuesFile(t *testing.T) { matchValues(t, data) } -func TestReadSchemaFile(t *testing.T) { - data, err := ReadSchemaFile("./testdata/test-values.schema.yaml") - if err != nil { - t.Fatalf("Error reading YAML file: %s", err) - } - matchSchema(t, data) -} - func TestValidateAgainstSchema(t *testing.T) { values, err := ReadValuesFile("./testdata/test-values.yaml") if err != nil { t.Fatalf("Error reading YAML file: %s", err) } - schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml") + schema, err := ioutil.ReadFile("./testdata/test-values.schema.yaml") if err != nil { t.Fatalf("Error reading YAML file: %s", err) } @@ -188,7 +181,7 @@ func TestValidateAgainstSchemaNegative(t *testing.T) { if err != nil { t.Fatalf("Error reading YAML file: %s", err) } - schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml") + schema, err := ioutil.ReadFile("./testdata/test-values.schema.yaml") if err != nil { t.Fatalf("Error reading YAML file: %s", err) } @@ -535,14 +528,14 @@ required: - addresses - employmentInfo ` - data, err := ReadSchema([]byte(schemaTest)) + data, err := ReadValues([]byte(schemaTest)) if err != nil { t.Fatalf("Error parsing bytes: %s", err) } matchSchema(t, data) } -func matchSchema(t *testing.T, data Schema) { +func matchSchema(t *testing.T, data map[string]interface{}) { if o, err := ttpl("{{len .required}}", data); err != nil { t.Errorf("len required: %s", err) } else if o != "4" { diff --git a/pkg/lint/rules/values.go b/pkg/lint/rules/values.go index bc37aa584..01de9546d 100644 --- a/pkg/lint/rules/values.go +++ b/pkg/lint/rules/values.go @@ -17,6 +17,7 @@ limitations under the License. package rules import ( + "io/ioutil" "os" "path/filepath" @@ -55,7 +56,7 @@ func validateValuesFile(valuesPath string) error { ext := filepath.Ext(valuesPath) schemaPath := valuesPath[:len(valuesPath)-len(ext)] + ".schema.yaml" - schema, err := chartutil.ReadSchemaFile(schemaPath) + schema, err := ioutil.ReadFile(schemaPath) if len(schema) == 0 { return nil }