Skip to content

Commit

Permalink
Provide a helper to set the registryClient in cmd
Browse files Browse the repository at this point in the history
If enabled the registryClient is set using a helper that accepts the TLS
flags. This keeps the client creation consistent accross the different
commands.

Signed-off-by: Soule BA <bah.soule@gmail.com>
  • Loading branch information
souleb authored and sabre1041 committed Mar 3, 2023
1 parent c94306f commit 11738dd
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 58 deletions.
8 changes: 7 additions & 1 deletion cmd/helm/install.go
Expand Up @@ -136,6 +136,12 @@ func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return compInstall(args, toComplete, client)
},
RunE: func(_ *cobra.Command, args []string) error {
registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
client.SetRegistryClient(registryClient)

rel, err := runInstall(args, client, valueOpts, out)
if err != nil {
return errors.Wrap(err, "INSTALLATION FAILED")
Expand Down Expand Up @@ -203,7 +209,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
}
client.ReleaseName = name

cp, err := client.ChartPathOptions.LocateChart(chart, out, settings)
cp, err := client.ChartPathOptions.LocateChart(chart, settings)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/helm/pull.go
Expand Up @@ -43,7 +43,7 @@ result in an error, and the chart will not be saved locally.
`

func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewPullWithOpts(action.WithConfig(cfg), action.WithPullOptWriter(out))
client := action.NewPullWithOpts(action.WithConfig(cfg))

cmd := &cobra.Command{
Use: "pull [chart URL | repo/chartname] [...]",
Expand All @@ -64,6 +64,12 @@ func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client.Version = ">0.0.0-0"
}

registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
client.SetRegistryClient(registryClient)

for i := 0; i < len(args); i++ {
output, err := client.Run(args[i])
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/helm/push.go
Expand Up @@ -67,6 +67,11 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
registryClient, err := newRegistryClient(o.certFile, o.keyFile, o.caFile, o.insecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
cfg.RegistryClient = registryClient
chartRef := args[0]
remote := args[1]
client := action.NewPushWithOpts(action.WithPushConfig(cfg),
Expand Down
30 changes: 28 additions & 2 deletions cmd/helm/root.go
Expand Up @@ -152,7 +152,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
flags.ParseErrorsWhitelist.UnknownFlags = true
flags.Parse(args)

registryClient, err := newRegistryClient(out)
registryClient, err := newDefaultRegistryClient()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -257,7 +257,22 @@ func checkForExpiredRepos(repofile string) {

}

func newRegistryClient(out io.Writer) (*registry.Client, error) {
func newRegistryClient(certFile, keyFile, caFile string, insecureSkipTLSverify bool) (*registry.Client, error) {
if certFile != "" && keyFile != "" || caFile != "" || insecureSkipTLSverify {
registryClient, err := newRegistryClientWithTLS(certFile, keyFile, caFile, insecureSkipTLSverify)
if err != nil {
return nil, err
}
return registryClient, nil
}
registryClient, err := newDefaultRegistryClient()
if err != nil {
return nil, err
}
return registryClient, nil
}

func newDefaultRegistryClient() (*registry.Client, error) {
// Create a new registry client
registryClient, err := registry.NewClient(
registry.ClientOptDebug(settings.Debug),
Expand All @@ -270,3 +285,14 @@ func newRegistryClient(out io.Writer) (*registry.Client, error) {
}
return registryClient, nil
}

func newRegistryClientWithTLS(certFile, keyFile, caFile string, insecureSkipTLSverify bool) (*registry.Client, error) {
// Create a new registry client
registryClient, err := registry.NewRegistryClientWithTLS(os.Stderr, certFile, keyFile, caFile, insecureSkipTLSverify,
settings.RegistryConfig, settings.Debug,
)
if err != nil {
return nil, err
}
return registryClient, nil
}
43 changes: 36 additions & 7 deletions cmd/helm/show.go
Expand Up @@ -84,7 +84,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
ValidArgsFunction: validArgsFunc,
RunE: func(cmd *cobra.Command, args []string) error {
client.OutputFormat = action.ShowAll
output, err := runShow(args, client, out)
err := addRegistryClient(client)
if err != nil {
return err
}
output, err := runShow(args, client)
if err != nil {
return err
}
Expand All @@ -101,7 +105,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
ValidArgsFunction: validArgsFunc,
RunE: func(cmd *cobra.Command, args []string) error {
client.OutputFormat = action.ShowValues
output, err := runShow(args, client, out)
err := addRegistryClient(client)
if err != nil {
return err
}
output, err := runShow(args, client)
if err != nil {
return err
}
Expand All @@ -118,7 +126,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
ValidArgsFunction: validArgsFunc,
RunE: func(cmd *cobra.Command, args []string) error {
client.OutputFormat = action.ShowChart
output, err := runShow(args, client, out)
err := addRegistryClient(client)
if err != nil {
return err
}
output, err := runShow(args, client)
if err != nil {
return err
}
Expand All @@ -135,7 +147,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
ValidArgsFunction: validArgsFunc,
RunE: func(cmd *cobra.Command, args []string) error {
client.OutputFormat = action.ShowReadme
output, err := runShow(args, client, out)
err := addRegistryClient(client)
if err != nil {
return err
}
output, err := runShow(args, client)
if err != nil {
return err
}
Expand All @@ -152,7 +168,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
ValidArgsFunction: validArgsFunc,
RunE: func(cmd *cobra.Command, args []string) error {
client.OutputFormat = action.ShowCRDs
output, err := runShow(args, client, out)
err := addRegistryClient(client)
if err != nil {
return err
}
output, err := runShow(args, client)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,16 +211,25 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) {
}
}

func runShow(args []string, client *action.Show, out io.Writer) (string, error) {
func runShow(args []string, client *action.Show) (string, error) {
debug("Original chart version: %q", client.Version)
if client.Version == "" && client.Devel {
debug("setting version to >0.0.0-0")
client.Version = ">0.0.0-0"
}

cp, err := client.ChartPathOptions.LocateChart(args[0], out, settings)
cp, err := client.ChartPathOptions.LocateChart(args[0], settings)
if err != nil {
return "", err
}
return client.Run(cp)
}

func addRegistryClient(client *action.Show) error {
registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
client.SetRegistryClient(registryClient)
return nil
}
6 changes: 6 additions & 0 deletions cmd/helm/template.go
Expand Up @@ -73,6 +73,12 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client.KubeVersion = parsedKubeVersion
}

registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
client.SetRegistryClient(registryClient)

client.DryRun = true
client.ReleaseName = "release-name"
client.Replace = true // Skip the name check
Expand Down
8 changes: 7 additions & 1 deletion cmd/helm/upgrade.go
Expand Up @@ -90,6 +90,12 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
client.Namespace = settings.Namespace()

registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify)
if err != nil {
return fmt.Errorf("missing registry client: %w", err)
}
client.SetRegistryClient(registryClient)

// Fixes #7002 - Support reading values from STDIN for `upgrade` command
// Must load values AFTER determining if we have to call install so that values loaded from stdin are are not read twice
if client.Install {
Expand Down Expand Up @@ -136,7 +142,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client.Version = ">0.0.0-0"
}

chartPath, err := client.ChartPathOptions.LocateChart(args[1], out, settings)
chartPath, err := client.ChartPathOptions.LocateChart(args[1], settings)
if err != nil {
return err
}
Expand Down
25 changes: 9 additions & 16 deletions pkg/action/install.go
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -137,6 +136,11 @@ func NewInstall(cfg *Configuration) *Install {
return in
}

// SetRegistryClient sets the registry client for the install action
func (i *Install) SetRegistryClient(registryClient *registry.Client) {
i.ChartPathOptions.registryClient = registryClient
}

func (i *Install) installCRDs(crds []chart.CRD) error {
// We do these one file at a time in the order they were read.
totalItems := []*resource.Info{}
Expand Down Expand Up @@ -676,22 +680,11 @@ OUTER:
// - URL
//
// If 'verify' was set on ChartPathOptions, this will attempt to also verify the chart.
func (c *ChartPathOptions) LocateChart(name string, out io.Writer, 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) {
if (c.CertFile != "" && c.KeyFile != "") || c.CaFile != "" || c.InsecureSkipTLSverify {
registryClient, err := registry.NewRegistryClientWithTLS(out, c.CertFile, c.KeyFile, c.CaFile,
c.InsecureSkipTLSverify, settings.RegistryConfig, settings.Debug)
if err != nil {
return "", err
}
c.registryClient = registryClient
}
if c.registryClient == nil {
return "", fmt.Errorf("unable to lookup chart %q, missing registry client", name)
}
func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) (string, error) {
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
24 changes: 5 additions & 19 deletions pkg/action/pull.go
Expand Up @@ -18,7 +18,6 @@ package action

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -48,7 +47,6 @@ type Pull struct {
UntarDir string
DestDir string
cfg *Configuration
out io.Writer
}

type PullOpt func(*Pull)
Expand All @@ -59,13 +57,6 @@ func WithConfig(cfg *Configuration) PullOpt {
}
}

// WithOptWriter sets the registryOut field on the push configuration object.
func WithPullOptWriter(out io.Writer) PullOpt {
return func(p *Pull) {
p.out = out
}
}

// NewPull creates a new Pull object.
func NewPull() *Pull {
return NewPullWithOpts()
Expand All @@ -81,6 +72,11 @@ func NewPullWithOpts(opts ...PullOpt) *Pull {
return p
}

// SetRegistryClient sets the registry client on the pull configuration object.
func (p *Pull) SetRegistryClient(client *registry.Client) {
p.cfg.RegistryClient = client
}

// Run executes 'helm pull' against the given release.
func (p *Pull) Run(chartRef string) (string, error) {
var out strings.Builder
Expand All @@ -102,16 +98,6 @@ func (p *Pull) Run(chartRef string) (string, error) {
}

if registry.IsOCI(chartRef) {
// Provide a tls enabled client for the pull command if the user has
// specified the cert file or key file or ca file.
if (p.ChartPathOptions.CertFile != "" && p.ChartPathOptions.KeyFile != "") || p.ChartPathOptions.CaFile != "" || p.ChartPathOptions.InsecureSkipTLSverify {
registryClient, err := registry.NewRegistryClientWithTLS(p.out, p.ChartPathOptions.CertFile, p.ChartPathOptions.KeyFile, p.ChartPathOptions.CaFile,
p.ChartPathOptions.InsecureSkipTLSverify, p.Settings.RegistryConfig, p.Settings.Debug)
if err != nil {
return out.String(), err
}
p.cfg.RegistryClient = registryClient
}
c.Options = append(c.Options,
getter.WithRegistryClient(p.cfg.RegistryClient))
c.RegistryClient = p.cfg.RegistryClient
Expand Down
12 changes: 1 addition & 11 deletions pkg/action/push.go
Expand Up @@ -90,21 +90,11 @@ func (p *Push) Run(chartRef string, remote string) (string, error) {
Pushers: pusher.All(p.Settings),
Options: []pusher.Option{
pusher.WithTLSClientConfig(p.certFile, p.keyFile, p.caFile),
pusher.WithInsecureSkipTLSVerify(p.insecureSkipTLSverify),
},
}

if registry.IsOCI(remote) {
// Provide a tls enabled client for the pull command if the user has
// specified the cert file or key file or ca file.
if (p.certFile != "" && p.keyFile != "") || p.caFile != "" || p.insecureSkipTLSverify {
registryClient, err := registry.NewRegistryClientWithTLS(p.out, p.certFile, p.keyFile, p.caFile,
p.insecureSkipTLSverify, p.Settings.RegistryConfig, p.Settings.Debug)
if err != nil {
return out.String(), err
}
p.cfg.RegistryClient = registryClient
}

// Don't use the default registry client if tls options are set.
c.Options = append(c.Options, pusher.WithRegistryClient(p.cfg.RegistryClient))
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/action/show.go
Expand Up @@ -28,6 +28,7 @@ import (
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/registry"
)

// ShowOutputFormat is the format of the output of `helm show`
Expand Down Expand Up @@ -82,6 +83,11 @@ func NewShowWithConfig(output ShowOutputFormat, cfg *Configuration) *Show {
return sh
}

// SetRegistryClient sets the registry client to use when pulling a chart from a registry.
func (s *Show) SetRegistryClient(client *registry.Client) {
s.ChartPathOptions.registryClient = client
}

// Run executes 'helm show' against the given release.
func (s *Show) Run(chartpath string) (string, error) {
if s.chart == nil {
Expand Down

0 comments on commit 11738dd

Please sign in to comment.