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 3f8e557
Show file tree
Hide file tree
Showing 4 changed files with 151 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
}
115 changes: 115 additions & 0 deletions cmd/helm/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"helm.sh/helm/v3/internal/test/ensure"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
)

func TestPackage(t *testing.T) {
Expand Down Expand Up @@ -192,6 +193,120 @@ func TestSetAppVersion(t *testing.T) {
}
}

func TestAddValueOptions(t *testing.T) {
tests := []struct {
name string
args []string
expect string
err bool
}{
{
name: "package without set value",
args: []string{},
expect: `# The pod name
Name: my-alpine
`,
err: false,
},
{
name: "package set values",
args: []string{
"--set Name=other",
},
expect: `# The pod name
Name: other
`,
err: false,
},
{
name: "package append values",
args: []string{
"--set Key=other",
},
expect: `# The pod name
Name: my-alpine
Key: other
`,
err: false,
},
{
name: "package multiple values",
args: []string{
"--set a=b,c=d",
},
expect: `# The pod name
Name: my-alpine
a: b
c: d
`,
err: false,
},
{
name: "package more complex expressions",
args: []string{
"--set outer.inner=value",
},
expect: `# The pod name
Name: my-alpine
outer:
inner: value
`,
err: false,
},
{
name: "package lists values",
args: []string{
"--set List={a,b,c}",
},
expect: `# The pod name
Name: my-alpine
List:
- a
- b
- c
`,
err: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := ensure.TempDir(t)
t.Cleanup(func() {
os.RemoveAll(dir)
})

chartToPackage := "testdata/testcharts/issue1979"
cmd := fmt.Sprintf("package %s --destination=%s %s", chartToPackage, dir, strings.Join(tt.args, " "))
_, _, err := executeActionCommand(cmd)
if err != nil {
if tt.err {
return
}
t.Fatal(err)
}
if tt.err {
t.Fatalf("Expected error in test %q", tt.name)
}
chartPath := filepath.Join(dir, "alpine-0.1.0.tgz")
if fi, err := os.Stat(chartPath); err != nil {
t.Errorf("expected file %q, got err %q", chartPath, err)
} else if fi.Size() == 0 {
t.Errorf("file %q has zero bytes.", chartPath)
}
ch, err := loader.Load(chartPath)
if err != nil {
t.Fatalf("unexpected error loading packaged chart: %v", err)
}
for _, f := range ch.Raw {
if f.Name == chartutil.ValuesfileName && string(f.Data) != tt.expect {
t.Errorf("expected app-version %q, found %q", tt.expect, string(f.Data))
}
}
})
}
}

func TestPackageFileCompletion(t *testing.T) {
checkFileCompletion(t, "package", true)
checkFileCompletion(t, "package mypath", true) // Multiple paths can be given
Expand Down
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
)
34 changes: 34 additions & 0 deletions pkg/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/pkg/errors"
"golang.org/x/term"

kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
"sigs.k8s.io/yaml"

"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"
Expand Down Expand Up @@ -92,6 +97,35 @@ func (p *Package) Run(path string, vals map[string]interface{}) (string, error)
dest = p.Destination
}

// Override 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{
ListIncreaseDirection: kyaml.MergeOptionsListPrepend,
})
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 3f8e557

Please sign in to comment.