From 12a31c3cb137a3f0508c8a1c555dbb9a8f94d740 Mon Sep 17 00:00:00 2001 From: derailed Date: Tue, 6 Feb 2024 17:30:28 -0700 Subject: [PATCH] [Bug] Fix #1033, #1558 --- internal/client/config.go | 20 +++++++++++++++++++- internal/dao/helm_chart.go | 24 ++++++++++++++---------- internal/dao/helm_history.go | 8 ++++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/internal/client/config.go b/internal/client/config.go index 28f1d53d88..0dafc59e64 100644 --- a/internal/client/config.go +++ b/internal/client/config.go @@ -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. @@ -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) { diff --git a/internal/dao/helm_chart.go b/internal/dao/helm_chart.go index 0f1e8fdbcf..2efe9f6b60 100644 --- a/internal/dao/helm_chart.go +++ b/internal/dao/helm_chart.go @@ -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 ( @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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) @@ -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...) } diff --git a/internal/dao/helm_history.go b/internal/dao/helm_history.go index d589298cd6..3dd30deaea 100644 --- a/internal/dao/helm_history.go +++ b/internal/dao/helm_history.go @@ -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 } @@ -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 } @@ -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 } @@ -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 }