Skip to content

Commit 9ea86dd

Browse files
authored
Merge pull request moby#3544 from gabriel-samfira/fix-tracing-listener
[Windows] Fix tracing listener
2 parents 75123c6 + dc8d71e commit 9ea86dd

File tree

6 files changed

+94
-15
lines changed

6 files changed

+94
-15
lines changed

cmd/buildkitd/main.go

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

632632
var traceSocket string
633633
if tc != nil {
634-
traceSocket = filepath.Join(cfg.Root, "otel-grpc.sock")
634+
traceSocket = traceSocketPath(cfg.Root)
635635
if err := runTraceController(traceSocket, tc); err != nil {
636636
return nil, err
637637
}
@@ -814,14 +814,9 @@ func parseBoolOrAuto(s string) (*bool, error) {
814814
func runTraceController(p string, exp sdktrace.SpanExporter) error {
815815
server := grpc.NewServer()
816816
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})
817-
uid := os.Getuid()
818-
l, err := sys.GetLocalListener(p, uid, uid)
817+
l, err := getLocalListener(p)
819818
if err != nil {
820-
return err
821-
}
822-
if err := os.Chmod(p, 0666); err != nil {
823-
l.Close()
824-
return err
819+
return errors.Wrap(err, "creating trace controller listener")
825820
}
826821
go server.Serve(l)
827822
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,11 +7,37 @@ import (
77
"crypto/tls"
88
"net"
99

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

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

executor/oci/spec.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ const (
3535
NoProcessSandbox
3636
)
3737

38+
var tracingEnvVars = []string{
39+
"OTEL_TRACES_EXPORTER=otlp",
40+
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=" + getTracingSocket(),
41+
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc",
42+
}
43+
3844
func (pm ProcessMode) String() string {
3945
switch pm {
4046
case ProcessSandbox:
@@ -112,7 +118,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
112118

113119
if tracingSocket != "" {
114120
// 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")
121+
meta.Env = append(meta.Env, tracingEnvVars...)
116122
meta.Env = append(meta.Env, traceexec.Environ(ctx)...)
117123
}
118124

@@ -183,12 +189,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
183189
}
184190

185191
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-
})
192+
s.Mounts = append(s.Mounts, getTracingSocketMount(tracingSocket))
192193
}
193194

194195
s.Mounts = dedupMounts(s.Mounts)

executor/oci/spec_unix.go

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

24+
const (
25+
tracingSocketPath = "/dev/otel-grpc.sock"
26+
)
27+
2428
func generateMountOpts(resolvConf, hostsFile string) ([]oci.SpecOpts, error) {
2529
return []oci.SpecOpts{
2630
// https://github.com/moby/buildkit/issues/429
@@ -122,3 +126,16 @@ func withDefaultProfile() oci.SpecOpts {
122126
return err
123127
}
124128
}
129+
130+
func getTracingSocketMount(socket string) specs.Mount {
131+
return specs.Mount{
132+
Destination: tracingSocketPath,
133+
Type: "bind",
134+
Source: socket,
135+
Options: []string{"ro", "rbind"},
136+
}
137+
}
138+
139+
func getTracingSocket() string {
140+
return fmt.Sprintf("unix://%s", tracingSocketPath)
141+
}

executor/oci/spec_windows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
package oci
55

66
import (
7+
"fmt"
8+
"path/filepath"
9+
710
"github.com/containerd/containerd/oci"
811
"github.com/docker/docker/pkg/idtools"
912
"github.com/moby/buildkit/solver/pb"
13+
specs "github.com/opencontainers/runtime-spec/specs-go"
1014
"github.com/pkg/errors"
1115
)
1216

17+
const (
18+
tracingSocketPath = "//./pipe/otel-grpc"
19+
)
20+
1321
func generateMountOpts(resolvConf, hostsFile string) ([]oci.SpecOpts, error) {
1422
return nil, nil
1523
}
@@ -43,3 +51,15 @@ func generateRlimitOpts(ulimits []*pb.Ulimit) ([]oci.SpecOpts, error) {
4351
}
4452
return nil, errors.New("no support for POSIXRlimit on Windows")
4553
}
54+
55+
func getTracingSocketMount(socket string) specs.Mount {
56+
return specs.Mount{
57+
Destination: filepath.FromSlash(tracingSocketPath),
58+
Source: socket,
59+
Options: []string{"ro"},
60+
}
61+
}
62+
63+
func getTracingSocket() string {
64+
return fmt.Sprintf("npipe://%s", filepath.ToSlash(tracingSocketPath))
65+
}

0 commit comments

Comments
 (0)