Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Match the Helm user experience for packaging/publishing a chart with …
Browse files Browse the repository at this point in the history
…a version provided on the command line
  • Loading branch information
John Esmet committed Dec 4, 2019
1 parent a0dff58 commit 157a4cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
5 changes: 3 additions & 2 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,13 @@ func main() {
})

cmd.Command("publish", "Publish a Helm chart using files from the current directory", func(cmd *cli.Cmd) {
cmd.Spec = "[-r]"
cmd.Spec = "[-r] [--version]"
repositoryArg := cmd.StringOpt("r repository", "", "The chart repository to use")
versionArg := cmd.StringOpt("version", "", "The chart version to publish. Overrides any version present in Chart.yaml")

cmd.Action = func() {
repository := ctx.DetermineHelmRepository(repositoryArg)
err := helm.Publish(ctx, repository)
err := helm.Publish(ctx, repository, *versionArg)
check(err)
os.Exit(0)
}
Expand Down
38 changes: 28 additions & 10 deletions helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ type ChartYaml struct {
Version string
}

func readChartYaml(ctx *ankh.ExecutionContext, path string) (map[string]interface{}, ChartYaml, error) {
func readChartYaml(ctx *ankh.ExecutionContext, path string, lenient bool) (map[string]interface{}, ChartYaml, error) {
rawYaml := make(map[string]interface{})
chartYaml := ChartYaml{}

Expand Down Expand Up @@ -352,7 +352,11 @@ func readChartYaml(ctx *ankh.ExecutionContext, path string) (map[string]interfac

version, ok := rawYaml["version"].(string)
if !ok {
return rawYaml, chartYaml, fmt.Errorf("Chart.yaml missing `version`, or its type is not a string.")
if lenient {
version = ""
} else {
return rawYaml, chartYaml, fmt.Errorf("Chart.yaml missing `version`, or its type is not a string.")
}
}

chartYaml = ChartYaml{
Expand Down Expand Up @@ -392,14 +396,23 @@ func writeChartYaml(ctx *ankh.ExecutionContext, chartYaml map[string]interface{}
return nil
}

func Publish(ctx *ankh.ExecutionContext, repository string) error {
_, chartYaml, err := readChartYaml(ctx, "Chart.yaml")
func Publish(ctx *ankh.ExecutionContext, repository string, versionOverride string) error {
_, chartYaml, err := readChartYaml(ctx, "Chart.yaml", true)
if err != nil {
return err
}

chartName := chartYaml.Name
chartVersion := chartYaml.Version
if versionOverride != "" {
chartVersion = versionOverride
ctx.Logger.Infof("Using chart version %v from command line", versionOverride)
} else {
ctx.Logger.Infof("Using chart version %v from Chart.yaml", chartVersion)
}

wd, _ := os.Getwd()
localTarballPath := fmt.Sprintf("%v/%v-%v.tgz", wd, chartYaml.Name, chartYaml.Version)
localTarballPath := fmt.Sprintf("%v/%v-%v.tgz", wd, chartName, chartVersion)
removeTarball := func() {
err = os.Remove(localTarballPath)
if err != nil && !os.IsNotExist(err) {
Expand All @@ -412,14 +425,19 @@ func Publish(ctx *ankh.ExecutionContext, repository string) error {
removeTarball()
defer removeTarball()

helmArgs := []string{ctx.AnkhConfig.Helm.Command, "package", wd}
helmArgs := []string{ctx.AnkhConfig.Helm.Command, "package"}
if versionOverride != "" {
helmArgs = append(helmArgs, []string{"--version", versionOverride}...)
}
helmArgs = append(helmArgs, wd)
helmCmd := execContext(helmArgs[0], helmArgs[1:]...)

var stderr bytes.Buffer
helmCmd.Stderr = &stderr

// Use helm to create a package tarball
ctx.Logger.Infof("Packaging '%v-%v'", chartYaml.Name, chartYaml.Version)
ctx.Logger.Infof("Packaging '%v-%v'", chartName, chartVersion)
ctx.Logger.Debugf("Running command %v", helmCmd)
err = helmCmd.Run()
var helmError = string(stderr.Bytes())
if err != nil {
Expand All @@ -430,7 +448,7 @@ func Publish(ctx *ankh.ExecutionContext, repository string) error {
return fmt.Errorf("error running helm command '%v': %v%v",
strings.Join(helmCmd.Args, " "), err, outputMsg)
}
ctx.Logger.Infof("Finished packaging '%v-%v'", chartYaml.Name, chartYaml.Version)
ctx.Logger.Infof("Finished packaging '%v-%v'", chartName, chartVersion)

// Open up and read the contents of the package in order to PUT it upstream
localTarballFile, err := os.Open(localTarballPath)
Expand All @@ -445,7 +463,7 @@ func Publish(ctx *ankh.ExecutionContext, repository string) error {
return err
}

upstreamTarballPath := fmt.Sprintf("%v/%v-%v.tgz", repository, chartYaml.Name, chartYaml.Version)
upstreamTarballPath := fmt.Sprintf("%v/%v-%v.tgz", repository, chartName, chartVersion)
ctx.Logger.Infof("Publishing '%v'", upstreamTarballPath)

// Create a request with the chart on the PUT body
Expand Down Expand Up @@ -600,7 +618,7 @@ func Inspect(ctx *ankh.ExecutionContext, repository string, singleChart string)
}

func Bump(ctx *ankh.ExecutionContext, semVerType string) error {
rawYaml, chartYaml, err := readChartYaml(ctx, "Chart.yaml")
rawYaml, chartYaml, err := readChartYaml(ctx, "Chart.yaml", false)
if err != nil {
return err
}
Expand Down

0 comments on commit 157a4cc

Please sign in to comment.