Skip to content

Commit

Permalink
Fix panic with OCI for install, upgrade, and show
Browse files Browse the repository at this point in the history
When range support for OCI went in via #10527 it created a situation
where some lookups for a chart could cause a panic. This change
makes sure the registry client is available to lookup OCI charts

Signed-off-by: Matt Farina <matt.farina@suse.com>
  • Loading branch information
mattfarina committed Jan 13, 2022
1 parent 390daca commit 548ec55
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/helm/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
newCreateCmd(out),
newDependencyCmd(actionConfig, out),
newPullCmd(actionConfig, out),
newShowCmd(out),
newShowCmd(actionConfig, out),
newLintCmd(out),
newPackageCmd(out),
newRepoCmd(out),
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ This command inspects a chart (directory, file, or URL) and displays the content
of the CustomResourceDefinition files
`

func newShowCmd(out io.Writer) *cobra.Command {
client := action.NewShow(action.ShowAll)
func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewShowWithConfig(action.ShowAll, cfg)

showCommand := &cobra.Command{
Use: "show",
Expand Down
22 changes: 15 additions & 7 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,20 @@ type ChartPathOptions struct {
Username string // --username
Verify bool // --verify
Version string // --version

// registryClient provides a registry client but is not added with
// options from a flag
registryClient *registry.Client
}

// NewInstall creates a new Install object with the given configuration.
func NewInstall(cfg *Configuration) *Install {
return &Install{
in := &Install{
cfg: cfg,
}
in.ChartPathOptions.registryClient = cfg.RegistryClient

return in
}

func (i *Install) installCRDs(crds []chart.CRD) error {
Expand Down Expand Up @@ -662,6 +669,12 @@ OUTER:
//
// If 'verify' was set on ChartPathOptions, this will attempt to also verify the chart.
func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) (string, error) {
// If there is no registry client and the name is in an OCI registry return
// an error and a lookup will not occur.
if registry.IsOCI(name) && c.registryClient == nil {
return "", fmt.Errorf("unable to lookup chart %q, missing registry client", name)
}

name = strings.TrimSpace(name)
version := strings.TrimSpace(c.Version)

Expand Down Expand Up @@ -692,12 +705,7 @@ func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) (
},
RepositoryConfig: settings.RepositoryConfig,
RepositoryCache: settings.RepositoryCache,
}

if registry.IsOCI(name) {
if version != "" {
dl.Options = append(dl.Options, getter.WithTagName(version))
}
RegistryClient: c.registryClient,
}

if c.Verify {
Expand Down
12 changes: 12 additions & 0 deletions pkg/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,24 @@ type Show struct {
}

// NewShow creates a new Show object with the given configuration.
// Deprecated: Use NewShowWithConfig
// TODO Helm 4: Fold NewShowWithConfig back into NewShow
func NewShow(output ShowOutputFormat) *Show {
return &Show{
OutputFormat: output,
}
}

// NewShowWithConfig creates a new Show object with the given configuration.
func NewShowWithConfig(output ShowOutputFormat, cfg *Configuration) *Show {
sh := &Show{
OutputFormat: output,
}
sh.ChartPathOptions.registryClient = cfg.RegistryClient

return sh
}

// Run executes 'helm show' against the given release.
func (s *Show) Run(chartpath string) (string, error) {
if s.chart == nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/action/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
)

func TestShow(t *testing.T) {
client := NewShow(ShowAll)
config := actionConfigFixture(t)
client := NewShowWithConfig(ShowAll, config)
client.chart = &chart.Chart{
Metadata: &chart.Metadata{Name: "alpine"},
Files: []*chart.File{
Expand Down
5 changes: 4 additions & 1 deletion pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ type resultMessage struct {

// NewUpgrade creates a new Upgrade object with the given configuration.
func NewUpgrade(cfg *Configuration) *Upgrade {
return &Upgrade{
up := &Upgrade{
cfg: cfg,
}
up.ChartPathOptions.registryClient = cfg.RegistryClient

return up
}

// Run executes the upgrade on the given release.
Expand Down

0 comments on commit 548ec55

Please sign in to comment.