Implementing AllNamespaces in action.NewList

Signed-off-by: Vaibhav Sharma <17532va@gmail.com>
pull/11674/head
Vaibhav Sharma 3 years ago
parent eb7150fc61
commit 93add79311

@ -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()

@ -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,6 +113,7 @@ 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.
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{}

@ -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

@ -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()

Loading…
Cancel
Save