Skip to content

Commit

Permalink
feat: enables container filter to be configured at multiple places (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 authored Dec 14, 2024
2 parents a62cef7 + 9f2d172 commit d3e573d
Show file tree
Hide file tree
Showing 30 changed files with 508 additions and 401 deletions.
13 changes: 11 additions & 2 deletions internal/agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,17 @@ func (c *Client) FindContainer(ctx context.Context, containerID string) (docker.
}, nil
}

func (c *Client) ListContainers(ctx context.Context) ([]docker.Container, error) {
response, err := c.client.ListContainers(ctx, &pb.ListContainersRequest{})
func (c *Client) ListContainers(ctx context.Context, filter docker.ContainerFilter) ([]docker.Container, error) {
in := &pb.ListContainersRequest{}

if filter != nil {
in.Filter = make(map[string]*pb.RepeatedString)
for k, v := range filter {
in.Filter[k] = &pb.RepeatedString{Values: v}
}
}

response, err := c.client.ListContainers(ctx, in)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions internal/agent/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (m *MockedClient) ContainerEvents(ctx context.Context, events chan<- docker
return args.Error(0)
}

func (m *MockedClient) ListContainers(ctx context.Context) ([]docker.Container, error) {
args := m.Called(ctx)
func (m *MockedClient) ListContainers(ctx context.Context, filter docker.ContainerFilter) ([]docker.Container, error) {
args := m.Called(ctx, filter)
return args.Get(0).([]docker.Container), args.Error(1)
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func init() {
}

client = &MockedClient{}
client.On("ListContainers", mock.Anything).Return([]docker.Container{
client.On("ListContainers", mock.Anything, mock.Anything).Return([]docker.Container{
{
ID: "123456",
Name: "test",
Expand Down Expand Up @@ -127,7 +127,7 @@ func init() {
Stats: utils.NewRingBuffer[docker.ContainerStat](300),
}, nil)

server, _ := NewServer(client, certs, "test")
server, _ := NewServer(client, certs, "test", docker.ContainerFilter{})

go server.Serve(lis)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestListContainers(t *testing.T) {
t.Fatal(err)
}

containers, _ := rpc.ListContainers(context.Background())
containers, _ := rpc.ListContainers(context.Background(), docker.ContainerFilter{})

assert.Equal(t, containers, []docker.Container{
{
Expand Down
Loading

0 comments on commit d3e573d

Please sign in to comment.