Merge pull request #2601 from tonistiigi/init-fixes

Improvements based on inittrace
This commit is contained in:
Tõnis Tiigi 2024-07-24 10:52:25 -07:00 committed by GitHub
commit 3d542f3d31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 232 additions and 189 deletions

View File

@ -75,7 +75,12 @@ type WithGetName interface {
GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(hcl.Expression) hcl.Diagnostics) (string, error) GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(hcl.Expression) hcl.Diagnostics) (string, error)
} }
var errUndefined = errors.New("undefined") // errUndefined is returned when a variable or function is not defined.
type errUndefined struct{}
func (errUndefined) Error() string {
return "undefined"
}
func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map[string]struct{}, allowMissing bool) hcl.Diagnostics { func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map[string]struct{}, allowMissing bool) hcl.Diagnostics {
fns, hcldiags := funcCalls(exp) fns, hcldiags := funcCalls(exp)
@ -85,7 +90,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map
for _, fn := range fns { for _, fn := range fns {
if err := p.resolveFunction(ectx, fn); err != nil { if err := p.resolveFunction(ectx, fn); err != nil {
if allowMissing && errors.Is(err, errUndefined) { if allowMissing && errors.Is(err, errUndefined{}) {
continue continue
} }
return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr())
@ -139,7 +144,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map
} }
for _, block := range blocks { for _, block := range blocks {
if err := p.resolveBlock(block, target); err != nil { if err := p.resolveBlock(block, target); err != nil {
if allowMissing && errors.Is(err, errUndefined) { if allowMissing && errors.Is(err, errUndefined{}) {
continue continue
} }
return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr())
@ -147,7 +152,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map
} }
} else { } else {
if err := p.resolveValue(ectx, v.RootName()); err != nil { if err := p.resolveValue(ectx, v.RootName()); err != nil {
if allowMissing && errors.Is(err, errUndefined) { if allowMissing && errors.Is(err, errUndefined{}) {
continue continue
} }
return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr())
@ -169,7 +174,7 @@ func (p *parser) resolveFunction(ectx *hcl.EvalContext, name string) error {
} }
f, ok := p.funcs[name] f, ok := p.funcs[name]
if !ok { if !ok {
return errors.Wrapf(errUndefined, "function %q does not exist", name) return errors.Wrapf(errUndefined{}, "function %q does not exist", name)
} }
if _, ok := p.progressF[key(ectx, name)]; ok { if _, ok := p.progressF[key(ectx, name)]; ok {
return errors.Errorf("function cycle not allowed for %s", name) return errors.Errorf("function cycle not allowed for %s", name)
@ -259,7 +264,7 @@ func (p *parser) resolveValue(ectx *hcl.EvalContext, name string) (err error) {
if _, builtin := p.opt.Vars[name]; !ok && !builtin { if _, builtin := p.opt.Vars[name]; !ok && !builtin {
vr, ok := p.vars[name] vr, ok := p.vars[name]
if !ok { if !ok {
return errors.Wrapf(errUndefined, "variable %q does not exist", name) return errors.Wrapf(errUndefined{}, "variable %q does not exist", name)
} }
def = vr.Default def = vr.Default
ectx = p.ectx ectx = p.ectx

View File

@ -1,6 +1,7 @@
package hclparser package hclparser
import ( import (
"errors"
"time" "time"
"github.com/hashicorp/go-cty-funcs/cidr" "github.com/hashicorp/go-cty-funcs/cidr"
@ -9,174 +10,187 @@ import (
"github.com/hashicorp/go-cty-funcs/uuid" "github.com/hashicorp/go-cty-funcs/uuid"
"github.com/hashicorp/hcl/v2/ext/tryfunc" "github.com/hashicorp/hcl/v2/ext/tryfunc"
"github.com/hashicorp/hcl/v2/ext/typeexpr" "github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib" "github.com/zclconf/go-cty/cty/function/stdlib"
) )
var stdlibFunctions = map[string]function.Function{ type funcDef struct {
"absolute": stdlib.AbsoluteFunc, name string
"add": stdlib.AddFunc, fn function.Function
"and": stdlib.AndFunc, factory func() function.Function
"base64decode": encoding.Base64DecodeFunc, }
"base64encode": encoding.Base64EncodeFunc,
"bcrypt": crypto.BcryptFunc, var stdlibFunctions = []funcDef{
"byteslen": stdlib.BytesLenFunc, {name: "absolute", fn: stdlib.AbsoluteFunc},
"bytesslice": stdlib.BytesSliceFunc, {name: "add", fn: stdlib.AddFunc},
"can": tryfunc.CanFunc, {name: "and", fn: stdlib.AndFunc},
"ceil": stdlib.CeilFunc, {name: "base64decode", fn: encoding.Base64DecodeFunc},
"chomp": stdlib.ChompFunc, {name: "base64encode", fn: encoding.Base64EncodeFunc},
"chunklist": stdlib.ChunklistFunc, {name: "bcrypt", fn: crypto.BcryptFunc},
"cidrhost": cidr.HostFunc, {name: "byteslen", fn: stdlib.BytesLenFunc},
"cidrnetmask": cidr.NetmaskFunc, {name: "bytesslice", fn: stdlib.BytesSliceFunc},
"cidrsubnet": cidr.SubnetFunc, {name: "can", fn: tryfunc.CanFunc},
"cidrsubnets": cidr.SubnetsFunc, {name: "ceil", fn: stdlib.CeilFunc},
"coalesce": stdlib.CoalesceFunc, {name: "chomp", fn: stdlib.ChompFunc},
"coalescelist": stdlib.CoalesceListFunc, {name: "chunklist", fn: stdlib.ChunklistFunc},
"compact": stdlib.CompactFunc, {name: "cidrhost", fn: cidr.HostFunc},
"concat": stdlib.ConcatFunc, {name: "cidrnetmask", fn: cidr.NetmaskFunc},
"contains": stdlib.ContainsFunc, {name: "cidrsubnet", fn: cidr.SubnetFunc},
"convert": typeexpr.ConvertFunc, {name: "cidrsubnets", fn: cidr.SubnetsFunc},
"csvdecode": stdlib.CSVDecodeFunc, {name: "coalesce", fn: stdlib.CoalesceFunc},
"distinct": stdlib.DistinctFunc, {name: "coalescelist", fn: stdlib.CoalesceListFunc},
"divide": stdlib.DivideFunc, {name: "compact", fn: stdlib.CompactFunc},
"element": stdlib.ElementFunc, {name: "concat", fn: stdlib.ConcatFunc},
"equal": stdlib.EqualFunc, {name: "contains", fn: stdlib.ContainsFunc},
"flatten": stdlib.FlattenFunc, {name: "convert", fn: typeexpr.ConvertFunc},
"floor": stdlib.FloorFunc, {name: "csvdecode", fn: stdlib.CSVDecodeFunc},
"format": stdlib.FormatFunc, {name: "distinct", fn: stdlib.DistinctFunc},
"formatdate": stdlib.FormatDateFunc, {name: "divide", fn: stdlib.DivideFunc},
"formatlist": stdlib.FormatListFunc, {name: "element", fn: stdlib.ElementFunc},
"greaterthan": stdlib.GreaterThanFunc, {name: "equal", fn: stdlib.EqualFunc},
"greaterthanorequalto": stdlib.GreaterThanOrEqualToFunc, {name: "flatten", fn: stdlib.FlattenFunc},
"hasindex": stdlib.HasIndexFunc, {name: "floor", fn: stdlib.FloorFunc},
"indent": stdlib.IndentFunc, {name: "format", fn: stdlib.FormatFunc},
"index": stdlib.IndexFunc, {name: "formatdate", fn: stdlib.FormatDateFunc},
"indexof": indexOfFunc, {name: "formatlist", fn: stdlib.FormatListFunc},
"int": stdlib.IntFunc, {name: "greaterthan", fn: stdlib.GreaterThanFunc},
"join": stdlib.JoinFunc, {name: "greaterthanorequalto", fn: stdlib.GreaterThanOrEqualToFunc},
"jsondecode": stdlib.JSONDecodeFunc, {name: "hasindex", fn: stdlib.HasIndexFunc},
"jsonencode": stdlib.JSONEncodeFunc, {name: "indent", fn: stdlib.IndentFunc},
"keys": stdlib.KeysFunc, {name: "index", fn: stdlib.IndexFunc},
"length": stdlib.LengthFunc, {name: "indexof", factory: indexOfFunc},
"lessthan": stdlib.LessThanFunc, {name: "int", fn: stdlib.IntFunc},
"lessthanorequalto": stdlib.LessThanOrEqualToFunc, {name: "join", fn: stdlib.JoinFunc},
"log": stdlib.LogFunc, {name: "jsondecode", fn: stdlib.JSONDecodeFunc},
"lookup": stdlib.LookupFunc, {name: "jsonencode", fn: stdlib.JSONEncodeFunc},
"lower": stdlib.LowerFunc, {name: "keys", fn: stdlib.KeysFunc},
"max": stdlib.MaxFunc, {name: "length", fn: stdlib.LengthFunc},
"md5": crypto.Md5Func, {name: "lessthan", fn: stdlib.LessThanFunc},
"merge": stdlib.MergeFunc, {name: "lessthanorequalto", fn: stdlib.LessThanOrEqualToFunc},
"min": stdlib.MinFunc, {name: "log", fn: stdlib.LogFunc},
"modulo": stdlib.ModuloFunc, {name: "lookup", fn: stdlib.LookupFunc},
"multiply": stdlib.MultiplyFunc, {name: "lower", fn: stdlib.LowerFunc},
"negate": stdlib.NegateFunc, {name: "max", fn: stdlib.MaxFunc},
"not": stdlib.NotFunc, {name: "md5", fn: crypto.Md5Func},
"notequal": stdlib.NotEqualFunc, {name: "merge", fn: stdlib.MergeFunc},
"or": stdlib.OrFunc, {name: "min", fn: stdlib.MinFunc},
"parseint": stdlib.ParseIntFunc, {name: "modulo", fn: stdlib.ModuloFunc},
"pow": stdlib.PowFunc, {name: "multiply", fn: stdlib.MultiplyFunc},
"range": stdlib.RangeFunc, {name: "negate", fn: stdlib.NegateFunc},
"regex_replace": stdlib.RegexReplaceFunc, {name: "not", fn: stdlib.NotFunc},
"regex": stdlib.RegexFunc, {name: "notequal", fn: stdlib.NotEqualFunc},
"regexall": stdlib.RegexAllFunc, {name: "or", fn: stdlib.OrFunc},
"replace": stdlib.ReplaceFunc, {name: "parseint", fn: stdlib.ParseIntFunc},
"reverse": stdlib.ReverseFunc, {name: "pow", fn: stdlib.PowFunc},
"reverselist": stdlib.ReverseListFunc, {name: "range", fn: stdlib.RangeFunc},
"rsadecrypt": crypto.RsaDecryptFunc, {name: "regex_replace", fn: stdlib.RegexReplaceFunc},
"sethaselement": stdlib.SetHasElementFunc, {name: "regex", fn: stdlib.RegexFunc},
"setintersection": stdlib.SetIntersectionFunc, {name: "regexall", fn: stdlib.RegexAllFunc},
"setproduct": stdlib.SetProductFunc, {name: "replace", fn: stdlib.ReplaceFunc},
"setsubtract": stdlib.SetSubtractFunc, {name: "reverse", fn: stdlib.ReverseFunc},
"setsymmetricdifference": stdlib.SetSymmetricDifferenceFunc, {name: "reverselist", fn: stdlib.ReverseListFunc},
"setunion": stdlib.SetUnionFunc, {name: "rsadecrypt", fn: crypto.RsaDecryptFunc},
"sha1": crypto.Sha1Func, {name: "sethaselement", fn: stdlib.SetHasElementFunc},
"sha256": crypto.Sha256Func, {name: "setintersection", fn: stdlib.SetIntersectionFunc},
"sha512": crypto.Sha512Func, {name: "setproduct", fn: stdlib.SetProductFunc},
"signum": stdlib.SignumFunc, {name: "setsubtract", fn: stdlib.SetSubtractFunc},
"slice": stdlib.SliceFunc, {name: "setsymmetricdifference", fn: stdlib.SetSymmetricDifferenceFunc},
"sort": stdlib.SortFunc, {name: "setunion", fn: stdlib.SetUnionFunc},
"split": stdlib.SplitFunc, {name: "sha1", fn: crypto.Sha1Func},
"strlen": stdlib.StrlenFunc, {name: "sha256", fn: crypto.Sha256Func},
"substr": stdlib.SubstrFunc, {name: "sha512", fn: crypto.Sha512Func},
"subtract": stdlib.SubtractFunc, {name: "signum", fn: stdlib.SignumFunc},
"timeadd": stdlib.TimeAddFunc, {name: "slice", fn: stdlib.SliceFunc},
"timestamp": timestampFunc, {name: "sort", fn: stdlib.SortFunc},
"title": stdlib.TitleFunc, {name: "split", fn: stdlib.SplitFunc},
"trim": stdlib.TrimFunc, {name: "strlen", fn: stdlib.StrlenFunc},
"trimprefix": stdlib.TrimPrefixFunc, {name: "substr", fn: stdlib.SubstrFunc},
"trimspace": stdlib.TrimSpaceFunc, {name: "subtract", fn: stdlib.SubtractFunc},
"trimsuffix": stdlib.TrimSuffixFunc, {name: "timeadd", fn: stdlib.TimeAddFunc},
"try": tryfunc.TryFunc, {name: "timestamp", factory: timestampFunc},
"upper": stdlib.UpperFunc, {name: "title", fn: stdlib.TitleFunc},
"urlencode": encoding.URLEncodeFunc, {name: "trim", fn: stdlib.TrimFunc},
"uuidv4": uuid.V4Func, {name: "trimprefix", fn: stdlib.TrimPrefixFunc},
"uuidv5": uuid.V5Func, {name: "trimspace", fn: stdlib.TrimSpaceFunc},
"values": stdlib.ValuesFunc, {name: "trimsuffix", fn: stdlib.TrimSuffixFunc},
"zipmap": stdlib.ZipmapFunc, {name: "try", fn: tryfunc.TryFunc},
{name: "upper", fn: stdlib.UpperFunc},
{name: "urlencode", fn: encoding.URLEncodeFunc},
{name: "uuidv4", fn: uuid.V4Func},
{name: "uuidv5", fn: uuid.V5Func},
{name: "values", fn: stdlib.ValuesFunc},
{name: "zipmap", fn: stdlib.ZipmapFunc},
} }
// indexOfFunc constructs a function that finds the element index for a given // indexOfFunc constructs a function that finds the element index for a given
// value in a list. // value in a list.
var indexOfFunc = function.New(&function.Spec{ func indexOfFunc() function.Function {
Params: []function.Parameter{ return function.New(&function.Spec{
{ Params: []function.Parameter{
Name: "list", {
Type: cty.DynamicPseudoType, Name: "list",
Type: cty.DynamicPseudoType,
},
{
Name: "value",
Type: cty.DynamicPseudoType,
},
}, },
{ Type: function.StaticReturnType(cty.Number),
Name: "value", Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
Type: cty.DynamicPseudoType, if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
}, return cty.NilVal, errors.New("argument must be a list or tuple")
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
return cty.NilVal, errors.New("argument must be a list or tuple")
}
if !args[0].IsKnown() {
return cty.UnknownVal(cty.Number), nil
}
if args[0].LengthInt() == 0 { // Easy path
return cty.NilVal, errors.New("cannot search an empty list")
}
for it := args[0].ElementIterator(); it.Next(); {
i, v := it.Element()
eq, err := stdlib.Equal(v, args[1])
if err != nil {
return cty.NilVal, err
} }
if !eq.IsKnown() {
if !args[0].IsKnown() {
return cty.UnknownVal(cty.Number), nil return cty.UnknownVal(cty.Number), nil
} }
if eq.True() {
return i, nil
}
}
return cty.NilVal, errors.New("item not found")
}, if args[0].LengthInt() == 0 { // Easy path
}) return cty.NilVal, errors.New("cannot search an empty list")
}
for it := args[0].ElementIterator(); it.Next(); {
i, v := it.Element()
eq, err := stdlib.Equal(v, args[1])
if err != nil {
return cty.NilVal, err
}
if !eq.IsKnown() {
return cty.UnknownVal(cty.Number), nil
}
if eq.True() {
return i, nil
}
}
return cty.NilVal, errors.New("item not found")
},
})
}
// timestampFunc constructs a function that returns a string representation of the current date and time. // timestampFunc constructs a function that returns a string representation of the current date and time.
// //
// This function was imported from terraform's datetime utilities. // This function was imported from terraform's datetime utilities.
var timestampFunc = function.New(&function.Spec{ func timestampFunc() function.Function {
Params: []function.Parameter{}, return function.New(&function.Spec{
Type: function.StaticReturnType(cty.String), Params: []function.Parameter{},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { Type: function.StaticReturnType(cty.String),
return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
}, return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil
}) },
})
}
func Stdlib() map[string]function.Function { func Stdlib() map[string]function.Function {
funcs := make(map[string]function.Function, len(stdlibFunctions)) funcs := make(map[string]function.Function, len(stdlibFunctions))
for k, v := range stdlibFunctions { for _, v := range stdlibFunctions {
funcs[k] = v if v.factory != nil {
funcs[v.name] = v.factory()
} else {
funcs[v.name] = v.fn
}
} }
return funcs return funcs
} }

View File

@ -34,7 +34,7 @@ func TestIndexOf(t *testing.T) {
for name, test := range tests { for name, test := range tests {
name, test := name, test name, test := name, test
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
got, err := indexOfFunc.Call([]cty.Value{test.input, test.key}) got, err := indexOfFunc().Call([]cty.Value{test.input, test.key})
if err != nil { if err != nil {
if test.wantErr { if test.wantErr {
return return

View File

@ -48,11 +48,6 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
var (
errStdinConflict = errors.New("invalid argument: can't use stdin for both build context and dockerfile")
errDockerfileConflict = errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")
)
const ( const (
printFallbackImage = "docker/dockerfile:1.5@sha256:dbbd5e059e8a07ff7ea6233b213b36aa516b4c53c645f1817a4dd18b83cbea56" printFallbackImage = "docker/dockerfile:1.5@sha256:dbbd5e059e8a07ff7ea6233b213b36aa516b4c53c645f1817a4dd18b83cbea56"
printLintFallbackImage = "docker.io/docker/dockerfile-upstream:1.8.1@sha256:e87caa74dcb7d46cd820352bfea12591f3dba3ddc4285e19c7dcd13359f7cefd" printLintFallbackImage = "docker.io/docker/dockerfile-upstream:1.8.1@sha256:e87caa74dcb7d46cd820352bfea12591f3dba3ddc4285e19c7dcd13359f7cefd"

View File

@ -379,7 +379,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
target.FrontendInputs["dockerfile"] = *inp.ContextState target.FrontendInputs["dockerfile"] = *inp.ContextState
case inp.ContextPath == "-": case inp.ContextPath == "-":
if inp.DockerfilePath == "-" { if inp.DockerfilePath == "-" {
return nil, errStdinConflict return nil, errors.Errorf("invalid argument: can't use stdin for both build context and dockerfile")
} }
buf := bufio.NewReader(inp.InStream) buf := bufio.NewReader(inp.InStream)
@ -395,7 +395,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
target.Session = append(target.Session, up) target.Session = append(target.Session, up)
} else { } else {
if inp.DockerfilePath != "" { if inp.DockerfilePath != "" {
return nil, errDockerfileConflict return nil, errors.Errorf("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")
} }
// stdin is dockerfile // stdin is dockerfile
dockerfileReader = buf dockerfileReader = buf

View File

@ -207,6 +207,7 @@ func attachIO(ctx context.Context, stream msgStream, initMessage *pb.InitMessage
if cfg.signal != nil { if cfg.signal != nil {
eg.Go(func() error { eg.Go(func() error {
names := signalNames()
for { for {
var sig syscall.Signal var sig syscall.Signal
select { select {
@ -216,7 +217,7 @@ func attachIO(ctx context.Context, stream msgStream, initMessage *pb.InitMessage
case <-ctx.Done(): case <-ctx.Done():
return nil return nil
} }
name := sigToName[sig] name := names[sig]
if name == "" { if name == "" {
continue continue
} }
@ -380,12 +381,12 @@ func copyToStream(fd uint32, snd msgStream, r io.Reader) error {
}) })
} }
var sigToName = map[syscall.Signal]string{} func signalNames() map[syscall.Signal]string {
m := make(map[syscall.Signal]string, len(signal.SignalMap))
func init() {
for name, value := range signal.SignalMap { for name, value := range signal.SignalMap {
sigToName[value] = name m[value] = name
} }
return m
} }
type debugStream struct { type debugStream struct {

View File

@ -29,7 +29,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
_, err := d.DockerAPI.ServerVersion(ctx) _, err := d.DockerAPI.ServerVersion(ctx)
if err != nil { if err != nil {
return nil, errors.Wrapf(driver.ErrNotConnecting, err.Error()) return nil, errors.Wrapf(driver.ErrNotConnecting{}, err.Error())
} }
return &driver.Info{ return &driver.Info{
Status: driver.Running, Status: driver.Running,
@ -39,7 +39,7 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
func (d *Driver) Version(ctx context.Context) (string, error) { func (d *Driver) Version(ctx context.Context) (string, error) {
v, err := d.DockerAPI.ServerVersion(ctx) v, err := d.DockerAPI.ServerVersion(ctx)
if err != nil { if err != nil {
return "", errors.Wrapf(driver.ErrNotConnecting, err.Error()) return "", errors.Wrapf(driver.ErrNotConnecting{}, err.Error())
} }
if bkversion, _ := resolveBuildKitVersion(v.Version); bkversion != "" { if bkversion, _ := resolveBuildKitVersion(v.Version); bkversion != "" {
return bkversion, nil return bkversion, nil

View File

@ -14,8 +14,17 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var ErrNotRunning = errors.Errorf("driver not running") type ErrNotRunning struct{}
var ErrNotConnecting = errors.Errorf("driver not connecting")
func (ErrNotRunning) Error() string {
return "driver not running"
}
type ErrNotConnecting struct{}
func (ErrNotConnecting) Error() string {
return "driver not connecting"
}
type Status int type Status int
@ -105,7 +114,7 @@ func Boot(ctx, clientContext context.Context, d *DriverHandle, pw progress.Write
c, err := d.Client(clientContext) c, err := d.Client(clientContext)
if err != nil { if err != nil {
if errors.Cause(err) == ErrNotRunning && try <= 2 { if errors.Is(err, ErrNotRunning{}) && try <= 2 {
continue continue
} }
return nil, err return nil, err

View File

@ -7,7 +7,6 @@ import (
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
@ -53,10 +52,17 @@ const (
LabelApp = "app" LabelApp = "app"
) )
var ( type ErrReservedAnnotationPlatform struct{}
ErrReservedAnnotationPlatform = errors.Errorf("the annotation \"%s\" is reserved and cannot be customized", AnnotationPlatform)
ErrReservedLabelApp = errors.Errorf("the label \"%s\" is reserved and cannot be customized", LabelApp) func (ErrReservedAnnotationPlatform) Error() string {
) return fmt.Sprintf("the annotation %q is reserved and cannot be customized", AnnotationPlatform)
}
type ErrReservedLabelApp struct{}
func (ErrReservedLabelApp) Error() string {
return fmt.Sprintf("the label %q is reserved and cannot be customized", LabelApp)
}
func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.ConfigMap, err error) { func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.ConfigMap, err error) {
labels := map[string]string{ labels := map[string]string{
@ -73,14 +79,14 @@ func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.Config
for k, v := range opt.CustomAnnotations { for k, v := range opt.CustomAnnotations {
if k == AnnotationPlatform { if k == AnnotationPlatform {
return nil, nil, ErrReservedAnnotationPlatform return nil, nil, ErrReservedAnnotationPlatform{}
} }
annotations[k] = v annotations[k] = v
} }
for k, v := range opt.CustomLabels { for k, v := range opt.CustomLabels {
if k == LabelApp { if k == LabelApp {
return nil, nil, ErrReservedLabelApp return nil, nil, ErrReservedLabelApp{}
} }
labels[k] = v labels[k] = v
} }

View File

@ -1,7 +1,7 @@
//go:build !windows //go:build !windows
// +build !windows // +build !windows
package remote package remoteutil
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package remote package remoteutil
import ( import (
"context" "context"

View File

@ -1,18 +1,19 @@
package remote package remoteutil
import ( import (
"net/url" "net/url"
"slices"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var schemes = map[string]struct{}{ var schemes = []string{
"tcp": {}, "docker-container",
"unix": {}, "kube-pod",
"ssh": {}, "npipe",
"docker-container": {}, "ssh",
"kube-pod": {}, "tcp",
"npipe": {}, "unix",
} }
func IsValidEndpoint(ep string) error { func IsValidEndpoint(ep string) error {
@ -20,7 +21,7 @@ func IsValidEndpoint(ep string) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to parse endpoint %s", ep) return errors.Wrapf(err, "failed to parse endpoint %s", ep)
} }
if _, ok := schemes[endpoint.Scheme]; !ok { if _, ok := slices.BinarySearch(schemes, endpoint.Scheme); !ok {
return errors.Errorf("unrecognized url scheme %s", endpoint.Scheme) return errors.Errorf("unrecognized url scheme %s", endpoint.Scheme)
} }
return nil return nil

View File

@ -0,0 +1,12 @@
package remoteutil
import (
"slices"
"testing"
"github.com/stretchr/testify/require"
)
func TestSchemes(t *testing.T) {
require.True(t, slices.IsSorted(schemes))
}