detect new container from project have started when running `compose logs` with no explicit services

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-05-25 17:02:08 +02:00
parent 6f6e1635fd
commit 01d91c490c
3 changed files with 29 additions and 8 deletions

View File

@ -25,7 +25,6 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/utils"
)
@ -67,8 +66,8 @@ func (s *composeService) Events(ctx context.Context, projectName string, options
err := options.Consumer(api.Event{
Timestamp: timestamp,
Service: service,
Container: event.ID,
Status: event.Status,
Container: event.Actor.ID,
Status: event.Action,
Attributes: attributes,
})
if err != nil {

View File

@ -51,13 +51,12 @@ func (s *composeService) Logs(
if err != nil {
return err
}
}
if len(options.Services) == 0 {
} else if len(options.Services) == 0 {
// we run with an explicit compose.yaml, so only consider services defined in this file
options.Services = project.ServiceNames()
containers = containers.filter(isService(options.Services...))
}
containers = containers.filter(isService(options.Services...))
eg, ctx := errgroup.WithContext(ctx)
for _, c := range containers {
c := c

View File

@ -155,13 +155,31 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
required = services
}
// predicate to tell if a container we receive event for should be considered or ignored
ofInterest := func(c moby.Container) bool {
if len(services) > 0 {
// we only watch some services
return utils.Contains(services, c.Labels[api.ServiceLabel])
}
return true
}
// predicate to tell if a container we receive event for should be watched until termination
isRequired := func(c moby.Container) bool {
if len(services) > 0 && len(required) > 0 {
// we only watch some services
return utils.Contains(required, c.Labels[api.ServiceLabel])
}
return true
}
var (
expected []string
watched = map[string]int{}
replaced []string
)
for _, c := range containers {
if utils.Contains(required, c.Labels[api.ServiceLabel]) {
if isRequired(c) {
expected = append(expected, c.ID)
}
watched[c.ID] = 0
@ -265,6 +283,11 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
if utils.Contains(expected, id) {
expected = append(expected, container.ID)
}
} else if ofInterest(container) {
watched[container.ID] = 1
if isRequired(container) {
expected = append(expected, container.ID)
}
}
}
if len(expected) == 0 {