Skip to content

Commit f1567d3

Browse files
committed
use helm template to render if any
1 parent fc8fd21 commit f1567d3

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

cmd/kots/cli/template.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"os/exec"
78
"path/filepath"
89
"strings"
910

@@ -79,16 +80,20 @@ func TemplateCmd() *cobra.Command {
7980
if err != nil {
8081
return errors.Wrap(err, "failed to render raw template")
8182
}
82-
fmt.Println(rendered)
83+
log.Info(rendered)
8384
return nil
8485
}
8586

8687
// render all mode, similar to helm template
8788
// we will utilize pull command to fetch and render manifests from upstream
89+
log.ActionWithSpinner("Pulling app from upstream and rendering templates...")
8890
err := pullAndRender(license.Spec.AppSlug, licenseFile, configFile)
91+
log.FinishSpinner()
92+
8993
if err != nil {
9094
return errors.Wrap(err, "failed to render all templates")
9195
}
96+
9297
return nil
9398
}
9499

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

121-
fmt.Print(rendered)
126+
log.Info(rendered)
122127

123128
return nil
124129
},
@@ -272,7 +277,7 @@ func pullAndRender(appSlug string, licensePath string, configPath string) error
272277
return errors.Wrap(err, "failed to read kotsKinds directory")
273278
}
274279

275-
var manifetsToRender []string
280+
manifetsToRender := make(map[string]string)
276281
for _, file := range files {
277282
if file.IsDir() {
278283
continue
@@ -281,10 +286,53 @@ func pullAndRender(appSlug string, licensePath string, configPath string) error
281286
if err != nil {
282287
return errors.Wrap(err, "failed to read file")
283288
}
284-
manifetsToRender = append(manifetsToRender, string(content))
289+
manifetsToRender[file.Name()] = string(content)
290+
}
291+
for k, m := range manifetsToRender {
292+
fmt.Println("---")
293+
fmt.Printf("# Source: %s\n", k)
294+
fmt.Println(m)
285295
}
286-
for _, m := range manifetsToRender {
287-
fmt.Printf("---\n%s\n", m)
296+
297+
// also render helm charts with helm template if any
298+
helmChartsDir := filepath.Join(tempDir, "helm")
299+
if _, err := os.Stat(helmChartsDir); err == nil {
300+
var chartPath string
301+
var valuesPath string
302+
303+
err := filepath.Walk(helmChartsDir, func(path string, info os.FileInfo, err error) error {
304+
if err != nil {
305+
return err
306+
}
307+
308+
if info.IsDir() {
309+
return nil
310+
}
311+
312+
if filepath.Ext(path) == ".tgz" {
313+
chartPath = path
314+
}
315+
316+
if info.Name() == "values.yaml" {
317+
valuesPath = path
318+
}
319+
320+
return nil
321+
})
322+
323+
if err != nil {
324+
return errors.Wrap(err, "failed to walk helm charts directory")
325+
}
326+
327+
if chartPath != "" && valuesPath != "" {
328+
args := []string{"template", "tmp-release", chartPath, "--values", valuesPath}
329+
cmd := exec.Command("helm", args...)
330+
output, err := cmd.CombinedOutput()
331+
if err != nil {
332+
return errors.Wrap(err, "failed to run helm template")
333+
}
334+
fmt.Println(string(output))
335+
}
288336
}
289337

290338
return nil

pkg/pull/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func Pull(upstreamURI string, pullOptions PullOptions) (string, error) {
358358
ReportWriter: pullOptions.ReportWriter,
359359
}
360360

361-
if needsConfig {
361+
if needsConfig && pullOptions.ConfigFile == "" {
362362
if err := kotsutil.WriteKotsKinds(renderedKotsKindsMap, u.GetKotsKindsDir(writeUpstreamOptions)); err != nil {
363363
return "", errors.Wrap(err, "failed to write the rendered kots kinds")
364364
}

0 commit comments

Comments
 (0)