Skip to content

Commit b1cfb22

Browse files
committed
add list container api
1 parent eb91918 commit b1cfb22

File tree

11 files changed

+303
-162
lines changed

11 files changed

+303
-162
lines changed

cluster/calcium/meta.go

+4
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ func (c *calcium) GetContainers(ids []string) ([]*types.Container, error) {
8383
func (c *calcium) ContainerDeployed(ID, appname, entrypoint, nodename, data string) error {
8484
return c.store.ContainerDeployed(ID, appname, entrypoint, nodename, data)
8585
}
86+
87+
func (c *calcium) ListContainers(appname, entrypoint, nodename string) ([]*types.Container, error) {
88+
return c.store.ListContainers(appname, entrypoint, nodename)
89+
}

cluster/cluster.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Cluster interface {
3131
RemoveContainer(ids []string, force bool) (chan *types.RemoveContainerMessage, error)
3232
Backup(id, srcPath string) (*types.BackupMessage, error)
3333
ReallocResource(ids []string, cpu float64, mem int64) (chan *types.ReallocResourceMessage, error)
34+
ListContainers(appname, entrypoint, nodename string) ([]*types.Container, error)
3435

3536
// used by agent
3637
GetNodeByName(nodename string) (*types.Node, error)

rpc/gen/core.pb.go

+180-146
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/gen/core.proto

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ service CoreRPC {
1919
rpc Backup(BackupOptions) returns (BackupMessage) {};
2020
rpc GetNodeByName(GetNodeOptions) returns (Node) {};
2121
rpc ContainerDeployed(ContainerDeployedOptions) returns (Empty) {};
22+
rpc ListContainers(DeployStatusOptions) returns (Containers) {};
2223

2324
rpc BuildImage(BuildImageOptions) returns (stream BuildImageMessage) {};
2425
rpc RemoveImage(RemoveImageOptions) returns (stream RemoveImageMessage) {};

rpc/gen/core_pb2.py

+33-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/gen/core_pb2_grpc.py

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def __init__(self, channel):
8989
request_serializer=core__pb2.ContainerDeployedOptions.SerializeToString,
9090
response_deserializer=core__pb2.Empty.FromString,
9191
)
92+
self.ListContainers = channel.unary_unary(
93+
'/pb.CoreRPC/ListContainers',
94+
request_serializer=core__pb2.DeployStatusOptions.SerializeToString,
95+
response_deserializer=core__pb2.Containers.FromString,
96+
)
9297
self.BuildImage = channel.unary_stream(
9398
'/pb.CoreRPC/BuildImage',
9499
request_serializer=core__pb2.BuildImageOptions.SerializeToString,
@@ -235,6 +240,13 @@ def ContainerDeployed(self, request, context):
235240
context.set_details('Method not implemented!')
236241
raise NotImplementedError('Method not implemented!')
237242

243+
def ListContainers(self, request, context):
244+
# missing associated documentation comment in .proto file
245+
pass
246+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
247+
context.set_details('Method not implemented!')
248+
raise NotImplementedError('Method not implemented!')
249+
238250
def BuildImage(self, request, context):
239251
# missing associated documentation comment in .proto file
240252
pass
@@ -362,6 +374,11 @@ def add_CoreRPCServicer_to_server(servicer, server):
362374
request_deserializer=core__pb2.ContainerDeployedOptions.FromString,
363375
response_serializer=core__pb2.Empty.SerializeToString,
364376
),
377+
'ListContainers': grpc.unary_unary_rpc_method_handler(
378+
servicer.ListContainers,
379+
request_deserializer=core__pb2.DeployStatusOptions.FromString,
380+
response_serializer=core__pb2.Containers.SerializeToString,
381+
),
365382
'BuildImage': grpc.unary_stream_rpc_method_handler(
366383
servicer.BuildImage,
367384
request_deserializer=core__pb2.BuildImageOptions.FromString,

rpc/rpc.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ func (v *vibranium) GetContainer(ctx context.Context, id *pb.ContainerID) (*pb.C
157157
return toRPCContainer(container, string(bytes)), nil
158158
}
159159

160+
//ListContainers by appname with optional entrypoint and nodename
161+
func (v *vibranium) ListContainers(ctx context.Context, opts *pb.DeployStatusOptions) (*pb.Containers, error) {
162+
containers, err := v.cluster.ListContainers(opts.Appname, opts.Entrypoint, opts.Nodename)
163+
if err != nil {
164+
return nil, err
165+
}
166+
167+
return &pb.Containers{Containers: toRPCContainers(containers)}, nil
168+
}
169+
160170
//GetContainers
161171
//like GetContainer, information should be returned
162172
func (v *vibranium) GetContainers(ctx context.Context, cids *pb.ContainerIDs) (*pb.Containers, error) {
@@ -165,21 +175,7 @@ func (v *vibranium) GetContainers(ctx context.Context, cids *pb.ContainerIDs) (*
165175
return nil, err
166176
}
167177

168-
cs := []*pb.Container{}
169-
for _, c := range containers {
170-
info, err := c.Inspect()
171-
if err != nil {
172-
continue
173-
}
174-
175-
bytes, err := json.Marshal(info)
176-
if err != nil {
177-
continue
178-
}
179-
180-
cs = append(cs, toRPCContainer(c, string(bytes)))
181-
}
182-
return &pb.Containers{Containers: cs}, nil
178+
return &pb.Containers{Containers: toRPCContainers(containers)}, nil
183179
}
184180

185181
// list networks for pod

rpc/transform.go

+17
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,20 @@ func toRPCRunAndWaitMessage(msg *types.RunAndWaitMessage) *pb.RunAndWaitMessage
212212
Data: msg.Data,
213213
}
214214
}
215+
216+
func toRPCContainers(containers []*types.Container) []*pb.Container {
217+
cs := []*pb.Container{}
218+
for _, c := range containers {
219+
info, err := c.Inspect()
220+
if err != nil {
221+
continue
222+
}
223+
224+
bytes, err := json.Marshal(info)
225+
if err != nil {
226+
continue
227+
}
228+
cs = append(cs, toRPCContainer(c, string(bytes)))
229+
}
230+
return cs
231+
}

store/etcd/container.go

+30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"path/filepath"
7+
"strings"
78

89
"context"
910

@@ -129,3 +130,32 @@ func (k *krypton) WatchDeployStatus(appname, entrypoint, nodename string) etcdcl
129130
key := filepath.Join(containerDeployPrefix, appname, entrypoint, nodename)
130131
return k.etcd.Watcher(key, &etcdclient.WatcherOptions{Recursive: true})
131132
}
133+
134+
func (k *krypton) ListContainers(appname, entrypoint, nodename string) ([]*types.Container, error) {
135+
if appname == "" {
136+
entrypoint = ""
137+
}
138+
if entrypoint == "" {
139+
nodename = ""
140+
}
141+
key := filepath.Join(containerDeployPrefix, appname, entrypoint, nodename)
142+
resp, err := k.etcd.Get(context.Background(), key, &etcdclient.GetOptions{Recursive: true})
143+
if err != nil {
144+
return []*types.Container{}, err
145+
}
146+
containerIDs := getContainerDeployData(resp.Node.Key, resp.Node.Nodes)
147+
return k.GetContainers(containerIDs)
148+
}
149+
150+
func getContainerDeployData(prefix string, nodes etcdclient.Nodes) []string {
151+
result := []string{}
152+
for _, node := range nodes {
153+
if len(node.Nodes) > 0 {
154+
result = append(result, getContainerDeployData(node.Key, node.Nodes)...)
155+
} else {
156+
key := strings.TrimLeft(strings.TrimLeft(node.Key, prefix), "/")
157+
result = append(result, key)
158+
}
159+
}
160+
return result
161+
}

store/mock/store.go

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ func (m *MockStore) AddNode(name, endpoint, podname, ca, cert, key string, cpu i
8484
return nil, args.Error(1)
8585
}
8686

87+
func (m *MockStore) ListContainers(appname, entrypoint, nodename string) ([]*types.Container, error) {
88+
args := m.Called(appname, entrypoint, nodename)
89+
if args.Get(0) != nil {
90+
return args.Get(0).([]*types.Container), args.Error(1)
91+
}
92+
return nil, args.Error(1)
93+
}
94+
8795
func (m *MockStore) CleanContainerData(ID, appname, entrypoint, nodename string) error {
8896
args := m.Called(ID, appname, entrypoint, nodename)
8997
return args.Error(0)

store/store.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Store interface {
3232
GetContainers(ids []string) ([]*types.Container, error)
3333
ContainerDeployed(ID, appname, entrypoint, nodename, data string) error
3434
WatchDeployStatus(appname, entrypoint, nodename string) etcdclient.Watcher
35+
ListContainers(appname, entrypoint, nodename string) ([]*types.Container, error)
3536

3637
// distributed lock
3738
CreateLock(key string, ttl int) (lock.DistributedLock, error)

0 commit comments

Comments
 (0)