vendor: update buildkit to v0.8

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2020-12-07 22:01:24 -08:00
parent 080e9981c7
commit 69a1419ab1
323 changed files with 20129 additions and 8394 deletions

View File

@ -33,8 +33,12 @@ func ToGRPC(err error) error {
st = status.New(Code(err), err.Error())
}
if st.Code() != Code(err) {
code := Code(err)
if code == codes.OK {
code = codes.Unknown
}
pb := st.Proto()
pb.Code = int32(Code(err))
pb.Code = int32(code)
st = status.FromProto(pb)
}
@ -96,9 +100,10 @@ func Code(err error) codes.Code {
Unwrap() error
})
if ok {
return Code(wrapped.Unwrap())
if err := wrapped.Unwrap(); err != nil {
return Code(err)
}
}
return status.FromContextError(err).Code()
}
@ -120,7 +125,9 @@ func AsGRPCStatus(err error) (*status.Status, bool) {
Unwrap() error
})
if ok {
return AsGRPCStatus(wrapped.Unwrap())
if err := wrapped.Unwrap(); err != nil {
return AsGRPCStatus(err)
}
}
return nil, false
@ -174,6 +181,10 @@ func FromGRPC(err error) error {
err = d.WrapError(err)
}
if err != nil {
stack.Helper()
}
return stack.Enable(err)
}
@ -182,6 +193,10 @@ type withCode struct {
error
}
func (e *withCode) Code() codes.Code {
return e.code
}
func (e *withCode) Unwrap() error {
return e.error
}

View File

@ -2,27 +2,53 @@ package grpcerrors
import (
"context"
"log"
"os"
"github.com/moby/buildkit/util/stack"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
oldErr := err
if err != nil {
stack.Helper()
err = ToGRPC(err)
}
if oldErr != nil && err == nil {
logErr := errors.Wrap(err, "invalid grpc error conversion")
if os.Getenv("BUILDKIT_DEBUG_PANIC_ON_ERROR") == "1" {
panic(logErr)
}
log.Printf("%v", logErr)
err = oldErr
}
return resp, err
}
func StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return ToGRPC(handler(srv, ss))
err := ToGRPC(handler(srv, ss))
if err != nil {
stack.Helper()
}
return err
}
func UnaryClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return FromGRPC(invoker(ctx, method, req, reply, cc, opts...))
err := FromGRPC(invoker(ctx, method, req, reply, cc, opts...))
if err != nil {
stack.Helper()
}
return err
}
func StreamClientInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
s, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
stack.Helper()
}
return s, ToGRPC(err)
}

View File

@ -0,0 +1,106 @@
package progresswriter
import (
"context"
"strings"
"sync"
"github.com/moby/buildkit/client"
"golang.org/x/sync/errgroup"
)
type MultiWriter struct {
w Writer
eg *errgroup.Group
once sync.Once
ready chan struct{}
}
func (mw *MultiWriter) WithPrefix(pfx string, force bool) Writer {
in := make(chan *client.SolveStatus)
out := mw.w.Status()
p := &prefixed{
main: mw.w,
in: in,
}
mw.eg.Go(func() error {
mw.once.Do(func() {
close(mw.ready)
})
for {
select {
case v, ok := <-in:
if ok {
if force {
for _, v := range v.Vertexes {
v.Name = addPrefix(pfx, v.Name)
}
}
out <- v
} else {
return nil
}
case <-mw.Done():
return mw.Err()
}
}
})
return p
}
func (mw *MultiWriter) Done() <-chan struct{} {
return mw.w.Done()
}
func (mw *MultiWriter) Err() error {
return mw.w.Err()
}
func (mw *MultiWriter) Status() chan *client.SolveStatus {
return nil
}
type prefixed struct {
main Writer
in chan *client.SolveStatus
}
func (p *prefixed) Done() <-chan struct{} {
return p.main.Done()
}
func (p *prefixed) Err() error {
return p.main.Err()
}
func (p *prefixed) Status() chan *client.SolveStatus {
return p.in
}
func NewMultiWriter(pw Writer) *MultiWriter {
if pw == nil {
return nil
}
eg, _ := errgroup.WithContext(context.TODO())
ready := make(chan struct{})
go func() {
<-ready
eg.Wait()
close(pw.Status())
}()
return &MultiWriter{
w: pw,
eg: eg,
ready: ready,
}
}
func addPrefix(pfx, name string) string {
if strings.HasPrefix(name, "[") {
return "[" + pfx + " " + name[1:]
}
return "[" + pfx + "] " + name
}

View File

@ -0,0 +1,94 @@
package progresswriter
import (
"context"
"os"
"github.com/containerd/console"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/pkg/errors"
)
type printer struct {
status chan *client.SolveStatus
done <-chan struct{}
err error
}
func (p *printer) Done() <-chan struct{} {
return p.done
}
func (p *printer) Err() error {
return p.err
}
func (p *printer) Status() chan *client.SolveStatus {
if p == nil {
return nil
}
return p.status
}
type tee struct {
Writer
status chan *client.SolveStatus
}
func (t *tee) Status() chan *client.SolveStatus {
return t.status
}
func Tee(w Writer, ch chan *client.SolveStatus) Writer {
st := make(chan *client.SolveStatus)
t := &tee{
status: st,
Writer: w,
}
go func() {
for v := range st {
w.Status() <- v
ch <- v
}
close(w.Status())
close(ch)
}()
return t
}
func NewPrinter(ctx context.Context, out console.File, mode string) (Writer, error) {
statusCh := make(chan *client.SolveStatus)
doneCh := make(chan struct{})
pw := &printer{
status: statusCh,
done: doneCh,
}
if v := os.Getenv("BUILDKIT_PROGRESS"); v != "" && mode == "auto" {
mode = v
}
var c console.Console
switch mode {
case "auto", "tty", "":
if cons, err := console.ConsoleFromFile(out); err == nil {
c = cons
} else {
if mode == "tty" {
return nil, errors.Wrap(err, "failed to get console")
}
}
case "plain":
default:
return nil, errors.Errorf("invalid progress mode %s", mode)
}
go func() {
// not using shared context to not disrupt display but let is finish reporting errors
pw.err = progressui.DisplaySolveStatus(ctx, "", c, out, statusCh)
close(doneCh)
}()
return pw, nil
}

View File

@ -0,0 +1,93 @@
package progresswriter
import (
"time"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/identity"
"github.com/opencontainers/go-digest"
)
type Logger func(*client.SolveStatus)
type SubLogger interface {
Wrap(name string, fn func() error) error
Log(stream int, dt []byte)
}
func Wrap(name string, l Logger, fn func(SubLogger) error) (err error) {
if l == nil {
return nil
}
dgst := digest.FromBytes([]byte(identity.NewID()))
tm := time.Now()
l(&client.SolveStatus{
Vertexes: []*client.Vertex{{
Digest: dgst,
Name: name,
Started: &tm,
}},
})
defer func() {
tm2 := time.Now()
errMsg := ""
if err != nil {
errMsg = err.Error()
}
l(&client.SolveStatus{
Vertexes: []*client.Vertex{{
Digest: dgst,
Name: name,
Started: &tm,
Completed: &tm2,
Error: errMsg,
}},
})
}()
return fn(&subLogger{dgst, l})
}
type subLogger struct {
dgst digest.Digest
logger Logger
}
func (sl *subLogger) Wrap(name string, fn func() error) (err error) {
tm := time.Now()
sl.logger(&client.SolveStatus{
Statuses: []*client.VertexStatus{{
Vertex: sl.dgst,
ID: name,
Timestamp: time.Now(),
Started: &tm,
}},
})
defer func() {
tm2 := time.Now()
sl.logger(&client.SolveStatus{
Statuses: []*client.VertexStatus{{
Vertex: sl.dgst,
ID: name,
Timestamp: time.Now(),
Started: &tm,
Completed: &tm2,
}},
})
}()
return fn()
}
func (sl *subLogger) Log(stream int, dt []byte) {
sl.logger(&client.SolveStatus{
Logs: []*client.VertexLog{{
Vertex: sl.dgst,
Stream: stream,
Data: dt,
Timestamp: time.Now(),
}},
})
}

View File

@ -0,0 +1,71 @@
package progresswriter
import (
"time"
"github.com/moby/buildkit/client"
)
func ResetTime(in Writer) Writer {
w := &pw{Writer: in, status: make(chan *client.SolveStatus), tm: time.Now()}
go func() {
for {
select {
case <-in.Done():
return
case st, ok := <-w.status:
if !ok {
close(in.Status())
return
}
if w.diff == nil {
for _, v := range st.Vertexes {
if v.Started != nil {
d := v.Started.Sub(w.tm)
w.diff = &d
}
}
}
if w.diff != nil {
for _, v := range st.Vertexes {
if v.Started != nil {
d := v.Started.Add(-*w.diff)
v.Started = &d
}
if v.Completed != nil {
d := v.Completed.Add(-*w.diff)
v.Completed = &d
}
}
for _, v := range st.Statuses {
if v.Started != nil {
d := v.Started.Add(-*w.diff)
v.Started = &d
}
if v.Completed != nil {
d := v.Completed.Add(-*w.diff)
v.Completed = &d
}
v.Timestamp = v.Timestamp.Add(-*w.diff)
}
for _, v := range st.Logs {
v.Timestamp = v.Timestamp.Add(-*w.diff)
}
}
in.Status() <- st
}
}
}()
return w
}
type pw struct {
Writer
tm time.Time
diff *time.Duration
status chan *client.SolveStatus
}
func (p *pw) Status() chan *client.SolveStatus {
return p.status
}

View File

@ -0,0 +1,46 @@
package progresswriter
import (
"time"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/identity"
"github.com/opencontainers/go-digest"
)
type Writer interface {
Done() <-chan struct{}
Err() error
Status() chan *client.SolveStatus
}
func Write(w Writer, name string, f func() error) {
status := w.Status()
dgst := digest.FromBytes([]byte(identity.NewID()))
tm := time.Now()
vtx := client.Vertex{
Digest: dgst,
Name: name,
Started: &tm,
}
status <- &client.SolveStatus{
Vertexes: []*client.Vertex{&vtx},
}
var err error
if f != nil {
err = f()
}
tm2 := time.Now()
vtx2 := vtx
vtx2.Completed = &tm2
if err != nil {
vtx2.Error = err.Error()
}
status <- &client.SolveStatus{
Vertexes: []*client.Vertex{&vtx2},
}
}

View File

@ -0,0 +1,51 @@
package sshutil
import (
"fmt"
"net"
"strconv"
"strings"
"golang.org/x/crypto/ssh"
)
const defaultPort = 22
var errCallbackDone = fmt.Errorf("callback failed on purpose")
// addDefaultPort appends a default port if hostport doesn't contain one
func addDefaultPort(hostport string, defaultPort int) string {
_, _, err := net.SplitHostPort(hostport)
if err == nil {
return hostport
}
hostport = net.JoinHostPort(hostport, strconv.Itoa(defaultPort))
return hostport
}
// SshKeyScan scans a ssh server for the hostkey; server should be in the form hostname, or hostname:port
func SSHKeyScan(server string) (string, error) {
var key string
KeyScanCallback := func(hostport string, remote net.Addr, pubKey ssh.PublicKey) error {
hostname, _, err := net.SplitHostPort(hostport)
if err != nil {
return err
}
key = strings.TrimSpace(fmt.Sprintf("%s %s", hostname, string(ssh.MarshalAuthorizedKey(pubKey))))
return errCallbackDone
}
config := &ssh.ClientConfig{
HostKeyCallback: KeyScanCallback,
}
server = addDefaultPort(server, defaultPort)
conn, err := ssh.Dial("tcp", server, config)
if key != "" {
// as long as we get the key, the function worked
err = nil
}
if conn != nil {
conn.Close()
}
return key, err
}

View File

@ -4,15 +4,22 @@ import (
"fmt"
io "io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
)
var helpers map[string]struct{}
var helpersMu sync.RWMutex
func init() {
typeurl.Register((*Stack)(nil), "github.com/moby/buildkit", "stack.Stack+json")
helpers = map[string]struct{}{}
}
var version string
@ -23,6 +30,19 @@ func SetVersionInfo(v, r string) {
revision = r
}
func Helper() {
var pc [1]uintptr
n := runtime.Callers(2, pc[:])
if n == 0 {
return
}
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
helpersMu.Lock()
helpers[frame.Function] = struct{}{}
helpersMu.Unlock()
}
func Traces(err error) []*Stack {
var st []*Stack
@ -52,6 +72,7 @@ func Enable(err error) error {
if err == nil {
return nil
}
Helper()
if !hasLocalStackTrace(err) {
return errors.WithStack(err)
}
@ -112,6 +133,8 @@ func (w *formatter) Format(s fmt.State, verb rune) {
func convertStack(s errors.StackTrace) *Stack {
var out Stack
helpersMu.RLock()
defer helpersMu.RUnlock()
for _, f := range s {
dt, err := f.MarshalText()
if err != nil {
@ -121,6 +144,9 @@ func convertStack(s errors.StackTrace) *Stack {
if len(p) != 2 {
continue
}
if _, ok := helpers[p[0]]; ok {
continue
}
idx := strings.LastIndexByte(p[1], ':')
if idx == -1 {
continue

18
vendor/github.com/moby/buildkit/util/system/path.go generated vendored Normal file
View File

@ -0,0 +1,18 @@
package system
// DefaultPathEnvUnix is unix style list of directories to search for
// executables. Each directory is separated from the next by a colon
// ':' character .
const DefaultPathEnvUnix = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
// DefaultPathEnvWindows is windows style list of directories to search for
// executables. Each directory is separated from the next by a colon
// ';' character .
const DefaultPathEnvWindows = "c:\\Windows\\System32;c:\\Windows"
func DefaultPathEnv(os string) string {
if os == "windows" {
return DefaultPathEnvWindows
}
return DefaultPathEnvUnix
}

View File

@ -2,11 +2,6 @@
package system
// DefaultPathEnv is unix style list of directories to search for
// executables. Each directory is separated from the next by a colon
// ':' character .
const DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
// CheckSystemDriveAndRemoveDriveLetter verifies that a path, if it includes a drive letter,
// is the system drive. This is a no-op on Linux.
func CheckSystemDriveAndRemoveDriveLetter(path string) (string, error) {

View File

@ -8,10 +8,6 @@ import (
"strings"
)
// DefaultPathEnv is deliberately empty on Windows as the default path will be set by
// the container. Docker has no context of what the default path should be.
const DefaultPathEnv = ""
// CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path.
// This is used, for example, when validating a user provided path in docker cp.
// If a drive letter is supplied, it must be the system drive. The drive letter