diff --git a/cmd/helm/install.go b/cmd/helm/install.go index b16a921b0..18be2d299 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -35,6 +35,7 @@ import ( "k8s.io/helm/pkg/action" "k8s.io/helm/pkg/chart" "k8s.io/helm/pkg/chart/loader" + "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/downloader" "k8s.io/helm/pkg/getter" "k8s.io/helm/pkg/hapi/release" @@ -231,12 +232,9 @@ func (o *installOptions) run(out io.Writer) error { return err } - chartType := chartRequested.Metadata.Type - if strings.EqualFold(chartType, "library") { - return errors.New("Library charts are not installable") - } - if chartType != "" && !strings.EqualFold(chartType, "application") { - return errors.New("Invalid chart type. Valid types are: application or library") + validInstallableChart, err := chartutil.IsChartInstallable(chartRequested) + if !validInstallableChart { + return err } if req := chartRequested.Metadata.Dependencies; req != nil { diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index 0a96d010f..1a42c95f1 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -136,7 +136,7 @@ func TestInstall(t *testing.T) { name: "install chart with bad type", cmd: "install badtype testdata/testcharts/chart-bad-type", wantError: true, - golden: "output/template-chart-bad-type.txt", + golden: "output/install-chart-bad-type.txt", }, } diff --git a/cmd/helm/package.go b/cmd/helm/package.go index 6ff9d348c..04395607d 100644 --- a/cmd/helm/package.go +++ b/cmd/helm/package.go @@ -22,7 +22,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "syscall" "github.com/Masterminds/semver" @@ -135,10 +134,9 @@ func (o *packageOptions) run(out io.Writer) error { return err } - chartType := ch.Metadata.Type - if chartType != "" && !strings.EqualFold(chartType, "library") && - !strings.EqualFold(chartType, "application") { - return errors.New("Invalid chart type. Valid types are: application or library") + validChartType, err := chartutil.IsValidChartType(ch) + if !validChartType { + return err } overrideVals, err := o.mergedValues() diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 81053c208..f89aaed2b 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -157,12 +157,9 @@ func (o *templateOptions) run(out io.Writer) error { return err } - chartType := c.Metadata.Type - if strings.EqualFold(chartType, "library") { - return errors.New("Library charts are not installable") - } - if chartType != "" && !strings.EqualFold(chartType, "application") { - return errors.New("Invalid chart type. Valid types are: application or library") + validInstallableChart, err := chartutil.IsChartInstallable(c) + if !validInstallableChart { + return err } if req := c.Metadata.Dependencies; req != nil { diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index 2d5976bba..d34409e44 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -86,7 +86,7 @@ func TestTemplateCmd(t *testing.T) { name: "check chart bad type", cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/chart-bad-type"), wantError: true, - golden: "output/template-chart-bad-type.txt", + golden: "output/install-chart-bad-type.txt", }, } runTestCmd(t, tests) diff --git a/cmd/helm/testdata/output/install-chart-bad-type.txt b/cmd/helm/testdata/output/install-chart-bad-type.txt new file mode 100644 index 000000000..22308ac51 --- /dev/null +++ b/cmd/helm/testdata/output/install-chart-bad-type.txt @@ -0,0 +1 @@ +Error: Invalid chart types are not installable diff --git a/cmd/helm/testdata/output/template-chart-bad-type.txt b/cmd/helm/testdata/output/template-chart-bad-type.txt deleted file mode 100644 index dd109fe4a..000000000 --- a/cmd/helm/testdata/output/template-chart-bad-type.txt +++ /dev/null @@ -1 +0,0 @@ -Error: Invalid chart type. Valid types are: application or library diff --git a/pkg/chartutil/chartfile.go b/pkg/chartutil/chartfile.go index 38bcd54db..7e90966b4 100644 --- a/pkg/chartutil/chartfile.go +++ b/pkg/chartutil/chartfile.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "github.com/ghodss/yaml" "github.com/pkg/errors" @@ -82,3 +83,30 @@ func IsChartDir(dirName string) (bool, error) { return true, nil } + +// IsChartInstallable validates if a chart can be installed +// +// Application chart type is only installable +func IsChartInstallable(chart *chart.Chart) (bool, error) { + chartType := chart.Metadata.Type + if strings.EqualFold(chartType, "library") { + return false, errors.New("Library charts are not installable") + } + validChartType, _ := IsValidChartType(chart) + if !validChartType { + return false, errors.New("Invalid chart types are not installable") + } + return true, nil +} + +// IsValidChartType validates the chart type +// +// Valid types are: application or library +func IsValidChartType(chart *chart.Chart) (bool, error) { + chartType := chart.Metadata.Type + if chartType != "" && !strings.EqualFold(chartType, "library") && + !strings.EqualFold(chartType, "application") { + return false, errors.New("Invalid chart type. Valid types are: application or library") + } + return true, nil +}