Skip to content

Commit 341be67

Browse files
committed
fix(chart): refactor v3 chart handling and revert Settings export scope
- Rename imports: helmchartiface→helmchart, helmchart→v2chart - Inline chartAccessor.Name() (remove chartName variable) - Deduplicate CRD loop with crdRef struct - Add convertV3ToV2() for v3→v2 chart conversion - Enable TS rendering for v3 charts via v3→v2 conversion - RenderChartResult.Chart always returns *v2chart.Chart - Revert Settings→settings in 30+ cmd files, add exported alias instead Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
1 parent 388d186 commit 341be67

35 files changed

Lines changed: 280 additions & 176 deletions

pkg/chart/chart_render.go

Lines changed: 145 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6262
type 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+
328417
func 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+
420522
func 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
}

pkg/helm/pkg/cmd/dependency_build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ func newDependencyBuildCmd(out io.Writer) *cobra.Command {
6666
ChartPath: chartpath,
6767
Keyring: client.Keyring,
6868
SkipUpdate: client.SkipRefresh,
69-
Getters: getter.All(Settings),
69+
Getters: getter.All(settings),
7070
RegistryClient: registryClient,
71-
RepositoryConfig: Settings.RepositoryConfig,
72-
RepositoryCache: Settings.RepositoryCache,
73-
ContentCache: Settings.ContentCache,
74-
Debug: Settings.Debug,
71+
RepositoryConfig: settings.RepositoryConfig,
72+
RepositoryCache: settings.RepositoryCache,
73+
ContentCache: settings.ContentCache,
74+
Debug: settings.Debug,
7575
}
7676
if client.Verify {
7777
man.Verify = downloader.VerifyIfPossible

pkg/helm/pkg/cmd/dependency_update.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ func newDependencyUpdateCmd(_ *action.Configuration, out io.Writer) *cobra.Comma
7070
ChartPath: chartpath,
7171
Keyring: client.Keyring,
7272
SkipUpdate: client.SkipRefresh,
73-
Getters: getter.All(Settings),
73+
Getters: getter.All(settings),
7474
RegistryClient: registryClient,
75-
RepositoryConfig: Settings.RepositoryConfig,
76-
RepositoryCache: Settings.RepositoryCache,
77-
ContentCache: Settings.ContentCache,
78-
Debug: Settings.Debug,
75+
RepositoryConfig: settings.RepositoryConfig,
76+
RepositoryCache: settings.RepositoryCache,
77+
ContentCache: settings.ContentCache,
78+
Debug: settings.Debug,
7979
}
8080
if client.Verify {
8181
man.Verify = downloader.VerifyAlways

pkg/helm/pkg/cmd/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newEnvCmd(out io.Writer) *cobra.Command {
4545
return noMoreArgsComp()
4646
},
4747
Run: func(_ *cobra.Command, args []string) {
48-
envVars := Settings.EnvVars()
48+
envVars := settings.EnvVars()
4949

5050
if len(args) == 0 {
5151
// Sort the variables by alphabetical order.
@@ -64,7 +64,7 @@ func newEnvCmd(out io.Writer) *cobra.Command {
6464
}
6565

6666
func getSortedEnvVarKeys() []string {
67-
envVars := Settings.EnvVars()
67+
envVars := settings.EnvVars()
6868

6969
var keys []string
7070
for k := range envVars {

pkg/helm/pkg/cmd/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func compVersionFlag(chartRef string, _ string) ([]string, cobra.ShellCompDirect
259259
repoName := chartInfo[0]
260260
chartName := chartInfo[1]
261261

262-
path := filepath.Join(Settings.RepositoryCache, helmpath.CacheIndexFile(repoName))
262+
path := filepath.Join(settings.RepositoryCache, helmpath.CacheIndexFile(repoName))
263263

264264
var versions []string
265265
if indexFile, err := repo.LoadIndexFile(path); err == nil {

pkg/helm/pkg/cmd/flags_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ func outputFlagCompletionTest(t *testing.T, cmdName string) {
102102
func TestPostRendererFlagSetOnce(t *testing.T) {
103103
cfg := action.Configuration{}
104104
client := action.NewInstall(&cfg)
105-
Settings.PluginsDirectory = "testdata/helmhome/helm/plugins"
105+
settings.PluginsDirectory = "testdata/helmhome/helm/plugins"
106106
str := postRendererString{
107107
options: &postRendererOptions{
108108
renderer: &client.PostRenderer,
109-
settings: Settings,
109+
settings: settings,
110110
},
111111
}
112112
// Set the plugin name once

pkg/helm/pkg/cmd/get_all.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6363
debug: true,
6464
showMetadata: true,
6565
hideNotes: false,
66-
noColor: Settings.ShouldDisableColor(),
66+
noColor: settings.ShouldDisableColor(),
6767
})
6868
},
6969
}

0 commit comments

Comments
 (0)