Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions client/llb/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cerrdefs "github.com/containerd/errdefs"
"github.com/containerd/platforms"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/solver/pb"
digest "github.com/opencontainers/go-digest"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -109,12 +110,16 @@ func MarshalConstraints(base, override *Constraints) (*pb.Op, *pb.OpMetadata) {
opPlatform.OSFeatures = slices.Clone(c.Platform.OSFeatures)
}

return &pb.Op{
op := &pb.Op{
Platform: &opPlatform,
Constraints: &pb.WorkerConstraints{
Filter: c.WorkerConstraints,
},
}, c.Metadata.ToPB()
}
if c.GenerateIdentities {
op.Identity = identity.NewID()
}
return op, c.Metadata.ToPB()
}

type MarshalCache struct {
Expand Down
21 changes: 15 additions & 6 deletions client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,14 @@ func WithCaps(caps apicaps.CapSet) ConstraintsOpt {
})
}

// WithIdentities will add an identity to this operation that
// will force it to be unique.
func WithIdentities() ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
c.GenerateIdentities = true
})
}

type constraintsWrapper struct {
Constraints
}
Expand All @@ -653,12 +661,13 @@ func (cw *constraintsWrapper) applyConstraints(f func(c *Constraints)) {
}

type Constraints struct {
Platform *ocispecs.Platform
WorkerConstraints []string
Metadata OpMetadata
LocalUniqueID string
Caps *apicaps.CapSet
SourceLocations []*SourceLocation
Platform *ocispecs.Platform
WorkerConstraints []string
Metadata OpMetadata
LocalUniqueID string
Caps *apicaps.CapSet
SourceLocations []*SourceLocation
GenerateIdentities bool
}

// OpMetadata has a more friendly interface for pb.OpMetadata.
Expand Down
20 changes: 18 additions & 2 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builder

import (
"context"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -30,7 +31,8 @@ import (
const (
// Don't forget to update frontend documentation if you add
// a new build-arg: frontend/dockerfile/docs/reference.md
keySyntaxArg = "build-arg:BUILDKIT_SYNTAX"
keySyntaxArg = "build-arg:BUILDKIT_SYNTAX"
keyWithIdentities = "build-arg:BUILDKIT_WITH_IDENTITIES"
)

func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
Expand Down Expand Up @@ -67,6 +69,15 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
}
}

var withIdentities bool
if v, ok := opts[keyWithIdentities]; ok {
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Errorf("invalid boolean value for with identities: %s", v)
}
withIdentities = b
}

if capsError != nil {
return nil, capsError
}
Expand Down Expand Up @@ -138,7 +149,12 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
return nil, nil, nil, err
}

def, err := st.Marshal(ctx)
var co []llb.ConstraintsOpt
if withIdentities {
co = append(co, llb.WithIdentities())
}

def, err := st.Marshal(ctx, co...)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "failed to marshal LLB definition")
}
Expand Down
1 change: 1 addition & 0 deletions frontend/dockerfile/docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,7 @@ RUN echo "I'm building for $TARGETPLATFORM"
| `BUILDKIT_MULTI_PLATFORM` | Bool | Opt into deterministic output regardless of multi-platform output or not. |
| `BUILDKIT_SANDBOX_HOSTNAME` | String | Set the hostname (default `buildkitsandbox`) |
| `BUILDKIT_SYNTAX` | String | Set frontend image |
| `BUILDKIT_WITH_IDENTITIES` | Bool | Add unique ids for each LLB operation to prevent collisions. |
| `SOURCE_DATE_EPOCH` | Int | Set the Unix timestamp for created image and layers. More info from [reproducible builds](https://reproducible-builds.org/docs/source-date-epoch/). Supported since Dockerfile 1.5, BuildKit 0.11 |

#### Example: keep `.git` dir
Expand Down
3 changes: 3 additions & 0 deletions solver/pb/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type jsonOp struct {
}
Platform *Platform `json:"platform,omitempty"`
Constraints *WorkerConstraints `json:"constraints,omitempty"`
Identity string `json:"identity,omitempty"`
}

func (m *Op) MarshalJSON() ([]byte, error) {
Expand All @@ -35,6 +36,7 @@ func (m *Op) MarshalJSON() ([]byte, error) {
}
v.Platform = m.Platform
v.Constraints = m.Constraints
v.Identity = m.Identity
return json.Marshal(v)
}

Expand All @@ -61,6 +63,7 @@ func (m *Op) UnmarshalJSON(data []byte) error {
}
m.Platform = v.Platform
m.Constraints = v.Constraints
m.Identity = v.Identity
return nil
}

Expand Down
13 changes: 11 additions & 2 deletions solver/pb/ops.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions solver/pb/ops.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message Op {
}
Platform platform = 10;
WorkerConstraints constraints = 11;
string identity = 12;
}

// Platform is github.com/opencontainers/image-spec/specs-go/v1.Platform
Expand Down
47 changes: 47 additions & 0 deletions solver/pb/ops_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.