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
for _, f := range c.Templates {
n := filepath.Join(outdir, f.Name)
if err := ioutil.WriteFile(n, f.Data, 0755); err != nil {
return err
}
}
// Save files
for _, f := range c.Files {
n := filepath.Join(outdir, f.Name)
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
// Save templates and files
for _, o := range [][]*chart.File{c.Templates, c.Files} {
for _, f := range o {
n := filepath.Join(outdir, f.Name)
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{
{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 {
@ -97,6 +100,11 @@ func TestSaveDir(t *testing.T) {
if c2.Name() != c.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" {
t.Fatal("Files data did not match")
}

Loading…
Cancel
Save