Skip to content

Commit

Permalink
[Bug] Fix #1033, #1558
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Feb 7, 2024
1 parent efe6789 commit 12a31c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
20 changes: 19 additions & 1 deletion internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
defaultCallTimeoutDuration time.Duration = 15 * time.Second

// UsePersistentConfig caches client config to avoid reloads.
UsePersistentConfig = true
UsePersistentConfig = false
)

// Config tracks a kubernetes configuration.
Expand Down Expand Up @@ -85,6 +85,24 @@ func (c *Config) SwitchContext(name string) error {
return nil
}

func (c *Config) Clone(ns string) (*genericclioptions.ConfigFlags, error) {
flags := genericclioptions.NewConfigFlags(false)
ct, err := c.CurrentContextName()
if err != nil {
return nil, err
}
cl, err := c.CurrentClusterName()
if err != nil {
return nil, err
}
flags.Context, flags.ClusterName = &ct, &cl
flags.Namespace = &ns
flags.Timeout = c.Flags().Timeout
flags.KubeConfig = c.Flags().KubeConfig

return flags, nil
}

// CurrentClusterName returns the currently active cluster name.
func (c *Config) CurrentClusterName() (string, error) {
if isSet(c.flags.ClusterName) {
Expand Down
24 changes: 14 additions & 10 deletions internal/dao/helm_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"helm.sh/helm/v3/pkg/action"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
Expand All @@ -31,7 +32,7 @@ type HelmChart struct {

// List returns a collection of resources.
func (h *HelmChart) List(ctx context.Context, ns string) ([]runtime.Object, error) {
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +56,7 @@ func (h *HelmChart) List(ctx context.Context, ns string) ([]runtime.Object, erro
// Get returns a resource.
func (h *HelmChart) Get(_ context.Context, path string) (runtime.Object, error) {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return nil, err
}
Expand All @@ -70,7 +71,7 @@ func (h *HelmChart) Get(_ context.Context, path string) (runtime.Object, error)
// GetValues returns values for a release
func (h *HelmChart) GetValues(path string, allValues bool) ([]byte, error) {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +88,7 @@ func (h *HelmChart) GetValues(path string, allValues bool) ([]byte, error) {
// Describe returns the chart notes.
func (h *HelmChart) Describe(path string) (string, error) {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return "", err
}
Expand All @@ -102,7 +103,7 @@ func (h *HelmChart) Describe(path string) (string, error) {
// ToYAML returns the chart manifest.
func (h *HelmChart) ToYAML(path string, showManaged bool) (string, error) {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return "", err
}
Expand All @@ -122,10 +123,13 @@ func (h *HelmChart) Delete(_ context.Context, path string, _ *metav1.DeletionPro
// Uninstall uninstalls a HelmChart.
func (h *HelmChart) Uninstall(path string, keepHist bool) error {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
flags := h.Client().Config().Flags()
flags.Namespace = &ns
cfg, err := ensureHelmConfig(flags, ns)
if err != nil {
return err
}

u := action.NewUninstall(cfg)
u.KeepHistory = keepHist
res, err := u.Run(n)
Expand All @@ -140,13 +144,13 @@ func (h *HelmChart) Uninstall(path string, keepHist bool) error {
}

// ensureHelmConfig return a new configuration.
func ensureHelmConfig(c client.Connection, ns string) (*action.Configuration, error) {
func ensureHelmConfig(flags *genericclioptions.ConfigFlags, ns string) (*action.Configuration, error) {
cfg := new(action.Configuration)
err := cfg.Init(c.Config().Flags(), ns, os.Getenv("HELM_DRIVER"), helmLogger)
err := cfg.Init(flags, ns, os.Getenv("HELM_DRIVER"), helmLogger)

return cfg, err
}

func helmLogger(s string, args ...interface{}) {
log.Debug().Msgf("%s %v", s, args)
func helmLogger(fmt string, args ...interface{}) {
log.Debug().Msgf("[Helm] "+fmt, args...)
}
8 changes: 4 additions & 4 deletions internal/dao/helm_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *HelmHistory) List(ctx context.Context, _ string) ([]runtime.Object, err
}
ns, n := client.Namespaced(path)

cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,7 @@ func (h *HelmHistory) Get(_ context.Context, path string) (runtime.Object, error
}

ns, n := client.Namespaced(fqn)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (h *HelmHistory) GetValues(path string, allValues bool) ([]byte, error) {

func (h *HelmHistory) Rollback(_ context.Context, path, rev string) error {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return err
}
Expand All @@ -152,7 +152,7 @@ func (h *HelmHistory) Rollback(_ context.Context, path, rev string) error {
// Delete uninstall a Helm.
func (h *HelmHistory) Delete(_ context.Context, path string, _ *metav1.DeletionPropagation, _ Grace) error {
ns, n := client.Namespaced(path)
cfg, err := ensureHelmConfig(h.Client(), ns)
cfg, err := ensureHelmConfig(h.Client().Config().Flags(), ns)
if err != nil {
return err
}
Expand Down

0 comments on commit 12a31c3

Please sign in to comment.