vendor: update buildkit to opentelemetry support

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-06-15 21:02:39 -07:00
parent 6ba080d337
commit 334c93fbbe
829 changed files with 89541 additions and 24438 deletions

View File

@ -7,18 +7,23 @@ import (
"io/ioutil"
"net"
"net/url"
"strings"
"github.com/containerd/containerd/defaults"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client/connhelper"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/grpchijack"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/grpcerrors"
opentracing "github.com/opentracing/opentracing-go"
"github.com/moby/buildkit/util/tracing/otlptracegrpc"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
@ -41,6 +46,10 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
var unary []grpc.UnaryClientInterceptor
var stream []grpc.StreamClientInterceptor
var customTracer bool // allows manually setting disabling tracing even if tracer in context
var tracerProvider trace.TracerProvider
var tracerDelegate TracerDelegate
for _, o := range opts {
if _, ok := o.(*withFailFast); ok {
gopts = append(gopts, grpc.FailOnNonTempDialError(true))
@ -54,14 +63,30 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
needWithInsecure = false
}
if wt, ok := o.(*withTracer); ok {
unary = append(unary, otgrpc.OpenTracingClientInterceptor(wt.tracer, otgrpc.LogPayloads()))
stream = append(stream, otgrpc.OpenTracingStreamClientInterceptor(wt.tracer))
customTracer = true
tracerProvider = wt.tp
}
if wd, ok := o.(*withDialer); ok {
gopts = append(gopts, grpc.WithContextDialer(wd.dialer))
needDialer = false
}
if wt, ok := o.(*withTracerDelegate); ok {
tracerDelegate = wt
}
}
if !customTracer {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
tracerProvider = span.TracerProvider()
}
}
if tracerProvider != nil {
var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
unary = append(unary, filterInterceptor(otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(tracerProvider), otelgrpc.WithPropagators(propagators))))
stream = append(stream, otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(tracerProvider), otelgrpc.WithPropagators(propagators)))
}
if needDialer {
dialFn, err := resolveDialer(address)
if err != nil {
@ -104,12 +129,27 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
if err != nil {
return nil, errors.Wrapf(err, "failed to dial %q . make sure buildkitd is running", address)
}
c := &Client{
conn: conn,
}
if tracerDelegate != nil {
_ = c.setupDelegatedTracing(ctx, tracerDelegate) // ignore error
}
return c, nil
}
func (c *Client) setupDelegatedTracing(ctx context.Context, td TracerDelegate) error {
pd := otlptracegrpc.NewClient(c.conn)
e, err := otlptrace.New(ctx, pd)
if err != nil {
return nil
}
return td.SetSpanExporter(ctx, e)
}
func (c *Client) controlClient() controlapi.ControlClient {
return controlapi.NewControlClient(c.conn)
}
@ -182,12 +222,26 @@ func loadCredentials(opts *withCredentials) (grpc.DialOption, error) {
return grpc.WithTransportCredentials(credentials.NewTLS(cfg)), nil
}
func WithTracer(t opentracing.Tracer) ClientOpt {
func WithTracerProvider(t trace.TracerProvider) ClientOpt {
return &withTracer{t}
}
type withTracer struct {
tracer opentracing.Tracer
tp trace.TracerProvider
}
type TracerDelegate interface {
SetSpanExporter(context.Context, sdktrace.SpanExporter) error
}
func WithTracerDelegate(td TracerDelegate) ClientOpt {
return &withTracerDelegate{
TracerDelegate: td,
}
}
type withTracerDelegate struct {
TracerDelegate
}
func resolveDialer(address string) (func(context.Context, string) (net.Conn, error), error) {
@ -201,3 +255,12 @@ func resolveDialer(address string) (func(context.Context, string) (net.Conn, err
// basic dialer
return dialer, nil
}
func filterInterceptor(intercept grpc.UnaryClientInterceptor) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if strings.HasSuffix(method, "opentelemetry.proto.collector.trace.v1.TraceService/Export") {
return invoker(ctx, method, req, reply, cc, opts...)
}
return intercept(ctx, method, req, reply, cc, invoker, opts...)
}
}

View File

@ -10,7 +10,7 @@ import (
)
type asyncState struct {
f func(context.Context, State) (State, error)
f func(context.Context, State, *Constraints) (State, error)
prev State
target State
set bool
@ -22,8 +22,8 @@ func (as *asyncState) Output() Output {
return as
}
func (as *asyncState) Vertex(ctx context.Context) Vertex {
err := as.Do(ctx)
func (as *asyncState) Vertex(ctx context.Context, c *Constraints) Vertex {
err := as.Do(ctx, c)
if err != nil {
return &errVertex{err}
}
@ -32,13 +32,13 @@ func (as *asyncState) Vertex(ctx context.Context) Vertex {
if out == nil {
return nil
}
return out.Vertex(ctx)
return out.Vertex(ctx, c)
}
return nil
}
func (as *asyncState) ToInput(ctx context.Context, c *Constraints) (*pb.Input, error) {
err := as.Do(ctx)
err := as.Do(ctx, c)
if err != nil {
return nil, err
}
@ -52,12 +52,12 @@ func (as *asyncState) ToInput(ctx context.Context, c *Constraints) (*pb.Input, e
return nil, nil
}
func (as *asyncState) Do(ctx context.Context) error {
func (as *asyncState) Do(ctx context.Context, c *Constraints) error {
_, err := as.g.Do(ctx, "", func(ctx context.Context) (interface{}, error) {
if as.set {
return as.target, as.err
}
res, err := as.f(ctx, as.prev)
res, err := as.f(ctx, as.prev, c)
if err != nil {
select {
case <-ctx.Done():
@ -82,7 +82,7 @@ type errVertex struct {
err error
}
func (v *errVertex) Validate(context.Context) error {
func (v *errVertex) Validate(context.Context, *Constraints) error {
return v.err
}
func (v *errVertex) Marshal(context.Context, *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {

View File

@ -105,11 +105,11 @@ func (d *DefinitionOp) ToInput(ctx context.Context, c *Constraints) (*pb.Input,
return d.Output().ToInput(ctx, c)
}
func (d *DefinitionOp) Vertex(context.Context) Vertex {
func (d *DefinitionOp) Vertex(context.Context, *Constraints) Vertex {
return d
}
func (d *DefinitionOp) Validate(context.Context) error {
func (d *DefinitionOp) Validate(context.Context, *Constraints) error {
// Scratch state has no digest, ops or metas.
if d.dgst == "" {
return nil
@ -151,7 +151,7 @@ func (d *DefinitionOp) Marshal(ctx context.Context, c *Constraints) (digest.Dige
return "", nil, nil, nil, errors.Errorf("cannot marshal empty definition op")
}
if err := d.Validate(ctx); err != nil {
if err := d.Validate(ctx, c); err != nil {
return "", nil, nil, nil, err
}

View File

@ -95,18 +95,18 @@ func (e *ExecOp) GetMount(target string) Output {
return nil
}
func (e *ExecOp) Validate(ctx context.Context) error {
func (e *ExecOp) Validate(ctx context.Context, c *Constraints) error {
if e.isValidated {
return nil
}
args, err := getArgs(e.base)(ctx)
args, err := getArgs(e.base)(ctx, c)
if err != nil {
return err
}
if len(args) == 0 {
return errors.Errorf("arguments are required")
}
cwd, err := getDir(e.base)(ctx)
cwd, err := getDir(e.base)(ctx, c)
if err != nil {
return err
}
@ -115,7 +115,7 @@ func (e *ExecOp) Validate(ctx context.Context) error {
}
for _, m := range e.mounts {
if m.source != nil {
if err := m.source.Vertex(ctx).Validate(ctx); err != nil {
if err := m.source.Vertex(ctx, c).Validate(ctx, c); err != nil {
return err
}
}
@ -128,7 +128,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
if e.Cached(c) {
return e.Load()
}
if err := e.Validate(ctx); err != nil {
if err := e.Validate(ctx, c); err != nil {
return "", nil, nil, nil, err
}
// make sure mounts are sorted
@ -136,7 +136,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
return e.mounts[i].target < e.mounts[j].target
})
env, err := getEnv(e.base)(ctx)
env, err := getEnv(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -165,22 +165,22 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
}
}
args, err := getArgs(e.base)(ctx)
args, err := getArgs(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
cwd, err := getDir(e.base)(ctx)
cwd, err := getDir(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
user, err := getUser(e.base)(ctx)
user, err := getUser(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
hostname, err := getHostname(e.base)(ctx)
hostname, err := getHostname(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -192,7 +192,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
User: user,
Hostname: hostname,
}
extraHosts, err := getExtraHosts(e.base)(ctx)
extraHosts, err := getExtraHosts(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -204,12 +204,12 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
meta.ExtraHosts = hosts
}
network, err := getNetwork(e.base)(ctx)
network, err := getNetwork(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
security, err := getSecurity(e.base)(ctx)
security, err := getSecurity(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -233,6 +233,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
HttpsProxy: p.HTTPSProxy,
FtpProxy: p.FTPProxy,
NoProxy: p.NoProxy,
AllProxy: p.AllProxy,
}
addCap(&e.constraints, pb.CapExecMetaProxy)
}
@ -262,7 +263,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
}
if e.constraints.Platform == nil {
p, err := getPlatform(e.base)(ctx)
p, err := getPlatform(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -645,6 +646,7 @@ type ProxyEnv struct {
HTTPSProxy string
FTPProxy string
NoProxy string
AllProxy string
}
type CacheMountSharingMode int

View File

@ -56,6 +56,10 @@ type subAction interface {
toProtoAction(context.Context, string, pb.InputIndex) (pb.IsFileAction, error)
}
type capAdder interface {
addCaps(*FileOp)
}
type FileAction struct {
state *State
prev *FileAction
@ -427,6 +431,8 @@ type CopyInfo struct {
Mode *os.FileMode
FollowSymlinks bool
CopyDirContentsOnly bool
IncludePatterns []string
ExcludePatterns []string
AttemptUnpack bool
CreateDestPath bool
AllowWildcard bool
@ -458,6 +464,8 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
Src: src,
Dest: normalizePath(parent, a.dest, true),
Owner: a.info.ChownOpt.marshal(base),
IncludePatterns: a.info.IncludePatterns,
ExcludePatterns: a.info.ExcludePatterns,
AllowWildcard: a.info.AllowWildcard,
AllowEmptyWildcard: a.info.AllowEmptyWildcard,
FollowSymlink: a.info.FollowSymlinks,
@ -496,6 +504,12 @@ func (a *fileActionCopy) sourcePath(ctx context.Context) (string, error) {
return p, nil
}
func (a *fileActionCopy) addCaps(f *FileOp) {
if len(a.info.IncludePatterns) != 0 || len(a.info.ExcludePatterns) != 0 {
addCap(&f.constraints, pb.CapFileCopyIncludeExcludePatterns)
}
}
type CreatedTime time.Time
func WithCreatedTime(t time.Time) CreatedTime {
@ -530,7 +544,7 @@ type FileOp struct {
isValidated bool
}
func (f *FileOp) Validate(context.Context) error {
func (f *FileOp) Validate(context.Context, *Constraints) error {
if f.isValidated {
return nil
}
@ -653,7 +667,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
if f.Cached(c) {
return f.Load()
}
if err := f.Validate(ctx); err != nil {
if err := f.Validate(ctx, c); err != nil {
return "", nil, nil, nil, err
}
@ -662,7 +676,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
pfo := &pb.FileOp{}
if f.constraints.Platform == nil {
p, err := getPlatform(*f.action.state)(ctx)
p, err := getPlatform(*f.action.state)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
@ -682,6 +696,10 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
pop.Inputs = state.inputs
for i, st := range state.actions {
if adder, isCapAdder := st.action.(capAdder); isCapAdder {
adder.addCaps(f)
}
output := pb.OutputIndex(-1)
if i+1 == len(state.actions) {
output = 0

View File

@ -39,8 +39,8 @@ func addEnvf(key, value string, replace bool, v ...interface{}) StateOption {
value = fmt.Sprintf(value, v...)
}
return func(s State) State {
return s.withValue(keyEnv, func(ctx context.Context) (interface{}, error) {
env, err := getEnv(s)(ctx)
return s.withValue(keyEnv, func(ctx context.Context, c *Constraints) (interface{}, error) {
env, err := getEnv(s)(ctx, c)
if err != nil {
return nil, err
}
@ -62,9 +62,9 @@ func dirf(value string, replace bool, v ...interface{}) StateOption {
value = fmt.Sprintf(value, v...)
}
return func(s State) State {
return s.withValue(keyDir, func(ctx context.Context) (interface{}, error) {
return s.withValue(keyDir, func(ctx context.Context, c *Constraints) (interface{}, error) {
if !path.IsAbs(value) {
prev, err := getDir(s)(ctx)
prev, err := getDir(s)(ctx, c)
if err != nil {
return nil, err
}
@ -92,9 +92,9 @@ func Reset(other State) StateOption {
}
}
func getEnv(s State) func(context.Context) (EnvList, error) {
return func(ctx context.Context) (EnvList, error) {
v, err := s.getValue(keyEnv)(ctx)
func getEnv(s State) func(context.Context, *Constraints) (EnvList, error) {
return func(ctx context.Context, c *Constraints) (EnvList, error) {
v, err := s.getValue(keyEnv)(ctx, c)
if err != nil {
return nil, err
}
@ -105,9 +105,9 @@ func getEnv(s State) func(context.Context) (EnvList, error) {
}
}
func getDir(s State) func(context.Context) (string, error) {
return func(ctx context.Context) (string, error) {
v, err := s.getValue(keyDir)(ctx)
func getDir(s State) func(context.Context, *Constraints) (string, error) {
return func(ctx context.Context, c *Constraints) (string, error) {
v, err := s.getValue(keyDir)(ctx, c)
if err != nil {
return "", err
}
@ -118,9 +118,9 @@ func getDir(s State) func(context.Context) (string, error) {
}
}
func getArgs(s State) func(context.Context) ([]string, error) {
return func(ctx context.Context) ([]string, error) {
v, err := s.getValue(keyArgs)(ctx)
func getArgs(s State) func(context.Context, *Constraints) ([]string, error) {
return func(ctx context.Context, c *Constraints) ([]string, error) {
v, err := s.getValue(keyArgs)(ctx, c)
if err != nil {
return nil, err
}
@ -131,9 +131,9 @@ func getArgs(s State) func(context.Context) ([]string, error) {
}
}
func getUser(s State) func(context.Context) (string, error) {
return func(ctx context.Context) (string, error) {
v, err := s.getValue(keyUser)(ctx)
func getUser(s State) func(context.Context, *Constraints) (string, error) {
return func(ctx context.Context, c *Constraints) (string, error) {
v, err := s.getValue(keyUser)(ctx, c)
if err != nil {
return "", err
}
@ -150,9 +150,9 @@ func Hostname(str string) StateOption {
}
}
func getHostname(s State) func(context.Context) (string, error) {
return func(ctx context.Context) (string, error) {
v, err := s.getValue(keyHostname)(ctx)
func getHostname(s State) func(context.Context, *Constraints) (string, error) {
return func(ctx context.Context, c *Constraints) (string, error) {
v, err := s.getValue(keyHostname)(ctx, c)
if err != nil {
return "", err
}
@ -188,9 +188,9 @@ func platform(p specs.Platform) StateOption {
}
}
func getPlatform(s State) func(context.Context) (*specs.Platform, error) {
return func(ctx context.Context) (*specs.Platform, error) {
v, err := s.getValue(keyPlatform)(ctx)
func getPlatform(s State) func(context.Context, *Constraints) (*specs.Platform, error) {
return func(ctx context.Context, c *Constraints) (*specs.Platform, error) {
v, err := s.getValue(keyPlatform)(ctx, c)
if err != nil {
return nil, err
}
@ -204,8 +204,8 @@ func getPlatform(s State) func(context.Context) (*specs.Platform, error) {
func extraHost(host string, ip net.IP) StateOption {
return func(s State) State {
return s.withValue(keyExtraHost, func(ctx context.Context) (interface{}, error) {
v, err := getExtraHosts(s)(ctx)
return s.withValue(keyExtraHost, func(ctx context.Context, c *Constraints) (interface{}, error) {
v, err := getExtraHosts(s)(ctx, c)
if err != nil {
return nil, err
}
@ -214,9 +214,9 @@ func extraHost(host string, ip net.IP) StateOption {
}
}
func getExtraHosts(s State) func(context.Context) ([]HostIP, error) {
return func(ctx context.Context) ([]HostIP, error) {
v, err := s.getValue(keyExtraHost)(ctx)
func getExtraHosts(s State) func(context.Context, *Constraints) ([]HostIP, error) {
return func(ctx context.Context, c *Constraints) ([]HostIP, error) {
v, err := s.getValue(keyExtraHost)(ctx, c)
if err != nil {
return nil, err
}
@ -237,9 +237,9 @@ func Network(v pb.NetMode) StateOption {
return s.WithValue(keyNetwork, v)
}
}
func getNetwork(s State) func(context.Context) (pb.NetMode, error) {
return func(ctx context.Context) (pb.NetMode, error) {
v, err := s.getValue(keyNetwork)(ctx)
func getNetwork(s State) func(context.Context, *Constraints) (pb.NetMode, error) {
return func(ctx context.Context, c *Constraints) (pb.NetMode, error) {
v, err := s.getValue(keyNetwork)(ctx, c)
if err != nil {
return 0, err
}
@ -256,9 +256,9 @@ func Security(v pb.SecurityMode) StateOption {
return s.WithValue(keySecurity, v)
}
}
func getSecurity(s State) func(context.Context) (pb.SecurityMode, error) {
return func(ctx context.Context) (pb.SecurityMode, error) {
v, err := s.getValue(keySecurity)(ctx)
func getSecurity(s State) func(context.Context, *Constraints) (pb.SecurityMode, error) {
return func(ctx context.Context, c *Constraints) (pb.SecurityMode, error) {
v, err := s.getValue(keySecurity)(ctx, c)
if err != nil {
return 0, err
}

View File

@ -36,7 +36,7 @@ func NewSource(id string, attrs map[string]string, c Constraints) *SourceOp {
return s
}
func (s *SourceOp) Validate(ctx context.Context) error {
func (s *SourceOp) Validate(ctx context.Context, c *Constraints) error {
if s.err != nil {
return s.err
}
@ -50,7 +50,7 @@ func (s *SourceOp) Marshal(ctx context.Context, constraints *Constraints) (diges
if s.Cached(constraints) {
return s.Load()
}
if err := s.Validate(ctx); err != nil {
if err := s.Validate(ctx, constraints); err != nil {
return "", nil, nil, nil, err
}
@ -121,9 +121,13 @@ func Image(ref string, opts ...ImageOption) State {
src.err = err
} else if info.metaResolver != nil {
if _, ok := r.(reference.Digested); ok || !info.resolveDigest {
return NewState(src.Output()).Async(func(ctx context.Context, st State) (State, error) {
return NewState(src.Output()).Async(func(ctx context.Context, st State, c *Constraints) (State, error) {
p := info.Constraints.Platform
if p == nil {
p = c.Platform
}
_, dt, err := info.metaResolver.ResolveImageConfig(ctx, ref, ResolveImageConfigOpt{
Platform: info.Constraints.Platform,
Platform: p,
ResolveMode: info.resolveMode.String(),
})
if err != nil {
@ -132,9 +136,13 @@ func Image(ref string, opts ...ImageOption) State {
return st.WithImageConfig(dt)
})
}
return Scratch().Async(func(ctx context.Context, _ State) (State, error) {
return Scratch().Async(func(ctx context.Context, _ State, c *Constraints) (State, error) {
p := info.Constraints.Platform
if p == nil {
p = c.Platform
}
dgst, dt, err := info.metaResolver.ResolveImageConfig(context.TODO(), ref, ResolveImageConfigOpt{
Platform: info.Constraints.Platform,
Platform: p,
ResolveMode: info.resolveMode.String(),
})
if err != nil {
@ -361,6 +369,12 @@ func Local(name string, opts ...LocalOption) State {
attrs[pb.AttrSharedKeyHint] = gi.SharedKeyHint
addCap(&gi.Constraints, pb.CapSourceLocalSharedKeyHint)
}
if gi.Differ.Type != "" {
attrs[pb.AttrLocalDiffer] = string(gi.Differ.Type)
if gi.Differ.Required {
addCap(&gi.Constraints, pb.CapSourceLocalDiffer)
}
}
addCap(&gi.Constraints, pb.CapSourceLocal)
@ -423,6 +437,32 @@ func SharedKeyHint(h string) LocalOption {
})
}
func Differ(t DiffType, required bool) LocalOption {
return localOptionFunc(func(li *LocalInfo) {
li.Differ = DifferInfo{
Type: t,
Required: required,
}
})
}
type DiffType string
const (
// DiffNone will do no file comparisons, all files in the Local source will
// be retransmitted.
DiffNone DiffType = pb.AttrLocalDifferNone
// DiffMetadata will compare file metadata (size, modified time, mode, owner,
// group, device and link name) to determine if the files in the Local source need
// to be retransmitted. This is the default behavior.
DiffMetadata DiffType = pb.AttrLocalDifferMetadata
)
type DifferInfo struct {
Type DiffType
Required bool
}
type LocalInfo struct {
constraintsWrapper
SessionID string
@ -430,6 +470,7 @@ type LocalInfo struct {
ExcludePatterns string
FollowPaths string
SharedKeyHint string
Differ DifferInfo
}
func HTTP(url string, opts ...HTTPOption) State {

View File

@ -19,11 +19,11 @@ type StateOption func(State) State
type Output interface {
ToInput(context.Context, *Constraints) (*pb.Input, error)
Vertex(context.Context) Vertex
Vertex(context.Context, *Constraints) Vertex
}
type Vertex interface {
Validate(context.Context) error
Validate(context.Context, *Constraints) error
Marshal(context.Context, *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error)
Output() Output
Inputs() []Output
@ -41,7 +41,7 @@ type State struct {
out Output
prev *State
key interface{}
value func(context.Context) (interface{}, error)
value func(context.Context, *Constraints) (interface{}, error)
opts []ConstraintsOpt
async *asyncState
}
@ -58,12 +58,12 @@ func (s State) ensurePlatform() State {
}
func (s State) WithValue(k, v interface{}) State {
return s.withValue(k, func(context.Context) (interface{}, error) {
return s.withValue(k, func(context.Context, *Constraints) (interface{}, error) {
return v, nil
})
}
func (s State) withValue(k interface{}, v func(context.Context) (interface{}, error)) State {
func (s State) withValue(k interface{}, v func(context.Context, *Constraints) (interface{}, error)) State {
return State{
out: s.Output(),
prev: &s, // doesn't need to be original pointer
@ -72,21 +72,25 @@ func (s State) withValue(k interface{}, v func(context.Context) (interface{}, er
}
}
func (s State) Value(ctx context.Context, k interface{}) (interface{}, error) {
return s.getValue(k)(ctx)
func (s State) Value(ctx context.Context, k interface{}, co ...ConstraintsOpt) (interface{}, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return s.getValue(k)(ctx, c)
}
func (s State) getValue(k interface{}) func(context.Context) (interface{}, error) {
func (s State) getValue(k interface{}) func(context.Context, *Constraints) (interface{}, error) {
if s.key == k {
return s.value
}
if s.async != nil {
return func(ctx context.Context) (interface{}, error) {
err := s.async.Do(ctx)
return func(ctx context.Context, c *Constraints) (interface{}, error) {
err := s.async.Do(ctx, c)
if err != nil {
return nil, err
}
return s.async.target.getValue(k)(ctx)
return s.async.target.getValue(k)(ctx, c)
}
}
if s.prev == nil {
@ -95,7 +99,7 @@ func (s State) getValue(k interface{}) func(context.Context) (interface{}, error
return s.prev.getValue(k)
}
func (s State) Async(f func(context.Context, State) (State, error)) State {
func (s State) Async(f func(context.Context, State, *Constraints) (State, error)) State {
s2 := State{
async: &asyncState{f: f, prev: s},
}
@ -111,9 +115,6 @@ func (s State) Marshal(ctx context.Context, co ...ConstraintsOpt) (*Definition,
def := &Definition{
Metadata: make(map[digest.Digest]pb.OpMetadata, 0),
}
if s.Output() == nil || s.Output().Vertex(ctx) == nil {
return def, nil
}
defaultPlatform := platforms.Normalize(platforms.DefaultSpec())
c := &Constraints{
@ -123,10 +124,12 @@ func (s State) Marshal(ctx context.Context, co ...ConstraintsOpt) (*Definition,
for _, o := range append(s.opts, co...) {
o.SetConstraintsOption(c)
}
if s.Output() == nil || s.Output().Vertex(ctx, c) == nil {
return def, nil
}
smc := newSourceMapCollector()
def, err := marshal(ctx, s.Output().Vertex(ctx), def, smc, map[digest.Digest]struct{}{}, map[Vertex]struct{}{}, c)
def, err := marshal(ctx, s.Output().Vertex(ctx, c), def, smc, map[digest.Digest]struct{}{}, map[Vertex]struct{}{}, c)
if err != nil {
return def, err
}
@ -176,7 +179,7 @@ func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollect
}
for _, inp := range v.Inputs() {
var err error
def, err = marshal(ctx, inp.Vertex(ctx), def, s, cache, vertexCache, c)
def, err = marshal(ctx, inp.Vertex(ctx, c), def, s, cache, vertexCache, c)
if err != nil {
return def, err
}
@ -199,8 +202,8 @@ func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollect
return def, nil
}
func (s State) Validate(ctx context.Context) error {
return s.Output().Vertex(ctx).Validate(ctx)
func (s State) Validate(ctx context.Context, c *Constraints) error {
return s.Output().Vertex(ctx, c).Validate(ctx, c)
}
func (s State) Output() Output {
@ -287,8 +290,12 @@ func (s State) Dirf(str string, v ...interface{}) State {
return Dirf(str, v...)(s)
}
func (s State) GetEnv(ctx context.Context, key string) (string, bool, error) {
env, err := getEnv(s)(ctx)
func (s State) GetEnv(ctx context.Context, key string, co ...ConstraintsOpt) (string, bool, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
env, err := getEnv(s)(ctx, c)
if err != nil {
return "", false, err
}
@ -296,20 +303,32 @@ func (s State) GetEnv(ctx context.Context, key string) (string, bool, error) {
return v, ok, nil
}
func (s State) Env(ctx context.Context) ([]string, error) {
env, err := getEnv(s)(ctx)
func (s State) Env(ctx context.Context, co ...ConstraintsOpt) ([]string, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
env, err := getEnv(s)(ctx, c)
if err != nil {
return nil, err
}
return env.ToArray(), nil
}
func (s State) GetDir(ctx context.Context) (string, error) {
return getDir(s)(ctx)
func (s State) GetDir(ctx context.Context, co ...ConstraintsOpt) (string, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getDir(s)(ctx, c)
}
func (s State) GetArgs(ctx context.Context) ([]string, error) {
return getArgs(s)(ctx)
func (s State) GetArgs(ctx context.Context, co ...ConstraintsOpt) ([]string, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getArgs(s)(ctx, c)
}
func (s State) Reset(s2 State) State {
@ -324,31 +343,47 @@ func (s State) Hostname(v string) State {
return Hostname(v)(s)
}
func (s State) GetHostname(ctx context.Context) (string, error) {
return getHostname(s)(ctx)
func (s State) GetHostname(ctx context.Context, co ...ConstraintsOpt) (string, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getHostname(s)(ctx, c)
}
func (s State) Platform(p specs.Platform) State {
return platform(p)(s)
}
func (s State) GetPlatform(ctx context.Context) (*specs.Platform, error) {
return getPlatform(s)(ctx)
func (s State) GetPlatform(ctx context.Context, co ...ConstraintsOpt) (*specs.Platform, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getPlatform(s)(ctx, c)
}
func (s State) Network(n pb.NetMode) State {
return Network(n)(s)
}
func (s State) GetNetwork(ctx context.Context) (pb.NetMode, error) {
return getNetwork(s)(ctx)
func (s State) GetNetwork(ctx context.Context, co ...ConstraintsOpt) (pb.NetMode, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getNetwork(s)(ctx, c)
}
func (s State) Security(n pb.SecurityMode) State {
return Security(n)(s)
}
func (s State) GetSecurity(ctx context.Context) (pb.SecurityMode, error) {
return getSecurity(s)(ctx)
func (s State) GetSecurity(ctx context.Context, co ...ConstraintsOpt) (pb.SecurityMode, error) {
c := &Constraints{}
for _, f := range co {
f.SetConstraintsOption(c)
}
return getSecurity(s)(ctx, c)
}
func (s State) With(so ...StateOption) State {
@ -390,7 +425,7 @@ func (o *output) ToInput(ctx context.Context, c *Constraints) (*pb.Input, error)
return &pb.Input{Digest: dgst, Index: index}, nil
}
func (o *output) Vertex(context.Context) Vertex {
func (o *output) Vertex(context.Context, *Constraints) Vertex {
return o.vertex
}
@ -565,6 +600,6 @@ func Require(filters ...string) ConstraintsOpt {
})
}
func nilValue(context.Context) (interface{}, error) {
func nilValue(context.Context, *Constraints) (interface{}, error) {
return nil, nil
}

View File

@ -22,10 +22,10 @@ import (
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/entitlements"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
fstypes "github.com/tonistiigi/fsutil/types"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)
@ -93,8 +93,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
statusContext, cancelStatus := context.WithCancel(context.Background())
defer cancelStatus()
if span := opentracing.SpanFromContext(ctx); span != nil {
statusContext = opentracing.ContextWithSpan(statusContext, span)
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
statusContext = trace.ContextWithSpan(statusContext, span)
}
s := opt.SharedSession