fix list not showing multiple releases with same name in different namespaces (#6756)

Signed-off-by: Karuppiah Natarajan <karuppiah7890@gmail.com>
pull/6778/head
Karuppiah Natarajan 5 years ago committed by Martin Hickey
parent 0f94af1f3a
commit bfd8250803

@ -17,6 +17,7 @@ limitations under the License.
package action package action
import ( import (
"path"
"regexp" "regexp"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
@ -218,27 +219,23 @@ func (l *List) sort(rels []*release.Release) {
} }
// filterList returns a list scrubbed of old releases. // filterList returns a list scrubbed of old releases.
func filterList(rels []*release.Release) []*release.Release { func filterList(releases []*release.Release) []*release.Release {
idx := map[string]int{} latestReleases := make(map[string]*release.Release)
for _, r := range rels { for _, rls := range releases {
name, version := r.Name, r.Version name, namespace := rls.Name, rls.Namespace
if max, ok := idx[name]; ok { key := path.Join(namespace, name)
// check if we have a greater version already if latestRelease, exists := latestReleases[key]; exists && latestRelease.Version > rls.Version {
if max > version { continue
continue
}
} }
idx[name] = version latestReleases[key] = rls
} }
uniq := make([]*release.Release, 0, len(idx)) var list = make([]*release.Release, 0, len(latestReleases))
for _, r := range rels { for _, rls := range latestReleases {
if idx[r.Name] == r.Version { list = append(list, rls)
uniq = append(uniq, r)
}
} }
return uniq return list
} }
// setStateMask calculates the state mask based on parameters. // setStateMask calculates the state mask based on parameters.

@ -159,6 +159,7 @@ func TestList_LimitOffsetOutOfBounds(t *testing.T) {
is.NoError(err) is.NoError(err)
is.Len(list, 2) is.Len(list, 2)
} }
func TestList_StateMask(t *testing.T) { func TestList_StateMask(t *testing.T) {
is := assert.New(t) is := assert.New(t)
lister := newListFixture(t) lister := newListFixture(t)
@ -166,7 +167,8 @@ func TestList_StateMask(t *testing.T) {
one, err := lister.cfg.Releases.Get("one", 1) one, err := lister.cfg.Releases.Get("one", 1)
is.NoError(err) is.NoError(err)
one.SetStatus(release.StatusUninstalled, "uninstalled") one.SetStatus(release.StatusUninstalled, "uninstalled")
lister.cfg.Releases.Update(one) err = lister.cfg.Releases.Update(one)
is.NoError(err)
res, err := lister.Run() res, err := lister.Run()
is.NoError(err) is.NoError(err)
@ -222,11 +224,6 @@ func makeMeSomeReleases(store *storage.Storage, t *testing.T) {
three.Name = "three" three.Name = "three"
three.Namespace = "default" three.Namespace = "default"
three.Version = 3 three.Version = 3
four := releaseStub()
four.Name = "four"
four.Namespace = "default"
four.Version = 4
four.Info.Status = release.StatusSuperseded
for _, rel := range []*release.Release{one, two, three} { for _, rel := range []*release.Release{one, two, three} {
if err := store.Create(rel); err != nil { if err := store.Create(rel); err != nil {
@ -238,3 +235,29 @@ func makeMeSomeReleases(store *storage.Storage, t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, all, 3, "sanity test: three items added") assert.Len(t, all, 3, "sanity test: three items added")
} }
func TestFilterList(t *testing.T) {
one := releaseStub()
one.Name = "one"
one.Namespace = "default"
one.Version = 1
two := releaseStub()
two.Name = "two"
two.Namespace = "default"
two.Version = 1
anotherOldOne := releaseStub()
anotherOldOne.Name = "one"
anotherOldOne.Namespace = "testing"
anotherOldOne.Version = 1
anotherOne := releaseStub()
anotherOne.Name = "one"
anotherOne.Namespace = "testing"
anotherOne.Version = 2
list := []*release.Release{one, two, anotherOne}
expectedFilteredList := []*release.Release{one, two, anotherOne}
filteredList := filterList(list)
assert.ElementsMatch(t, expectedFilteredList, filteredList)
}

@ -51,7 +51,7 @@ func TestConfigMapGet(t *testing.T) {
} }
} }
func TestUNcompressedConfigMapGet(t *testing.T) { func TestUncompressedConfigMapGet(t *testing.T) {
vers := 1 vers := 1
name := "smug-pigeon" name := "smug-pigeon"
namespace := "default" namespace := "default"

Loading…
Cancel
Save