From 4971ed5077d0e466d2731b5f143547375ef468ca Mon Sep 17 00:00:00 2001 From: Matthew Fisher Date: Mon, 23 Sep 2019 15:00:16 -0700 Subject: [PATCH] fix(list): scrub list returned from .List() This way only the latest release is displayed with `helm list`. Signed-off-by: Matthew Fisher --- cmd/helm/testdata/output/list-all.txt | 1 - pkg/action/list.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/helm/testdata/output/list-all.txt b/cmd/helm/testdata/output/list-all.txt index 1872236fd..d8c5efd5d 100644 --- a/cmd/helm/testdata/output/list-all.txt +++ b/cmd/helm/testdata/output/list-all.txt @@ -5,6 +5,5 @@ groot default 1 2016-01-16 00:00:01 +0000 UTC uninstalled chi hummingbird default 1 2016-01-16 00:00:03 +0000 UTC deployed chickadee-1.0.0 iguana default 2 2016-01-16 00:00:04 +0000 UTC deployed chickadee-1.0.0 rocket default 1 2016-01-16 00:00:02 +0000 UTC failed chickadee-1.0.0 -starlord default 1 2016-01-16 00:00:01 +0000 UTC superseded chickadee-1.0.0 starlord default 2 2016-01-16 00:00:01 +0000 UTC deployed chickadee-1.0.0 thanos default 1 2016-01-16 00:00:01 +0000 UTC pending-install chickadee-1.0.0 diff --git a/pkg/action/list.go b/pkg/action/list.go index f502269c6..201a0db3e 100644 --- a/pkg/action/list.go +++ b/pkg/action/list.go @@ -171,6 +171,8 @@ func (l *List) Run() ([]*release.Release, error) { return results, nil } + results = filterList(results) + // Unfortunately, we have to sort before truncating, which can incur substantial overhead l.sort(results) @@ -218,6 +220,30 @@ func (l *List) sort(rels []*release.Release) { } } +// filterList returns a list scrubbed of old releases. +func filterList(rels []*release.Release) []*release.Release { + idx := map[string]int{} + + for _, r := range rels { + name, version := r.Name, r.Version + if max, ok := idx[name]; ok { + // check if we have a greater version already + if max > version { + continue + } + } + idx[name] = version + } + + uniq := make([]*release.Release, 0, len(idx)) + for _, r := range rels { + if idx[r.Name] == r.Version { + uniq = append(uniq, r) + } + } + return uniq +} + // setStateMask calculates the state mask based on parameters. func (l *List) SetStateMask() { if l.All {