From 9e869700c0bbb76ebec94b7b12871d6fe27b59ab Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Thu, 23 Nov 2017 11:02:26 -0500 Subject: [PATCH] fix(helm): add --app-version flag to 'helm package' When 'helm package --app-version foo' is run, this will set the AppVersion field to 'foo' in the packaged chart. Signed-off-by: Arash Deshmeh --- cmd/helm/package.go | 7 +++++++ cmd/helm/package_test.go | 33 +++++++++++++++++++++++++++++++++ docs/helm/helm_package.md | 3 ++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/helm/package.go b/cmd/helm/package.go index 44c34a315..ed44382c7 100644 --- a/cmd/helm/package.go +++ b/cmd/helm/package.go @@ -56,6 +56,7 @@ type packageCmd struct { key string keyring string version string + appVersion string destination string dependencyUpdate bool @@ -99,6 +100,7 @@ func newPackageCmd(out io.Writer) *cobra.Command { f.StringVar(&pkg.key, "key", "", "name of the key to use when signing. Used if --sign is true") f.StringVar(&pkg.keyring, "keyring", defaultKeyring(), "location of a public keyring") f.StringVar(&pkg.version, "version", "", "set the version on the chart to this semver version") + f.StringVar(&pkg.appVersion, "app-version", "", "set the appVersion on the chart to this version") f.StringVarP(&pkg.destination, "destination", "d", ".", "location to write the chart.") f.BoolVarP(&pkg.dependencyUpdate, "dependency-update", "u", false, `update dependencies from "requirements.yaml" to dir "charts/" before packaging`) @@ -139,6 +141,11 @@ func (p *packageCmd) run() error { debug("Setting version to %s", p.version) } + if p.appVersion != "" { + ch.Metadata.AppVersion = p.appVersion + debug("Setting appVersion to %s", p.appVersion) + } + if filepath.Base(path) != ch.Metadata.Name { return fmt.Errorf("directory name (%s) and Chart.yaml name (%s) must match", filepath.Base(path), ch.Metadata.Name) } diff --git a/cmd/helm/package_test.go b/cmd/helm/package_test.go index eb6e02830..d338c29cd 100644 --- a/cmd/helm/package_test.go +++ b/cmd/helm/package_test.go @@ -25,6 +25,7 @@ import ( "github.com/spf13/cobra" + "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/proto/hapi/chart" ) @@ -200,6 +201,38 @@ func TestPackage(t *testing.T) { } } +func TestSetAppVersion(t *testing.T) { + var ch *chart.Chart + expectedAppVersion := "app-version-foo" + tmp, _ := ioutil.TempDir("", "helm-package-app-version-") + defer os.RemoveAll(tmp) + + c := newPackageCmd(&bytes.Buffer{}) + flags := map[string]string{ + "destination": tmp, + "app-version": expectedAppVersion, + } + setFlags(c, flags) + err := c.RunE(c, []string{"testdata/testcharts/alpine"}) + if err != nil { + t.Errorf("unexpected error %q", err) + } + + chartPath := filepath.Join(tmp, "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 = chartutil.Load(chartPath) + if err != nil { + t.Errorf("unexpected error loading packaged chart: %v", err) + } + if ch.Metadata.AppVersion != expectedAppVersion { + t.Errorf("expected app-version %q, found %q", expectedAppVersion, ch.Metadata.AppVersion) + } +} + func setFlags(cmd *cobra.Command, flags map[string]string) { dest := cmd.Flags() for f, v := range flags { diff --git a/docs/helm/helm_package.md b/docs/helm/helm_package.md index c4b506641..71960f41e 100644 --- a/docs/helm/helm_package.md +++ b/docs/helm/helm_package.md @@ -23,6 +23,7 @@ helm package [flags] [CHART_PATH] [...] ### Options ``` + --app-version string set the appVersion on the chart to this version -u, --dependency-update update dependencies from "requirements.yaml" to dir "charts/" before packaging -d, --destination string location to write the chart. (default ".") --key string name of the key to use when signing. Used if --sign is true @@ -46,4 +47,4 @@ helm package [flags] [CHART_PATH] [...] ### SEE ALSO * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 7-Nov-2017 +###### Auto generated by spf13/cobra on 24-Nov-2017