-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.go
950 lines (848 loc) · 25.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"os"
"os/user"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // SA1019 deprecated
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes/docker"
"github.com/containerd/containerd/sys"
sddaemon "github.com/coreos/go-systemd/v22/daemon"
"github.com/docker/docker/pkg/reexec"
"github.com/gofrs/flock"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/moby/buildkit/cache/remotecache"
"github.com/moby/buildkit/cache/remotecache/azblob"
"github.com/moby/buildkit/cache/remotecache/gha"
inlineremotecache "github.com/moby/buildkit/cache/remotecache/inline"
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
registryremotecache "github.com/moby/buildkit/cache/remotecache/registry"
s3remotecache "github.com/moby/buildkit/cache/remotecache/s3"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/control"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/frontend"
dockerfile "github.com/moby/buildkit/frontend/dockerfile/builder"
"github.com/moby/buildkit/frontend/gateway"
"github.com/moby/buildkit/frontend/gateway/forwarder"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/archutil"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/profiler"
"github.com/moby/buildkit/util/resolver"
"github.com/moby/buildkit/util/stack"
"github.com/moby/buildkit/util/tracing/detect"
_ "github.com/moby/buildkit/util/tracing/detect/jaeger"
_ "github.com/moby/buildkit/util/tracing/env"
"github.com/moby/buildkit/util/tracing/transform"
"github.com/moby/buildkit/version"
"github.com/moby/buildkit/worker"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.etcd.io/bbolt"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
tracev1 "go.opentelemetry.io/proto/otlp/collector/trace/v1"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthv1 "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
func init() {
apicaps.ExportedProduct = "buildkit"
stack.SetVersionInfo(version.Version, version.Revision)
//nolint:staticcheck // SA1019 deprecated
seed.WithTimeAndRand()
if reexec.Init() {
os.Exit(0)
}
// enable in memory recording for buildkitd traces
detect.Recorder = detect.NewTraceRecorder()
}
var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
type workerInitializerOpt struct {
config *config.Config
sessionManager *session.Manager
traceSocket string
}
type workerInitializer struct {
fn func(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error)
// less priority number, more preferred
priority int
}
var (
appFlags []cli.Flag
workerInitializers []workerInitializer
)
func registerWorkerInitializer(wi workerInitializer, flags ...cli.Flag) {
workerInitializers = append(workerInitializers, wi)
sort.Slice(workerInitializers,
func(i, j int) bool {
return workerInitializers[i].priority < workerInitializers[j].priority
})
appFlags = append(appFlags, flags...)
}
func main() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision)
}
app := cli.NewApp()
app.Name = "buildkitd"
app.Usage = "build daemon"
app.Version = version.Version
defaultConf, err := defaultConf()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
rootlessUsage := "set all the default options to be compatible with rootless containers"
if userns.RunningInUserNS() {
app.Flags = append(app.Flags, cli.BoolTFlag{
Name: "rootless",
Usage: rootlessUsage + " (default: true)",
})
} else {
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "rootless",
Usage: rootlessUsage,
})
}
groupValue := func(gid *int) string {
if gid == nil {
return ""
}
return strconv.Itoa(*gid)
}
app.Flags = append(app.Flags,
cli.StringFlag{
Name: "config",
Usage: "path to config file",
Value: defaultConfigPath(),
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in logs",
},
cli.BoolFlag{
Name: "trace",
Usage: "enable trace output in logs (highly verbose, could affect performance)",
},
cli.StringFlag{
Name: "root",
Usage: "path to state directory",
Value: defaultConf.Root,
},
cli.StringSliceFlag{
Name: "addr",
Usage: "listening address (socket or tcp)",
Value: &cli.StringSlice{defaultConf.GRPC.Address[0]},
},
// Add format flag to control log formatter
cli.StringFlag{
Name: "log-format",
Usage: "log formatter: json or text",
Value: "text",
},
cli.StringFlag{
Name: "group",
Usage: "group (name or gid) which will own all Unix socket listening addresses",
Value: groupValue(defaultConf.GRPC.GID),
},
cli.StringFlag{
Name: "debugaddr",
Usage: "debugging address (eg. 0.0.0.0:6060)",
Value: defaultConf.GRPC.DebugAddress,
},
cli.StringFlag{
Name: "tlscert",
Usage: "certificate file to use",
Value: defaultConf.GRPC.TLS.Cert,
},
cli.StringFlag{
Name: "tlskey",
Usage: "key file to use",
Value: defaultConf.GRPC.TLS.Key,
},
cli.StringFlag{
Name: "tlscacert",
Usage: "ca certificate to verify clients",
Value: defaultConf.GRPC.TLS.CA,
},
cli.StringSliceFlag{
Name: "allow-insecure-entitlement",
Usage: "allows insecure entitlements e.g. network.host, security.insecure",
},
cli.StringFlag{
Name: "otel-socket-path",
Usage: "OTEL collector trace socket path",
},
)
app.Flags = append(app.Flags, appFlags...)
app.Flags = append(app.Flags, serviceFlags()...)
app.Action = func(c *cli.Context) error {
// TODO: On Windows this always returns -1. The actual "are you admin" check is very Windows-specific.
// See https://github.com/golang/go/issues/28804#issuecomment-505326268 for the "short" version.
if os.Geteuid() > 0 {
return errors.New("rootless mode requires to be executed as the mapped root in a user namespace; you may use RootlessKit for setting up the namespace")
}
ctx, cancel := context.WithCancelCause(appcontext.Context())
defer cancel(errors.WithStack(context.Canceled))
cfg, err := config.LoadFile(c.GlobalString("config"))
if err != nil {
return err
}
setDefaultConfig(&cfg)
if err := applyMainFlags(c, &cfg); err != nil {
return err
}
logFormat := cfg.Log.Format
switch logFormat {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
case "text", "":
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
default:
return errors.Errorf("unsupported log type %q", logFormat)
}
if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if cfg.Trace {
logrus.SetLevel(logrus.TraceLevel)
}
if cfg.GRPC.DebugAddress != "" {
if err := setupDebugHandlers(cfg.GRPC.DebugAddress); err != nil {
return err
}
}
tp, err := detect.TracerProvider()
if err != nil {
return err
}
mp, err := detect.MeterProvider()
if err != nil {
return err
}
streamTracer := otelgrpc.StreamServerInterceptor( //nolint:staticcheck // TODO(thaJeztah): ignore SA1019 for deprecated options: see https://github.com/moby/buildkit/issues/4681
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithMeterProvider(mp),
otelgrpc.WithPropagators(propagators),
)
unary := grpc_middleware.ChainUnaryServer(unaryInterceptor(ctx, tp, mp), grpcerrors.UnaryServerInterceptor)
stream := grpc_middleware.ChainStreamServer(streamTracer, grpcerrors.StreamServerInterceptor)
opts := []grpc.ServerOption{grpc.UnaryInterceptor(unary), grpc.StreamInterceptor(stream)}
server := grpc.NewServer(opts...)
// relative path does not work with nightlyone/lockfile
root, err := filepath.Abs(cfg.Root)
if err != nil {
return err
}
cfg.Root = root
if err := os.MkdirAll(root, 0700); err != nil {
return errors.Wrapf(err, "failed to create %s", root)
}
// Stop if we are registering or unregistering against Windows SCM.
stop, err := registerUnregisterService(cfg.Root)
if err != nil {
bklog.L.Fatal(err)
}
if stop {
return nil
}
lockPath := filepath.Join(root, "buildkitd.lock")
lock := flock.New(lockPath)
locked, err := lock.TryLock()
if err != nil {
return errors.Wrapf(err, "could not lock %s", lockPath)
}
if !locked {
return errors.Errorf("could not lock %s, another instance running?", lockPath)
}
defer func() {
lock.Unlock()
os.RemoveAll(lockPath)
}()
// listeners have to be initialized before the controller
// https://github.com/moby/buildkit/issues/4618
listeners, err := newGRPCListeners(cfg.GRPC)
if err != nil {
return err
}
controller, err := newController(c, &cfg)
if err != nil {
return err
}
defer controller.Close()
healthv1.RegisterHealthServer(server, health.NewServer())
controller.Register(server)
reflection.Register(server)
ents := c.GlobalStringSlice("allow-insecure-entitlement")
if len(ents) > 0 {
cfg.Entitlements = []string{}
for _, e := range ents {
switch e {
case "security.insecure":
cfg.Entitlements = append(cfg.Entitlements, e)
case "network.host":
cfg.Entitlements = append(cfg.Entitlements, e)
default:
return errors.Errorf("invalid entitlement : %s", e)
}
}
}
// Launch as a Windows Service if necessary
if err := launchService(server); err != nil {
bklog.L.Fatal(err)
}
errCh := make(chan error, 1)
if err := serveGRPC(server, listeners, errCh); err != nil {
return err
}
select {
case serverErr := <-errCh:
err = serverErr
cancel(err)
case <-ctx.Done():
err = context.Cause(ctx)
}
bklog.G(ctx).Infof("stopping server")
if os.Getenv("NOTIFY_SOCKET") != "" {
notified, notifyErr := sddaemon.SdNotify(false, sddaemon.SdNotifyStopping)
bklog.G(ctx).Debugf("SdNotifyStopping notified=%v, err=%v", notified, notifyErr)
}
server.GracefulStop()
return err
}
app.After = func(_ *cli.Context) error {
return detect.Shutdown(context.TODO())
}
profiler.Attach(app)
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "buildkitd: %+v\n", err)
os.Exit(1)
}
}
func newGRPCListeners(cfg config.GRPCConfig) ([]net.Listener, error) {
addrs := cfg.Address
if len(addrs) == 0 {
return nil, errors.New("--addr cannot be empty")
}
tlsConfig, err := serverCredentials(cfg.TLS)
if err != nil {
return nil, err
}
listeners := make([]net.Listener, 0, len(addrs))
for _, addr := range addrs {
l, err := getListener(addr, *cfg.UID, *cfg.GID, tlsConfig)
if err != nil {
for _, l := range listeners {
l.Close()
}
return listeners, err
}
listeners = append(listeners, l)
}
return listeners, nil
}
func serveGRPC(server *grpc.Server, listeners []net.Listener, errCh chan error) error {
if os.Getenv("NOTIFY_SOCKET") != "" {
notified, notifyErr := sddaemon.SdNotify(false, sddaemon.SdNotifyReady)
bklog.L.Debugf("SdNotifyReady notified=%v, err=%v", notified, notifyErr)
}
eg, _ := errgroup.WithContext(context.Background())
for _, l := range listeners {
func(l net.Listener) {
eg.Go(func() error {
defer l.Close()
bklog.L.Infof("running server on %s", l.Addr())
return server.Serve(l)
})
}(l)
}
go func() {
errCh <- eg.Wait()
}()
return nil
}
func defaultConfigPath() string {
if isRootlessConfig() {
return filepath.Join(appdefaults.UserConfigDir(), "buildkitd.toml")
}
return filepath.Join(appdefaults.ConfigDir, "buildkitd.toml")
}
func defaultConf() (config.Config, error) {
cfg, err := config.LoadFile(defaultConfigPath())
if err != nil {
var pe *os.PathError
if !errors.As(err, &pe) {
return config.Config{}, err
}
bklog.L.Warnf("failed to load default config: %v", err)
}
setDefaultConfig(&cfg)
return cfg, nil
}
func setDefaultNetworkConfig(nc config.NetworkConfig) config.NetworkConfig {
if nc.Mode == "" {
nc.Mode = "auto"
}
if nc.CNIConfigPath == "" {
if isRootlessConfig() {
nc.CNIConfigPath = appdefaults.UserCNIConfigPath
} else {
nc.CNIConfigPath = appdefaults.DefaultCNIConfigPath
}
}
if nc.CNIBinaryPath == "" {
nc.CNIBinaryPath = appdefaults.DefaultCNIBinDir
}
if nc.BridgeName == "" {
nc.BridgeName = appdefaults.BridgeName
}
if nc.BridgeSubnet == "" {
nc.BridgeSubnet = appdefaults.BridgeSubnet
}
return nc
}
func setDefaultConfig(cfg *config.Config) {
orig := *cfg
if cfg.Root == "" {
cfg.Root = appdefaults.Root
}
if len(cfg.GRPC.Address) == 0 {
cfg.GRPC.Address = []string{appdefaults.Address}
}
if cfg.Workers.OCI.Platforms == nil {
cfg.Workers.OCI.Platforms = formatPlatforms(archutil.SupportedPlatforms(false))
}
if cfg.Workers.Containerd.Platforms == nil {
cfg.Workers.Containerd.Platforms = formatPlatforms(archutil.SupportedPlatforms(false))
}
cfg.Workers.OCI.NetworkConfig = setDefaultNetworkConfig(cfg.Workers.OCI.NetworkConfig)
cfg.Workers.Containerd.NetworkConfig = setDefaultNetworkConfig(cfg.Workers.Containerd.NetworkConfig)
if isRootlessConfig() {
if orig.Root == "" {
cfg.Root = appdefaults.UserRoot()
}
if len(orig.GRPC.Address) == 0 {
cfg.GRPC.Address = []string{appdefaults.UserAddress()}
}
appdefaults.EnsureUserAddressDir()
}
if cfg.OTEL.SocketPath == "" {
cfg.OTEL.SocketPath = appdefaults.TraceSocketPath(isRootlessConfig())
}
}
var isRootlessConfigOnce sync.Once
var isRootlessConfigValue bool
// isRootlessConfig is true if we should be using the rootless config
// defaults instead of the normal defaults.
func isRootlessConfig() bool {
isRootlessConfigOnce.Do(func() {
if !userns.RunningInUserNS() {
// Default value is false so keep it that way.
return
}
// if buildkitd is being executed as the mapped-root (not only EUID==0 but also $USER==root)
// in a user namespace, we don't want to load the rootless changes in the
// configuration.
u := os.Getenv("USER")
isRootlessConfigValue = u != "" && u != "root"
})
return isRootlessConfigValue
}
func applyMainFlags(c *cli.Context, cfg *config.Config) error {
if c.IsSet("debug") {
cfg.Debug = c.Bool("debug")
}
if c.IsSet("trace") {
cfg.Trace = c.Bool("trace")
}
if c.IsSet("root") {
cfg.Root = c.String("root")
}
if c.IsSet("log-format") {
cfg.Log.Format = c.String("log-format")
}
if c.IsSet("addr") || len(cfg.GRPC.Address) == 0 {
cfg.GRPC.Address = c.StringSlice("addr")
}
if c.IsSet("allow-insecure-entitlement") {
// override values from config
cfg.Entitlements = c.StringSlice("allow-insecure-entitlement")
}
if c.IsSet("debugaddr") {
cfg.GRPC.DebugAddress = c.String("debugaddr")
}
if cfg.GRPC.UID == nil {
uid := os.Getuid()
cfg.GRPC.UID = &uid
}
if cfg.GRPC.GID == nil {
gid := os.Getgid()
cfg.GRPC.GID = &gid
}
if group := c.String("group"); group != "" {
gid, err := groupToGid(group)
if err != nil {
return err
}
cfg.GRPC.GID = &gid
}
if tlscert := c.String("tlscert"); tlscert != "" {
cfg.GRPC.TLS.Cert = tlscert
}
if tlskey := c.String("tlskey"); tlskey != "" {
cfg.GRPC.TLS.Key = tlskey
}
if tlsca := c.String("tlscacert"); tlsca != "" {
cfg.GRPC.TLS.CA = tlsca
}
if c.IsSet("otel-socket-path") {
cfg.OTEL.SocketPath = c.String("otel-socket-path")
}
applyPlatformFlags(c)
return nil
}
// Convert a string containing either a group name or a stringified gid into a numeric id)
func groupToGid(group string) (int, error) {
if group == "" {
return os.Getgid(), nil
}
var (
err error
id int
)
// Try and parse as a number, if the error is ErrSyntax
// (i.e. its not a number) then we carry on and try it as a
// name.
if id, err = strconv.Atoi(group); err == nil {
return id, nil
} else if err.(*strconv.NumError).Err != strconv.ErrSyntax {
return 0, err
}
ginfo, err := user.LookupGroup(group)
if err != nil {
return 0, err
}
group = ginfo.Gid
if id, err = strconv.Atoi(group); err != nil {
return 0, err
}
return id, nil
}
func getListener(addr string, uid, gid int, tlsConfig *tls.Config) (net.Listener, error) {
addrSlice := strings.SplitN(addr, "://", 2)
if len(addrSlice) < 2 {
return nil, errors.Errorf("address %s does not contain proto, you meant unix://%s ?",
addr, addr)
}
proto := addrSlice[0]
listenAddr := addrSlice[1]
switch proto {
case "unix", "npipe":
if tlsConfig != nil {
bklog.L.Warnf("TLS is disabled for %s", addr)
}
return sys.GetLocalListener(listenAddr, uid, gid)
case "fd":
return listenFD(listenAddr, tlsConfig)
case "tcp":
l, err := net.Listen("tcp", listenAddr)
if err != nil {
return nil, err
}
if tlsConfig == nil {
bklog.L.Warnf("TLS is not enabled for %s. enabling mutual TLS authentication is highly recommended", addr)
return l, nil
}
return tls.NewListener(l, tlsConfig), nil
default:
return nil, errors.Errorf("addr %s not supported", addr)
}
}
func unaryInterceptor(globalCtx context.Context, tp trace.TracerProvider, mp metric.MeterProvider) grpc.UnaryServerInterceptor {
withTrace := otelgrpc.UnaryServerInterceptor( //nolint:staticcheck // TODO(thaJeztah): ignore SA1019 for deprecated options: see https://github.com/moby/buildkit/issues/4681
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithMeterProvider(mp),
otelgrpc.WithPropagators(propagators),
)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
ctx, cancel := context.WithCancelCause(ctx)
defer cancel(errors.WithStack(context.Canceled))
go func() {
select {
case <-ctx.Done():
case <-globalCtx.Done():
cancel(context.Cause(globalCtx))
}
}()
if strings.HasSuffix(info.FullMethod, "opentelemetry.proto.collector.trace.v1.TraceService/Export") {
return handler(ctx, req)
}
resp, err = withTrace(ctx, req, info, handler)
if err != nil {
bklog.G(ctx).Errorf("%s returned error: %v", info.FullMethod, err)
if logrus.GetLevel() >= logrus.DebugLevel {
fmt.Fprintf(os.Stderr, "%+v", stack.Formatter(grpcerrors.FromGRPC(err)))
}
}
return
}
}
func serverCredentials(cfg config.TLSConfig) (*tls.Config, error) {
certFile := cfg.Cert
keyFile := cfg.Key
caFile := cfg.CA
if certFile == "" && keyFile == "" {
return nil, nil
}
err := errors.New("you must specify key and cert file if one is specified")
if certFile == "" {
return nil, err
}
if keyFile == "" {
return nil, err
}
certificate, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Wrap(err, "could not load server key pair")
}
tlsConf := &tls.Config{
Certificates: []tls.Certificate{certificate},
}
if caFile != "" {
certPool := x509.NewCertPool()
ca, err := os.ReadFile(caFile)
if err != nil {
return nil, errors.Wrap(err, "could not read ca certificate")
}
// Append the client certificates from the CA
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return nil, errors.New("failed to append ca cert")
}
tlsConf.ClientAuth = tls.RequireAndVerifyClientCert
tlsConf.ClientCAs = certPool
}
return tlsConf, nil
}
func newController(c *cli.Context, cfg *config.Config) (*control.Controller, error) {
sessionManager, err := session.NewManager()
if err != nil {
return nil, err
}
tc, _, err := detect.Exporter()
if err != nil {
return nil, err
}
var traceSocket string
if tc != nil {
traceSocket = cfg.OTEL.SocketPath
if err := runTraceController(traceSocket, tc); err != nil {
return nil, err
}
}
wc, err := newWorkerController(c, workerInitializerOpt{
config: cfg,
sessionManager: sessionManager,
traceSocket: traceSocket,
})
if err != nil {
return nil, err
}
frontends := map[string]frontend.Frontend{}
frontends["dockerfile.v0"] = forwarder.NewGatewayForwarder(wc.Infos(), dockerfile.Build)
frontends["gateway.v0"] = gateway.NewGatewayFrontend(wc.Infos())
cacheStorage, err := bboltcachestorage.NewStore(filepath.Join(cfg.Root, "cache.db"))
if err != nil {
return nil, err
}
historyDB, err := bbolt.Open(filepath.Join(cfg.Root, "history.db"), 0600, nil)
if err != nil {
return nil, err
}
resolverFn := resolverFunc(cfg)
w, err := wc.GetDefault()
if err != nil {
return nil, err
}
remoteCacheExporterFuncs := map[string]remotecache.ResolveCacheExporterFunc{
"registry": registryremotecache.ResolveCacheExporterFunc(sessionManager, resolverFn),
"local": localremotecache.ResolveCacheExporterFunc(sessionManager),
"inline": inlineremotecache.ResolveCacheExporterFunc(),
"gha": gha.ResolveCacheExporterFunc(),
"s3": s3remotecache.ResolveCacheExporterFunc(),
"azblob": azblob.ResolveCacheExporterFunc(),
}
remoteCacheImporterFuncs := map[string]remotecache.ResolveCacheImporterFunc{
"registry": registryremotecache.ResolveCacheImporterFunc(sessionManager, w.ContentStore(), resolverFn),
"local": localremotecache.ResolveCacheImporterFunc(sessionManager),
"gha": gha.ResolveCacheImporterFunc(),
"s3": s3remotecache.ResolveCacheImporterFunc(),
"azblob": azblob.ResolveCacheImporterFunc(),
}
return control.NewController(control.Opt{
SessionManager: sessionManager,
WorkerController: wc,
Frontends: frontends,
ResolveCacheExporterFuncs: remoteCacheExporterFuncs,
ResolveCacheImporterFuncs: remoteCacheImporterFuncs,
CacheManager: solver.NewCacheManager(context.TODO(), "local", cacheStorage, worker.NewCacheResultStorage(wc)),
Entitlements: cfg.Entitlements,
TraceCollector: tc,
HistoryDB: historyDB,
CacheStore: cacheStorage,
LeaseManager: w.LeaseManager(),
ContentStore: w.ContentStore(),
HistoryConfig: cfg.History,
})
}
func resolverFunc(cfg *config.Config) docker.RegistryHosts {
return resolver.NewRegistryConfig(cfg.Registries)
}
func newWorkerController(c *cli.Context, wiOpt workerInitializerOpt) (*worker.Controller, error) {
wc := &worker.Controller{}
nWorkers := 0
for _, wi := range workerInitializers {
ws, err := wi.fn(c, wiOpt)
if err != nil {
return nil, err
}
for _, w := range ws {
p := w.Platforms(false)
bklog.L.Infof("found worker %q, labels=%v, platforms=%v", w.ID(), w.Labels(), formatPlatforms(p))
archutil.WarnIfUnsupported(p)
if err = wc.Add(w); err != nil {
return nil, err
}
nWorkers++
}
}
if nWorkers == 0 {
return nil, errors.New("no worker found, rebuild the buildkit daemon?")
}
defaultWorker, err := wc.GetDefault()
if err != nil {
return nil, err
}
bklog.L.Infof("found %d workers, default=%q", nWorkers, defaultWorker.ID())
bklog.L.Warn("currently, only the default worker can be used.")
return wc, nil
}
func attrMap(sl []string) (map[string]string, error) {
m := map[string]string{}
for _, v := range sl {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("invalid value %s", v)
}
m[parts[0]] = parts[1]
}
return m, nil
}
func formatPlatforms(p []ocispecs.Platform) []string {
str := make([]string, 0, len(p))
for _, pp := range p {
str = append(str, platforms.Format(platforms.Normalize(pp)))
}
return str
}
func parsePlatforms(platformsStr []string) ([]ocispecs.Platform, error) {
out := make([]ocispecs.Platform, 0, len(platformsStr))
for _, s := range platformsStr {
p, err := platforms.Parse(s)
if err != nil {
return nil, err
}
out = append(out, platforms.Normalize(p))
}
return out, nil
}
func getGCPolicy(cfg config.GCConfig, root string) []client.PruneInfo {
if cfg.GC != nil && !*cfg.GC {
return nil
}
if len(cfg.GCPolicy) == 0 {
cfg.GCPolicy = config.DefaultGCPolicy(cfg.GCKeepStorage)
}
out := make([]client.PruneInfo, 0, len(cfg.GCPolicy))
for _, rule := range cfg.GCPolicy {
out = append(out, client.PruneInfo{
Filter: rule.Filters,
All: rule.All,
KeepBytes: rule.KeepBytes.AsBytes(root),
KeepDuration: rule.KeepDuration.Duration,
})
}
return out
}
func getBuildkitVersion() client.BuildkitVersion {
return client.BuildkitVersion{
Package: version.Package,
Version: version.Version,
Revision: version.Revision,
}
}
func getDNSConfig(cfg *config.DNSConfig) *oci.DNSConfig {
var dns *oci.DNSConfig
if cfg != nil {
dns = &oci.DNSConfig{
Nameservers: cfg.Nameservers,
Options: cfg.Options,
SearchDomains: cfg.SearchDomains,
}
}
return dns
}
// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.EqualFold(s, "auto") {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}
func runTraceController(p string, exp sdktrace.SpanExporter) error {
server := grpc.NewServer()
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})
l, err := getLocalListener(p)
if err != nil {
return errors.Wrap(err, "creating trace controller listener")
}
go server.Serve(l)
return nil
}
type traceCollector struct {
tracev1.UnimplementedTraceServiceServer
exporter sdktrace.SpanExporter
}
func (t *traceCollector) Export(ctx context.Context, req *tracev1.ExportTraceServiceRequest) (*tracev1.ExportTraceServiceResponse, error) {
err := t.exporter.ExportSpans(ctx, transform.Spans(req.GetResourceSpans()))
if err != nil {
return nil, err
}
return &tracev1.ExportTraceServiceResponse{}, nil
}