protobuf: remove gogoproto

Removes gogo/protobuf from buildx and updates to a version of
moby/buildkit where gogo is removed.

This also changes how the proto files are generated. This is because
newer versions of protobuf are more strict about name conflicts. If two
files have the same name (even if they are relative paths) and are used
in different protoc commands, they'll conflict in the registry.

Since protobuf file generation doesn't work very well with
`paths=source_relative`, this removes the `go:generate` expression and
just relies on the dockerfile to perform the generation.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2024-10-02 15:51:59 -05:00
parent 8e47387d02
commit b35a0f4718
592 changed files with 46288 additions and 110420 deletions

View File

@ -151,6 +151,7 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
gopts = append(gopts, grpc.WithStreamInterceptor(grpcerrors.StreamClientInterceptor))
gopts = append(gopts, customDialOptions...)
//nolint:staticcheck // ignore SA1019 NewClient has different behavior and needs to be tested
conn, err := grpc.DialContext(ctx, address, gopts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to dial %q . make sure buildkitd is running", address)

View File

@ -43,14 +43,20 @@ func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*Usa
ID: d.ID,
Mutable: d.Mutable,
InUse: d.InUse,
Size: d.Size_,
Size: d.Size,
Parents: d.Parents,
CreatedAt: d.CreatedAt,
CreatedAt: d.CreatedAt.AsTime(),
Description: d.Description,
UsageCount: int(d.UsageCount),
LastUsedAt: d.LastUsedAt,
RecordType: UsageRecordType(d.RecordType),
Shared: d.Shared,
LastUsedAt: func() *time.Time {
if d.LastUsedAt != nil {
ts := d.LastUsedAt.AsTime()
return &ts
}
return nil
}(),
RecordType: UsageRecordType(d.RecordType),
Shared: d.Shared,
})
}

View File

@ -8,6 +8,7 @@ import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
// DefinitionOp implements llb.Vertex using a marshalled definition.
@ -19,7 +20,7 @@ type DefinitionOp struct {
mu sync.Mutex
ops map[digest.Digest]*pb.Op
defs map[digest.Digest][]byte
metas map[digest.Digest]pb.OpMetadata
metas map[digest.Digest]*pb.OpMetadata
sources map[digest.Digest][]*SourceLocation
platforms map[digest.Digest]*ocispecs.Platform
dgst digest.Digest
@ -29,7 +30,7 @@ type DefinitionOp struct {
// NewDefinitionOp returns a new operation from a marshalled definition.
func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
if def == nil {
if def.IsNil() {
return nil, errors.New("invalid nil input definition to definition op")
}
@ -40,7 +41,7 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
var dgst digest.Digest
for _, dt := range def.Def {
var op pb.Op
if err := (&op).Unmarshal(dt); err != nil {
if err := proto.Unmarshal(dt, &op); err != nil {
return nil, errors.Wrap(err, "failed to parse llb proto op")
}
dgst = digest.FromBytes(dt)
@ -89,14 +90,19 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
var index pb.OutputIndex
if dgst != "" {
index = ops[dgst].Inputs[0].Index
dgst = ops[dgst].Inputs[0].Digest
index = pb.OutputIndex(ops[dgst].Inputs[0].Index)
dgst = digest.Digest(ops[dgst].Inputs[0].Digest)
}
metas := make(map[digest.Digest]*pb.OpMetadata, len(def.Metadata))
for k, v := range def.Metadata {
metas[digest.Digest(k)] = v
}
return &DefinitionOp{
ops: ops,
defs: defs,
metas: def.Metadata,
metas: metas,
sources: srcs,
platforms: platforms,
dgst: dgst,
@ -163,7 +169,7 @@ func (d *DefinitionOp) Marshal(ctx context.Context, c *Constraints) (digest.Dige
defer d.mu.Unlock()
meta := d.metas[d.dgst]
return d.dgst, d.defs[d.dgst], &meta, d.sources[d.dgst], nil
return d.dgst, d.defs[d.dgst], meta, d.sources[d.dgst], nil
}
func (d *DefinitionOp) Output() Output {
@ -207,7 +213,7 @@ func (d *DefinitionOp) Inputs() []Output {
for _, input := range op.Inputs {
var vtx *DefinitionOp
d.mu.Lock()
if existingIndexes, ok := d.loadInputCache(input.Digest); ok {
if existingIndexes, ok := d.loadInputCache(digest.Digest(input.Digest)); ok {
if int(input.Index) < len(existingIndexes) && existingIndexes[input.Index] != nil {
vtx = existingIndexes[input.Index]
}
@ -218,19 +224,19 @@ func (d *DefinitionOp) Inputs() []Output {
defs: d.defs,
metas: d.metas,
platforms: d.platforms,
dgst: input.Digest,
index: input.Index,
dgst: digest.Digest(input.Digest),
index: pb.OutputIndex(input.Index),
inputCache: d.inputCache,
sources: d.sources,
}
existingIndexes, _ := d.loadInputCache(input.Digest)
existingIndexes, _ := d.loadInputCache(digest.Digest(input.Digest))
indexDiff := int(input.Index) - len(existingIndexes)
if indexDiff >= 0 {
// make room in the slice for the new index being set
existingIndexes = append(existingIndexes, make([]*DefinitionOp, indexDiff+1)...)
}
existingIndexes[input.Index] = vtx
d.storeInputCache(input.Digest, existingIndexes)
d.storeInputCache(digest.Digest(input.Digest), existingIndexes)
}
d.mu.Unlock()

View File

@ -31,8 +31,8 @@ func (m *DiffOp) Validate(ctx context.Context, constraints *Constraints) error {
}
func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
if m.Cached(constraints) {
return m.Load()
if dgst, dt, md, srcs, err := m.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
if err := m.Validate(ctx, constraints); err != nil {
return "", nil, nil, nil, err
@ -43,9 +43,9 @@ func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.
op := &pb.DiffOp{}
op.Lower = &pb.LowerDiffInput{Input: pb.InputIndex(len(proto.Inputs))}
op.Lower = &pb.LowerDiffInput{Input: int64(len(proto.Inputs))}
if m.lower == nil {
op.Lower.Input = pb.Empty
op.Lower.Input = int64(pb.Empty)
} else {
pbLowerInput, err := m.lower.ToInput(ctx, constraints)
if err != nil {
@ -54,9 +54,9 @@ func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.
proto.Inputs = append(proto.Inputs, pbLowerInput)
}
op.Upper = &pb.UpperDiffInput{Input: pb.InputIndex(len(proto.Inputs))}
op.Upper = &pb.UpperDiffInput{Input: int64(len(proto.Inputs))}
if m.upper == nil {
op.Upper.Input = pb.Empty
op.Upper.Input = int64(pb.Empty)
} else {
pbUpperInput, err := m.upper.ToInput(ctx, constraints)
if err != nil {
@ -67,13 +67,12 @@ func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.
proto.Op = &pb.Op_Diff{Diff: op}
dt, err := proto.Marshal()
dt, err := deterministicMarshal(proto)
if err != nil {
return "", nil, nil, nil, err
}
m.Store(dt, md, m.constraints.SourceLocations, constraints)
return m.Load()
return m.Store(dt, md, m.constraints.SourceLocations, constraints)
}
func (m *DiffOp) Output() Output {

View File

@ -12,6 +12,7 @@ import (
"github.com/moby/buildkit/util/system"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
func NewExecOp(base State, proxyEnv *ProxyEnv, readOnly bool, c Constraints) *ExecOp {
@ -128,9 +129,10 @@ func (e *ExecOp) Validate(ctx context.Context, c *Constraints) error {
}
func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
if e.Cached(c) {
return e.Load()
if dgst, dt, md, srcs, err := e.Load(c); err == nil {
return dgst, dt, md, srcs, nil
}
if err := e.Validate(ctx, c); err != nil {
return "", nil, nil, nil, err
}
@ -193,6 +195,17 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
return "", nil, nil, nil, err
}
var validExitCodes []int32
if codes, err := getValidExitCodes(e.base)(ctx, c); err != nil {
return "", nil, nil, nil, err
} else if codes != nil {
validExitCodes = make([]int32, len(codes))
for i, code := range codes {
validExitCodes[i] = int32(code)
}
addCap(&e.constraints, pb.CapExecValidExitCode)
}
meta := &pb.Meta{
Args: args,
Env: env.ToArray(),
@ -201,6 +214,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
Hostname: hostname,
CgroupParent: cgrpParent,
RemoveMountStubsRecursive: true,
ValidExitCodes: validExitCodes,
}
extraHosts, err := getExtraHosts(e.base)(ctx, c)
@ -330,7 +344,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
newInput := true
for i, inp2 := range pop.Inputs {
if *inp == *inp2 {
if proto.Equal(inp, inp2) {
inputIndex = pb.InputIndex(i)
newInput = false
break
@ -351,10 +365,10 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
}
pm := &pb.Mount{
Input: inputIndex,
Input: int64(inputIndex),
Dest: m.target,
Readonly: m.readonly,
Output: outputIndex,
Output: int64(outputIndex),
Selector: m.selector,
}
if m.cacheID != "" {
@ -382,7 +396,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
if m.tmpfs {
pm.MountType = pb.MountType_TMPFS
pm.TmpfsOpt = &pb.TmpfsOpt{
Size_: m.tmpfsOpt.Size,
Size: m.tmpfsOpt.Size,
}
}
peo.Mounts = append(peo.Mounts, pm)
@ -398,7 +412,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
}
if s.Target != nil {
pm := &pb.Mount{
Input: pb.Empty,
Input: int64(pb.Empty),
Dest: *s.Target,
MountType: pb.MountType_SECRET,
SecretOpt: &pb.SecretOpt{
@ -415,7 +429,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
for _, s := range e.ssh {
pm := &pb.Mount{
Input: pb.Empty,
Input: int64(pb.Empty),
Dest: s.Target,
MountType: pb.MountType_SSH,
SSHOpt: &pb.SSHOpt{
@ -429,12 +443,11 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
peo.Mounts = append(peo.Mounts, pm)
}
dt, err := pop.Marshal()
dt, err := deterministicMarshal(pop)
if err != nil {
return "", nil, nil, nil, err
}
e.Store(dt, md, e.constraints.SourceLocations, c)
return e.Load()
return e.Store(dt, md, e.constraints.SourceLocations, c)
}
func (e *ExecOp) Output() Output {
@ -581,6 +594,7 @@ func Shlex(str string) RunOption {
ei.State = shlexf(str, false)(ei.State)
})
}
func Shlexf(str string, v ...interface{}) RunOption {
return runOptionFunc(func(ei *ExecInfo) {
ei.State = shlexf(str, true, v...)(ei.State)
@ -605,6 +619,12 @@ func AddUlimit(name UlimitName, soft int64, hard int64) RunOption {
})
}
func ValidExitCodes(codes ...int) RunOption {
return runOptionFunc(func(ei *ExecInfo) {
ei.State = validExitCodes(codes...)(ei.State)
})
}
func WithCgroupParent(cp string) RunOption {
return runOptionFunc(func(ei *ExecInfo) {
ei.State = ei.State.WithCgroupParent(cp)

View File

@ -12,6 +12,7 @@ import (
"github.com/moby/buildkit/solver/pb"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
// Examples:
@ -271,9 +272,11 @@ type ChownOpt struct {
func (co ChownOpt) SetMkdirOption(mi *MkdirInfo) {
mi.ChownOpt = &co
}
func (co ChownOpt) SetMkfileOption(mi *MkfileInfo) {
mi.ChownOpt = &co
}
func (co ChownOpt) SetCopyOption(mi *CopyInfo) {
mi.ChownOpt = &co
}
@ -299,7 +302,8 @@ func (up *UserOpt) marshal(base pb.InputIndex) *pb.UserOpt {
}
if up.Name != "" {
return &pb.UserOpt{User: &pb.UserOpt_ByName{ByName: &pb.NamedUserOpt{
Name: up.Name, Input: base}}}
Name: up.Name, Input: int64(base),
}}}
}
return &pb.UserOpt{User: &pb.UserOpt_ByID{ByID: uint32(up.UID)}}
}
@ -648,7 +652,7 @@ func (ms *marshalState) addInput(c *Constraints, o Output) (pb.InputIndex, error
return 0, err
}
for i, inp2 := range ms.inputs {
if *inp == *inp2 {
if proto.Equal(inp, inp2) {
return pb.InputIndex(i), nil
}
}
@ -726,9 +730,10 @@ func (ms *marshalState) add(fa *FileAction, c *Constraints) (*fileActionState, e
}
func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
if f.Cached(c) {
return f.Load()
if dgst, dt, md, srcs, err := f.Load(c); err == nil {
return dgst, dt, md, srcs, nil
}
if err := f.Validate(ctx, c); err != nil {
return "", nil, nil, nil, err
}
@ -786,17 +791,16 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
pfo.Actions = append(pfo.Actions, &pb.FileAction{
Input: getIndex(st.input, len(state.inputs), st.inputRelative),
SecondaryInput: getIndex(st.input2, len(state.inputs), st.input2Relative),
Output: output,
Output: int64(output),
Action: action,
})
}
dt, err := pop.Marshal()
dt, err := deterministicMarshal(pop)
if err != nil {
return "", nil, nil, nil, err
}
f.Store(dt, md, f.constraints.SourceLocations, c)
return f.Load()
return f.Store(dt, md, f.constraints.SourceLocations, c)
}
func normalizePath(parent, p string, keepSlash bool) string {
@ -826,9 +830,9 @@ func (f *FileOp) Inputs() []Output {
return f.action.allOutputs(map[Output]struct{}{}, []Output{})
}
func getIndex(input pb.InputIndex, len int, relative *int) pb.InputIndex {
func getIndex(input pb.InputIndex, len int, relative *int) int64 {
if relative != nil {
return pb.InputIndex(len + *relative)
return int64(len + *relative)
}
return input
return int64(input)
}

View File

@ -2,34 +2,43 @@ package llb
import (
"io"
"maps"
"sync"
cerrdefs "github.com/containerd/errdefs"
"github.com/containerd/platforms"
"github.com/moby/buildkit/solver/pb"
digest "github.com/opencontainers/go-digest"
"google.golang.org/protobuf/proto"
)
// Definition is the LLB definition structure with per-vertex metadata entries
// Corresponds to the Definition structure defined in solver/pb.Definition.
type Definition struct {
Def [][]byte
Metadata map[digest.Digest]pb.OpMetadata
Metadata map[digest.Digest]OpMetadata
Source *pb.Source
Constraints *Constraints
}
func (def *Definition) ToPB() *pb.Definition {
metas := make(map[string]*pb.OpMetadata, len(def.Metadata))
for dgst, meta := range def.Metadata {
metas[string(dgst)] = meta.ToPB()
}
return &pb.Definition{
Def: def.Def,
Source: def.Source,
Metadata: maps.Clone(def.Metadata),
Metadata: metas,
}
}
func (def *Definition) FromPB(x *pb.Definition) {
def.Def = x.Def
def.Source = x.Source
def.Metadata = maps.Clone(x.Metadata)
def.Metadata = make(map[digest.Digest]OpMetadata, len(x.Metadata))
for dgst, meta := range x.Metadata {
def.Metadata[digest.Digest(dgst)] = NewOpMetadata(meta)
}
}
func (def *Definition) Head() (digest.Digest, error) {
@ -40,14 +49,14 @@ func (def *Definition) Head() (digest.Digest, error) {
last := def.Def[len(def.Def)-1]
var pop pb.Op
if err := (&pop).Unmarshal(last); err != nil {
if err := proto.Unmarshal(last, &pop); err != nil {
return "", err
}
if len(pop.Inputs) == 0 {
return "", nil
}
return pop.Inputs[0].Digest, nil
return digest.Digest(pop.Inputs[0].Digest), nil
}
func WriteTo(def *Definition, w io.Writer) error {
@ -65,7 +74,7 @@ func ReadFrom(r io.Reader) (*Definition, error) {
return nil, err
}
var pbDef pb.Definition
if err := pbDef.Unmarshal(b); err != nil {
if err := proto.Unmarshal(b, &pbDef); err != nil {
return nil, err
}
var def Definition
@ -104,27 +113,41 @@ func MarshalConstraints(base, override *Constraints) (*pb.Op, *pb.OpMetadata) {
Constraints: &pb.WorkerConstraints{
Filter: c.WorkerConstraints,
},
}, &c.Metadata
}, c.Metadata.ToPB()
}
type MarshalCache struct {
digest digest.Digest
dt []byte
md *pb.OpMetadata
srcs []*SourceLocation
constraints *Constraints
cache sync.Map
}
func (mc *MarshalCache) Cached(c *Constraints) bool {
return mc.dt != nil && mc.constraints == c
func (mc *MarshalCache) Load(c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
v, ok := mc.cache.Load(c)
if !ok {
return "", nil, nil, nil, cerrdefs.ErrNotFound
}
res := v.(*marshalCacheResult)
return res.digest, res.dt, res.md, res.srcs, nil
}
func (mc *MarshalCache) Load() (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
return mc.digest, mc.dt, mc.md, mc.srcs, nil
func (mc *MarshalCache) Store(dt []byte, md *pb.OpMetadata, srcs []*SourceLocation, c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
res := &marshalCacheResult{
digest: digest.FromBytes(dt),
dt: dt,
md: md,
srcs: srcs,
}
mc.cache.Store(c, res)
return res.digest, res.dt, res.md, res.srcs, nil
}
func (mc *MarshalCache) Store(dt []byte, md *pb.OpMetadata, srcs []*SourceLocation, c *Constraints) {
mc.digest = digest.FromBytes(dt)
mc.dt = dt
mc.md = md
mc.constraints = c
mc.srcs = srcs
type marshalCacheResult struct {
digest digest.Digest
dt []byte
md *pb.OpMetadata
srcs []*SourceLocation
}
func deterministicMarshal[Message proto.Message](m Message) ([]byte, error) {
return proto.MarshalOptions{Deterministic: true}.Marshal(m)
}

View File

@ -32,9 +32,10 @@ func (m *MergeOp) Validate(ctx context.Context, constraints *Constraints) error
}
func (m *MergeOp) Marshal(ctx context.Context, constraints *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
if m.Cached(constraints) {
return m.Load()
if dgst, dt, md, srcs, err := m.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
if err := m.Validate(ctx, constraints); err != nil {
return "", nil, nil, nil, err
}
@ -44,7 +45,7 @@ func (m *MergeOp) Marshal(ctx context.Context, constraints *Constraints) (digest
op := &pb.MergeOp{}
for _, input := range m.inputs {
op.Inputs = append(op.Inputs, &pb.MergeInput{Input: pb.InputIndex(len(pop.Inputs))})
op.Inputs = append(op.Inputs, &pb.MergeInput{Input: int64(len(pop.Inputs))})
pbInput, err := input.ToInput(ctx, constraints)
if err != nil {
return "", nil, nil, nil, err
@ -53,13 +54,12 @@ func (m *MergeOp) Marshal(ctx context.Context, constraints *Constraints) (digest
}
pop.Op = &pb.Op_Merge{Merge: op}
dt, err := pop.Marshal()
dt, err := deterministicMarshal(pop)
if err != nil {
return "", nil, nil, nil, err
}
m.Store(dt, md, m.constraints.SourceLocations, constraints)
return m.Load()
return m.Store(dt, md, m.constraints.SourceLocations, constraints)
}
func (m *MergeOp) Output() Output {

View File

@ -18,14 +18,15 @@ import (
type contextKeyT string
var (
keyArgs = contextKeyT("llb.exec.args")
keyDir = contextKeyT("llb.exec.dir")
keyEnv = contextKeyT("llb.exec.env")
keyExtraHost = contextKeyT("llb.exec.extrahost")
keyHostname = contextKeyT("llb.exec.hostname")
keyUlimit = contextKeyT("llb.exec.ulimit")
keyCgroupParent = contextKeyT("llb.exec.cgroup.parent")
keyUser = contextKeyT("llb.exec.user")
keyArgs = contextKeyT("llb.exec.args")
keyDir = contextKeyT("llb.exec.dir")
keyEnv = contextKeyT("llb.exec.env")
keyExtraHost = contextKeyT("llb.exec.extrahost")
keyHostname = contextKeyT("llb.exec.hostname")
keyUlimit = contextKeyT("llb.exec.ulimit")
keyCgroupParent = contextKeyT("llb.exec.cgroup.parent")
keyUser = contextKeyT("llb.exec.user")
keyValidExitCodes = contextKeyT("llb.exec.validexitcodes")
keyPlatform = contextKeyT("llb.platform")
keyNetwork = contextKeyT("llb.network")
@ -165,6 +166,25 @@ func getUser(s State) func(context.Context, *Constraints) (string, error) {
}
}
func validExitCodes(codes ...int) StateOption {
return func(s State) State {
return s.WithValue(keyValidExitCodes, codes)
}
}
func getValidExitCodes(s State) func(context.Context, *Constraints) ([]int, error) {
return func(ctx context.Context, c *Constraints) ([]int, error) {
v, err := s.getValue(keyValidExitCodes)(ctx, c)
if err != nil {
return nil, err
}
if v != nil {
return v.([]int), nil
}
return nil, nil
}
}
// Hostname returns a [StateOption] which sets the hostname used for containers created by [State.Run].
// This is the equivalent of [State.Hostname]
// See [State.With] for where to use this.
@ -263,7 +283,7 @@ func ulimit(name UlimitName, soft int64, hard int64) StateOption {
if err != nil {
return nil, err
}
return append(v, pb.Ulimit{
return append(v, &pb.Ulimit{
Name: string(name),
Soft: soft,
Hard: hard,
@ -272,14 +292,14 @@ func ulimit(name UlimitName, soft int64, hard int64) StateOption {
}
}
func getUlimit(s State) func(context.Context, *Constraints) ([]pb.Ulimit, error) {
return func(ctx context.Context, c *Constraints) ([]pb.Ulimit, error) {
func getUlimit(s State) func(context.Context, *Constraints) ([]*pb.Ulimit, error) {
return func(ctx context.Context, c *Constraints) ([]*pb.Ulimit, error) {
v, err := s.getValue(keyUlimit)(ctx, c)
if err != nil {
return nil, err
}
if v != nil {
return v.([]pb.Ulimit), nil
return v.([]*pb.Ulimit), nil
}
return nil, nil
}
@ -312,6 +332,7 @@ func Network(v pb.NetMode) StateOption {
return s.WithValue(keyNetwork, v)
}
}
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)
@ -334,6 +355,7 @@ func Security(v pb.SecurityMode) StateOption {
return s.WithValue(keySecurity, v)
}
}
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)

View File

@ -49,9 +49,10 @@ func (s *SourceOp) Validate(ctx context.Context, c *Constraints) error {
}
func (s *SourceOp) Marshal(ctx context.Context, constraints *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
if s.Cached(constraints) {
return s.Load()
if dgst, dt, md, srcs, err := s.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
if err := s.Validate(ctx, constraints); err != nil {
return "", nil, nil, nil, err
}
@ -76,13 +77,12 @@ func (s *SourceOp) Marshal(ctx context.Context, constraints *Constraints) (diges
proto.Platform = nil
}
dt, err := proto.Marshal()
dt, err := deterministicMarshal(proto)
if err != nil {
return "", nil, nil, nil, err
}
s.Store(dt, md, s.constraints.SourceLocations, constraints)
return s.Load()
return s.Store(dt, md, s.constraints.SourceLocations, constraints)
}
func (s *SourceOp) Output() Output {

View File

@ -14,6 +14,7 @@ import (
"github.com/moby/buildkit/util/apicaps"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
protobuf "google.golang.org/protobuf/proto"
)
type StateOption func(State) State
@ -139,7 +140,7 @@ func (s State) SetMarshalDefaults(co ...ConstraintsOpt) State {
func (s State) Marshal(ctx context.Context, co ...ConstraintsOpt) (*Definition, error) {
c := NewConstraints(append(s.opts, co...)...)
def := &Definition{
Metadata: make(map[digest.Digest]pb.OpMetadata, 0),
Metadata: make(map[digest.Digest]OpMetadata, 0),
Constraints: c,
}
@ -157,7 +158,7 @@ func (s State) Marshal(ctx context.Context, co ...ConstraintsOpt) (*Definition,
return def, err
}
proto := &pb.Op{Inputs: []*pb.Input{inp}}
dt, err := proto.Marshal()
dt, err := protobuf.Marshal(proto)
if err != nil {
return def, err
}
@ -210,7 +211,7 @@ func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollect
}
vertexCache[v] = struct{}{}
if opMeta != nil {
def.Metadata[dgst] = mergeMetadata(def.Metadata[dgst], *opMeta)
def.Metadata[dgst] = mergeMetadata(def.Metadata[dgst], NewOpMetadata(opMeta))
}
s.Add(dgst, sls)
if _, ok := cache[dgst]; ok {
@ -509,7 +510,7 @@ func (o *output) ToInput(ctx context.Context, c *Constraints) (*pb.Input, error)
if err != nil {
return nil, err
}
return &pb.Input{Digest: dgst, Index: index}, nil
return &pb.Input{Digest: string(dgst), Index: int64(index)}, nil
}
func (o *output) Vertex(context.Context, *Constraints) Vertex {
@ -560,7 +561,7 @@ func (fn constraintsOptFunc) SetGitOption(gi *GitInfo) {
gi.applyConstraints(fn)
}
func mergeMetadata(m1, m2 pb.OpMetadata) pb.OpMetadata {
func mergeMetadata(m1, m2 OpMetadata) OpMetadata {
if m2.IgnoreCache {
m1.IgnoreCache = true
}
@ -654,12 +655,60 @@ func (cw *constraintsWrapper) applyConstraints(f func(c *Constraints)) {
type Constraints struct {
Platform *ocispecs.Platform
WorkerConstraints []string
Metadata pb.OpMetadata
Metadata OpMetadata
LocalUniqueID string
Caps *apicaps.CapSet
SourceLocations []*SourceLocation
}
// OpMetadata has a more friendly interface for pb.OpMetadata.
type OpMetadata struct {
IgnoreCache bool `json:"ignore_cache,omitempty"`
Description map[string]string `json:"description,omitempty"`
ExportCache *pb.ExportCache `json:"export_cache,omitempty"`
Caps map[apicaps.CapID]bool `json:"caps,omitempty"`
ProgressGroup *pb.ProgressGroup `json:"progress_group,omitempty"`
}
func NewOpMetadata(mpb *pb.OpMetadata) OpMetadata {
var m OpMetadata
m.FromPB(mpb)
return m
}
func (m OpMetadata) ToPB() *pb.OpMetadata {
caps := make(map[string]bool, len(m.Caps))
for k, v := range m.Caps {
caps[string(k)] = v
}
return &pb.OpMetadata{
IgnoreCache: m.IgnoreCache,
Description: m.Description,
ExportCache: m.ExportCache,
Caps: caps,
ProgressGroup: m.ProgressGroup,
}
}
func (m *OpMetadata) FromPB(mpb *pb.OpMetadata) {
if mpb == nil {
return
}
m.IgnoreCache = mpb.IgnoreCache
m.Description = mpb.Description
m.ExportCache = mpb.ExportCache
if len(mpb.Caps) > 0 {
m.Caps = make(map[apicaps.CapID]bool, len(mpb.Caps))
for k, v := range mpb.Caps {
m.Caps[apicaps.CapID(k)] = v
}
} else {
m.Caps = nil
}
m.ProgressGroup = mpb.ProgressGroup
}
func Platform(p ocispecs.Platform) ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
c.Platform = &p

View File

@ -18,7 +18,9 @@ func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOpti
req := &controlapi.PruneRequest{
Filter: info.Filter,
KeepDuration: int64(info.KeepDuration),
KeepBytes: int64(info.KeepBytes),
MinStorage: int64(info.MinStorage),
MaxStorage: int64(info.MaxStorage),
Free: int64(info.Free),
}
if info.All {
req.All = true
@ -41,14 +43,20 @@ func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOpti
ID: d.ID,
Mutable: d.Mutable,
InUse: d.InUse,
Size: d.Size_,
Size: d.Size,
Parents: d.Parents,
CreatedAt: d.CreatedAt,
CreatedAt: d.CreatedAt.AsTime(),
Description: d.Description,
UsageCount: int(d.UsageCount),
LastUsedAt: d.LastUsedAt,
RecordType: UsageRecordType(d.RecordType),
Shared: d.Shared,
LastUsedAt: func() *time.Time {
if d.LastUsedAt != nil {
ts := d.LastUsedAt.AsTime()
return &ts
}
return nil
}(),
RecordType: UsageRecordType(d.RecordType),
Shared: d.Shared,
}
}
}
@ -59,10 +67,13 @@ type PruneOption interface {
}
type PruneInfo struct {
Filter []string `json:"filter"`
All bool `json:"all"`
Filter []string `json:"filter"`
KeepDuration time.Duration `json:"keepDuration"`
KeepBytes int64 `json:"keepBytes"`
MinStorage int64 `json:"minStorage"`
MaxStorage int64 `json:"maxStorage"`
Free int64 `json:"free"`
}
type pruneOptionFunc func(*PruneInfo)
@ -75,9 +86,11 @@ var PruneAll = pruneOptionFunc(func(pi *PruneInfo) {
pi.All = true
})
func WithKeepOpt(duration time.Duration, bytes int64) PruneOption {
func WithKeepOpt(duration time.Duration, minStorage int64, maxStorage int64, free int64) PruneOption {
return pruneOptionFunc(func(pi *PruneInfo) {
pi.KeepDuration = duration
pi.KeepBytes = bytes
pi.MinStorage = minStorage
pi.MaxStorage = maxStorage
pi.Free = free
})
}

View File

@ -31,6 +31,7 @@ import (
fstypes "github.com/tonistiigi/fsutil/types"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)
type SolveOpt struct {
@ -276,8 +277,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
Frontend: opt.Frontend,
FrontendAttrs: frontendAttrs,
FrontendInputs: frontendInputs,
Cache: cacheOpt.options,
Entitlements: opt.AllowedEntitlements,
Cache: &cacheOpt.options,
Entitlements: entitlementsToPB(opt.AllowedEntitlements),
Internal: opt.Internal,
SourcePolicy: opt.SourcePolicy,
})
@ -394,7 +395,7 @@ func prepareSyncedFiles(def *llb.Definition, localMounts map[string]fsutil.FS) (
} else {
for _, dt := range def.Def {
var op pb.Op
if err := (&op).Unmarshal(dt); err != nil {
if err := proto.Unmarshal(dt, &op); err != nil {
return nil, errors.Wrap(err, "failed to parse llb proto op")
}
if src := op.GetSource(); src != nil {
@ -549,3 +550,11 @@ func prepareMounts(opt *SolveOpt) (map[string]fsutil.FS, error) {
}
return mounts, nil
}
func entitlementsToPB(entitlements []entitlements.Entitlement) []string {
clone := make([]string, len(entitlements))
for i, e := range entitlements {
clone[i] = string(e)
}
return clone
}

View File

@ -1,25 +1,30 @@
package client
import (
"time"
controlapi "github.com/moby/buildkit/api/services/control"
digest "github.com/opencontainers/go-digest"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
var emptyLogVertexSize int
func init() {
emptyLogVertex := controlapi.VertexLog{}
emptyLogVertexSize = emptyLogVertex.Size()
emptyLogVertexSize = proto.Size(&emptyLogVertex)
}
func NewSolveStatus(resp *controlapi.StatusResponse) *SolveStatus {
s := &SolveStatus{}
for _, v := range resp.Vertexes {
s.Vertexes = append(s.Vertexes, &Vertex{
Digest: v.Digest,
Inputs: v.Inputs,
Digest: digest.Digest(v.Digest),
Inputs: digestSliceFromPB(v.Inputs),
Name: v.Name,
Started: v.Started,
Completed: v.Completed,
Started: timestampFromPB(v.Started),
Completed: timestampFromPB(v.Completed),
Error: v.Error,
Cached: v.Cached,
ProgressGroup: v.ProgressGroup,
@ -28,26 +33,26 @@ func NewSolveStatus(resp *controlapi.StatusResponse) *SolveStatus {
for _, v := range resp.Statuses {
s.Statuses = append(s.Statuses, &VertexStatus{
ID: v.ID,
Vertex: v.Vertex,
Vertex: digest.Digest(v.Vertex),
Name: v.Name,
Total: v.Total,
Current: v.Current,
Timestamp: v.Timestamp,
Started: v.Started,
Completed: v.Completed,
Timestamp: v.Timestamp.AsTime(),
Started: timestampFromPB(v.Started),
Completed: timestampFromPB(v.Completed),
})
}
for _, v := range resp.Logs {
s.Logs = append(s.Logs, &VertexLog{
Vertex: v.Vertex,
Vertex: digest.Digest(v.Vertex),
Stream: int(v.Stream),
Data: v.Msg,
Timestamp: v.Timestamp,
Timestamp: v.Timestamp.AsTime(),
})
}
for _, v := range resp.Warnings {
s.Warnings = append(s.Warnings, &VertexWarning{
Vertex: v.Vertex,
Vertex: digest.Digest(v.Vertex),
Level: int(v.Level),
Short: v.Short,
Detail: v.Detail,
@ -66,11 +71,11 @@ func (ss *SolveStatus) Marshal() (out []*controlapi.StatusResponse) {
sr := controlapi.StatusResponse{}
for _, v := range ss.Vertexes {
sr.Vertexes = append(sr.Vertexes, &controlapi.Vertex{
Digest: v.Digest,
Inputs: v.Inputs,
Digest: string(v.Digest),
Inputs: digestSliceToPB(v.Inputs),
Name: v.Name,
Started: v.Started,
Completed: v.Completed,
Started: timestampToPB(v.Started),
Completed: timestampToPB(v.Completed),
Error: v.Error,
Cached: v.Cached,
ProgressGroup: v.ProgressGroup,
@ -79,21 +84,21 @@ func (ss *SolveStatus) Marshal() (out []*controlapi.StatusResponse) {
for _, v := range ss.Statuses {
sr.Statuses = append(sr.Statuses, &controlapi.VertexStatus{
ID: v.ID,
Vertex: v.Vertex,
Vertex: string(v.Vertex),
Name: v.Name,
Current: v.Current,
Total: v.Total,
Timestamp: v.Timestamp,
Started: v.Started,
Completed: v.Completed,
Timestamp: timestamppb.New(v.Timestamp),
Started: timestampToPB(v.Started),
Completed: timestampToPB(v.Completed),
})
}
for i, v := range ss.Logs {
sr.Logs = append(sr.Logs, &controlapi.VertexLog{
Vertex: v.Vertex,
Vertex: string(v.Vertex),
Stream: int64(v.Stream),
Msg: v.Data,
Timestamp: v.Timestamp,
Timestamp: timestamppb.New(v.Timestamp),
})
logSize += len(v.Data) + emptyLogVertexSize
// avoid logs growing big and split apart if they do
@ -107,7 +112,7 @@ func (ss *SolveStatus) Marshal() (out []*controlapi.StatusResponse) {
}
for _, v := range ss.Warnings {
sr.Warnings = append(sr.Warnings, &controlapi.VertexWarning{
Vertex: v.Vertex,
Vertex: string(v.Vertex),
Level: int64(v.Level),
Short: v.Short,
Detail: v.Detail,
@ -123,3 +128,34 @@ func (ss *SolveStatus) Marshal() (out []*controlapi.StatusResponse) {
}
return
}
func digestSliceFromPB(elems []string) []digest.Digest {
clone := make([]digest.Digest, len(elems))
for i, e := range elems {
clone[i] = digest.Digest(e)
}
return clone
}
func digestSliceToPB(elems []digest.Digest) []string {
clone := make([]string, len(elems))
for i, e := range elems {
clone[i] = string(e)
}
return clone
}
func timestampFromPB(ts *timestamppb.Timestamp) *time.Time {
if ts != nil {
t := ts.AsTime()
return &t
}
return nil
}
func timestampToPB(ts *time.Time) *timestamppb.Timestamp {
if ts != nil {
return timestamppb.New(*ts)
}
return nil
}

View File

@ -65,7 +65,9 @@ func fromAPIGCPolicy(in []*apitypes.GCPolicy) []PruneInfo {
All: p.All,
Filter: p.Filters,
KeepDuration: time.Duration(p.KeepDuration),
KeepBytes: p.KeepBytes,
MinStorage: p.MinStorage,
MaxStorage: p.MaxStorage,
Free: p.Free,
})
}
return out