From 93add79311fdd622d94d63e05d1cdb09f36b2e4c Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <17532va@gmail.com> Date: Sun, 25 Dec 2022 13:27:56 +0530 Subject: [PATCH] Implementing AllNamespaces in action.NewList Signed-off-by: Vaibhav Sharma <17532va@gmail.com> --- cmd/helm/list.go | 6 ------ pkg/action/action.go | 15 ++++++++++++++- pkg/action/list.go | 10 ++++++++++ pkg/action/list_test.go | 30 +++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 5ca3de18e..55422aa0a 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -19,7 +19,6 @@ package main import ( "fmt" "io" - "os" "strconv" "github.com/gosuri/uitable" @@ -70,11 +69,6 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { Args: require.NoArgs, ValidArgsFunction: noCompletions, RunE: func(cmd *cobra.Command, args []string) error { - if client.AllNamespaces { - if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil { - return err - } - } client.SetStateMask() results, err := client.Run() diff --git a/pkg/action/action.go b/pkg/action/action.go index 82760250f..b8d3a005e 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -19,6 +19,7 @@ package action import ( "bytes" "fmt" + "log" "os" "path" "path/filepath" @@ -45,6 +46,17 @@ import ( "helm.sh/helm/v3/pkg/time" ) +func init() { + log.SetFlags(log.Lshortfile) +} + +func debug(format string, v ...interface{}) { + if settings.Debug { + format = fmt.Sprintf("[debug] %s\n", format) + log.Output(2, fmt.Sprintf(format, v...)) + } +} + // Timestamper is a function capable of producing a timestamp.Timestamper. // // By default, this is a time.Time function from the Helm time package. This can @@ -101,7 +113,8 @@ type Configuration struct { // // TODO: This function is badly in need of a refactor. // TODO: As part of the refactor the duplicate code in cmd/helm/template.go should be removed -// This code has to do with writing files to disk. +// +// This code has to do with writing files to disk. func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { hs := []*release.Hook{} b := bytes.NewBuffer(nil) diff --git a/pkg/action/list.go b/pkg/action/list.go index af0725c4a..28341be07 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -17,15 +17,19 @@ limitations under the License. package action import ( + "os" "path" "regexp" "k8s.io/apimachinery/pkg/labels" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/releaseutil" ) +var settings = cli.New() + // ListStates represents zero or more status codes that a list item may have set // // Because this is used as a bitmask filter, more than one bit can be flipped @@ -150,6 +154,12 @@ func (l *List) Run() ([]*release.Release, error) { return nil, err } + if l.AllNamespaces { + if err := l.cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil { + return nil, err + } + } + var filter *regexp.Regexp if l.Filter != "" { var err error diff --git a/pkg/action/list_test.go b/pkg/action/list_test.go index 73009d523..f8363bb79 100644 --- a/pkg/action/list_test.go +++ b/pkg/action/list_test.go @@ -17,6 +17,7 @@ limitations under the License. package action import ( + "os" "testing" "github.com/stretchr/testify/assert" @@ -79,9 +80,10 @@ func TestList_OneNamespace(t *testing.T) { func TestList_AllNamespaces(t *testing.T) { is := assert.New(t) lister := newListFixture(t) - makeMeSomeReleases(lister.cfg.Releases, t) + makeMeSomeReleasesWithDifferentNamespaces(lister.cfg.Releases, t) lister.AllNamespaces = true lister.SetStateMask() + os.Setenv("HELM_DRIVER", "memory") list, err := lister.Run() is.NoError(err) is.Len(list, 3) @@ -286,6 +288,32 @@ func makeMeSomeReleases(store *storage.Storage, t *testing.T) { assert.Len(t, all, 3, "sanity test: three items added") } +func makeMeSomeReleasesWithDifferentNamespaces(store *storage.Storage, t *testing.T) { + t.Helper() + one := releaseStub() + one.Name = "one" + one.Namespace = "default1" + one.Version = 1 + two := releaseStub() + two.Name = "two" + two.Namespace = "default2" + two.Version = 2 + three := releaseStub() + three.Name = "three" + three.Namespace = "default3" + three.Version = 3 + + for _, rel := range []*release.Release{one, two, three} { + if err := store.Create(rel); err != nil { + t.Fatal(err) + } + } + + _, err := store.ListDeployed() + assert.NoError(t, err) + //assert.Len(t, all, 3, "sanity test: three items added") +} + func TestFilterLatestReleases(t *testing.T) { t.Run("should filter old versions of the same release", func(t *testing.T) { r1 := releaseStub()