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 <adeshmeh@ca.ibm.com>
pull/3196/head
Arash Deshmeh 7 years ago
parent b7c2a95f42
commit 9e869700c0

@ -56,6 +56,7 @@ type packageCmd struct {
key string key string
keyring string keyring string
version string version string
appVersion string
destination string destination string
dependencyUpdate bool 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.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.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.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.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`) 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) 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 { 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) return fmt.Errorf("directory name (%s) and Chart.yaml name (%s) must match", filepath.Base(path), ch.Metadata.Name)
} }

@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/proto/hapi/chart" "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) { func setFlags(cmd *cobra.Command, flags map[string]string) {
dest := cmd.Flags() dest := cmd.Flags()
for f, v := range flags { for f, v := range flags {

@ -23,6 +23,7 @@ helm package [flags] [CHART_PATH] [...]
### Options ### 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 -u, --dependency-update update dependencies from "requirements.yaml" to dir "charts/" before packaging
-d, --destination string location to write the chart. (default ".") -d, --destination string location to write the chart. (default ".")
--key string name of the key to use when signing. Used if --sign is true --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 ### SEE ALSO
* [helm](helm.md) - The Helm package manager for Kubernetes. * [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

Loading…
Cancel
Save