fix(chartutil): Ensure nested template dir on save (#6177)

If a templates/ dir of a chart contained a subdirectory,
for example "templates/tests/test-db.yaml", an error was
being thrown on export due to missing the "templates/test"
directory prior to saving the template file itself.

Fixes #5757

Signed-off-by: Josh Dolitsky <jdolitsky@gmail.com>
pull/6183/head
Josh Dolitsky 6 years ago committed by GitHub
parent ad9c0c440b
commit cc5dece910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -63,25 +63,19 @@ func SaveDir(c *chart.Chart, dest string) error {
} }
} }
// Save templates // Save templates and files
for _, f := range c.Templates { for _, o := range [][]*chart.File{c.Templates, c.Files} {
n := filepath.Join(outdir, f.Name) for _, f := range o {
if err := ioutil.WriteFile(n, f.Data, 0755); err != nil { n := filepath.Join(outdir, f.Name)
return err
} d := filepath.Dir(n)
} if err := os.MkdirAll(d, 0755); err != nil {
return err
// Save files }
for _, f := range c.Files {
n := filepath.Join(outdir, f.Name) if err := ioutil.WriteFile(n, f.Data, 0755); err != nil {
return err
d := filepath.Dir(n) }
if err := os.MkdirAll(d, 0755); err != nil {
return err
}
if err := ioutil.WriteFile(n, f.Data, 0755); err != nil {
return err
} }
} }

@ -83,6 +83,9 @@ func TestSaveDir(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")},
}, },
Templates: []*chart.File{
{Name: "templates/nested/dir/thing.yaml", Data: []byte("abc: {{ .Values.abc }}")},
},
} }
if err := SaveDir(c, tmp); err != nil { if err := SaveDir(c, tmp); err != nil {
@ -97,6 +100,11 @@ func TestSaveDir(t *testing.T) {
if c2.Name() != c.Name() { if c2.Name() != c.Name() {
t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name()) t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name())
} }
if len(c2.Templates) != 1 || c2.Templates[0].Name != "templates/nested/dir/thing.yaml" {
t.Fatal("Templates data did not match")
}
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")
} }

Loading…
Cancel
Save