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

@ -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