Skip to content

Commit 7d6bee2

Browse files
Fix tracing listener on Windows
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent fd0c25c commit 7d6bee2

File tree

6 files changed

+77
-15
lines changed

6 files changed

+77
-15
lines changed

cmd/buildkitd/main.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func newController(c *cli.Context, cfg *config.Config) (*control.Controller, err
630630

631631
var traceSocket string
632632
if tc != nil {
633-
traceSocket = filepath.Join(cfg.Root, "otel-grpc.sock")
633+
traceSocket = traceSocketPath(cfg.Root)
634634
if err := runTraceController(traceSocket, tc); err != nil {
635635
return nil, err
636636
}
@@ -813,14 +813,9 @@ func parseBoolOrAuto(s string) (*bool, error) {
813813
func runTraceController(p string, exp sdktrace.SpanExporter) error {
814814
server := grpc.NewServer()
815815
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})
816-
uid := os.Getuid()
817-
l, err := sys.GetLocalListener(p, uid, uid)
816+
l, err := getLocalListener(p)
818817
if err != nil {
819-
return err
820-
}
821-
if err := os.Chmod(p, 0666); err != nil {
822-
l.Close()
823-
return err
818+
return errors.Wrap(err, "creating trace controller listener")
824819
}
825820
go server.Serve(l)
826821
return nil

cmd/buildkitd/main_unix.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ package main
66
import (
77
"crypto/tls"
88
"net"
9+
"os"
10+
"path/filepath"
911
"syscall"
1012

13+
"github.com/containerd/containerd/sys"
1114
"github.com/coreos/go-systemd/v22/activation"
1215
"github.com/pkg/errors"
1316
)
@@ -43,3 +46,20 @@ func listenFD(addr string, tlsConfig *tls.Config) (net.Listener, error) {
4346
//TODO: systemd fd selection (default is 3)
4447
return nil, errors.New("not supported yet")
4548
}
49+
50+
func traceSocketPath(root string) string {
51+
return filepath.Join(root, "otel-grpc.sock")
52+
}
53+
54+
func getLocalListener(listenerPath string) (net.Listener, error) {
55+
uid := os.Getuid()
56+
l, err := sys.GetLocalListener(listenerPath, uid, uid)
57+
if err != nil {
58+
return nil, err
59+
}
60+
if err := os.Chmod(listenerPath, 0666); err != nil {
61+
l.Close()
62+
return nil, err
63+
}
64+
return l, nil
65+
}

cmd/buildkitd/main_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,36 @@ import (
77
"crypto/tls"
88
"net"
99

10+
"github.com/Microsoft/go-winio"
1011
_ "github.com/moby/buildkit/solver/llbsolver/ops"
1112
"github.com/pkg/errors"
1213
)
1314

15+
const (
16+
defaultTraceSocketPath = `\\.\pipe\otel-grpc`
17+
)
18+
1419
func listenFD(addr string, tlsConfig *tls.Config) (net.Listener, error) {
1520
return nil, errors.New("listening server on fd not supported on windows")
1621
}
22+
23+
func traceSocketPath(root string) string {
24+
return defaultTraceSocketPath
25+
}
26+
27+
func getLocalListener(listenerPath string) (net.Listener, error) {
28+
pc := &winio.PipeConfig{
29+
// Allow generic read and generic write access to authenticated users
30+
// and system users. On Linux, this pipe seems to be given rw access to
31+
// user, group and others (666).
32+
// TODO(gabriel-samfira): should we restrict access to this pipe to just
33+
// authenticated users? Or Administrators group?
34+
SecurityDescriptor: "D:P(A;;GRGW;;;AU)(A;;GRGW;;;SY)",
35+
}
36+
37+
listener, err := winio.ListenPipe(listenerPath, pc)
38+
if err != nil {
39+
return nil, errors.Wrap(err, "creating listener")
40+
}
41+
return listener, nil
42+
}

executor/oci/spec.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"path"
66
"path/filepath"
7+
"runtime"
78
"strings"
89
"sync"
910

@@ -112,7 +113,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
112113

113114
if tracingSocket != "" {
114115
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
115-
meta.Env = append(meta.Env, "OTEL_TRACES_EXPORTER=otlp", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=unix:///dev/otel-grpc.sock", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc")
116+
meta.Env = append(meta.Env, tracingEnvVars...)
116117
meta.Env = append(meta.Env, traceexec.Environ(ctx)...)
117118
}
118119

@@ -183,12 +184,20 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
183184
}
184185

185186
if tracingSocket != "" {
186-
s.Mounts = append(s.Mounts, specs.Mount{
187-
Destination: "/dev/otel-grpc.sock",
188-
Type: "bind",
189-
Source: tracingSocket,
190-
Options: []string{"ro", "rbind"},
191-
})
187+
if runtime.GOOS == "windows" {
188+
s.Mounts = append(s.Mounts, specs.Mount{
189+
Destination: `\\.\pipe\otel-grpc`,
190+
Source: tracingSocket,
191+
Options: []string{"ro"},
192+
})
193+
} else {
194+
s.Mounts = append(s.Mounts, specs.Mount{
195+
Destination: "/dev/otel-grpc.sock",
196+
Type: "bind",
197+
Source: tracingSocket,
198+
Options: []string{"ro", "rbind"},
199+
})
200+
}
192201
}
193202

194203
s.Mounts = dedupMounts(s.Mounts)

executor/oci/spec_unix.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import (
2121
"github.com/pkg/errors"
2222
)
2323

24+
var tracingEnvVars = []string{
25+
"OTEL_TRACES_EXPORTER=otlp",
26+
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=unix:///dev/otel-grpc.sock",
27+
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc",
28+
}
29+
2430
func generateMountOpts(resolvConf, hostsFile string) ([]oci.SpecOpts, error) {
2531
return []oci.SpecOpts{
2632
// https://github.com/moby/buildkit/issues/429

executor/oci/spec_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"github.com/pkg/errors"
1111
)
1212

13+
var tracingEnvVars = []string{
14+
"OTEL_TRACES_EXPORTER=otlp",
15+
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=npipe:////./pipe/otel-grpc",
16+
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc",
17+
}
18+
1319
func generateMountOpts(resolvConf, hostsFile string) ([]oci.SpecOpts, error) {
1420
return nil, nil
1521
}

0 commit comments

Comments
 (0)