Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logs: filter to services from current Compose file #9811

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ func logsCommand(p *projectOptions, backend api.Service) *cobra.Command {
}

func runLogs(ctx context.Context, backend api.Service, opts logsOptions, services []string) error {
projectName, err := opts.toProjectName()
project, name, err := opts.projectOrName()
if err != nil {
return err
}
consumer := formatter.NewLogConsumer(ctx, os.Stdout, !opts.noColor, !opts.noPrefix)
return backend.Logs(ctx, projectName, consumer, api.LogOptions{
return backend.Logs(ctx, name, consumer, api.LogOptions{
Project: project,
Services: services,
Follow: opts.follow,
Tail: opts.tail,
Expand Down
9 changes: 7 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ type ServiceStatus struct {

// LogOptions defines optional parameters for the `Log` API
type LogOptions struct {
Project *types.Project
Services []string
Tail string
Since string
Expand Down Expand Up @@ -429,7 +430,7 @@ type Stack struct {

// LogConsumer is a callback to process log messages from services
type LogConsumer interface {
Log(service, container, message string)
Log(containerName, service, message string)
Status(container, msg string)
Register(container string)
}
Expand All @@ -439,7 +440,11 @@ type ContainerEventListener func(event ContainerEvent)

// ContainerEvent notify an event has been collected on source container implementing Service
type ContainerEvent struct {
Type int
Type int
// Container is the name of the container _without the project prefix_.
//
// This is only suitable for display purposes within Compose, as it's
// not guaranteed to be unique across services.
Container string
Service string
Line string
Expand Down
29 changes: 21 additions & 8 deletions pkg/compose/convergence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"testing"

"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/mocks"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/golang/mock/gomock"
"gotest.tools/assert"

"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/mocks"
)

func TestContainerName(t *testing.T) {
Expand Down Expand Up @@ -77,7 +78,9 @@ func TestServiceLinks(t *testing.T) {

apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

s.Links = []string{"db"}
Expand All @@ -99,7 +102,9 @@ func TestServiceLinks(t *testing.T) {
defer mockCtrl.Finish()
apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

s.Links = []string{"db:db"}
Expand All @@ -121,7 +126,9 @@ func TestServiceLinks(t *testing.T) {
defer mockCtrl.Finish()
apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

s.Links = []string{"db:dbname"}
Expand All @@ -143,7 +150,9 @@ func TestServiceLinks(t *testing.T) {
defer mockCtrl.Finish()
apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

s.Links = []string{"db:dbname"}
Expand All @@ -169,7 +178,9 @@ func TestServiceLinks(t *testing.T) {
defer mockCtrl.Finish()
apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

s.Links = []string{}
Expand Down Expand Up @@ -203,7 +214,9 @@ func TestWaitDependencies(t *testing.T) {

apiClient := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(apiClient).AnyTimes()

t.Run("should skip dependencies with scale 0", func(t *testing.T) {
Expand Down
24 changes: 18 additions & 6 deletions pkg/compose/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func TestDown(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
Expand Down Expand Up @@ -85,7 +87,9 @@ func TestDownRemoveOrphans(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(true)).Return(
Expand Down Expand Up @@ -122,7 +126,9 @@ func TestDownRemoveVolumes(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
Expand All @@ -149,7 +155,9 @@ func TestDownRemoveImageLocal(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

container := testContainer("service1", "123", false)
Expand Down Expand Up @@ -180,7 +188,9 @@ func TestDownRemoveImageLocalNoLabel(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

container := testContainer("service1", "123", false)
Expand Down Expand Up @@ -208,7 +218,9 @@ func TestDownRemoveImageAll(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt(false)).Return(
Expand Down
16 changes: 11 additions & 5 deletions pkg/compose/kill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ import (

const testProject = "testProject"

var tested = composeService{}

func TestKillAll(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

name := strings.ToLower(testProject)
Expand Down Expand Up @@ -74,7 +74,9 @@ func TestKillSignal(t *testing.T) {

api := mocks.NewMockAPIClient(mockCtrl)
cli := mocks.NewMockCli(mockCtrl)
tested.dockerCli = cli
tested := composeService{
dockerCli: cli,
}
cli.EXPECT().Client().Return(api).AnyTimes()

name := strings.ToLower(testProject)
Expand All @@ -97,9 +99,13 @@ func TestKillSignal(t *testing.T) {
}

func testContainer(service string, id string, oneOff bool) moby.Container {
// canonical docker names in the API start with a leading slash, some
// parts of Compose code will attempt to strip this off, so make sure
// it's consistently present
name := "/" + strings.TrimPrefix(id, "/")
return moby.Container{
ID: id,
Names: []string{id},
Names: []string{name},
Labels: containerLabels(service, oneOff),
}
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,32 @@ import (
"github.com/docker/compose/v2/pkg/utils"
)

func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
func (s *composeService) Logs(
ctx context.Context,
projectName string,
consumer api.LogConsumer,
options api.LogOptions,
) error {
projectName = strings.ToLower(projectName)

containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
if err != nil {
return err
}

project := options.Project
if project == nil {
project, err = s.getProjectWithResources(ctx, containers, projectName)
if err != nil {
return err
}
}

if len(options.Services) == 0 {
options.Services = project.ServiceNames()
}

containers = containers.filter(isService(options.Services...))
eg, ctx := errgroup.WithContext(ctx)
for _, c := range containers {
c := c
Expand Down
Loading