vendor: update buildkit to v0.19.0-rc1

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2025-01-14 14:20:26 -08:00
parent 630066bfc5
commit 44fa243d58
1910 changed files with 95196 additions and 50438 deletions

View File

@ -10,7 +10,7 @@ import (
"time"
contentapi "github.com/containerd/containerd/api/services/content/v1"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/v2/defaults"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client/connhelper"
"github.com/moby/buildkit/session"

View File

@ -16,7 +16,6 @@ import (
// For example, after marshalling a LLB state and sending over the wire, the
// LLB state can be reconstructed from the definition.
type DefinitionOp struct {
MarshalCache
mu sync.Mutex
ops map[digest.Digest]*pb.Op
defs map[digest.Digest][]byte

View File

@ -8,7 +8,7 @@ import (
)
type DiffOp struct {
MarshalCache
cache MarshalCache
lower Output
upper Output
output Output
@ -31,7 +31,10 @@ 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 dgst, dt, md, srcs, err := m.Load(constraints); err == nil {
cache := m.cache.Acquire()
defer cache.Release()
if dgst, dt, md, srcs, err := cache.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
if err := m.Validate(ctx, constraints); err != nil {
@ -72,7 +75,7 @@ func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.
return "", nil, nil, nil, err
}
return m.Store(dt, md, m.constraints.SourceLocations, constraints)
return cache.Store(dt, md, m.constraints.SourceLocations, constraints)
}
func (m *DiffOp) Output() Output {

View File

@ -51,7 +51,7 @@ type mount struct {
}
type ExecOp struct {
MarshalCache
cache MarshalCache
proxyEnv *ProxyEnv
root Output
mounts []*mount
@ -63,6 +63,9 @@ type ExecOp struct {
}
func (e *ExecOp) AddMount(target string, source Output, opt ...MountOption) Output {
cache := e.cache.Acquire()
defer cache.Release()
m := &mount{
target: target,
source: source,
@ -84,7 +87,7 @@ func (e *ExecOp) AddMount(target string, source Output, opt ...MountOption) Outp
}
m.output = o
}
e.Store(nil, nil, nil, nil)
cache.Store(nil, nil, nil, nil)
e.isValidated = false
return m.output
}
@ -128,7 +131,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 dgst, dt, md, srcs, err := e.Load(c); err == nil {
cache := e.cache.Acquire()
defer cache.Release()
if dgst, dt, md, srcs, err := cache.Load(c); err == nil {
return dgst, dt, md, srcs, nil
}
@ -446,7 +452,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
if err != nil {
return "", nil, nil, nil, err
}
return e.Store(dt, md, e.constraints.SourceLocations, c)
return cache.Store(dt, md, e.constraints.SourceLocations, c)
}
func (e *ExecOp) Output() Output {

View File

@ -85,6 +85,13 @@ func (fa *FileAction) Mkfile(p string, m os.FileMode, dt []byte, opt ...MkfileOp
return a
}
// Symlink creates a symlink at `newpath` that points to `oldpath`
func (fa *FileAction) Symlink(oldpath, newpath string, opt ...SymlinkOption) *FileAction {
a := Symlink(oldpath, newpath, opt...)
a.prev = fa
return a
}
func (fa *FileAction) Rm(p string, opt ...RmOption) *FileAction {
a := Rm(p, opt...)
a.prev = fa
@ -193,6 +200,7 @@ type ChownOption interface {
MkdirOption
MkfileOption
CopyOption
SymlinkOption
}
type mkdirOptionFunc func(*MkdirInfo)
@ -290,6 +298,10 @@ func (co ChownOpt) SetCopyOption(mi *CopyInfo) {
mi.ChownOpt = &co
}
func (co ChownOpt) SetSymlinkOption(si *SymlinkInfo) {
si.ChownOpt = &co
}
func (co *ChownOpt) marshal(base pb.InputIndex) *pb.ChownOpt {
if co == nil {
return nil
@ -337,6 +349,57 @@ func Mkfile(p string, m os.FileMode, dt []byte, opts ...MkfileOption) *FileActio
}
}
// SymlinkInfo is the modifiable options used to create symlinks
type SymlinkInfo struct {
ChownOpt *ChownOpt
CreatedTime *time.Time
}
func (si *SymlinkInfo) SetSymlinkOption(si2 *SymlinkInfo) {
*si2 = *si
}
type SymlinkOption interface {
SetSymlinkOption(*SymlinkInfo)
}
// Symlink creates a symlink at `newpath` that points to `oldpath`
func Symlink(oldpath, newpath string, opts ...SymlinkOption) *FileAction {
var si SymlinkInfo
for _, o := range opts {
o.SetSymlinkOption(&si)
}
return &FileAction{
action: &fileActionSymlink{
oldpath: oldpath,
newpath: newpath,
info: si,
},
}
}
type fileActionSymlink struct {
oldpath string
newpath string
info SymlinkInfo
}
func (s *fileActionSymlink) addCaps(f *FileOp) {
addCap(&f.constraints, pb.CapFileSymlinkCreate)
}
func (s *fileActionSymlink) toProtoAction(_ context.Context, _ string, base pb.InputIndex) (pb.IsFileAction, error) {
return &pb.FileAction_Symlink{
Symlink: &pb.FileActionSymlink{
Oldpath: s.oldpath,
Newpath: s.newpath,
Owner: s.info.ChownOpt.marshal(base),
Timestamp: marshalTime(s.info.CreatedTime),
},
}, nil
}
type MkfileOption interface {
SetMkfileOption(*MkfileInfo)
}
@ -606,6 +669,10 @@ func (c CreatedTime) SetMkfileOption(mi *MkfileInfo) {
mi.CreatedTime = (*time.Time)(&c)
}
func (c CreatedTime) SetSymlinkOption(si *SymlinkInfo) {
si.CreatedTime = (*time.Time)(&c)
}
func (c CreatedTime) SetCopyOption(mi *CopyInfo) {
mi.CreatedTime = (*time.Time)(&c)
}
@ -746,7 +813,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 dgst, dt, md, srcs, err := f.Load(c); err == nil {
cache := f.Acquire()
defer cache.Release()
if dgst, dt, md, srcs, err := cache.Load(c); err == nil {
return dgst, dt, md, srcs, nil
}
@ -816,7 +886,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
if err != nil {
return "", nil, nil, nil, err
}
return f.Store(dt, md, f.constraints.SourceLocations, c)
return cache.Store(dt, md, f.constraints.SourceLocations, c)
}
func normalizePath(parent, p string, keepSlash bool) string {

View File

@ -117,30 +117,45 @@ func MarshalConstraints(base, override *Constraints) (*pb.Op, *pb.OpMetadata) {
}
type MarshalCache struct {
cache sync.Map
mu sync.Mutex
cache map[*Constraints]*marshalCacheResult
}
func (mc *MarshalCache) Load(c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
v, ok := mc.cache.Load(c)
type MarshalCacheInstance struct {
*MarshalCache
}
func (mc *MarshalCache) Acquire() *MarshalCacheInstance {
mc.mu.Lock()
return &MarshalCacheInstance{mc}
}
func (mc *MarshalCacheInstance) Load(c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
res, ok := mc.cache[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) Store(dt []byte, md *pb.OpMetadata, srcs []*SourceLocation, c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
func (mc *MarshalCacheInstance) 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)
if mc.cache == nil {
mc.cache = make(map[*Constraints]*marshalCacheResult)
}
mc.cache[c] = res
return res.digest, res.dt, res.md, res.srcs, nil
}
func (mc *MarshalCacheInstance) Release() {
mc.mu.Unlock()
}
type marshalCacheResult struct {
digest digest.Digest
dt []byte

View File

@ -9,7 +9,7 @@ import (
)
type MergeOp struct {
MarshalCache
cache MarshalCache
inputs []Output
output Output
constraints Constraints
@ -32,7 +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 dgst, dt, md, srcs, err := m.Load(constraints); err == nil {
cache := m.cache.Acquire()
defer cache.Release()
if dgst, dt, md, srcs, err := cache.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
@ -59,7 +62,7 @@ func (m *MergeOp) Marshal(ctx context.Context, constraints *Constraints) (digest
return "", nil, nil, nil, err
}
return m.Store(dt, md, m.constraints.SourceLocations, constraints)
return cache.Store(dt, md, m.constraints.SourceLocations, constraints)
}
func (m *MergeOp) Output() Output {

View File

@ -20,7 +20,7 @@ import (
)
type SourceOp struct {
MarshalCache
cache MarshalCache
id string
attrs map[string]string
output Output
@ -49,7 +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 dgst, dt, md, srcs, err := s.Load(constraints); err == nil {
cache := s.cache.Acquire()
defer cache.Release()
if dgst, dt, md, srcs, err := cache.Load(constraints); err == nil {
return dgst, dt, md, srcs, nil
}
@ -82,7 +85,7 @@ func (s *SourceOp) Marshal(ctx context.Context, constraints *Constraints) (diges
return "", nil, nil, nil, err
}
return s.Store(dt, md, s.constraints.SourceLocations, constraints)
return cache.Store(dt, md, s.constraints.SourceLocations, constraints)
}
func (s *SourceOp) Output() Output {

View File

@ -3,10 +3,12 @@ package ociindex
import (
"encoding/json"
"io"
"maps"
"os"
"path"
"syscall"
"github.com/containerd/containerd/v2/pkg/reference"
"github.com/gofrs/flock"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -15,6 +17,8 @@ import (
const (
// lockFileSuffix is the suffix of the lock file
lockFileSuffix = ".lock"
annotationImageName = "io.containerd.image.name"
)
type StoreIndex struct {
@ -23,6 +27,19 @@ type StoreIndex struct {
layoutPath string
}
type NameOrTag struct {
isTag bool
value string
}
func Name(name string) NameOrTag {
return NameOrTag{value: name}
}
func Tag(tag string) NameOrTag {
return NameOrTag{isTag: true, value: tag}
}
func NewStoreIndex(storePath string) StoreIndex {
indexPath := path.Join(storePath, ocispecs.ImageIndexFile)
layoutPath := path.Join(storePath, ocispecs.ImageLayoutFile)
@ -61,7 +78,7 @@ func (s StoreIndex) Read() (*ocispecs.Index, error) {
return &idx, nil
}
func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
func (s StoreIndex) Put(desc ocispecs.Descriptor, names ...NameOrTag) error {
// lock the store to prevent concurrent access
lock := flock.New(s.lockPath)
locked, err := lock.TryLock()
@ -107,8 +124,19 @@ func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
}
setOCIIndexDefaults(&idx)
if err = insertDesc(&idx, desc, tag); err != nil {
return err
namesp := make([]*NameOrTag, 0, len(names))
for _, n := range names {
namesp = append(namesp, &n)
}
if len(names) == 0 {
namesp = append(namesp, nil)
}
for _, name := range namesp {
if err = insertDesc(&idx, desc, name); err != nil {
return err
}
}
idxData, err = json.Marshal(idx)
@ -130,6 +158,12 @@ func (s StoreIndex) Get(tag string) (*ocispecs.Descriptor, error) {
return nil, err
}
for _, m := range idx.Manifests {
if t, ok := m.Annotations[annotationImageName]; ok && t == tag {
return &m, nil
}
}
for _, m := range idx.Manifests {
if t, ok := m.Annotations[ocispecs.AnnotationRefName]; ok && t == tag {
return &m, nil
@ -165,20 +199,34 @@ func setOCIIndexDefaults(index *ocispecs.Index) {
// insertDesc puts desc to index with tag.
// Existing manifests with the same tag will be removed from the index.
func insertDesc(index *ocispecs.Index, desc ocispecs.Descriptor, tag string) error {
func insertDesc(index *ocispecs.Index, in ocispecs.Descriptor, name *NameOrTag) error {
if index == nil {
return nil
}
if tag != "" {
// make a copy to not modify the input descriptor
desc := in
desc.Annotations = maps.Clone(in.Annotations)
if name != nil {
if desc.Annotations == nil {
desc.Annotations = make(map[string]string)
}
desc.Annotations[ocispecs.AnnotationRefName] = tag
// remove existing manifests with the same tag
imgName, refName := name.value, name.value
if name.isTag {
imgName = ""
} else {
refName = ociReferenceName(imgName)
}
if imgName != "" {
desc.Annotations[annotationImageName] = imgName
}
desc.Annotations[ocispecs.AnnotationRefName] = refName
// remove existing manifests with the same tag/name
var manifests []ocispecs.Descriptor
for _, m := range index.Manifests {
if m.Annotations[ocispecs.AnnotationRefName] != tag {
if m.Annotations[ocispecs.AnnotationRefName] != refName || m.Annotations[annotationImageName] != imgName {
manifests = append(manifests, m)
}
}
@ -187,3 +235,20 @@ func insertDesc(index *ocispecs.Index, desc ocispecs.Descriptor, tag string) err
index.Manifests = append(index.Manifests, desc)
return nil
}
// ociReferenceName takes the loosely defined reference name same way as
// containerd tar exporter does.
func ociReferenceName(name string) string {
// OCI defines the reference name as only a tag excluding the
// repository. The containerd annotation contains the full image name
// since the tag is insufficient for correctly naming and referring to an
// image
var ociRef string
if spec, err := reference.Parse(name); err == nil {
ociRef = spec.Object
} else {
ociRef = name
}
return ociRef
}

View File

@ -10,8 +10,8 @@ import (
"strings"
"time"
"github.com/containerd/containerd/content"
contentlocal "github.com/containerd/containerd/content/local"
"github.com/containerd/containerd/v2/core/content"
contentlocal "github.com/containerd/containerd/v2/plugins/content/local"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/client/ociindex"
@ -345,7 +345,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}
for storePath, tag := range cacheOpt.storesToUpdate {
idx := ociindex.NewStoreIndex(storePath)
if err := idx.Put(tag, manifestDesc); err != nil {
if err := idx.Put(manifestDesc, ociindex.Tag(tag)); err != nil {
return nil, err
}
}
@ -360,12 +360,16 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
return nil, err
}
for _, storePath := range storesToUpdate {
tag := "latest"
names := []ociindex.NameOrTag{ociindex.Tag("latest")}
if t, ok := res.ExporterResponse["image.name"]; ok {
tag = t
inp := strings.Split(t, ",")
names = make([]ociindex.NameOrTag, len(inp))
for i, n := range inp {
names[i] = ociindex.Name(n)
}
}
idx := ociindex.NewStoreIndex(storePath)
if err := idx.Put(tag, manifestDesc); err != nil {
if err := idx.Put(manifestDesc, names...); err != nil {
return nil, err
}
}