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

don't fail logs when driver:none is set #10107

Merged
merged 1 commit into from Dec 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions pkg/compose/logs.go
Expand Up @@ -20,9 +20,12 @@ import (
"context"
"io"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/stdcopy"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -59,7 +62,12 @@ func (s *composeService) Logs(
for _, c := range containers {
c := c
eg.Go(func() error {
return s.logContainers(ctx, consumer, c, options)
err := s.logContainers(ctx, consumer, c, options)
if _, ok := err.(errdefs.ErrNotImplemented); ok {
logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(c), err.Error())
return nil
}
return err
})
}

Expand All @@ -79,13 +87,24 @@ func (s *composeService) Logs(
}

eg.Go(func() error {
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container, t time.Time) error {
printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach,
Container: getContainerNameWithoutProject(c),
Service: c.Labels[api.ServiceLabel],
})
return s.logContainers(ctx, consumer, c, options)
err := s.logContainers(ctx, consumer, c, api.LogOptions{
Follow: options.Follow,
Since: t.Format(time.RFC3339Nano),
Until: options.Until,
Tail: options.Tail,
Timestamps: options.Timestamps,
})
if _, ok := err.(errdefs.ErrNotImplemented); ok {
// ignore
return nil
}
return err
})
printer.Stop()
return err
Expand Down
25 changes: 8 additions & 17 deletions pkg/compose/start.go
Expand Up @@ -19,6 +19,7 @@ package compose
import (
"context"
"strings"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/utils"
Expand Down Expand Up @@ -59,9 +60,10 @@ func (s *composeService) start(ctx context.Context, projectName string, options
}

eg.Go(func() error {
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached, func(container moby.Container) error {
return s.attachContainer(ctx, container, listener)
})
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached,
func(container moby.Container, _ time.Time) error {
return s.attachContainer(ctx, container, listener)
})
})
}

Expand Down Expand Up @@ -107,7 +109,7 @@ func getDependencyCondition(service types.ServiceConfig, project *types.Project)
return ServiceConditionRunningOrHealthy
}

type containerWatchFn func(container moby.Container) error
type containerWatchFn func(container moby.Container, t time.Time) error

// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
Expand Down Expand Up @@ -167,7 +169,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
restarted := watched[container.ID]
watched[container.ID] = restarted + 1
// Container terminated.
willRestart := willContainerRestart(inspected, restarted)
willRestart := inspected.State.Restarting

listener(api.ContainerEvent{
Type: api.ContainerEventExit,
Expand All @@ -193,7 +195,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
}
if mustAttach {
// Container restarted, need to re-attach
err := onStart(container)
err := onStart(container, event.Timestamp)
if err != nil {
return err
}
Expand All @@ -210,14 +212,3 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
}
return err
}

func willContainerRestart(container moby.ContainerJSON, restarted int) bool {
policy := container.HostConfig.RestartPolicy
if policy.IsAlways() || policy.IsUnlessStopped() {
return true
}
if policy.IsOnFailure() {
return container.State.ExitCode != 0 && policy.MaximumRetryCount > restarted
}
return false
}
5 changes: 5 additions & 0 deletions pkg/e2e/fixtures/logs-test/restart.yaml
@@ -0,0 +1,5 @@
services:
ping:
image: alpine
command: "sh -c 'ping -c 1 localhost && exit 1'"
restart: "on-failure:2"
19 changes: 19 additions & 0 deletions pkg/e2e/logs_test.go
Expand Up @@ -56,3 +56,22 @@ func TestLocalComposeLogs(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
}

func TestLocalComposeLogsFollow(t *testing.T) {
c := NewParallelCLI(t)

const projectName = "compose-e2e-logs-restart"

t.Run("up", func(t *testing.T) {
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/restart.yaml", "--project-name", projectName, "up", "-d")
})

t.Run("logs", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "logs", "--follow")
assert.Check(t, strings.Count(res.Combined(), "PING localhost (127.0.0.1)") == 2)
})

t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
}