Compare commits

...

8 Commits

Author SHA1 Message Date
Laura Brehm 0829a101c1
Merge f632123c41 into a5fb752ecf 2024-09-18 11:18:33 +01:00
Sebastiaan van Stijn a5fb752ecf
Merge pull request #5445 from jsternberg/lowercase-windows-drive
command: change drive to lowercase for wsl path
2024-09-18 12:15:45 +02:00
Laura Brehm 4e64c59d64
Merge pull request #5446 from thaJeztah/codeql_updates
gha: update codeql workflow to go1.22.7
2024-09-18 11:04:32 +01:00
Jonathan A. Sternberg 3472bbc28a
command: change drive to lowercase for wsl path
On Windows, the drive casing doesn't matter outside of WSL. For WSL, the
drives are lowercase. When we're producing a WSL path, lowercase the
drive letter.

Co-authored-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
Co-authored-by: Laura Brehm <laurabrehm@hey.com>

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-09-18 10:59:08 +01:00
Laura Brehm 649e564ee0
Merge pull request #5444 from jsternberg/handle-otel-errors
telemetry: pass otel errors to the otel handler for shutdown and force flush
2024-09-18 10:39:55 +01:00
Sebastiaan van Stijn e1213edcc6
gha: update codeql workflow to go1.22.7
commit d7d56599ca updated this
repository to go1.22, but the codeql action didn't specify a
patch version, and was missed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-17 21:39:56 +02:00
Jonathan A. Sternberg b1956f5073
telemetry: pass otel errors to the otel handler for shutdown and force flush
Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
2024-09-17 10:47:04 -05:00
Laura Brehm f632123c41
WIP: add configurable detach delay to logs
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-07-30 15:01:19 +01:00
9 changed files with 145 additions and 18 deletions

View File

@ -67,7 +67,7 @@ jobs:
name: Update Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: 1.22.7
-
name: Initialize CodeQL
uses: github/codeql-action/init@v3

View File

@ -3,11 +3,14 @@ package container
import (
"context"
"io"
"strconv"
"time"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/pkg/stdcopy"
"github.com/spf13/cobra"
)
@ -20,6 +23,8 @@ type logsOptions struct {
details bool
tail string
detachDelay int
container string
}
@ -49,6 +54,7 @@ func NewLogsCommand(dockerCli command.Cli) *cobra.Command {
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")
flags.IntVarP(&opts.detachDelay, "delay", "d", 0, "Number of seconds to wait for container restart before exiting")
return cmd
}
@ -58,6 +64,45 @@ func runLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions) erro
return err
}
since := opts.since
for {
restarting := make(chan events.Message)
if opts.detachDelay != 0 {
go func() {
eventsCtx, eventsCtxCancel := context.WithCancel(ctx)
eventC, eventErrs := dockerCli.Client().Events(eventsCtx, events.ListOptions{})
defer eventsCtxCancel()
for {
select {
case event := <-eventC:
if event.Action == events.ActionRestart && event.Actor.ID == c.ID {
restarting <- event
return
}
case <-eventErrs:
return
}
}
}()
}
opts.since = since
err = streamLogs(ctx, dockerCli, opts, c)
if opts.detachDelay == 0 {
return err
}
select {
case restartEvent := <-restarting:
since = strconv.FormatInt(restartEvent.Time, 10)
case <-time.After(time.Duration(opts.detachDelay) * time.Second):
return err
}
}
}
func streamLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions, c container.InspectResponse) error {
responseBody, err := dockerCli.Client().ContainerLogs(ctx, c.ID, container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
@ -78,5 +123,6 @@ func runLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions) erro
} else {
_, err = stdcopy.StdCopy(dockerCli.Out(), dockerCli.Err(), responseBody)
}
return err
}

View File

@ -180,7 +180,7 @@ func toWslPath(s string) string {
if !ok {
return ""
}
return fmt.Sprintf("mnt/%s%s", drive, p)
return fmt.Sprintf("mnt/%s%s", strings.ToLower(drive), p)
}
func parseUNCPath(s string) (drive, p string, ok bool) {

View File

@ -1,6 +1,7 @@
package command
import (
"io/fs"
"net/url"
"testing"
"testing/fstest"
@ -9,21 +10,48 @@ import (
)
func TestWslSocketPath(t *testing.T) {
u, err := url.Parse("unix:////./c:/my/file/path")
assert.NilError(t, err)
testCases := []struct {
doc string
fs fs.FS
url string
expected string
}{
{
doc: "filesystem where WSL path does not exist",
fs: fstest.MapFS{
"my/file/path": {},
},
url: "unix:////./c:/my/file/path",
expected: "",
},
{
doc: "filesystem where WSL path exists",
fs: fstest.MapFS{
"mnt/c/my/file/path": {},
},
url: "unix:////./c:/my/file/path",
expected: "/mnt/c/my/file/path",
},
{
doc: "filesystem where WSL path exists uppercase URL",
fs: fstest.MapFS{
"mnt/c/my/file/path": {},
},
url: "unix:////./C:/my/file/path",
expected: "/mnt/c/my/file/path",
},
}
for _, tc := range testCases {
t.Run(tc.doc, func(t *testing.T) {
u, err := url.Parse(tc.url)
assert.NilError(t, err)
// Ensure host is empty.
assert.Equal(t, u.Host, "")
// Use a filesystem where the WSL path exists.
fs := fstest.MapFS{
"mnt/c/my/file/path": {},
}
assert.Equal(t, wslSocketPath(u.Path, fs), "/mnt/c/my/file/path")
result := wslSocketPath(u.Path, tc.fs)
// Use a filesystem where the WSL path doesn't exist.
fs = fstest.MapFS{
"my/file/path": {},
assert.Equal(t, result, tc.expected)
})
}
assert.Equal(t, wslSocketPath(u.Path, fs), "")
}

View File

@ -9,6 +9,7 @@ import (
"github.com/docker/cli/cli/version"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
@ -94,7 +95,9 @@ func startCobraCommandTimer(mp metric.MeterProvider, attrs []attribute.KeyValue)
metric.WithAttributes(cmdStatusAttrs...),
)
if mp, ok := mp.(MeterProvider); ok {
mp.ForceFlush(ctx)
if err := mp.ForceFlush(ctx); err != nil {
otel.Handle(err)
}
}
}
}

View File

@ -358,7 +358,9 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
mp := dockerCli.MeterProvider()
if mp, ok := mp.(command.MeterProvider); ok {
defer mp.Shutdown(ctx)
if err := mp.Shutdown(ctx); err != nil {
otel.Handle(err)
}
} else {
fmt.Fprint(dockerCli.Err(), "Warning: Unexpected OTEL error, metrics may not be flushed")
}

View File

@ -11,6 +11,7 @@ Fetch the logs of a container
| Name | Type | Default | Description |
|:---------------------|:---------|:--------|:---------------------------------------------------------------------------------------------------|
| `-d`, `--delay` | `int` | `0` | Number of seconds to wait for container restart before exiting |
| `--details` | `bool` | | Show extra details provided to logs |
| `-f`, `--follow` | `bool` | | Follow log output |
| `--since` | `string` | | Show logs since timestamp (e.g. `2013-01-02T13:23:37Z`) or relative (e.g. `42m` for 42 minutes) |

View File

@ -11,6 +11,7 @@ Fetch the logs of a container
| Name | Type | Default | Description |
|:---------------------|:---------|:--------|:---------------------------------------------------------------------------------------------------|
| `-d`, `--delay` | `int` | `0` | Number of seconds to wait for container restart before exiting |
| `--details` | `bool` | | Show extra details provided to logs |
| `-f`, `--follow` | `bool` | | Follow log output |
| `--since` | `string` | | Show logs since timestamp (e.g. `2013-01-02T13:23:37Z`) or relative (e.g. `42m` for 42 minutes) |

View File

@ -0,0 +1,46 @@
package container
import (
"strings"
"testing"
"time"
"github.com/docker/cli/e2e/internal/fixtures"
"gotest.tools/v3/icmd"
"gotest.tools/v3/poll"
)
func TestLogsReattach(t *testing.T) {
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage,
"sh", "-c", "echo hi; while true; do sleep 1; done")
result.Assert(t, icmd.Success)
containerID := strings.TrimSpace(result.Stdout())
cmd := icmd.Command("docker", "logs", "-f", "-d", "5", containerID)
// cmd := icmd.Command("docker", "logs", containerID)
result = icmd.StartCmd(cmd)
poll.WaitOn(t, func(t poll.LogT) poll.Result {
if strings.Contains(result.Stdout(), "hi") {
return poll.Success()
}
return poll.Continue("waiting")
}, poll.WithDelay(1*time.Second), poll.WithTimeout(5*time.Second))
icmd.RunCommand("docker", "restart", containerID).Assert(t, icmd.Success)
poll.WaitOn(t, func(t poll.LogT) poll.Result {
// if there is another "hi" then the container was successfully restarted,
// printed "hi" again and `docker logs` stayed attached
if strings.Contains(result.Stdout(), "hi\nhi") { //nolint:dupword
return poll.Success()
}
return poll.Continue(result.Stdout())
}, poll.WithDelay(1*time.Second), poll.WithTimeout(10*time.Second))
icmd.RunCommand("docker", "stop", containerID).Assert(t, icmd.Success)
icmd.WaitOnCmd(time.Second*10, result).Assert(t, icmd.Expected{
ExitCode: 0,
})
}