Skip to content

Commit

Permalink
Fix bugs in native Helm support (#1281)
Browse files Browse the repository at this point in the history
Clean up logic and handle bugs related
to chart URLs and APIVersion handling.
  • Loading branch information
lblackstone authored Aug 28, 2020
1 parent d9e686f commit 1c8399c
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions provider/pkg/provider/invoke_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package provider

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -111,10 +114,9 @@ func helmTemplate(opts HelmChartOpts) (string, error) {
}

type chart struct {
opts HelmChartOpts
chartDir string
chartName string
helmHome *string // Previous setting of HELM_HOME env var (if any)
opts HelmChartOpts
chartDir string
helmHome *string // Previous setting of HELM_HOME env var (if any)
}

// fetch runs the `helm fetch` action to fetch a Chart from a remote URL.
Expand Down Expand Up @@ -153,17 +155,15 @@ func (c *chart) fetch() error {
p.Version = c.opts.HelmFetchOpts.Version
} // If both are set, prefer the top-level version over the FetchOpts version.

if c.opts.HelmFetchOpts.Repo == "" {
splits := strings.Split(c.opts.Chart, "/")
if len(splits) != 2 {
return pkgerrors.Errorf("chart repo not specified: %s", c.opts.Chart)
chartRef := func() string {
if len(c.opts.Repo) > 0 {
return fmt.Sprintf("%s/%s", strings.TrimSuffix(c.opts.Repo, "/"), c.opts.Chart)
}
c.chartName = splits[1]
} else {
c.chartName = c.opts.Chart

return c.opts.Chart
}

_, err := p.Run(c.opts.Chart)
_, err := p.Run(chartRef())
if err != nil {
return pkgerrors.Wrap(err, "failed to pull chart")
}
Expand All @@ -183,14 +183,33 @@ func (c *chart) template() (string, error) {
}

installAction := action.NewInstall(cfg)
installAction.APIVersions = c.opts.APIVersions
installAction.ClientOnly = true
installAction.DryRun = true
installAction.IncludeCRDs = true // TODO: handle this conditionally?
installAction.Namespace = c.opts.Namespace
installAction.ReleaseName = c.opts.ReleaseName
installAction.Version = c.opts.Version

chart, err := loader.Load(filepath.Join(c.chartDir, c.chartName))
chartName := func() string {
// Check if the chart value is a URL with a defined scheme.
if _url, err := url.Parse(c.opts.Chart); err == nil && len(_url.Scheme) > 0 {
// Chart path will be of the form `/name-version.tgz`
re := regexp.MustCompile(`^/(\w+)-(\S+)\.tgz$`)
matches := re.FindStringSubmatch(_url.Path)
if len(matches) > 1 {
return matches[1]
}
}

splits := strings.Split(c.opts.Chart, "/")
if len(splits) == 2 {
return splits[1]
}
return c.opts.Chart
}

chart, err := loader.Load(filepath.Join(c.chartDir, chartName()))
if err != nil {
return "", pkgerrors.Wrap(err, "failed to load chart from temp directory")
}
Expand Down

0 comments on commit 1c8399c

Please sign in to comment.