From eb6e3dfd68f82a32f1f51bf85d161bf2f1bfbc59 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 21 Jan 2021 21:07:50 +0300 Subject: [PATCH] feat: sort resources returned from the List() API This fixes user-facing `talosctl get `, and also provides stable order for controllers listing other resources. This also aligns with the way etcd API works (returns sorted keys). Signed-off-by: Andrey Smirnov --- pkg/state/conformance/state.go | 2 -- pkg/state/impl/inmem/collection.go | 8 +++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/state/conformance/state.go b/pkg/state/conformance/state.go index 1517d810..279f9353 100644 --- a/pkg/state/conformance/state.go +++ b/pkg/state/conformance/state.go @@ -80,8 +80,6 @@ func (suite *StateSuite) TestCRD() { ids[i] = list.Items[i].String() } - sort.Strings(ids) - suite.Assert().Equal([]string{path2.String(), path1.String()}, ids) } else { suite.Assert().Len(list.Items, 1) diff --git a/pkg/state/impl/inmem/collection.go b/pkg/state/impl/inmem/collection.go index f61060ee..dd1a4d06 100644 --- a/pkg/state/impl/inmem/collection.go +++ b/pkg/state/impl/inmem/collection.go @@ -6,6 +6,7 @@ package inmem import ( "context" + "sort" "sync" "github.com/talos-systems/os-runtime/pkg/resource" @@ -75,7 +76,6 @@ func (collection *ResourceCollection) Get(resourceID resource.ID) (resource.Reso // List resources. func (collection *ResourceCollection) List() (resource.List, error) { collection.mu.Lock() - defer collection.mu.Unlock() result := resource.List{ Items: make([]resource.Resource, 0, len(collection.storage)), @@ -85,6 +85,12 @@ func (collection *ResourceCollection) List() (resource.List, error) { result.Items = append(result.Items, res.DeepCopy()) } + collection.mu.Unlock() + + sort.Slice(result.Items, func(i, j int) bool { + return result.Items[i].Metadata().ID() < result.Items[j].Metadata().ID() + }) + return result, nil }