Skip to content

Commit

Permalink
Merge pull request #6 from lazychanger/feat/support-include
Browse files Browse the repository at this point in the history
Feat/support include
  • Loading branch information
lazychanger authored Oct 13, 2022
2 parents 6951ed4 + dae925d commit 02ef1ac
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 50 deletions.
16 changes: 16 additions & 0 deletions cmd/helm-variable-in-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,19 @@ func checkIfInstallable(ch *chart.Chart) error {
}
return errors.Errorf("%s charts are not installable", ch.Metadata.Type)
}

func createRelease(i *action.Install, cfg *action.Configuration, chrt *chart.Chart, rawVals map[string]interface{}) *release.Release {
ts := cfg.Now()
return &release.Release{
Name: i.ReleaseName,
Namespace: i.Namespace,
Chart: chrt,
Config: rawVals,
Info: &release.Info{
FirstDeployed: ts,
LastDeployed: ts,
Status: release.StatusUnknown,
},
Version: 1,
}
}
4 changes: 3 additions & 1 deletion example/simple-example/vivs/values.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nameOverride: "{{ .Chart.Name }}-{{ .Chart.Version }}"
nameOverride: "{{ .Chart.Name }}-{{ .Chart.Version }}"

test: {{ include "simple-example.fullname" . }}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
helm.sh/helm/v3 v3.10.1
k8s.io/client-go v0.25.2
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -86,6 +87,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand All @@ -96,7 +98,6 @@ require (
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
114 changes: 67 additions & 47 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package engine

import (
"bytes"
"fmt"
"github.com/lazychanger/helm-variable-in-values/pkg/utils"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/engine"
"log"
"os"
"path"
"regexp"
"runtime/debug"
"strings"
)

Expand All @@ -29,85 +30,79 @@ func (e *Engine) RenderTo(dst string) []string {
dst = path.Join(e.cfg.WorkDir, e.cfg.Chart.ChartFullPath(), dst)
}

if err := os.MkdirAll(dst, os.ModePerm); err != nil {
log.Panicln(err)
}

e.vivFileDirs = []string{dst}

defer func() {
if err := recover(); err != nil {
log.Println(err)
debug.PrintStack()
}
}()

outputFiles, err := e.eachChart(dst, e.cfg.Chart, "")
outputFiles, err := e.eachChart(e.cfg.Chart, "")
if err != nil {
log.Println(err)
return []string{}
}
return outputFiles
}

func (e *Engine) RenderToTemp() []string {
return e.RenderTo("vivTemp")
}

func (e *Engine) eachChart(dst string, chart *chart.Chart, node string) ([]string, error) {
outputFiles := make([]string, 0)

for _, file := range chart.Raw {
if !strings.HasPrefix(file.Name, "vivs") {
continue
}

vivfilePath := path.Join(chart.ChartFullPath()[len(e.cfg.Chart.ChartFullPath()):], file.Name)
vivfileFullpath := path.Join(dst, strings.ReplaceAll(vivfilePath, "/", "_"))

if err := e.render(vivfileFullpath, node, e.cfg.Values, file.Data); err != nil {
return outputFiles, errors.Wrap(err, fmt.Sprintf("vivfile generate failed. %s", vivfilePath))
}
e.cfg.Chart.Templates = append(e.cfg.Chart.Templates, outputFiles...)

outputFiles = append(outputFiles, vivfileFullpath)
tmpls, err := engine.Render(e.cfg.Chart, e.cfg.Values)
if err != nil {
return []string{}
}

for _, d := range chart.Dependencies() {
outputRealFilepath := make([]string, len(outputFiles))

subChartOutputFiles, err := e.eachChart(dst, d, fmt.Sprintf("%s.%s", node, d.Name()))
for i, f := range outputFiles {
filename := path.Join(e.cfg.Chart.Name(), f.Name)
realfilepath := path.Join(dst, strings.ReplaceAll(filename, "/", "_"))

_ = tmpls[filename]
newdata, err := addRootNode(getNode(f.Name), []byte(tmpls[filename]))
if err != nil {
return outputFiles, errors.Wrap(err, fmt.Sprintf("subchart generate failed. %s", d.Name()))
log.Panic(err)
}
writeFile(realfilepath, newdata)

outputFiles = append(outputFiles, subChartOutputFiles...)
outputRealFilepath[i] = realfilepath
}

return outputFiles, nil
return outputRealFilepath
}

func (e *Engine) render(output string, root string, values map[string]interface{}, template []byte) error {
func (e *Engine) RenderToTemp() []string {
return e.RenderTo("vivTemp")
}

if err := os.MkdirAll(path.Dir(output), os.ModePerm); err != nil {
return err
}
func (e *Engine) eachChart(ch *chart.Chart, node string) ([]*chart.File, error) {

f, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return err
renderFiles := make([]*chart.File, 0)

for _, f := range ch.Raw {
if !strings.HasPrefix(f.Name, "vivs/") || f.Name == "" || len(f.Data) == 0 {
continue
}
f.Name = path.Join(ch.ChartFullPath()[len(e.cfg.Chart.Name()):], f.Name)
renderFiles = append(renderFiles, f)
}

buf := &bytes.Buffer{}
for _, d := range ch.Dependencies() {

if err := utils.Tmpl(buf, string(template), values); err != nil {
return err
}
subChartOutputFiles, err := e.eachChart(d, fmt.Sprintf("%s.%s", node, d.Name()))

newData, err := addRootNode(root, buf.Bytes())
if err != nil {
return err
}
if err != nil {
return renderFiles, errors.Wrap(err, fmt.Sprintf("subchart generate failed. %s", d.Name()))
}

if _, err := f.Write(newData); err != nil {
return err
renderFiles = append(renderFiles, subChartOutputFiles...)
}

return nil
return renderFiles, nil
}

func (e *Engine) Clear() {
Expand All @@ -134,3 +129,28 @@ func addRootNode(root string, data []byte) ([]byte, error) {

return current.Top().MarshalWithYAML()
}

func writeFile(filepath string, data []byte) {
f, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
log.Panic(err)
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
log.Panic(err)
}
}

var partten = "charts/([a-zA-Z]+[a-zA-Z0-9]+)"

func getNode(name string) string {
r := regexp.MustCompile(partten)

nodes := make([]string, 1)
for _, match := range r.FindAllStringSubmatch(name, -1) {
nodes = append(nodes, match[1])
}

return strings.Join(nodes, ".")
}
11 changes: 11 additions & 0 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package engine

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetNodes(t *testing.T) {
assert.Equal(t, getNode("simple-example/charts/ingressAlias/charts/service/vivs/values.yaml"), ".ingressAlias.service")
assert.Equal(t, getNode("simple-example/charts/ingressAlias/vivs/values.yaml"), ".ingressAlias")
}
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "viv"
version: "0.1.6"
version: "0.2.1"
usage: "get the last release name"
command: "$HELM_PLUGIN_DIR/bin/helm-viv"
description: "get the last release name"
Expand Down

0 comments on commit 02ef1ac

Please sign in to comment.