From de3b61306fc29335162176c2124ca46a3bd84259 Mon Sep 17 00:00:00 2001 From: Gerard Nguyen Date: Thu, 4 Apr 2024 16:10:55 +1100 Subject: [PATCH] use helm template to render if any --- cmd/kots/cli/template.go | 60 ++++++++++++++++++++++++++++++++++++---- pkg/pull/pull.go | 2 +- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/cmd/kots/cli/template.go b/cmd/kots/cli/template.go index d06f2817d6..bf21e64c95 100644 --- a/cmd/kots/cli/template.go +++ b/cmd/kots/cli/template.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "os/exec" "path/filepath" "strings" @@ -79,16 +80,20 @@ func TemplateCmd() *cobra.Command { if err != nil { return errors.Wrap(err, "failed to render raw template") } - fmt.Println(rendered) + log.Info(rendered) return nil } // render all mode, similar to helm template // we will utilize pull command to fetch and render manifests from upstream + log.ActionWithSpinner("Pulling app from upstream and rendering templates...") err := pullAndRender(license.Spec.AppSlug, licenseFile, configFile) + log.FinishSpinner() + if err != nil { return errors.Wrap(err, "failed to render all templates") } + return nil } @@ -118,7 +123,7 @@ func TemplateCmd() *cobra.Command { return errors.Wrap(err, "failed to render template") } - fmt.Print(rendered) + log.Info(rendered) return nil }, @@ -272,7 +277,7 @@ func pullAndRender(appSlug string, licensePath string, configPath string) error return errors.Wrap(err, "failed to read kotsKinds directory") } - var manifetsToRender []string + manifetsToRender := make(map[string]string) for _, file := range files { if file.IsDir() { continue @@ -281,10 +286,53 @@ func pullAndRender(appSlug string, licensePath string, configPath string) error if err != nil { return errors.Wrap(err, "failed to read file") } - manifetsToRender = append(manifetsToRender, string(content)) + manifetsToRender[file.Name()] = string(content) + } + for k, m := range manifetsToRender { + fmt.Println("---") + fmt.Printf("# Source: %s\n", k) + fmt.Println(m) } - for _, m := range manifetsToRender { - fmt.Printf("---\n%s\n", m) + + // also render helm charts with helm template if any + helmChartsDir := filepath.Join(tempDir, "helm") + if _, err := os.Stat(helmChartsDir); err == nil { + var chartPath string + var valuesPath string + + err := filepath.Walk(helmChartsDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + if filepath.Ext(path) == ".tgz" { + chartPath = path + } + + if info.Name() == "values.yaml" { + valuesPath = path + } + + return nil + }) + + if err != nil { + return errors.Wrap(err, "failed to walk helm charts directory") + } + + if chartPath != "" && valuesPath != "" { + args := []string{"template", "tmp-release", chartPath, "--values", valuesPath} + cmd := exec.Command("helm", args...) + output, err := cmd.CombinedOutput() + if err != nil { + return errors.Wrap(err, "failed to run helm template") + } + fmt.Println(string(output)) + } } return nil diff --git a/pkg/pull/pull.go b/pkg/pull/pull.go index 7ccb73fd20..f61c13b7d2 100644 --- a/pkg/pull/pull.go +++ b/pkg/pull/pull.go @@ -358,7 +358,7 @@ func Pull(upstreamURI string, pullOptions PullOptions) (string, error) { ReportWriter: pullOptions.ReportWriter, } - if needsConfig { + if needsConfig && pullOptions.ConfigFile == "" { if err := kotsutil.WriteKotsKinds(renderedKotsKindsMap, u.GetKotsKindsDir(writeUpstreamOptions)); err != nil { return "", errors.Wrap(err, "failed to write the rendered kots kinds") }