Skip to content

Commit 07fc8dd

Browse files
committed
fix: include rendered templates in helm manifest generation
Signed-off-by: PARTHIV PRATIM SAIKIA <parthivsaikia985@gmail.com>
1 parent da57e8d commit 07fc8dd

1 file changed

Lines changed: 61 additions & 5 deletions

File tree

utils/kubernetes/get-manifests.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,79 @@
11
package kubernetes
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"helm.sh/helm/v3/pkg/chart/loader"
8+
"helm.sh/helm/v3/pkg/chartutil"
9+
"helm.sh/helm/v3/pkg/engine"
510
)
611

12+
// GetManifestsFromHelm fetches the chart, loads it, and renders templates + CRDs
713
func GetManifestsFromHelm(url string) (string, error) {
814
chartLocation, err := fetchHelmChart(url, "")
915
if err != nil {
10-
return "", ErrApplyHelmChart(err)
16+
return "", ErrApplyHelmChart(fmt.Errorf("failed to fetch helm chart: %w", err))
1117
}
1218

1319
chart, err := loader.Load(chartLocation)
1420
if err != nil {
15-
return "", ErrApplyHelmChart(err)
21+
return "", ErrApplyHelmChart(fmt.Errorf("failed to load chart from %s: %w", chartLocation, err))
22+
}
23+
24+
releaseOptions := chartutil.ReleaseOptions{
25+
Name: "meshery-helm-release",
26+
Namespace: "default",
27+
Revision: 1,
28+
IsInstall: true,
1629
}
17-
manifests := ""
30+
31+
caps := chartutil.DefaultCapabilities
32+
33+
values, err := chartutil.ToRenderValues(chart, chartutil.Values{}, releaseOptions, caps)
34+
if err != nil {
35+
return "", ErrApplyHelmChart(fmt.Errorf("failed to generate render values: %w", err))
36+
}
37+
38+
renderedFiles, err := engine.Render(chart, values)
39+
if err != nil {
40+
return "", ErrApplyHelmChart(fmt.Errorf("failed to render chart templates: %w", err))
41+
}
42+
43+
var b strings.Builder
44+
45+
// Helper to safely append separators
46+
appendSeparator := func() {
47+
if b.Len() > 0 {
48+
b.WriteString("\n---\n")
49+
}
50+
}
51+
52+
// Append CRDs
1853
for _, crdobject := range chart.CRDObjects() {
19-
manifests += "\n---\n"
20-
manifests += string(crdobject.File.Data)
54+
appendSeparator()
55+
b.Write(crdobject.File.Data)
56+
}
57+
58+
// Append Rendered Templates
59+
for filename, fileContent := range renderedFiles {
60+
// Filter out non-manifest files
61+
if strings.HasSuffix(filename, "NOTES.txt") {
62+
continue
63+
}
64+
if strings.TrimSpace(fileContent) == "" {
65+
continue
66+
}
67+
68+
appendSeparator()
69+
b.WriteString(fileContent)
2170
}
71+
72+
manifests := b.String()
73+
74+
if strings.TrimSpace(manifests) == "" {
75+
return "", ErrApplyHelmChart(fmt.Errorf("chart rendered empty manifests"))
76+
}
77+
2278
return manifests, nil
2379
}

0 commit comments

Comments
 (0)