Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/nelm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newChartCommand(ctx context.Context, afterAllCommandsBuiltFuncs map[*cobra.
cli.GroupCommandOptions{},
)

cmd.AddCommand(newChartInitCommand(ctx, afterAllCommandsBuiltFuncs))
cmd.AddCommand(newChartRenderCommand(ctx, afterAllCommandsBuiltFuncs))
cmd.AddCommand(newChartDependencyCommand(ctx, afterAllCommandsBuiltFuncs))
cmd.AddCommand(newChartDownloadCommand(ctx, afterAllCommandsBuiltFuncs))
Expand Down
89 changes: 89 additions & 0 deletions cmd/nelm/chart_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"cmp"
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/werf/common-go/pkg/cli"
"github.com/werf/nelm/pkg/action"
"github.com/werf/nelm/pkg/common"
"github.com/werf/nelm/pkg/log"
)

type chartInitConfig struct {
action.ChartInitOptions

LogColorMode string
LogLevel string
}

func newChartInitCommand(ctx context.Context, afterAllCommandsBuiltFuncs map[*cobra.Command]func(cmd *cobra.Command) error) *cobra.Command {
cfg := &chartInitConfig{}

cmd := cli.NewSubCommand(
ctx,
"init [PATH]",
"Initialize a new chart.",
"Initialize a new chart in the specified directory. If PATH is not specified, uses the current directory.",
10, // priority for ordering in help
chartCmdGroup,
cli.SubCommandOptions{
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
},
},
func(cmd *cobra.Command, args []string) error {
ctx = log.SetupLogging(ctx, cmp.Or(log.Level(cfg.LogLevel), log.InfoLevel), log.SetupLoggingOptions{
ColorMode: cfg.LogColorMode,
})

if len(args) > 0 {
cfg.ChartDirPath = args[0]
}

if err := action.ChartInit(ctx, cfg.ChartInitOptions); err != nil {
return fmt.Errorf("chart init: %w", err)
}

return nil
},
)

afterAllCommandsBuiltFuncs[cmd] = func(cmd *cobra.Command) error {
if err := cli.AddFlag(cmd, &cfg.TS, "ts", false, "Initialize TypeScript chart", cli.AddFlagOptions{
Group: mainFlagGroup,
}); err != nil {
return fmt.Errorf("add flag: %w", err)
}

if err := cli.AddFlag(cmd, &cfg.TempDirPath, "temp-dir", "", "The directory for temporary files. By default, create a new directory in the default system directory for temporary files", cli.AddFlagOptions{
GetEnvVarRegexesFunc: cli.GetFlagGlobalEnvVarRegexes,
Group: miscFlagGroup,
Type: cli.FlagTypeDir,
}); err != nil {
return fmt.Errorf("add flag: %w", err)
}

if err := cli.AddFlag(cmd, &cfg.LogColorMode, "color-mode", common.DefaultLogColorMode, "Color mode for logs. "+allowedLogColorModesHelp(), cli.AddFlagOptions{
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
Group: miscFlagGroup,
}); err != nil {
return fmt.Errorf("add flag: %w", err)
}

if err := cli.AddFlag(cmd, &cfg.LogLevel, "log-level", string(log.InfoLevel), "Set log level. "+allowedLogLevelsHelp(), cli.AddFlagOptions{
GetEnvVarRegexesFunc: cli.GetFlagGlobalAndLocalEnvVarRegexes,
Group: miscFlagGroup,
}); err != nil {
return fmt.Errorf("add flag: %w", err)
}

return nil
}

return cmd
}
12 changes: 12 additions & 0 deletions cmd/nelm/chart_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"strings"

"github.com/samber/lo"
Expand All @@ -10,6 +11,8 @@ import (
helm_v3 "github.com/werf/3p-helm/cmd/helm"
"github.com/werf/3p-helm/pkg/chart/loader"
"github.com/werf/common-go/pkg/cli"
"github.com/werf/nelm/internal/tschart"
"github.com/werf/nelm/pkg/featgate"
"github.com/werf/nelm/pkg/log"
)

Expand All @@ -33,6 +36,15 @@ func newChartPackCommand(ctx context.Context, afterAllCommandsBuiltFuncs map[*co

loader.NoChartLockWarning = ""

if featgate.FeatGateTypescript.Enabled() {
transformer := tschart.NewTransformer()
for _, chartPath := range args {
if err := transformer.TransformChartDir(ctx, chartPath); err != nil {
return fmt.Errorf("transform TypeScript in %q: %w", chartPath, err)
}
}
}

if err := originalRunE(cmd, args); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ require (
github.com/docker/cli v25.0.5+incompatible
github.com/docker/docker v25.0.5+incompatible
github.com/dominikbraun/graph v0.23.0
github.com/dop251/goja v0.0.0-20251121114222-56b1242a5f86
github.com/dop251/goja_nodejs v0.0.0-20251015164255-5e94316bedaf
github.com/evanphx/json-patch v5.8.0+incompatible
github.com/evanw/esbuild v0.27.0
github.com/fluxcd/flagger v1.36.1
github.com/goccy/go-yaml v1.15.23
github.com/google/go-cmp v0.6.0
Expand Down Expand Up @@ -94,6 +97,7 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,16 @@ github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucVPgCo=
github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc=
github.com/dop251/goja v0.0.0-20251121114222-56b1242a5f86 h1:iY/kk+Fw7k49PRM4cS2wz9CVxO0jB61+h//XN9bbAS4=
github.com/dop251/goja v0.0.0-20251121114222-56b1242a5f86/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
github.com/dop251/goja_nodejs v0.0.0-20251015164255-5e94316bedaf h1:gbmvliZnCut4NjaPSNOQlfqBoZ9C5Dpf72mHMMYhgVE=
github.com/dop251/goja_nodejs v0.0.0-20251015164255-5e94316bedaf/go.mod h1:Tb7Xxye4LX7cT3i8YLvmPMGCV92IOi4CDZvm/V8ylc0=
github.com/emicklei/go-restful/v3 v3.11.2 h1:1onLa9DcsMYO9P+CXaL0dStDqQ2EHHXLiz+BtnqkLAU=
github.com/emicklei/go-restful/v3 v3.11.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v5.8.0+incompatible h1:1Av9pn2FyxPdvrWNQszj1g6D6YthSmvCfcN6SYclTJg=
github.com/evanphx/json-patch v5.8.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanw/esbuild v0.27.0 h1:1fbrgepqU1rZeu4VPcQRZJpvIfQpbrYqRr1wJdeMkfM=
github.com/evanw/esbuild v0.27.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f h1:Wl78ApPPB2Wvf/TIe2xdyJxTlb6obmF18d8QdkxNDu4=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f/go.mod h1:OSYXu++VVOHnXeitef/D8n/6y4QV8uLHSFXX4NeXMGc=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
Expand Down Expand Up @@ -148,6 +154,8 @@ github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF
github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q=
github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
Expand Down
40 changes: 37 additions & 3 deletions internal/chart/chart_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"maps"
"os"
"path"
"path/filepath"
Expand All @@ -16,7 +17,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/werf/3p-helm/pkg/action"
"github.com/werf/3p-helm/pkg/chart"
helmchart "github.com/werf/3p-helm/pkg/chart"
"github.com/werf/3p-helm/pkg/chart/loader"
"github.com/werf/3p-helm/pkg/chartutil"
"github.com/werf/3p-helm/pkg/cli"
Expand All @@ -31,7 +32,9 @@ import (
"github.com/werf/3p-helm/pkg/werf/helmopts"
"github.com/werf/nelm/internal/kube"
"github.com/werf/nelm/internal/resource/spec"
"github.com/werf/nelm/internal/tschart"
"github.com/werf/nelm/pkg/common"
"github.com/werf/nelm/pkg/featgate"
"github.com/werf/nelm/pkg/log"
)

Expand All @@ -53,7 +56,7 @@ type RenderChartOptions struct {
}

type RenderChartResult struct {
Chart *chart.Chart
Chart *helmchart.Chart
Notes string
ReleaseConfig map[string]interface{}
ResourceSpecs []*spec.ResourceSpec
Expand Down Expand Up @@ -147,6 +150,7 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
log.Default.TraceStruct(ctx, runtime, "Runtime:")

var isUpgrade bool

switch deployType {
case common.DeployTypeUpgrade, common.DeployTypeRollback:
isUpgrade = true
Expand Down Expand Up @@ -204,6 +208,17 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
return nil, fmt.Errorf("render resources for chart %q: %w", chart.Name(), err)
}

if featgate.FeatGateTypescript.Enabled() {
jsRenderedTemplates, err := renderJSTemplates(ctx, chartPath, chart, renderedValues)
if err != nil {
return nil, fmt.Errorf("render ts chart templates for chart %q: %w", chart.Name(), err)
}

if len(jsRenderedTemplates) > 0 {
maps.Copy(renderedTemplates, jsRenderedTemplates)
}
}

log.Default.TraceStruct(ctx, renderedTemplates, "Rendered contents of templates/:")

if r, err := renderedTemplatesToResourceSpecs(renderedTemplates, releaseNamespace, opts); err != nil {
Expand All @@ -229,7 +244,25 @@ func RenderChart(ctx context.Context, chartPath, releaseName, releaseNamespace s
}, nil
}

func validateChart(ctx context.Context, chart *chart.Chart) error {
func renderJSTemplates(
ctx context.Context,
chartPath string,
chart *helmchart.Chart,
renderedValues chartutil.Values,
) (map[string]string, error) {
log.Default.Debug(ctx, "Rendering TypeScript resources for chart %q and its dependencies", chart.Name())

jsEngine := tschart.NewEngine()

jsRenderedTemplates, err := jsEngine.RenderChartWithDependencies(ctx, chartPath, chart, renderedValues)
if err != nil {
return nil, err
}

return jsRenderedTemplates, nil
}

func validateChart(ctx context.Context, chart *helmchart.Chart) error {
if chart == nil {
return fmt.Errorf("load chart: %w", action.ErrMissingChart())
}
Expand All @@ -253,6 +286,7 @@ func validateChart(ctx context.Context, chart *chart.Chart) error {

func renderedTemplatesToResourceSpecs(renderedTemplates map[string]string, releaseNamespace string, opts RenderChartOptions) ([]*spec.ResourceSpec, error) {
var resources []*spec.ResourceSpec

for filePath, fileContent := range renderedTemplates {
if strings.HasPrefix(path.Base(filePath), "_") ||
strings.HasSuffix(filePath, action.NotesFileSuffix) ||
Expand Down
14 changes: 14 additions & 0 deletions internal/tschart/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tschart

import (
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
)

func SetupConsoleGlobal(runtime *goja.Runtime) {
registry := require.NewRegistry()
registry.Enable(runtime)

console.Enable(runtime)
}
Loading