Skip to content

Commit

Permalink
feat(package): add value options to 'helm package'
Browse files Browse the repository at this point in the history
Signed-off-by: cndoit18 <cndoit18@outlook.com>
  • Loading branch information
cndoit18 committed Oct 21, 2022
1 parent d79ae9f commit 2e13e43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/helm/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func newPackageCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
f.StringVar(&client.AppVersion, "app-version", "", "set the appVersion on the chart to this version")
f.StringVarP(&client.Destination, "destination", "d", ".", "location to write the chart.")
f.BoolVarP(&client.DependencyUpdate, "dependency-update", "u", false, `update dependencies from "Chart.yaml" to dir "charts/" before packaging`)
addValueOptionsFlags(f, valueOpts)

return cmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
k8s.io/klog/v2 v2.70.1
k8s.io/kubectl v0.25.2
oras.land/oras-go v1.2.0
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.3.0
)

Expand Down Expand Up @@ -160,6 +161,5 @@ require (
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
31 changes: 31 additions & 0 deletions pkg/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ import (
"github.com/pkg/errors"
"golang.org/x/term"

"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/provenance"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
"sigs.k8s.io/yaml"
)

// Package is the action for packaging a chart.
Expand Down Expand Up @@ -92,6 +96,33 @@ func (p *Package) Run(path string, vals map[string]interface{}) (string, error)
dest = p.Destination
}

// Merge into values.yaml
needAppend := len(vals) != 0

v, err := yaml.Marshal(vals)
if err != nil {
return "", err
}

for _, f := range ch.Raw {
if f.Name == chartutil.ValuesfileName {
raw, err := merge2.MergeStrings(string(v), string(f.Data), false, kyaml.MergeOptions{})
if err != nil {
return "", err
}

f.Data = []byte(raw)
needAppend = false
}
}

if needAppend {
ch.Raw = append(ch.Raw, &chart.File{
Name: chartutil.ValuesfileName,
Data: v,
})
}

name, err := chartutil.Save(ch, dest)
if err != nil {
return "", errors.Wrap(err, "failed to save")
Expand Down

0 comments on commit 2e13e43

Please sign in to comment.