Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
override helm.sh/helm/v3/pkg/engine.recAllTpls to support values `.Te…
Browse files Browse the repository at this point in the history
…aChart`
  • Loading branch information
yp05327 committed Feb 20, 2024
1 parent d6995e9 commit 5451cff
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cmd/teachart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewInstallCmd(ctx context.Context, globalOptions *options.GlobalOptions) *c
if err != nil {
return errors.Wrap(err, "Create compose client error")
}
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), nil)
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), false)
if err != nil {
return errors.Wrap(err, "Create helm engine error")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/teachart/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func runLint(ctx context.Context, cmd *cobra.Command, opts lintOptions) error {
logrus.Debugf("Temp directory created:%s", tempDir)

// render templates
renderEngine, err := engine.NewRenderEngine(chartDir, opts.GetTeaChart(), &engine.NewEngineOptions{Strict: true})
renderEngine, err := engine.NewRenderEngine(chartDir, opts.GetTeaChart(), true)
if err != nil {
return nil, errors.Wrap(err, "Create helm engine error")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/teachart/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewTemplateCmd(ctx context.Context, globalOptions *options.GlobalOptions) *
}

func runTemplate(ctx context.Context, cmd *cobra.Command, opts templateOptions) error {
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), nil)
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), false)
if err != nil {
return errors.Wrap(err, "Create helm engine error")
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
package engine

import (
"helm.sh/helm/v3/pkg/cli/values"
"github.com/yp05327/teachart/pkg/engine/helm"
"github.com/yp05327/teachart/pkg/values"
helm_values "helm.sh/helm/v3/pkg/cli/values"
)

type NewEngineOptions struct {
Strict bool
}
type RenderEngine interface {
Render(valueOpts values.Options, save bool) (map[string]string, error)
Render(valueOpts helm_values.Options, save bool) (map[string]string, error)
GetConfigPaths(files map[string]string) []string
}

func NewRenderEngine(chartDir string, teachart *TeaChart, opts *NewEngineOptions) (RenderEngine, error) {
return newHelm(chartDir, teachart, opts)
func NewRenderEngine(chartDir string, teachart *values.TeaChart, strict bool) (RenderEngine, error) {
return helm.New(chartDir, teachart, strict)
}
9 changes: 5 additions & 4 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import (

"github.com/stretchr/testify/assert"
"github.com/yp05327/teachart/pkg/app"
"helm.sh/helm/v3/pkg/cli/values"
"github.com/yp05327/teachart/pkg/values"
helm_values "helm.sh/helm/v3/pkg/cli/values"
)

const testChartDir = "../../tests/data/chart"
const testValuesFile = "../../tests/data/values.yaml"

var testTeaChart = TeaChart{
var testTeaChart = values.TeaChart{
ProjectName: "test",
ProjectDir: "",
TempDir: app.DefaultTemplatesDir,
}

func TestHelmRender(t *testing.T) {
e, err := NewRenderEngine(testChartDir, &testTeaChart, &NewEngineOptions{Strict: true})
e, err := NewRenderEngine(testChartDir, &testTeaChart, true)
assert.NoError(t, err)

_, err = e.Render(values.Options{
_, err = e.Render(helm_values.Options{
ValueFiles: []string{testValuesFile},
}, false)
assert.NoError(t, err)
Expand Down
40 changes: 10 additions & 30 deletions pkg/engine/helm.go → pkg/engine/helm/helm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2024 TeaChart Authors

package engine
package helm

import (
"fmt"
Expand All @@ -11,10 +11,11 @@ import (
"strings"

"github.com/yp05327/teachart/pkg/app"
"github.com/yp05327/teachart/pkg/values"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli/values"
helm_values "helm.sh/helm/v3/pkg/cli/values"
helm_engine "helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/getter"
)
Expand All @@ -23,55 +24,35 @@ type Helm struct {
engine *helm_engine.Engine
chart *chart.Chart

teachart *TeaChart
teachart *values.TeaChart
}

func newHelm(chartDir string, teachart *TeaChart, opts *NewEngineOptions) (*Helm, error) {
func New(chartDir string, teachart *values.TeaChart, strict bool) (*Helm, error) {
chart, err := loader.Load(chartDir)
if err != nil {
return nil, err
}
teachart.Metadata = chart.Metadata

// TODO support Dependencies

if opts == nil {
opts = &NewEngineOptions{}
}
return &Helm{
engine: &helm_engine.Engine{
Strict: opts.Strict,
Strict: strict,
},
chart: chart,
teachart: teachart,
}, nil
}

func (h *Helm) Render(valueOpts values.Options, save bool) (map[string]string, error) {
renderValues, err := h.getRenderValues(valueOpts)
if err != nil {
return nil, err
}
files, err := h.engine.Render(h.chart, renderValues)
if err != nil {
return nil, err
}
if save {
err = h.save(files)
}
return files, err
}

func (h *Helm) getRenderValues(valueOpts values.Options) (chartutil.Values, error) {
func (h *Helm) getRenderValues(valueOpts helm_values.Options) (chartutil.Values, error) {
// user define values
userValues, err := valueOpts.MergeValues(getter.Providers{})
if err != nil {
return nil, err
}

top := map[string]interface{}{
"Chart": h.chart.Metadata,
}

userValues["TeaChart"] = h.teachart
top := map[string]interface{}{}
values, err := chartutil.CoalesceValues(h.chart, userValues)
if err != nil {
return top, err
Expand All @@ -83,7 +64,6 @@ func (h *Helm) getRenderValues(valueOpts values.Options) (chartutil.Values, erro
}
top["Values"] = values
return top, nil

}

func (h *Helm) save(files map[string]string) error {
Expand Down
97 changes: 97 additions & 0 deletions pkg/engine/helm/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package helm

import (
"path"
_ "unsafe"

"github.com/yp05327/teachart/pkg/values"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
helm_values "helm.sh/helm/v3/pkg/cli/values"
helm_engine "helm.sh/helm/v3/pkg/engine"
)

type renderable struct {
// tpl is the current template.
tpl string
// vals are the values to be supplied to the template.
vals chartutil.Values
// namespace prefix to the templates of the current chart
basePath string
}

type files map[string][]byte

//go:linkname newFiles helm.sh/helm/v3/pkg/engine.newFiles
func newFiles(from []*chart.File) files

//go:linkname isTemplateValid helm.sh/helm/v3/pkg/engine.isTemplateValid
func isTemplateValid(ch *chart.Chart, templateName string) bool

//go:linkname render helm.sh/helm/v3/pkg/engine.(*Engine).render
func render(e *helm_engine.Engine, tpls map[string]renderable) (rendered map[string]string, err error)

// override helm.sh/helm/v3/pkg/engine.recAllTpls
func helm_engine_recAllTpls(c *chart.Chart, teachart *values.TeaChart, templates map[string]renderable, vals chartutil.Values) map[string]interface{} {
subCharts := make(map[string]interface{})
// override the chart meta data
chartMetaData := struct {
values.TeaChart
IsRoot bool
}{*teachart, c.IsRoot()}

next := map[string]interface{}{
"TeaChart": chartMetaData,
"Files": newFiles(c.Files),
"Release": vals["Release"],
"Capabilities": vals["Capabilities"],
"Values": make(chartutil.Values),
"Subcharts": subCharts,
}

// If there is a {{.Values.ThisChart}} in the parent metadata,
// copy that into the {{.Values}} for this template.
if c.IsRoot() {
next["Values"] = vals["Values"]
} else if vs, err := vals.Table("Values." + c.Name()); err == nil {
next["Values"] = vs
}

for _, child := range c.Dependencies() {
subCharts[child.Name()] = helm_engine_recAllTpls(child, teachart, templates, next)
}

newParentID := c.ChartFullPath()
for _, t := range c.Templates {
if t == nil {
continue
}
if !isTemplateValid(c, t.Name) {
continue
}
templates[path.Join(newParentID, t.Name)] = renderable{
tpl: string(t.Data),
vals: next,
basePath: path.Join(newParentID, "templates"),
}
}

return next
}

func (h *Helm) Render(valueOpts helm_values.Options, save bool) (map[string]string, error) {
renderValues, err := h.getRenderValues(valueOpts)
if err != nil {
return nil, err
}
templates := make(map[string]renderable)
helm_engine_recAllTpls(h.chart, h.teachart, templates, renderValues)
files, err := render(h.engine, templates)
if err != nil {
return nil, err
}
if save {
err = h.save(files)
}
return files, err
}
6 changes: 3 additions & 3 deletions pkg/options/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"

compose_cmd "github.com/docker/compose/v2/cmd/compose"
"github.com/yp05327/teachart/pkg/engine"
"github.com/yp05327/teachart/pkg/values"
)

// GlobalOptions is the global configuration
Expand Down Expand Up @@ -87,8 +87,8 @@ func (g *GlobalOptions) GetProjectOptions() *compose_cmd.ProjectOptions {
}
}

func (g *GlobalOptions) GetTeaChart() *engine.TeaChart {
return &engine.TeaChart{
func (g *GlobalOptions) GetTeaChart() *values.TeaChart {
return &values.TeaChart{
ProjectName: g.GetProjectName(),
ProjectDir: g.GetProjectDir(),
TempDir: g.GetTempDir(),
Expand Down
7 changes: 6 additions & 1 deletion pkg/engine/values.go → pkg/values/values.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright © 2024 TeaChart Authors

package engine
package values

import "helm.sh/helm/v3/pkg/chart"

// TeaChart is an extended chart metadata
type TeaChart struct {
*chart.Metadata

ProjectName string
ProjectDir string
TempDir string
Expand Down
14 changes: 7 additions & 7 deletions tests/data/chart/templates/test_values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ services:
image: nginx:latest
environment:
# should have .Values.TeaChart
- PROJECT_NAME={{ .Values.TeaChart.ProjectName }}
- PROJECT_DIR={{ .Values.TeaChart.ProjectDir }}
- TEMP_DIR={{ .Values.TeaChart.TempDir }}
- PROJECT_NAME={{ .TeaChart.ProjectName }}
- PROJECT_DIR={{ .TeaChart.ProjectDir }}
- TEMP_DIR={{ .TeaChart.TempDir }}
# helm chart metadata
- CHART_NAME={{ .Chart.Name }}
- CHART_DESCRIPTION={{ .Chart.Description }}
- CHART_VERSION={{ .Chart.Version }}
- CHART_APPVERSION={{ .Chart.AppVersion }}
- CHART_NAME={{ .TeaChart.Name }}
- CHART_DESCRIPTION={{ .TeaChart.Description }}
- CHART_VERSION={{ .TeaChart.Version }}
- CHART_APPVERSION={{ .TeaChart.AppVersion }}
# default values
- DEFAULT_STRING={{ .Values.default.string }}
# user values from file
Expand Down

0 comments on commit 5451cff

Please sign in to comment.