Skip to content

Commit

Permalink
ociindex: fix handling multiple names per descriptor
Browse files Browse the repository at this point in the history
Previous implementation mixed tags and names and
added invalid comma-separated reference annotation.

Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Dec 11, 2024
1 parent a3ddc3b commit 7c1309b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
74 changes: 65 additions & 9 deletions client/ociindex/ociindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"syscall"

"github.com/containerd/containerd/reference"
"github.com/gofrs/flock"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand All @@ -15,6 +16,8 @@ import (
const (
// lockFileSuffix is the suffix of the lock file
lockFileSuffix = ".lock"

annotationImageName = "io.containerd.image.name"
)

type StoreIndex struct {
Expand All @@ -23,6 +26,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)
Expand Down Expand Up @@ -61,7 +77,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()
Expand Down Expand Up @@ -107,8 +123,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)
Expand Down Expand Up @@ -165,25 +192,54 @@ 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, desc ocispecs.Descriptor, name *NameOrTag) error {
if index == nil {
return nil
}

if tag != "" {
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 {
manifests = append(manifests, m)
if m.Annotations[ocispecs.AnnotationRefName] != refName || m.Annotations[annotationImageName] != imgName {
if imgName == "" || m.Annotations[annotationImageName] != imgName {
manifests = append(manifests, m)
}
}
}
index.Manifests = manifests
}
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
}
6 changes: 3 additions & 3 deletions client/ociindex/ociindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestWriteSingleDescriptor(t *testing.T) {
store := NewStoreIndex(dir)

desc := randDescriptor("foo")
err := store.Put("", desc)
err := store.Put(desc)
require.NoError(t, err)

readDesc, err := store.GetSingle()
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestAddDescriptor(t *testing.T) {

store := NewStoreIndex(dir)
three := randDescriptor("baz")
err = store.Put("", three)
err = store.Put(three)
require.NoError(t, err)

readIdx, err := store.Read()
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAddDescriptorWithTag(t *testing.T) {

store := NewStoreIndex(dir)
three := randDescriptor("baz")
err = store.Put("ver1", three)
err = store.Put(three, Tag("ver1"))
require.NoError(t, err)

desc, err := store.Get("ver1")
Expand Down
12 changes: 8 additions & 4 deletions client/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 7c1309b

Please sign in to comment.