Skip to content

Commit

Permalink
feat: sort resources returned from the List() API
Browse files Browse the repository at this point in the history
This fixes user-facing `talosctl get <resource>`, 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 <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Jan 21, 2021
1 parent b8955a5 commit eb6e3df
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 0 additions & 2 deletions pkg/state/conformance/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion pkg/state/impl/inmem/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package inmem

import (
"context"
"sort"
"sync"

"github.com/talos-systems/os-runtime/pkg/resource"
Expand Down Expand Up @@ -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)),
Expand All @@ -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
}

Expand Down

0 comments on commit eb6e3df

Please sign in to comment.