diff options
Diffstat (limited to 'src/k8splugin/internal/helm/helm.go')
-rw-r--r-- | src/k8splugin/internal/helm/helm.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/k8splugin/internal/helm/helm.go b/src/k8splugin/internal/helm/helm.go index 2150758b..d3715fce 100644 --- a/src/k8splugin/internal/helm/helm.go +++ b/src/k8splugin/internal/helm/helm.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "regexp" + "sort" "strings" utils "github.com/onap/multicloud-k8s/src/k8splugin/internal" @@ -55,16 +56,17 @@ type Template interface { // TemplateClient implements the Template interface // It will also be used to maintain any localized state type TemplateClient struct { - whitespaceRegex *regexp.Regexp - kubeVersion string - kubeNameSpace string - releaseName string + emptyRegex *regexp.Regexp + kubeVersion string + kubeNameSpace string + releaseName string } // NewTemplateClient returns a new instance of TemplateClient func NewTemplateClient(k8sversion, namespace, releasename string) *TemplateClient { return &TemplateClient{ - whitespaceRegex: regexp.MustCompile(`^\s*$`), + // emptyRegex defines template content that could be considered empty yaml-wise + emptyRegex: regexp.MustCompile(`(?m)\A(^(\s*#.*|\s*)$\n?)*\z`), // defaultKubeVersion is the default value of --kube-version flag kubeVersion: k8sversion, kubeNameSpace: namespace, @@ -209,11 +211,19 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile continue } rmap := releaseutil.SplitManifests(v) - count := 0 - for _, v1 := range rmap { - key := fmt.Sprintf("%s-%d", k, count) - newRenderedTemplates[key] = v1 - count = count + 1 + + // Iterating over map can yield different order at times + // so first we'll sort keys + sortedKeys := make([]string, len(rmap)) + for k1, _ := range rmap { + sortedKeys = append(sortedKeys, k1) + } + // This makes empty files have the lowest indices + sort.Strings(sortedKeys) + + for k1, v1 := range sortedKeys { + key := fmt.Sprintf("%s-%d", k, k1) + newRenderedTemplates[key] = rmap[v1] } } @@ -232,7 +242,7 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile } // blank template after execution - if h.whitespaceRegex.MatchString(data) { + if h.emptyRegex.MatchString(data) { continue } @@ -260,7 +270,7 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile func getGroupVersionKind(data string) (schema.GroupVersionKind, error) { out, err := k8syaml.ToJSON([]byte(data)) if err != nil { - return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Converting yaml to json") + return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Converting yaml to json:\n"+data) } simpleMeta := json.SimpleMetaFactory{} |