Skip to content

Commit

Permalink
refactor: add helper function to initialize action config
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhanushkodi committed Sep 22, 2021
1 parent 87cc790 commit 537436a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
24 changes: 23 additions & 1 deletion cli/cmd/common/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package common

import "strings"
import (
"fmt"
"os"
"strings"

"helm.sh/helm/v3/pkg/action"
helmCLI "helm.sh/helm/v3/pkg/cli"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

// Abort returns true if the raw input string is not equal to "y" or "yes".
func Abort(raw string) bool {
Expand All @@ -10,3 +18,17 @@ func Abort(raw string) bool {
}
return false
}

// InitActionConfig initializes a Helm Go SDK action configuration. This function currently uses a hack to override the
// namespace field that gets set in the K8s client set up by the SDK.
func InitActionConfig(actionConfig *action.Configuration, namespace string, settings *helmCLI.EnvSettings, logger action.DebugLog) (*action.Configuration, error) {
getter := settings.RESTClientGetter()
configFlags := getter.(*genericclioptions.ConfigFlags)
configFlags.Namespace = &namespace
err := actionConfig.Init(settings.RESTClientGetter(), namespace,
os.Getenv("HELM_DRIVER"), logger)
if err != nil {
return nil, fmt.Errorf("error setting up helm action configuration to find existing installations: %s", err)
}
return actionConfig, nil
}
6 changes: 3 additions & 3 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ func (c *Command) Run(args []string) int {

// Setup action configuration for Helm Go SDK function calls.
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), c.flagNamespace,
os.Getenv("HELM_DRIVER"), uiLogger); err != nil {
c.UI.Output(err.Error())
actionConfig, err = common.InitActionConfig(actionConfig, c.flagNamespace, settings, uiLogger)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
}

Expand Down
17 changes: 2 additions & 15 deletions cli/cmd/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"helm.sh/helm/v3/pkg/action"
helmCLI "helm.sh/helm/v3/pkg/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -178,7 +177,7 @@ func (c *Command) Run(args []string) int {

// Search for Consul installation by calling `helm list`. Depends on what's already specified.
actionConfig := new(action.Configuration)
actionConfig, err = c.initActionConfig(actionConfig, c.flagNamespace, settings, uiLogger)
actionConfig, err = common.InitActionConfig(actionConfig, c.flagNamespace, settings, uiLogger)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
Expand Down Expand Up @@ -215,7 +214,7 @@ func (c *Command) Run(args []string) int {
}

// Actually call out to `helm delete`.
actionConfig, err = c.initActionConfig(actionConfig, foundReleaseNamespace, settings, uiLogger)
actionConfig, err = common.InitActionConfig(actionConfig, foundReleaseNamespace, settings, uiLogger)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
Expand Down Expand Up @@ -316,18 +315,6 @@ func (c *Command) Synopsis() string {
return "Uninstall Consul deployment."
}

func (c *Command) initActionConfig(actionConfig *action.Configuration, namespace string, settings *helmCLI.EnvSettings, logger action.DebugLog) (*action.Configuration, error) {
getter := settings.RESTClientGetter()
configFlags := getter.(*genericclioptions.ConfigFlags)
configFlags.Namespace = &namespace
err := actionConfig.Init(settings.RESTClientGetter(), namespace,
os.Getenv("HELM_DRIVER"), logger)
if err != nil {
return nil, fmt.Errorf("error setting up helm action configuration to find existing installations: %s", err)
}
return actionConfig, nil
}

func (c *Command) findExistingInstallation(actionConfig *action.Configuration) (bool, string, string, error) {
lister := action.NewList(actionConfig)
// lister.All will search for helm installations in all states, such as deployed, pending, uninstalling, etc.
Expand Down

0 comments on commit 537436a

Please sign in to comment.