Repository: helm/helm
Pull request #7356 (2020-01-08 18:29:03)

--registry-config flag is not honored
Fix for https://github.com/helm/helm/issues/7351


Patch:
diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go
index fcc7315f5..474a3d2d4 100644
--- a/cmd/helm/helm.go
+++ b/cmd/helm/helm.go
@@ -68,7 +68,11 @@ func main() {
 	initKubeLogs()
 
 	actionConfig := new(action.Configuration)
-	cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:])
+	cmd, err := newRootCmd(actionConfig, os.Stdout, os.Args[1:])
+	if err != nil {
+		debug("%+v", err)
+		os.Exit(1)
+	}
 
 	// run when each command's execute method is called
 	cobra.OnInitialize(func() {
diff --git a/cmd/helm/root.go b/cmd/helm/root.go
index 143745f29..fe917c75f 100644
--- a/cmd/helm/root.go
+++ b/cmd/helm/root.go
@@ -68,7 +68,7 @@ By default, the default directories depend on the Operating System. The defaults
 | Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |
 `
 
-func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) *cobra.Command {
+func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) (*cobra.Command, error) {
 	cmd := &cobra.Command{
 		Use:                    "helm",
 		Short:                  "The Helm package manager for Kubernetes.",
@@ -173,10 +173,10 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
 	registryClient, err := registry.NewClient(
 		registry.ClientOptDebug(settings.Debug),
 		registry.ClientOptWriter(out),
+		registry.ClientOptCredentialsFile(settings.RegistryConfig),
 	)
 	if err != nil {
-		// TODO: don't panic here, refactor newRootCmd to return error
-		panic(err)
+		return nil, err
 	}
 	actionConfig.RegistryClient = registryClient
 	cmd.AddCommand(
@@ -187,5 +187,5 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
 	// Find and add plugins
 	loadPlugins(cmd, out)
 
-	return cmd
+	return cmd, nil
 }
diff --git a/internal/experimental/registry/client.go b/internal/experimental/registry/client.go
index f664c9f38..540eddfa3 100644
--- a/internal/experimental/registry/client.go
+++ b/internal/experimental/registry/client.go
@@ -42,11 +42,13 @@ const (
 type (
 	// Client works with OCI-compliant registries and local Helm chart cache
 	Client struct {
-		debug      bool
-		out        io.Writer
-		authorizer *Authorizer
-		resolver   *Resolver
-		cache      *Cache
+		debug bool
+		// path to repository config file e.g. ~/.docker/config.json
+		credentialsFile string
+		out             io.Writer
+		authorizer      *Authorizer
+		resolver        *Resolver
+		cache           *Cache
 	}
 )
 
@@ -59,9 +61,11 @@ func NewClient(opts ...ClientOption) (*Client, error) {
 		opt(client)
 	}
 	// set defaults if fields are missing
+	if client.credentialsFile == "" {
+		client.credentialsFile = helmpath.CachePath("registry", CredentialsFileBasename)
+	}
 	if client.authorizer == nil {
-		credentialsFile := helmpath.CachePath("registry", CredentialsFileBasename)
-		authClient, err := auth.NewClient(credentialsFile)
+		authClient, err := auth.NewClient(client.credentialsFile)
 		if err != nil {
 			return nil, err
 		}
diff --git a/internal/experimental/registry/client_opts.go b/internal/experimental/registry/client_opts.go
index cd295813a..76b527492 100644
--- a/internal/experimental/registry/client_opts.go
+++ b/internal/experimental/registry/client_opts.go
@@ -60,3 +60,10 @@ func ClientOptCache(cache *Cache) ClientOption {
 		client.cache = cache
 	}
 }
+
+// ClientOptCache returns a function that sets the cache setting on a client options set
+func ClientOptCredentialsFile(credentialsFile string) ClientOption {
+	return func(client *Client) {
+		client.credentialsFile = credentialsFile
+	}
+}


Test patch:
diff --git a/cmd/helm/helm_test.go b/cmd/helm/helm_test.go
index 9ba9d78fb..5e59c41ed 100644
--- a/cmd/helm/helm_test.go
+++ b/cmd/helm/helm_test.go
@@ -115,7 +115,11 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string)
 		Log:          func(format string, v ...interface{}) {},
 	}
 
-	root := newRootCmd(actionConfig, buf, args)
+	root, err := newRootCmd(actionConfig, buf, args)
+	if err != nil {
+		return nil, "", err
+	}
+
 	root.SetOut(buf)
 	root.SetErr(buf)
 	root.SetArgs(args)