Merge pull request #6519 from SimonAlling/dev-v3

fix(pkg/chartutil): include values.schema.json in packaged chart
pull/6652/head
Matthew Fisher 5 years ago committed by GitHub
commit 0f26eeb8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,6 +35,8 @@ const (
ChartfileName = "Chart.yaml" ChartfileName = "Chart.yaml"
// ValuesfileName is the default values file name. // ValuesfileName is the default values file name.
ValuesfileName = "values.yaml" ValuesfileName = "values.yaml"
// SchemafileName is the default values schema file name.
SchemafileName = "values.schema.json"
// TemplatesDir is the relative directory name for templates. // TemplatesDir is the relative directory name for templates.
TemplatesDir = "templates" TemplatesDir = "templates"
// ChartsDir is the relative directory name for charts dependencies. // ChartsDir is the relative directory name for charts dependencies.

@ -19,6 +19,7 @@ package chartutil
import ( import (
"archive/tar" "archive/tar"
"compress/gzip" "compress/gzip"
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -56,6 +57,14 @@ func SaveDir(c *chart.Chart, dest string) error {
} }
} }
// Save values.schema.json if it exists
if c.Schema != nil {
filename := filepath.Join(outdir, SchemafileName)
if err := writeFile(filename, c.Schema); err != nil {
return err
}
}
// Save templates and files // Save templates and files
for _, o := range [][]*chart.File{c.Templates, c.Files} { for _, o := range [][]*chart.File{c.Templates, c.Files} {
for _, f := range o { for _, f := range o {
@ -124,6 +133,7 @@ func Save(c *chart.Chart, outDir string) (string, error) {
if err := writeTarContents(twriter, c, ""); err != nil { if err := writeTarContents(twriter, c, ""); err != nil {
rollback = true rollback = true
return filename, err
} }
return filename, err return filename, err
} }
@ -149,6 +159,16 @@ func writeTarContents(out *tar.Writer, c *chart.Chart, prefix string) error {
return err return err
} }
// Save values.schema.json if it exists
if c.Schema != nil {
if !json.Valid(c.Schema) {
return errors.New("Invalid JSON in " + SchemafileName)
}
if err := writeToTar(out, filepath.Join(base, SchemafileName), c.Schema); err != nil {
return err
}
}
// Save templates // Save templates
for _, f := range c.Templates { for _, f := range c.Templates {
n := filepath.Join(base, f.Name) n := filepath.Join(base, f.Name)

@ -17,10 +17,12 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"testing" "testing"
@ -46,7 +48,9 @@ func TestSave(t *testing.T) {
Files: []*chart.File{ Files: []*chart.File{
{Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")}, {Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")},
}, },
Schema: []byte("{\n \"title\": \"Values\"\n}"),
} }
chartWithInvalidJSON := withSchema(*c, []byte("{"))
where, err := Save(c, dest) where, err := Save(c, dest)
if err != nil { if err != nil {
@ -69,10 +73,32 @@ func TestSave(t *testing.T) {
if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" { if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" {
t.Fatal("Files data did not match") t.Fatal("Files data did not match")
} }
if !bytes.Equal(c.Schema, c2.Schema) {
indentation := 4
formattedExpected := Indent(indentation, string(c.Schema))
formattedActual := Indent(indentation, string(c2.Schema))
t.Fatalf("Schema data did not match.\nExpected:\n%s\nActual:\n%s", formattedExpected, formattedActual)
}
if _, err := Save(&chartWithInvalidJSON, dest); err == nil {
t.Fatalf("Invalid JSON was not caught while saving chart")
}
}) })
} }
} }
// Creates a copy with a different schema; does not modify anything.
func withSchema(chart chart.Chart, schema []byte) chart.Chart {
chart.Schema = schema
return chart
}
func Indent(n int, text string) string {
startOfLine := regexp.MustCompile(`(?m)^`)
indentation := strings.Repeat(" ", n)
return startOfLine.ReplaceAllLiteralString(text, indentation)
}
func TestSaveDir(t *testing.T) { func TestSaveDir(t *testing.T) {
tmp, err := ioutil.TempDir("", "helm-") tmp, err := ioutil.TempDir("", "helm-")
if err != nil { if err != nil {

Loading…
Cancel
Save