feat(create): add hidden --chart-api-version flag

Add --chart-api-version flag to helm create command to allow selecting
chart API version (v2 or v3) when creating a new chart.

- Default is v2 (existing behavior unchanged)
- v3 uses internal/chart/v3 scaffold generator
- Invalid versions return clear error message
- Works with --starter flag

Signed-off-by: Evans Mungai <mbuevans@gmail.com>
pull/31592/head
Evans Mungai 10 months ago
parent dbd07fa16c
commit 0b5649614d
No known key found for this signature in database
GPG Key ID: BBEB812143DD14E1

@ -23,6 +23,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
chartv3 "helm.sh/helm/v4/internal/chart/v3"
chartutilv3 "helm.sh/helm/v4/internal/chart/v3/util"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
chartutil "helm.sh/helm/v4/pkg/chart/v2/util" chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
"helm.sh/helm/v4/pkg/cmd/require" "helm.sh/helm/v4/pkg/cmd/require"
@ -54,6 +56,7 @@ type createOptions struct {
starter string // --starter starter string // --starter
name string name string
starterDir string starterDir string
chartAPIVersion string // --chart-api-version
} }
func newCreateCmd(out io.Writer) *cobra.Command { func newCreateCmd(out io.Writer) *cobra.Command {
@ -81,12 +84,25 @@ func newCreateCmd(out io.Writer) *cobra.Command {
} }
cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "the name or absolute path to Helm starter scaffold") cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "the name or absolute path to Helm starter scaffold")
cmd.Flags().StringVar(&o.chartAPIVersion, "chart-api-version", chart.APIVersionV2, "chart API version to use (v2 or v3)")
return cmd return cmd
} }
func (o *createOptions) run(out io.Writer) error { func (o *createOptions) run(out io.Writer) error {
fmt.Fprintf(out, "Creating %s\n", o.name) fmt.Fprintf(out, "Creating %s\n", o.name)
switch o.chartAPIVersion {
case chart.APIVersionV2, "":
return o.createV2Chart(out)
case chartv3.APIVersionV3:
return o.createV3Chart(out)
default:
return fmt.Errorf("unsupported chart API version: %s (supported: v2, v3)", o.chartAPIVersion)
}
}
func (o *createOptions) createV2Chart(out io.Writer) error {
chartname := filepath.Base(o.name) chartname := filepath.Base(o.name)
cfile := &chart.Metadata{ cfile := &chart.Metadata{
Name: chartname, Name: chartname,
@ -111,3 +127,29 @@ func (o *createOptions) run(out io.Writer) error {
_, err := chartutil.Create(chartname, filepath.Dir(o.name)) _, err := chartutil.Create(chartname, filepath.Dir(o.name))
return err return err
} }
func (o *createOptions) createV3Chart(out io.Writer) error {
chartname := filepath.Base(o.name)
cfile := &chartv3.Metadata{
Name: chartname,
Description: "A Helm chart for Kubernetes",
Type: "application",
Version: "0.1.0",
AppVersion: "0.1.0",
APIVersion: chartv3.APIVersionV3,
}
if o.starter != "" {
// Create from the starter
lstarter := filepath.Join(o.starterDir, o.starter)
// If path is absolute, we don't want to prefix it with helm starters folder
if filepath.IsAbs(o.starter) {
lstarter = o.starter
}
return chartutilv3.CreateFrom(cfile, filepath.Dir(o.name), lstarter)
}
chartutilv3.Stderr = out
_, err := chartutilv3.Create(chartname, filepath.Dir(o.name))
return err
}

@ -22,6 +22,9 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
chartv3 "helm.sh/helm/v4/internal/chart/v3"
loaderv3 "helm.sh/helm/v4/internal/chart/v3/loader"
chartutilv3 "helm.sh/helm/v4/internal/chart/v3/util"
"helm.sh/helm/v4/internal/test/ensure" "helm.sh/helm/v4/internal/test/ensure"
chart "helm.sh/helm/v4/pkg/chart/v2" chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/chart/v2/loader" "helm.sh/helm/v4/pkg/chart/v2/loader"
@ -60,27 +63,73 @@ func TestCreateCmd(t *testing.T) {
} }
func TestCreateStarterCmd(t *testing.T) { func TestCreateStarterCmd(t *testing.T) {
tests := []struct {
name string
chartAPIVersion string
useAbsolutePath bool
expectedVersion string
}{
{
name: "v2 with relative starter path",
chartAPIVersion: "",
useAbsolutePath: false,
expectedVersion: chart.APIVersionV2,
},
{
name: "v2 with absolute starter path",
chartAPIVersion: "",
useAbsolutePath: true,
expectedVersion: chart.APIVersionV2,
},
{
name: "v3 with relative starter path",
chartAPIVersion: "v3",
useAbsolutePath: false,
expectedVersion: chartv3.APIVersionV3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Chdir(t.TempDir()) t.Chdir(t.TempDir())
ensure.HelmHome(t) ensure.HelmHome(t)
cname := "testchart"
defer resetEnv()() defer resetEnv()()
// Create a starter. cname := "testchart"
// Create a starter using the appropriate chartutil
starterchart := helmpath.DataPath("starters") starterchart := helmpath.DataPath("starters")
os.MkdirAll(starterchart, 0o755) os.MkdirAll(starterchart, 0o755)
if dest, err := chartutil.Create("starterchart", starterchart); err != nil { var err error
t.Fatalf("Could not create chart: %s", err) var dest string
if tt.chartAPIVersion == "v3" {
dest, err = chartutilv3.Create("starterchart", starterchart)
} else { } else {
t.Logf("Created %s", dest) dest, err = chartutil.Create("starterchart", starterchart)
} }
if err != nil {
t.Fatalf("Could not create chart: %s", err)
}
t.Logf("Created %s", dest)
tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl") tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl")
if err := os.WriteFile(tplpath, []byte("test"), 0o644); err != nil { if err := os.WriteFile(tplpath, []byte("test"), 0o644); err != nil {
t.Fatalf("Could not write template: %s", err) t.Fatalf("Could not write template: %s", err)
} }
// Run a create // Build the command
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname)); err != nil { starterArg := "starterchart"
t.Errorf("Failed to run create: %s", err) if tt.useAbsolutePath {
return starterArg = filepath.Join(starterchart, "starterchart")
}
cmd := fmt.Sprintf("create --starter=%s", starterArg)
if tt.chartAPIVersion != "" {
cmd += fmt.Sprintf(" --chart-api-version=%s", tt.chartAPIVersion)
}
cmd += " " + cname
// Run create
if _, _, err := executeActionCommand(cmd); err != nil {
t.Fatalf("Failed to run create: %s", err)
} }
// Test that the chart is there // Test that the chart is there
@ -90,62 +139,66 @@ func TestCreateStarterCmd(t *testing.T) {
t.Fatalf("chart is not directory") t.Fatalf("chart is not directory")
} }
// Load and verify the chart
var chartName, apiVersion string
var templates []string
if tt.chartAPIVersion == "v3" {
c, err := loaderv3.LoadDir(cname)
if err != nil {
t.Fatal(err)
}
chartName = c.Name()
apiVersion = c.Metadata.APIVersion
for _, tpl := range c.Templates {
templates = append(templates, tpl.Name)
}
} else {
c, err := loader.LoadDir(cname) c, err := loader.LoadDir(cname)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
chartName = c.Name()
if c.Name() != cname { apiVersion = c.Metadata.APIVersion
t.Errorf("Expected %q name, got %q", cname, c.Name()) for _, tpl := range c.Templates {
templates = append(templates, tpl.Name)
} }
if c.Metadata.APIVersion != chart.APIVersionV2 {
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
} }
expectedNumberOfTemplates := 10 if chartName != cname {
if l := len(c.Templates); l != expectedNumberOfTemplates { t.Errorf("Expected %q name, got %q", cname, chartName)
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) }
if apiVersion != tt.expectedVersion {
t.Errorf("Wrong API version: expected %q, got %q", tt.expectedVersion, apiVersion)
} }
// Verify custom template exists
found := false found := false
for _, tpl := range c.Templates { for _, name := range templates {
if tpl.Name == "templates/foo.tpl" { if name == "templates/foo.tpl" {
found = true found = true
if data := string(tpl.Data); data != "test" { break
t.Errorf("Expected template 'test', got %q", data)
}
} }
} }
if !found { if !found {
t.Error("Did not find foo.tpl") t.Error("Did not find foo.tpl")
} }
})
}
} }
func TestCreateStarterAbsoluteCmd(t *testing.T) { func TestCreateFileCompletion(t *testing.T) {
checkFileCompletion(t, "create", true)
checkFileCompletion(t, "create myname", false)
}
func TestCreateCmdChartAPIVersionV2(t *testing.T) {
t.Chdir(t.TempDir()) t.Chdir(t.TempDir())
defer resetEnv()()
ensure.HelmHome(t) ensure.HelmHome(t)
cname := "testchart" cname := "testchart"
// Create a starter. // Run a create with explicit v2
starterchart := helmpath.DataPath("starters") if _, _, err := executeActionCommand("create --chart-api-version=v2 " + cname); err != nil {
os.MkdirAll(starterchart, 0o755) t.Fatalf("Failed to run create: %s", err)
if dest, err := chartutil.Create("starterchart", starterchart); err != nil {
t.Fatalf("Could not create chart: %s", err)
} else {
t.Logf("Created %s", dest)
}
tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl")
if err := os.WriteFile(tplpath, []byte("test"), 0o644); err != nil {
t.Fatalf("Could not write template: %s", err)
}
starterChartPath := filepath.Join(starterchart, "starterchart")
// Run a create
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil {
t.Errorf("Failed to run create: %s", err)
return
} }
// Test that the chart is there // Test that the chart is there
@ -164,29 +217,53 @@ func TestCreateStarterAbsoluteCmd(t *testing.T) {
t.Errorf("Expected %q name, got %q", cname, c.Name()) t.Errorf("Expected %q name, got %q", cname, c.Name())
} }
if c.Metadata.APIVersion != chart.APIVersionV2 { if c.Metadata.APIVersion != chart.APIVersionV2 {
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) t.Errorf("Wrong API version: expected %q, got %q", chart.APIVersionV2, c.Metadata.APIVersion)
} }
}
func TestCreateCmdChartAPIVersionV3(t *testing.T) {
t.Chdir(t.TempDir())
ensure.HelmHome(t)
cname := "testchart"
expectedNumberOfTemplates := 10 // Run a create with v3
if l := len(c.Templates); l != expectedNumberOfTemplates { if _, _, err := executeActionCommand("create --chart-api-version=v3 " + cname); err != nil {
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) t.Fatalf("Failed to run create: %s", err)
} }
found := false // Test that the chart is there
for _, tpl := range c.Templates { if fi, err := os.Stat(cname); err != nil {
if tpl.Name == "templates/foo.tpl" { t.Fatalf("no chart directory: %s", err)
found = true } else if !fi.IsDir() {
if data := string(tpl.Data); data != "test" { t.Fatalf("chart is not directory")
t.Errorf("Expected template 'test', got %q", data)
} }
c, err := loaderv3.LoadDir(cname)
if err != nil {
t.Fatal(err)
} }
if c.Name() != cname {
t.Errorf("Expected %q name, got %q", cname, c.Name())
} }
if !found { if c.Metadata.APIVersion != chartv3.APIVersionV3 {
t.Error("Did not find foo.tpl") t.Errorf("Wrong API version: expected %q, got %q", chartv3.APIVersionV3, c.Metadata.APIVersion)
} }
} }
func TestCreateFileCompletion(t *testing.T) { func TestCreateCmdInvalidChartAPIVersion(t *testing.T) {
checkFileCompletion(t, "create", true) t.Chdir(t.TempDir())
checkFileCompletion(t, "create myname", false) ensure.HelmHome(t)
cname := "testchart"
// Run a create with invalid version
_, _, err := executeActionCommand("create --chart-api-version=v1 " + cname)
if err == nil {
t.Fatal("Expected error for invalid API version, got nil")
}
expectedErr := "unsupported chart API version: v1 (supported: v2, v3)"
if err.Error() != expectedErr {
t.Errorf("Expected error %q, got %q", expectedErr, err.Error())
}
} }

Loading…
Cancel
Save