Skip to content

Commit

Permalink
use helm template to render if any
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed Apr 4, 2024
1 parent fc8fd21 commit f1567d3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
60 changes: 54 additions & 6 deletions cmd/kots/cli/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -118,7 +123,7 @@ func TemplateCmd() *cobra.Command {
return errors.Wrap(err, "failed to render template")
}

fmt.Print(rendered)
log.Info(rendered)

return nil
},
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit f1567d3

Please sign in to comment.