Change Schema type from map[string]interface{} to []byte

Signed-off-by: Ian Howell <ian.howell0@gmail.com>
pull/5350/head
Ian Howell 7 years ago
parent b96f8a0567
commit 44017710e7

@ -32,7 +32,7 @@ type Chart struct {
// Values are default config for this template. // Values are default config for this template.
Values map[string]interface{} Values map[string]interface{}
// Schema is an optional JSON schema for imposing structure on Values // 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, // Files are miscellaneous files in a chart archive,
// e.g. README, LICENSE, etc. // e.g. README, LICENSE, etc.
Files []*File Files []*File

@ -91,10 +91,7 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) {
} }
c.RawValues = f.Data c.RawValues = f.Data
case f.Name == "values.schema.yaml": case f.Name == "values.schema.yaml":
c.Schema = make(map[string]interface{}) c.Schema = f.Data
if err := yaml.Unmarshal(f.Data, &c.Schema); err != nil {
return c, errors.Wrap(err, "cannot load values.schema.yaml")
}
case strings.HasPrefix(f.Name, "templates/"): case strings.HasPrefix(f.Name, "templates/"):
c.Templates = append(c.Templates, &chart.File{Name: f.Name, Data: f.Data}) c.Templates = append(c.Templates, &chart.File{Name: f.Name, Data: f.Data})
case strings.HasPrefix(f.Name, "charts/"): case strings.HasPrefix(f.Name, "charts/"):

@ -17,6 +17,7 @@ limitations under the License.
package loader package loader
import ( import (
"bytes"
"testing" "testing"
"helm.sh/helm/pkg/chart" "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") 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") t.Error("Expected chart schema to be populated with default values")
} }

@ -46,9 +46,6 @@ const GlobalKey = "global"
// Values represents a collection of chart values. // Values represents a collection of chart values.
type Values map[string]interface{} 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. // YAML encodes the Values into a YAML string.
func (v Values) YAML() (string, error) { func (v Values) YAML() (string, error) {
b, err := yaml.Marshal(v) b, err := yaml.Marshal(v)
@ -128,15 +125,6 @@ func ReadValues(data []byte) (vals Values, err error) {
return vals, err 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. // ReadValuesFile will parse a YAML file into a map of values.
func ReadValuesFile(filename string) (Values, error) { func ReadValuesFile(filename string) (Values, error) {
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
@ -146,22 +134,13 @@ func ReadValuesFile(filename string) (Values, error) {
return ReadValues(data) 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 // 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) valuesJSON, err := convertYAMLToJSON(values)
if err != nil { if err != nil {
return err return err
} }
schemaJSON, err := convertYAMLToJSON(schema) schemaJSON, err := yaml.YAMLToJSON(schema)
if err != nil { if err != nil {
return err return err
} }

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"testing" "testing"
"text/template" "text/template"
@ -160,20 +161,12 @@ func TestReadValuesFile(t *testing.T) {
matchValues(t, data) 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) { func TestValidateAgainstSchema(t *testing.T) {
values, err := ReadValuesFile("./testdata/test-values.yaml") values, err := ReadValuesFile("./testdata/test-values.yaml")
if err != nil { if err != nil {
t.Fatalf("Error reading YAML file: %s", err) 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 { if err != nil {
t.Fatalf("Error reading YAML file: %s", err) t.Fatalf("Error reading YAML file: %s", err)
} }
@ -188,7 +181,7 @@ func TestValidateAgainstSchemaNegative(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error reading YAML file: %s", err) 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 { if err != nil {
t.Fatalf("Error reading YAML file: %s", err) t.Fatalf("Error reading YAML file: %s", err)
} }
@ -535,14 +528,14 @@ required:
- addresses - addresses
- employmentInfo - employmentInfo
` `
data, err := ReadSchema([]byte(schemaTest)) data, err := ReadValues([]byte(schemaTest))
if err != nil { if err != nil {
t.Fatalf("Error parsing bytes: %s", err) t.Fatalf("Error parsing bytes: %s", err)
} }
matchSchema(t, data) 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 { if o, err := ttpl("{{len .required}}", data); err != nil {
t.Errorf("len required: %s", err) t.Errorf("len required: %s", err)
} else if o != "4" { } else if o != "4" {

@ -17,6 +17,7 @@ limitations under the License.
package rules package rules
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -55,7 +56,7 @@ func validateValuesFile(valuesPath string) error {
ext := filepath.Ext(valuesPath) ext := filepath.Ext(valuesPath)
schemaPath := valuesPath[:len(valuesPath)-len(ext)] + ".schema.yaml" schemaPath := valuesPath[:len(valuesPath)-len(ext)] + ".schema.yaml"
schema, err := chartutil.ReadSchemaFile(schemaPath) schema, err := ioutil.ReadFile(schemaPath)
if len(schema) == 0 { if len(schema) == 0 {
return nil return nil
} }

Loading…
Cancel
Save