Skip to content

Commit

Permalink
Merge pull request #4304 from jedevc/v0.12.3-cherry-picks
Browse files Browse the repository at this point in the history
[v0.12] Cherry-picks for v0.12.3
  • Loading branch information
jedevc committed Oct 13, 2023
2 parents f94ed7c + 58a5cb9 commit 6560bb9
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 22 deletions.
24 changes: 19 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"strings"
"time"

contentapi "github.com/containerd/containerd/api/services/content/v1"
"github.com/containerd/containerd/defaults"
Expand Down Expand Up @@ -186,16 +187,29 @@ func (c *Client) Dialer() session.Dialer {
}

func (c *Client) Wait(ctx context.Context) error {
opts := []grpc.CallOption{grpc.WaitForReady(true)}
_, err := c.ControlClient().Info(ctx, &controlapi.InfoRequest{}, opts...)
if err != nil {
if code := grpcerrors.Code(err); code == codes.Unimplemented {
for {
_, err := c.ControlClient().Info(ctx, &controlapi.InfoRequest{})
if err == nil {
return nil
}

switch code := grpcerrors.Code(err); code {
case codes.Unavailable:
case codes.Unimplemented:
// only buildkit v0.11+ supports the info api, but an unimplemented
// response error is still a response so we can ignore it
return nil
default:
return err
}

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
}
c.conn.ResetConnectBackoff()
}
return err
}

func (c *Client) Close() error {
Expand Down
42 changes: 40 additions & 2 deletions client/llb/sourcemap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package llb

import (
"bytes"
"context"

"github.com/moby/buildkit/solver/pb"
Expand Down Expand Up @@ -47,6 +48,33 @@ func (s *SourceMap) Location(r []*pb.Range) ConstraintsOpt {
})
}

func equalSourceMap(sm1, sm2 *SourceMap) (out bool) {
if sm1 == nil || sm2 == nil {
return false
}
if sm1.Filename != sm2.Filename {
return false
}
if sm1.Language != sm2.Language {
return false
}
if len(sm1.Data) != len(sm2.Data) {
return false
}
if !bytes.Equal(sm1.Data, sm2.Data) {
return false
}
if sm1.Definition != nil && sm2.Definition != nil {
if len(sm1.Definition.Def) != len(sm2.Definition.Def) && len(sm1.Definition.Def) != 0 {
return false
}
if !bytes.Equal(sm1.Definition.Def[len(sm1.Definition.Def)-1], sm2.Definition.Def[len(sm2.Definition.Def)-1]) {
return false
}
}
return true
}

type SourceLocation struct {
SourceMap *SourceMap
Ranges []*pb.Range
Expand All @@ -69,8 +97,18 @@ func (smc *sourceMapCollector) Add(dgst digest.Digest, ls []*SourceLocation) {
for _, l := range ls {
idx, ok := smc.index[l.SourceMap]
if !ok {
idx = len(smc.maps)
smc.maps = append(smc.maps, l.SourceMap)
idx = -1
// slow equality check
for i, m := range smc.maps {
if equalSourceMap(m, l.SourceMap) {
idx = i
break
}
}
if idx == -1 {
idx = len(smc.maps)
smc.maps = append(smc.maps, l.SourceMap)
}
}
smc.index[l.SourceMap] = idx
}
Expand Down
8 changes: 0 additions & 8 deletions cmd/buildctl/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/pkg/errors"
"github.com/urfave/cli"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
)

// ResolveClient resolves a client from CLI args
Expand Down Expand Up @@ -69,12 +67,6 @@ func ResolveClient(c *cli.Context) (*client.Client, error) {

opts := []client.ClientOpt{client.WithFailFast()}

backoffConfig := backoff.DefaultConfig
backoffConfig.MaxDelay = 1 * time.Second
opts = append(opts, client.WithGRPCDialOption(
grpc.WithConnectParams(grpc.ConnectParams{Backoff: backoffConfig}),
))

ctx := CommandContext(c)

if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
Expand Down
1 change: 1 addition & 0 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ func newController(c *cli.Context, cfg *config.Config) (*control.Controller, err
Entitlements: cfg.Entitlements,
TraceCollector: tc,
HistoryDB: historyDB,
CacheStore: cacheStorage,
LeaseManager: w.LeaseManager(),
ContentStore: w.ContentStore(),
HistoryConfig: cfg.History,
Expand Down
14 changes: 13 additions & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/services/content/contentserver"
"github.com/docker/distribution/reference"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/hashstructure/v2"
controlapi "github.com/moby/buildkit/api/services/control"
apitypes "github.com/moby/buildkit/api/types"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/moby/buildkit/session/grpchijack"
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/solver/llbsolver"
"github.com/moby/buildkit/solver/llbsolver/proc"
"github.com/moby/buildkit/solver/pb"
Expand Down Expand Up @@ -61,6 +63,7 @@ type Opt struct {
Entitlements []string
TraceCollector sdktrace.SpanExporter
HistoryDB *bbolt.DB
CacheStore *bboltcachestorage.Store
LeaseManager *leaseutil.Manager
ContentStore *containerdsnapshot.Store
HistoryConfig *config.HistoryConfig
Expand Down Expand Up @@ -123,7 +126,16 @@ func NewController(opt Opt) (*Controller, error) {
}

func (c *Controller) Close() error {
return c.opt.WorkerController.Close()
rerr := c.opt.HistoryDB.Close()
if err := c.opt.WorkerController.Close(); err != nil {
rerr = multierror.Append(rerr, err)
}

if err := c.opt.CacheStore.Close(); err != nil {
rerr = multierror.Append(rerr, err)
}

return rerr
}

func (c *Controller) Register(server *grpc.Server) {
Expand Down
2 changes: 1 addition & 1 deletion executor/oci/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
return nil, nil, err
}

if cgroupNamespaceSupported() {
if cgroupV2NamespaceSupported() {
s.Linux.Namespaces = append(s.Linux.Namespaces, specs.LinuxNamespace{
Type: specs.CgroupNamespace,
})
Expand Down
14 changes: 11 additions & 3 deletions executor/oci/spec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ func getTracingSocket() string {
return fmt.Sprintf("unix://%s", tracingSocketPath)
}

func cgroupNamespaceSupported() bool {
func cgroupV2NamespaceSupported() bool {
// Check if cgroups v2 namespaces are supported. Trying to do cgroup
// namespaces with cgroups v1 results in EINVAL when we encounter a
// non-standard hierarchy.
// See https://github.com/moby/buildkit/issues/4108
cgroupNSOnce.Do(func() {
if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
supportsCgroupNS = true
if _, err := os.Stat("/proc/self/ns/cgroup"); os.IsNotExist(err) {
return
}
if _, err := os.Stat("/sys/fs/cgroup/cgroup.subtree_control"); os.IsNotExist(err) {
return
}
supportsCgroupNS = true
})
return supportsCgroupNS
}
2 changes: 1 addition & 1 deletion executor/oci/spec_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func getTracingSocket() string {
return fmt.Sprintf("npipe://%s", filepath.ToSlash(tracingSocketPath))
}

func cgroupNamespaceSupported() bool {
func cgroupV2NamespaceSupported() bool {
return false
}

0 comments on commit 6560bb9

Please sign in to comment.