mirror of https://github.com/helm/helm
Merge pull request #793 from technosophos/feat/values-yaml
feat(chartutil): Update to use YAML instead of TOML for values files.pull/798/head
commit
c570363e5b
@ -1,2 +0,0 @@
|
||||
# The pod name
|
||||
name = "my-alpine"
|
@ -0,0 +1,2 @@
|
||||
# The pod name
|
||||
name: my-alpine
|
@ -0,0 +1,78 @@
|
||||
package chartutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/chart"
|
||||
)
|
||||
|
||||
const (
|
||||
// ChartfileName is the default Chart file name.
|
||||
ChartfileName = "Chart.yaml"
|
||||
// ValuesfileName is the default values file name.
|
||||
ValuesfileName = "values.yaml"
|
||||
// TemplatesDir is the relative directory name for templates.
|
||||
TemplatesDir = "templates"
|
||||
// ChartsDir is the relative directory name for charts dependencies.
|
||||
ChartsDir = "charts"
|
||||
)
|
||||
|
||||
const defaultValues = `# Default values for %s.
|
||||
# This is a YAML-formatted file.
|
||||
# Declare name/value pairs to be passed into your templates.
|
||||
# name: value
|
||||
`
|
||||
|
||||
// Create creates a new chart in a directory.
|
||||
//
|
||||
// Inside of dir, this will create a directory based on the name of
|
||||
// chartfile.Name. It will then write the Chart.yaml into this directory and
|
||||
// create the (empty) appropriate directories.
|
||||
//
|
||||
// The returned string will point to the newly created directory. It will be
|
||||
// an absolute path, even if the provided base directory was relative.
|
||||
//
|
||||
// If dir does not exist, this will return an error.
|
||||
// If Chart.yaml or any directories cannot be created, this will return an
|
||||
// error. In such a case, this will attempt to clean up by removing the
|
||||
// new chart directory.
|
||||
func Create(chartfile *chart.Metadata, dir string) (string, error) {
|
||||
path, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return path, err
|
||||
}
|
||||
|
||||
if fi, err := os.Stat(path); err != nil {
|
||||
return path, err
|
||||
} else if !fi.IsDir() {
|
||||
return path, fmt.Errorf("no such directory %s", path)
|
||||
}
|
||||
|
||||
n := chartfile.Name
|
||||
cdir := filepath.Join(path, n)
|
||||
if fi, err := os.Stat(cdir); err == nil && !fi.IsDir() {
|
||||
return cdir, fmt.Errorf("file %s already exists and is not a directory", cdir)
|
||||
}
|
||||
if err := os.MkdirAll(cdir, 0755); err != nil {
|
||||
return cdir, err
|
||||
}
|
||||
|
||||
if err := SaveChartfile(filepath.Join(cdir, ChartfileName), chartfile); err != nil {
|
||||
return cdir, err
|
||||
}
|
||||
|
||||
val := []byte(fmt.Sprintf(defaultValues, chartfile.Name))
|
||||
if err := ioutil.WriteFile(filepath.Join(cdir, ValuesfileName), val, 0644); err != nil {
|
||||
return cdir, err
|
||||
}
|
||||
|
||||
for _, d := range []string{TemplatesDir, ChartsDir} {
|
||||
if err := os.MkdirAll(filepath.Join(cdir, d), 0755); err != nil {
|
||||
return cdir, err
|
||||
}
|
||||
}
|
||||
return cdir, nil
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package chartutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/chart"
|
||||
)
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
tdir, err := ioutil.TempDir("", "helm-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tdir)
|
||||
|
||||
cf := &chart.Metadata{Name: "foo"}
|
||||
|
||||
c, err := Create(cf, tdir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dir := filepath.Join(tdir, "foo")
|
||||
|
||||
mychart, err := LoadDir(c)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load newly created chart %q: %s", c, err)
|
||||
}
|
||||
|
||||
if mychart.Metadata.Name != "foo" {
|
||||
t.Errorf("Expected name to be 'foo', got %q", mychart.Metadata.Name)
|
||||
}
|
||||
|
||||
for _, d := range []string{TemplatesDir, ChartsDir} {
|
||||
if fi, err := os.Stat(filepath.Join(dir, d)); err != nil {
|
||||
t.Errorf("Expected %s dir: %s", d, err)
|
||||
} else if !fi.IsDir() {
|
||||
t.Errorf("Expected %s to be a directory.", d)
|
||||
}
|
||||
}
|
||||
|
||||
for _, f := range []string{ChartfileName, ValuesfileName} {
|
||||
if fi, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
||||
t.Errorf("Expected %s file: %s", f, err)
|
||||
} else if fi.IsDir() {
|
||||
t.Errorf("Expected %s to be a fle.", f)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package chartutil
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Expand uncompresses and extracts a chart into the specified directory.
|
||||
func Expand(dir string, r io.Reader) error {
|
||||
gr, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer gr.Close()
|
||||
tr := tar.NewReader(gr)
|
||||
for {
|
||||
header, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path := filepath.Clean(filepath.Join(dir, header.Name))
|
||||
info := header.FileInfo()
|
||||
if info.IsDir() {
|
||||
if err = os.MkdirAll(path, info.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.Copy(file, tr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
albatross = "true"
|
@ -0,0 +1 @@
|
||||
albatross: "true"
|
@ -0,0 +1,12 @@
|
||||
poet: "Coleridge"
|
||||
title: "Rime of the Ancient Mariner"
|
||||
stanza: ["at", "length", "did", "cross", "an", "Albatross"]
|
||||
|
||||
mariner:
|
||||
with: "crossbow"
|
||||
shot: "ALBATROSS"
|
||||
|
||||
water:
|
||||
water:
|
||||
where: "everywhere"
|
||||
nor: "any drop to drink"
|
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
# Default values for mast1.
|
||||
# This is a TOML-formatted file. https://github.com/toml-lang/toml
|
||||
# This is a YAML-formatted file.
|
||||
# Declare name/value pairs to be passed into your templates.
|
||||
# name = "value"
|
Binary file not shown.
@ -1,2 +0,0 @@
|
||||
# The pod name
|
||||
name = "my-alpine"
|
@ -0,0 +1,2 @@
|
||||
# The pod name
|
||||
name: "my-alpine"
|
Binary file not shown.
@ -1,6 +0,0 @@
|
||||
# A values file contains configuration.
|
||||
|
||||
name = "Some Name"
|
||||
|
||||
[section]
|
||||
name = "Name in a section"
|
@ -0,0 +1,6 @@
|
||||
# A values file contains configuration.
|
||||
|
||||
name: "Some Name"
|
||||
|
||||
section:
|
||||
name: "Name in a section"
|
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
# Default values for mariner.
|
||||
# This is a TOML-formatted file. https://github.com/toml-lang/toml
|
||||
# This is a YAML-formatted file. https://github.com/toml-lang/toml
|
||||
# Declare name/value pairs to be passed into your templates.
|
||||
# name = "value"
|
||||
# name: "value"
|
@ -0,0 +1,81 @@
|
||||
package chartutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
// ErrNoTable indicates that a chart does not have a matching table.
|
||||
var ErrNoTable = errors.New("no table")
|
||||
|
||||
// Values represents a collection of chart values.
|
||||
type Values map[string]interface{}
|
||||
|
||||
// Table gets a table (YAML subsection) from a Values object.
|
||||
//
|
||||
// The table is returned as a Values.
|
||||
//
|
||||
// Compound table names may be specified with dots:
|
||||
//
|
||||
// foo.bar
|
||||
//
|
||||
// The above will be evaluated as "The table bar inside the table
|
||||
// foo".
|
||||
//
|
||||
// An ErrNoTable is returned if the table does not exist.
|
||||
func (v Values) Table(name string) (Values, error) {
|
||||
names := strings.Split(name, ".")
|
||||
table := v
|
||||
var err error
|
||||
|
||||
for _, n := range names {
|
||||
table, err = tableLookup(table, n)
|
||||
if err != nil {
|
||||
return table, err
|
||||
}
|
||||
}
|
||||
return table, err
|
||||
}
|
||||
|
||||
// Encode writes serialized Values information to the given io.Writer.
|
||||
func (v Values) Encode(w io.Writer) error {
|
||||
//return yaml.NewEncoder(w).Encode(v)
|
||||
out, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func tableLookup(v Values, simple string) (Values, error) {
|
||||
v2, ok := v[simple]
|
||||
if !ok {
|
||||
return v, ErrNoTable
|
||||
}
|
||||
vv, ok := v2.(map[string]interface{})
|
||||
if !ok {
|
||||
return vv, ErrNoTable
|
||||
}
|
||||
return vv, nil
|
||||
}
|
||||
|
||||
// ReadValues will parse YAML byte data into a Values.
|
||||
func ReadValues(data []byte) (Values, error) {
|
||||
out := map[string]interface{}{}
|
||||
err := yaml.Unmarshal(data, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
// ReadValuesFile will parse a YAML file into a Values.
|
||||
func ReadValuesFile(filename string) (Values, error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return map[string]interface{}{}, err
|
||||
}
|
||||
return ReadValues(data)
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
package chartutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func TestReadValues(t *testing.T) {
|
||||
doc := `# Test YAML parse
|
||||
poet: "Coleridge"
|
||||
title: "Rime of the Ancient Mariner"
|
||||
stanza:
|
||||
- "at"
|
||||
- "length"
|
||||
- "did"
|
||||
- cross
|
||||
- an
|
||||
- Albatross
|
||||
|
||||
mariner:
|
||||
with: "crossbow"
|
||||
shot: "ALBATROSS"
|
||||
|
||||
water:
|
||||
water:
|
||||
where: "everywhere"
|
||||
nor: "any drop to drink"
|
||||
`
|
||||
|
||||
data, err := ReadValues([]byte(doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing bytes: %s", err)
|
||||
}
|
||||
matchValues(t, data)
|
||||
}
|
||||
|
||||
func TestReadValuesFile(t *testing.T) {
|
||||
data, err := ReadValuesFile("./testdata/coleridge.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading YAML file: %s", err)
|
||||
}
|
||||
matchValues(t, data)
|
||||
}
|
||||
|
||||
func ExampleValues() {
|
||||
doc := `
|
||||
title: "Moby Dick"
|
||||
chapter:
|
||||
one:
|
||||
title: "Loomings"
|
||||
two:
|
||||
title: "The Carpet-Bag"
|
||||
three:
|
||||
title: "The Spouter Inn"
|
||||
`
|
||||
d, err := ReadValues([]byte(doc))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ch1, err := d.Table("chapter.one")
|
||||
if err != nil {
|
||||
panic("could not find chapter one")
|
||||
}
|
||||
fmt.Print(ch1["title"])
|
||||
// Output:
|
||||
// Loomings
|
||||
}
|
||||
|
||||
func TestTable(t *testing.T) {
|
||||
doc := `
|
||||
title: "Moby Dick"
|
||||
chapter:
|
||||
one:
|
||||
title: "Loomings"
|
||||
two:
|
||||
title: "The Carpet-Bag"
|
||||
three:
|
||||
title: "The Spouter Inn"
|
||||
`
|
||||
d, err := ReadValues([]byte(doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse the White Whale: %s", err)
|
||||
}
|
||||
|
||||
if _, err := d.Table("title"); err == nil {
|
||||
t.Fatalf("Title is not a table.")
|
||||
}
|
||||
|
||||
if _, err := d.Table("chapter"); err != nil {
|
||||
t.Fatalf("Failed to get the chapter table: %s\n%v", err, d)
|
||||
}
|
||||
|
||||
if v, err := d.Table("chapter.one"); err != nil {
|
||||
t.Errorf("Failed to get chapter.one: %s", err)
|
||||
} else if v["title"] != "Loomings" {
|
||||
t.Errorf("Unexpected title: %s", v["title"])
|
||||
}
|
||||
|
||||
if _, err := d.Table("chapter.three"); err != nil {
|
||||
t.Errorf("Chapter three is missing: %s\n%v", err, d)
|
||||
}
|
||||
|
||||
if _, err := d.Table("chapter.OneHundredThirtySix"); err == nil {
|
||||
t.Errorf("I think you mean 'Epilogue'")
|
||||
}
|
||||
}
|
||||
|
||||
func matchValues(t *testing.T, data map[string]interface{}) {
|
||||
if data["poet"] != "Coleridge" {
|
||||
t.Errorf("Unexpected poet: %s", data["poet"])
|
||||
}
|
||||
|
||||
if o, err := ttpl("{{len .stanza}}", data); err != nil {
|
||||
t.Errorf("len stanza: %s", err)
|
||||
} else if o != "6" {
|
||||
t.Errorf("Expected 6, got %s", o)
|
||||
}
|
||||
|
||||
if o, err := ttpl("{{.mariner.shot}}", data); err != nil {
|
||||
t.Errorf(".mariner.shot: %s", err)
|
||||
} else if o != "ALBATROSS" {
|
||||
t.Errorf("Expected that mariner shot ALBATROSS")
|
||||
}
|
||||
|
||||
if o, err := ttpl("{{.water.water.where}}", data); err != nil {
|
||||
t.Errorf(".water.water.where: %s", err)
|
||||
} else if o != "everywhere" {
|
||||
t.Errorf("Expected water water everywhere")
|
||||
}
|
||||
}
|
||||
|
||||
func ttpl(tpl string, v map[string]interface{}) (string, error) {
|
||||
var b bytes.Buffer
|
||||
tt := template.Must(template.New("t").Parse(tpl))
|
||||
if err := tt.Execute(&b, v); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
name = "mariner"
|
@ -0,0 +1 @@
|
||||
name: "mariner"
|
@ -1,4 +0,0 @@
|
||||
# Default values for badchartfile.
|
||||
# This is a TOML-formatted file. https://github.com/toml-lang/toml
|
||||
# Declare name/value pairs to be passed into your templates.
|
||||
# name = "value"
|
@ -0,0 +1 @@
|
||||
# Default values for badchartfile.
|
@ -1,2 +1,2 @@
|
||||
# Invalid value for badvaluesfile for testing lint fails with invalid toml format
|
||||
name: "value"
|
||||
# Invalid value for badvaluesfile for testing lint fails with invalid yaml format
|
||||
name= "value"
|
@ -1 +0,0 @@
|
||||
name = "goodone here"
|
@ -0,0 +1 @@
|
||||
name: "goodone here"
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue