Ensure consistent manifest ordering

Signed-off-by: Mario Valderrama <woldy401@gmail.com>
pull/7816/head
Mario Valderrama 6 years ago
parent bdf1031e96
commit f1cc28effa

@ -22,6 +22,7 @@ import (
"io" "io"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -86,12 +87,21 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
// if we have a list of files to render, then check that each of the // if we have a list of files to render, then check that each of the
// provided files exists in the chart. // provided files exists in the chart.
if len(showFiles) > 0 { if len(showFiles) > 0 {
// This is necessary to ensure consistent manifest ordering when using --show-only
// with globs or directory names.
splitManifests := releaseutil.SplitManifests(manifests.String()) splitManifests := releaseutil.SplitManifests(manifests.String())
manifestsKeys := make([]string, 0, len(splitManifests))
for k := range splitManifests {
manifestsKeys = append(manifestsKeys, k)
}
sort.Sort(releaseutil.BySplitManifestsOrder(manifestsKeys))
manifestNameRegex := regexp.MustCompile("# Source: [^/]+/(.+)") manifestNameRegex := regexp.MustCompile("# Source: [^/]+/(.+)")
var manifestsToRender []string var manifestsToRender []string
for _, f := range showFiles { for _, f := range showFiles {
missing := true missing := true
for _, manifest := range splitManifests { for _, manifestKey := range manifestsKeys {
manifest := splitManifests[manifestKey]
submatch := manifestNameRegex.FindStringSubmatch(manifest) submatch := manifestNameRegex.FindStringSubmatch(manifest)
if len(submatch) == 0 { if len(submatch) == 0 {
continue continue

@ -94,6 +94,20 @@ func TestTemplateCmd(t *testing.T) {
cmd: fmt.Sprintf("template '%s' --show-only templates/service.yaml --show-only charts/subcharta/templates/service.yaml", chartPath), cmd: fmt.Sprintf("template '%s' --show-only templates/service.yaml --show-only charts/subcharta/templates/service.yaml", chartPath),
golden: "output/template-show-only-multiple.txt", golden: "output/template-show-only-multiple.txt",
}, },
{
name: "template with show-only directory",
cmd: fmt.Sprintf("template '%s' --show-only templates/subdir/", chartPath),
golden: "output/template-show-only-directory.txt",
// Repeat to ensure manifest ordering regressions are caught
repeat: 10,
},
{
name: "template with show-only glob",
cmd: fmt.Sprintf("template '%s' --show-only templates/subdir/role*", chartPath),
golden: "output/template-show-only-glob.txt",
// Repeat to ensure manifest ordering regressions are caught
repeat: 10,
},
{ {
name: "sorted output of manifests (order of filenames, then order of objects within each YAML file)", name: "sorted output of manifests (order of filenames, then order of objects within each YAML file)",
cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/object-order"), cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/object-order"),

Loading…
Cancel
Save