Skip to content

Commit

Permalink
limit listcontainers API response
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 23, 2019
1 parent 0de55d8 commit c3bb536
Show file tree
Hide file tree
Showing 11 changed files with 526 additions and 507 deletions.
2 changes: 1 addition & 1 deletion cluster/calcium/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *Calcium) ListPodNodes(ctx context.Context, podname string, all bool) ([

// ListContainers list containers
func (c *Calcium) ListContainers(ctx context.Context, opts *types.ListContainersOptions) ([]*types.Container, error) {
return c.store.ListContainers(ctx, opts.Appname, opts.Entrypoint, opts.Nodename)
return c.store.ListContainers(ctx, opts.Appname, opts.Entrypoint, opts.Nodename, opts.Limit)
}

// ListNodeContainers list containers belong to one node
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestListContainers(t *testing.T) {

store := &storemocks.Store{}
c.store = store
store.On("ListContainers", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(containers, nil)
store.On("ListContainers", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(containers, nil)
store.On("ListNodeContainers", mock.Anything, mock.Anything).Return(containers, nil)

cs, err := c.ListContainers(ctx, &types.ListContainersOptions{Appname: "", Entrypoint: "", Nodename: ""})
Expand Down
601 changes: 305 additions & 296 deletions rpc/gen/core.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rpc/gen/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ message ListContainersOptions {
string entrypoint = 2;
string nodename = 3;
map<string, string> labels = 4;
int64 limit = 5;
}

message DeployStatusOptions {
Expand Down
403 changes: 205 additions & 198 deletions rpc/gen/core_pb2.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (v *Vibranium) ListContainers(opts *pb.ListContainersOptions, stream pb.Cor
Appname: opts.Appname,
Entrypoint: opts.Entrypoint,
Nodename: opts.Nodename,
Limit: opts.Limit,
}
containers, err := v.cluster.ListContainers(stream.Context(), lsopts)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions store/etcdv3/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (m *Mercury) ContainerDeployed(ctx context.Context, ID, appname, entrypoint
}

// ListContainers list containers
func (m *Mercury) ListContainers(ctx context.Context, appname, entrypoint, nodename string) ([]*types.Container, error) {
func (m *Mercury) ListContainers(ctx context.Context, appname, entrypoint, nodename string, limit int64) ([]*types.Container, error) {
if appname == "" {
entrypoint = ""
}
Expand All @@ -91,7 +91,7 @@ func (m *Mercury) ListContainers(ctx context.Context, appname, entrypoint, noden
}
// 这里显式加个 / 来保证 prefix 是唯一的
key := filepath.Join(containerDeployPrefix, appname, entrypoint, nodename) + "/"
resp, err := m.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithKeysOnly())
resp, err := m.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithKeysOnly(), clientv3.WithLimit(limit))
if err != nil {
return []*types.Container{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion store/etcdv3/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestContainer(t *testing.T) {
assert.NoError(t, m.ContainerDeployed(ctx, ID, appname, entrypoint, nodename, []byte{}, 10))
assert.Error(t, m.ContainerDeployed(ctx, ID, appname, entrypoint, "n2", []byte(""), 0))
// ListContainers
containers, _ = m.ListContainers(ctx, appname, entrypoint, "")
containers, _ = m.ListContainers(ctx, appname, entrypoint, "", 1)
assert.Equal(t, len(containers), 1)
assert.Equal(t, containers[0].Name, name)
// ListNodeContainers
Expand Down
14 changes: 7 additions & 7 deletions store/mocks/Store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Store interface {
GetContainer(ctx context.Context, ID string) (*types.Container, error)
GetContainers(ctx context.Context, IDs []string) ([]*types.Container, error)
ContainerDeployed(ctx context.Context, ID, appname, entrypoint, nodename string, data []byte, ttl int64) error
ListContainers(ctx context.Context, appname, entrypoint, nodename string) ([]*types.Container, error)
ListContainers(ctx context.Context, appname, entrypoint, nodename string, limit int64) ([]*types.Container, error)
ListNodeContainers(ctx context.Context, nodename string) ([]*types.Container, error)
WatchDeployStatus(ctx context.Context, appname, entrypoint, nodename string) chan *types.DeployStatus

Expand Down
1 change: 1 addition & 0 deletions types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ListContainersOptions struct {
Appname string
Entrypoint string
Nodename string
Limit int64
}

// ReplaceOptions for replace container
Expand Down

0 comments on commit c3bb536

Please sign in to comment.