From 59cb959195d8badd2298a06bee1d3cf321168e88 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 13 Sep 2024 07:42:40 -0700 Subject: [PATCH] [v0.17] update docker/cli to 48a2cdff97 Brings in fix for telemetry under WSL2 Signed-off-by: Tonis Tiigi --- go.mod | 2 +- go.sum | 4 +- .../cli/cli/command/telemetry_docker.go | 120 ++++++++++++++++-- vendor/modules.txt | 2 +- 4 files changed, 116 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 24d06871bf9..56cac50f90e 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/containerd/typeurl/v2 v2.2.0 github.com/creack/pty v1.1.21 github.com/distribution/reference v0.6.0 - github.com/docker/cli v27.2.1+incompatible + github.com/docker/cli v27.2.2-0.20240913085431-48a2cdff970d+incompatible github.com/docker/cli-docs-tool v0.8.0 github.com/docker/docker v27.2.1+incompatible github.com/docker/go-units v0.5.0 diff --git a/go.sum b/go.sum index ba0c1181ea1..d13655cd486 100644 --- a/go.sum +++ b/go.sum @@ -124,8 +124,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= -github.com/docker/cli v27.2.1+incompatible h1:U5BPtiD0viUzjGAjV1p0MGB8eVA3L3cbIrnyWmSJI70= -github.com/docker/cli v27.2.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= +github.com/docker/cli v27.2.2-0.20240913085431-48a2cdff970d+incompatible h1:jbTJwvM0LKQUti9X1KfPCnRasE5894IGaqs5Y90YeXM= +github.com/docker/cli v27.2.2-0.20240913085431-48a2cdff970d+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= github.com/docker/cli-docs-tool v0.8.0 h1:YcDWl7rQJC3lJ7WVZRwSs3bc9nka97QLWfyJQli8yJU= github.com/docker/cli-docs-tool v0.8.0/go.mod h1:8TQQ3E7mOXoYUs811LiPdUnAhXrcVsBIrW21a5pUbdk= github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= diff --git a/vendor/github.com/docker/cli/cli/command/telemetry_docker.go b/vendor/github.com/docker/cli/cli/command/telemetry_docker.go index 94ab3a3929d..8c50dc3ef68 100644 --- a/vendor/github.com/docker/cli/cli/command/telemetry_docker.go +++ b/vendor/github.com/docker/cli/cli/command/telemetry_docker.go @@ -5,9 +5,14 @@ package command import ( "context" + "fmt" + "io/fs" "net/url" "os" "path" + "path/filepath" + "strings" + "unicode" "github.com/pkg/errors" "go.opentelemetry.io/otel" @@ -77,14 +82,7 @@ func dockerExporterOTLPEndpoint(cli Cli) (endpoint string, secure bool) { switch u.Scheme { case "unix": - // Unix sockets are a bit weird. OTEL seems to imply they - // can be used as an environment variable and are handled properly, - // but they don't seem to be as the behavior of the environment variable - // is to strip the scheme from the endpoint, but the underlying implementation - // needs the scheme to use the correct resolver. - // - // We'll just handle this in a special way and add the unix:// back to the endpoint. - endpoint = "unix://" + path.Join(u.Host, u.Path) + endpoint = unixSocketEndpoint(u) case "https": secure = true fallthrough @@ -135,3 +133,109 @@ func dockerMetricExporter(ctx context.Context, cli Cli) []sdkmetric.Option { } return []sdkmetric.Option{sdkmetric.WithReader(newCLIReader(exp))} } + +// unixSocketEndpoint converts the unix scheme from URL to +// an OTEL endpoint that can be used with the OTLP exporter. +// +// The OTLP exporter handles unix sockets in a strange way. +// It seems to imply they can be used as an environment variable +// and are handled properly, but they don't seem to be as the behavior +// of the environment variable is to strip the scheme from the endpoint +// while the underlying implementation needs the scheme to use the +// correct resolver. +func unixSocketEndpoint(u *url.URL) string { + // GRPC does not allow host to be used. + socketPath := u.Path + + // If we are on windows and we have an absolute path + // that references a letter drive, check to see if the + // WSL equivalent path exists and we should use that instead. + if isWsl() { + if p := wslSocketPath(socketPath, os.DirFS("/")); p != "" { + socketPath = p + } + } + // Enforce that we are using forward slashes. + return "unix://" + filepath.ToSlash(socketPath) +} + +// wslSocketPath will convert the referenced URL to a WSL-compatible +// path and check if that path exists. If the path exists, it will +// be returned. +func wslSocketPath(s string, f fs.FS) string { + if p := toWslPath(s); p != "" { + if _, err := stat(p, f); err == nil { + return "/" + p + } + } + return "" +} + +// toWslPath converts the referenced URL to a WSL-compatible +// path if this looks like a Windows absolute path. +// +// If no drive is in the URL, defaults to the C drive. +func toWslPath(s string) string { + drive, p, ok := parseUNCPath(s) + if !ok { + return "" + } + return fmt.Sprintf("mnt/%s%s", drive, p) +} + +func parseUNCPath(s string) (drive, p string, ok bool) { + // UNC paths use backslashes but we're using forward slashes + // so also enforce that here. + // + // In reality, this should have been enforced much earlier + // than here since backslashes aren't allowed in URLs, but + // we're going to code defensively here. + s = filepath.ToSlash(s) + + const uncPrefix = "//./" + if !strings.HasPrefix(s, uncPrefix) { + // Not a UNC path. + return "", "", false + } + s = s[len(uncPrefix):] + + parts := strings.SplitN(s, "/", 2) + if len(parts) != 2 { + // Not enough components. + return "", "", false + } + + drive, ok = splitWindowsDrive(parts[0]) + if !ok { + // Not a windows drive. + return "", "", false + } + return drive, "/" + parts[1], true +} + +// splitWindowsDrive checks if the string references a windows +// drive (such as c:) and returns the drive letter if it is. +func splitWindowsDrive(s string) (string, bool) { + if b := []rune(s); len(b) == 2 && unicode.IsLetter(b[0]) && b[1] == ':' { + return string(b[0]), true + } + return "", false +} + +func stat(p string, f fs.FS) (fs.FileInfo, error) { + if f, ok := f.(fs.StatFS); ok { + return f.Stat(p) + } + + file, err := f.Open(p) + if err != nil { + return nil, err + } + + defer file.Close() + return file.Stat() +} + +func isWsl() bool { + return os.Getenv("WSL_DISTRO_NAME") != "" +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 5a6508133f4..f653f6e37da 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -218,7 +218,7 @@ github.com/davecgh/go-spew/spew # github.com/distribution/reference v0.6.0 ## explicit; go 1.20 github.com/distribution/reference -# github.com/docker/cli v27.2.1+incompatible +# github.com/docker/cli v27.2.2-0.20240913085431-48a2cdff970d+incompatible ## explicit github.com/docker/cli/cli github.com/docker/cli/cli-plugins/hooks