@@ -21,11 +21,11 @@ import (
2121 v3chart "github.com/werf/nelm/pkg/helm/intern/chart/v3"
2222 chartv3util "github.com/werf/nelm/pkg/helm/intern/chart/v3/util"
2323 "github.com/werf/nelm/pkg/helm/pkg/action"
24- helmchartiface "github.com/werf/nelm/pkg/helm/pkg/chart"
24+ helmchart "github.com/werf/nelm/pkg/helm/pkg/chart"
2525 chartcommon "github.com/werf/nelm/pkg/helm/pkg/chart/common"
2626 chartcommonutil "github.com/werf/nelm/pkg/helm/pkg/chart/common/util"
2727 "github.com/werf/nelm/pkg/helm/pkg/chart/loader"
28- helmchart "github.com/werf/nelm/pkg/helm/pkg/chart/v2"
28+ v2chart "github.com/werf/nelm/pkg/helm/pkg/chart/v2"
2929 chartv2util "github.com/werf/nelm/pkg/helm/pkg/chart/v2/util"
3030 "github.com/werf/nelm/pkg/helm/pkg/cli/values"
3131 helmdownloader "github.com/werf/nelm/pkg/helm/pkg/downloader"
@@ -60,7 +60,7 @@ type RenderChartOptions struct {
6060}
6161
6262type RenderChartResult struct {
63- Chart * helmchart .Chart
63+ Chart * v2chart .Chart
6464 Notes string
6565 ReleaseConfig map [string ]interface {}
6666 ResourceSpecs []* spec.ResourceSpec
@@ -128,20 +128,20 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
128128 }
129129
130130 var (
131- chartV2 * helmchart .Chart
131+ chartV2 * v2chart .Chart
132132 chartV3 * v3chart.Chart
133133 )
134134
135135 switch c := loadedChart .(type ) {
136- case * helmchart .Chart :
136+ case * v2chart .Chart :
137137 chartV2 = c
138138 case * v3chart.Chart :
139139 chartV3 = c
140140 default :
141141 return nil , fmt .Errorf ("loaded chart has unexpected type %T" , loadedChart )
142142 }
143143
144- chartAccessor , err := helmchartiface .NewAccessor (loadedChart )
144+ chartAccessor , err := helmchart .NewAccessor (loadedChart )
145145 if err != nil {
146146 return nil , fmt .Errorf ("create chart accessor: %w" , err )
147147 }
@@ -165,8 +165,6 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
165165 log .Default .TraceStruct (ctx , loadedChart , "Chart after processing dependencies:" )
166166 log .Default .TraceStruct (ctx , overrideValues , "Merged override values after processing dependencies:" )
167167
168- chartName := chartAccessor .Name ()
169-
170168 var chartKubeVersion string
171169 if chartV2 != nil {
172170 chartKubeVersion = chartV2 .Metadata .KubeVersion
@@ -180,7 +178,7 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
180178 Remote : opts .Remote ,
181179 })
182180 if err != nil {
183- return nil , fmt .Errorf ("build capabilities for chart %q: %w" , chartName , err )
181+ return nil , fmt .Errorf ("build capabilities for chart %q: %w" , chartAccessor . Name () , err )
184182 }
185183
186184 log .Default .TraceStruct (ctx , caps , "Capabilities:" )
@@ -224,7 +222,7 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
224222 IsUpgrade : isUpgrade ,
225223 }, caps )
226224 if err != nil {
227- return nil , fmt .Errorf ("build rendered values for chart %q: %w" , chartName , err )
225+ return nil , fmt .Errorf ("build rendered values for chart %q: %w" , chartAccessor . Name () , err )
228226 }
229227
230228 renderedValues ["Runtime" ] = runtime
@@ -251,52 +249,58 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
251249 var resources []* spec.ResourceSpec
252250
253251 if ! opts .NoStandaloneCRDs {
252+ type crdRef struct {
253+ data []byte
254+ filename string
255+ }
256+
257+ var crds []crdRef
258+
254259 if chartV2 != nil {
255260 for _ , crd := range chartV2 .CRDObjects () {
256- for _ , manifest := range util .SplitManifests (string (crd .File .Data )) {
257- if res , err := spec .NewResourceSpecFromManifest (manifest , releaseNamespace , spec.ResourceSpecOptions {
258- StoreAs : common .StoreAsNone ,
259- FilePath : crd .Filename ,
260- }); err != nil {
261- return nil , fmt .Errorf ("construct standalone CRD for chart at %q: %w" , chartPath , err )
262- } else {
263- resources = append (resources , res )
264- }
265- }
261+ crds = append (crds , crdRef {data : crd .File .Data , filename : crd .Filename })
266262 }
267263 } else {
268264 for _ , crd := range chartV3 .CRDObjects () {
269- for _ , manifest := range util .SplitManifests (string (crd .File .Data )) {
270- if res , err := spec .NewResourceSpecFromManifest (manifest , releaseNamespace , spec.ResourceSpecOptions {
271- StoreAs : common .StoreAsNone ,
272- FilePath : crd .Filename ,
273- }); err != nil {
274- return nil , fmt .Errorf ("construct standalone CRD for chart at %q: %w" , chartPath , err )
275- } else {
276- resources = append (resources , res )
277- }
265+ crds = append (crds , crdRef {data : crd .File .Data , filename : crd .Filename })
266+ }
267+ }
268+
269+ for _ , crd := range crds {
270+ for _ , manifest := range util .SplitManifests (string (crd .data )) {
271+ if res , err := spec .NewResourceSpecFromManifest (manifest , releaseNamespace , spec.ResourceSpecOptions {
272+ StoreAs : common .StoreAsNone ,
273+ FilePath : crd .filename ,
274+ }); err != nil {
275+ return nil , fmt .Errorf ("construct standalone CRD for chart at %q: %w" , chartPath , err )
276+ } else {
277+ resources = append (resources , res )
278278 }
279279 }
280280 }
281281 }
282282
283283 renderedTemplates , err := engine .Render (ctx , loadedChart , renderedValues )
284284 if err != nil {
285- return nil , fmt .Errorf ("render resources for chart %q: %w" , chartName , err )
285+ return nil , fmt .Errorf ("render resources for chart %q: %w" , chartAccessor . Name () , err )
286286 }
287287
288288 if featgate .FeatGateTypescript .Enabled () {
289+ var tsChart * v2chart.Chart
289290 if chartV2 != nil {
290- jsRenderedTemplates , err := renderJSTemplates (ctx , chartPath , chartV2 , renderedValues )
291- if err != nil {
292- return nil , fmt .Errorf ("render ts chart templates for chart %q: %w" , chartName , err )
293- }
294-
295- if len (jsRenderedTemplates ) > 0 {
296- maps .Copy (renderedTemplates , jsRenderedTemplates )
297- }
291+ tsChart = chartV2
298292 } else {
299- log .Default .Debug (ctx , "Skipping TypeScript rendering for v3 chart" )
293+ // TODO(major): refactor to allow native v3 chart handling in TypeScript rendering
294+ tsChart = convertV3ToV2 (chartV3 )
295+ }
296+
297+ jsRenderedTemplates , err := renderJSTemplates (ctx , chartPath , tsChart , renderedValues )
298+ if err != nil {
299+ return nil , fmt .Errorf ("render ts chart templates for chart %q: %w" , chartAccessor .Name (), err )
300+ }
301+
302+ if len (jsRenderedTemplates ) > 0 {
303+ maps .Copy (renderedTemplates , jsRenderedTemplates )
300304 }
301305 }
302306
@@ -308,23 +312,108 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
308312 resources = append (resources , r ... )
309313 }
310314
311- notes := buildChartNotes (chartName , renderedTemplates , opts .SubchartNotes )
315+ notes := buildChartNotes (chartAccessor . Name () , renderedTemplates , opts .SubchartNotes )
312316
313317 log .Default .TraceStruct (ctx , notes , "Rendered notes:" )
314318
315319 sort .SliceStable (resources , func (i , j int ) bool {
316320 return spec .ResourceSpecSortHandler (resources [i ], resources [j ])
317321 })
318322
323+ var resultChart * v2chart.Chart
324+ if chartV2 != nil {
325+ resultChart = chartV2
326+ } else {
327+ // TODO(major): refactor to allow native v3 chart handling in nelm
328+ resultChart = convertV3ToV2 (chartV3 )
329+ }
330+
319331 return & RenderChartResult {
320- Chart : chartV2 ,
332+ Chart : resultChart ,
321333 Notes : notes ,
322334 ReleaseConfig : overrideValues ,
323335 ResourceSpecs : resources ,
324336 Values : renderedValues .AsMap (),
325337 }, nil
326338}
327339
340+ func convertV3ToV2 (src * v3chart.Chart ) * v2chart.Chart {
341+ dst := & v2chart.Chart {
342+ Raw : src .Raw ,
343+ Templates : src .Templates ,
344+ Values : src .Values ,
345+ Schema : src .Schema ,
346+ SchemaModTime : src .SchemaModTime ,
347+ Files : src .Files ,
348+ ModTime : src .ModTime ,
349+ RuntimeFiles : src .RuntimeFiles ,
350+ RuntimeDepsFiles : src .RuntimeDepsFiles ,
351+ ExtraValues : src .ExtraValues ,
352+ SecretsRuntimeData : src .SecretsRuntimeData ,
353+ }
354+
355+ if src .Metadata != nil {
356+ dst .Metadata = convertV3MetadataToV2 (src .Metadata )
357+ }
358+
359+ if src .Lock != nil {
360+ dst .Lock = convertV3LockToV2 (src .Lock )
361+ }
362+
363+ for _ , dep := range src .Dependencies () {
364+ dst .AddDependency (convertV3ToV2 (dep ))
365+ }
366+
367+ return dst
368+ }
369+
370+ func convertV3LockToV2 (src * v3chart.Lock ) * v2chart.Lock {
371+ dst := & v2chart.Lock {
372+ Generated : src .Generated ,
373+ Digest : src .Digest ,
374+ }
375+
376+ for _ , dependency := range src .Dependencies {
377+ dst .Dependencies = append (dst .Dependencies , convertV3DependencyToV2 (dependency ))
378+ }
379+
380+ return dst
381+ }
382+
383+ func convertV3MetadataToV2 (src * v3chart.Metadata ) * v2chart.Metadata {
384+ dst := & v2chart.Metadata {
385+ Name : src .Name ,
386+ Home : src .Home ,
387+ Sources : src .Sources ,
388+ Version : src .Version ,
389+ Description : src .Description ,
390+ Keywords : src .Keywords ,
391+ Icon : src .Icon ,
392+ APIVersion : src .APIVersion ,
393+ Condition : src .Condition ,
394+ Tags : src .Tags ,
395+ AppVersion : src .AppVersion ,
396+ Deprecated : src .Deprecated ,
397+ Annotations : src .Annotations ,
398+ KubeVersion : src .KubeVersion ,
399+ Type : src .Type ,
400+ }
401+
402+ for _ , maintainer := range src .Maintainers {
403+ dst .Maintainers = append (dst .Maintainers , & v2chart.Maintainer {
404+ Name : maintainer .Name ,
405+ Email : maintainer .Email ,
406+ URL : maintainer .URL ,
407+ })
408+ }
409+
410+ for _ , dependency := range src .Dependencies {
411+ dst .Dependencies = append (dst .Dependencies , convertV3DependencyToV2 (dependency ))
412+ }
413+
414+ return dst
415+ }
416+
328417func buildChartCapabilities (ctx context.Context , clientFactory kube.ClientFactorier , opts buildChartCapabilitiesOptions ) (* chartcommon.Capabilities , error ) {
329418 capabilities := & chartcommon.Capabilities {
330419 HelmVersion : chartcommon .DefaultCapabilities .HelmVersion ,
@@ -417,6 +506,19 @@ func buildContextFromJSONSets(jsonSets []string) (map[string]interface{}, error)
417506 return context , nil
418507}
419508
509+ func convertV3DependencyToV2 (src * v3chart.Dependency ) * v2chart.Dependency {
510+ return & v2chart.Dependency {
511+ Name : src .Name ,
512+ Version : src .Version ,
513+ Repository : src .Repository ,
514+ Condition : src .Condition ,
515+ Tags : src .Tags ,
516+ Enabled : src .Enabled ,
517+ ImportValues : src .ImportValues ,
518+ Alias : src .Alias ,
519+ }
520+ }
521+
420522func envOr (envVar , defaultVal string ) string {
421523 if v := os .Getenv (envVar ); v != "" {
422524 return v
@@ -442,7 +544,7 @@ func parseVerificationStrategy(s string) helmdownloader.VerificationStrategy {
442544 }
443545}
444546
445- func renderJSTemplates (ctx context.Context , chartPath string , chart * helmchart .Chart , renderedValues chartcommon.Values ) (map [string ]string , error ) {
547+ func renderJSTemplates (ctx context.Context , chartPath string , chart * v2chart .Chart , renderedValues chartcommon.Values ) (map [string ]string , error ) {
446548 log .Default .Debug (ctx , "Rendering TypeScript resources for chart %q and its dependencies" , chart .Name ())
447549
448550 result , err := ts .RenderChart (ctx , chart , renderedValues )
@@ -484,7 +586,7 @@ func renderedTemplatesToResourceSpecs(renderedTemplates map[string]string, relea
484586 return resources , nil
485587}
486588
487- func validateChart (ctx context.Context , chrt helmchartiface .Charter , acc helmchartiface .Accessor ) error {
589+ func validateChart (ctx context.Context , chrt helmchart .Charter , acc helmchart .Accessor ) error {
488590 if chrt == nil {
489591 return fmt .Errorf ("load chart: missing chart" )
490592 }
0 commit comments