@@ -4,9 +4,12 @@ import (
44 "context"
55 "fmt"
66 "maps"
7+ "os"
78 "strconv"
89
910 "github.com/replicatedhq/embedded-cluster/api/types"
11+ "github.com/replicatedhq/embedded-cluster/pkg-new/constants"
12+ "github.com/replicatedhq/embedded-cluster/pkg/helm"
1013 kotsv1beta2 "github.com/replicatedhq/kotskinds/apis/kots/v1beta2"
1114 kyaml "sigs.k8s.io/yaml"
1215)
@@ -63,6 +66,77 @@ func (m *appReleaseManager) TemplateHelmChartCRs(ctx context.Context, configValu
6366 return templatedCRs , nil
6467}
6568
69+ // DryRunHelmChart finds the corresponding chart archive and performs a dry run templating of a Helm chart
70+ func (m * appReleaseManager ) DryRunHelmChart (ctx context.Context , templatedCR * kotsv1beta2.HelmChart ) ([][]byte , error ) {
71+ if templatedCR == nil {
72+ return nil , fmt .Errorf ("templated CR is nil" )
73+ }
74+
75+ if m .releaseData == nil {
76+ return nil , fmt .Errorf ("release data not initialized" )
77+ }
78+
79+ // Check if the chart should be excluded
80+ if ! templatedCR .Spec .Exclude .IsEmpty () {
81+ exclude , err := templatedCR .Spec .Exclude .Boolean ()
82+ if err != nil {
83+ return nil , fmt .Errorf ("parse templated CR exclude: %w" , err )
84+ }
85+ if exclude {
86+ return nil , nil
87+ }
88+ }
89+
90+ // Find the corresponding chart archive for this HelmChart CR
91+ chartArchive , err := findChartArchive (m .releaseData .HelmChartArchives , templatedCR )
92+ if err != nil {
93+ return nil , fmt .Errorf ("find chart archive for %s: %w" , templatedCR .Name , err )
94+ }
95+
96+ // Generate Helm values from the templated CR
97+ helmValues , err := m .GenerateHelmValues (ctx , templatedCR )
98+ if err != nil {
99+ return nil , fmt .Errorf ("generate helm values for %s: %w" , templatedCR .Name , err )
100+ }
101+
102+ // Create a Helm client for dry run templating
103+ helmClient , err := helm .NewClient (helm.HelmOptions {})
104+ if err != nil {
105+ return nil , fmt .Errorf ("create helm client: %w" , err )
106+ }
107+ defer helmClient .Close ()
108+
109+ // Write chart archive to a temporary file
110+ chartPath , err := writeChartArchiveToTemp (chartArchive )
111+ if err != nil {
112+ return nil , fmt .Errorf ("write chart archive to temp: %w" , err )
113+ }
114+ defer os .Remove (chartPath )
115+
116+ // Fallback to admin console namespace if namespace is not set
117+ namespace := templatedCR .GetNamespace ()
118+ if namespace == "" {
119+ namespace = constants .KotsadmNamespace
120+ }
121+
122+ // Prepare install options for dry run
123+ installOpts := helm.InstallOptions {
124+ ReleaseName : templatedCR .GetReleaseName (),
125+ ChartPath : chartPath ,
126+ ChartVersion : templatedCR .GetChartVersion (),
127+ Values : helmValues ,
128+ Namespace : namespace ,
129+ }
130+
131+ // Perform dry run rendering
132+ manifests , err := helmClient .Render (ctx , installOpts )
133+ if err != nil {
134+ return nil , fmt .Errorf ("render helm chart %s: %w" , templatedCR .Name , err )
135+ }
136+
137+ return manifests , nil
138+ }
139+
66140// GenerateHelmValues generates Helm values for a single templated HelmChart custom resource
67141func (m * appReleaseManager ) GenerateHelmValues (ctx context.Context , templatedCR * kotsv1beta2.HelmChart ) (map [string ]any , error ) {
68142 if templatedCR == nil {
@@ -100,10 +174,10 @@ func (m *appReleaseManager) GenerateHelmValues(ctx context.Context, templatedCR
100174 }
101175
102176 // Convert MappedChartValue to standard Go interface{} using GetHelmValues
103- chartValues , err := templatedCR .Spec .GetHelmValues (mergedValues )
177+ helmValues , err := templatedCR .Spec .GetHelmValues (mergedValues )
104178 if err != nil {
105179 return nil , fmt .Errorf ("get helm values for chart %s: %w" , templatedCR .Name , err )
106180 }
107181
108- return chartValues , nil
182+ return helmValues , nil
109183}
0 commit comments