Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): cacheable v1.ImageIndex #1380

Merged
merged 1 commit into from
Jun 7, 2022
Merged
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
36 changes: 36 additions & 0 deletions pkg/v1/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,39 @@ func (i *image) LayerByDiffID(h v1.Hash) (v1.Layer, error) {
}
return l, err
}

// ImageIndex returns a new ImageIndex which wraps the given ImageIndex's
// children with either Image(child, c) or ImageIndex(child, c) depending on type.
func ImageIndex(ii v1.ImageIndex, c Cache) v1.ImageIndex {
return &imageIndex{
inner: ii,
c: c,
}
}

type imageIndex struct {
inner v1.ImageIndex
c Cache
}

func (ii *imageIndex) MediaType() (types.MediaType, error) { return ii.inner.MediaType() }
func (ii *imageIndex) Digest() (v1.Hash, error) { return ii.inner.Digest() }
func (ii *imageIndex) Size() (int64, error) { return ii.inner.Size() }
func (ii *imageIndex) IndexManifest() (*v1.IndexManifest, error) { return ii.inner.IndexManifest() }
func (ii *imageIndex) RawManifest() ([]byte, error) { return ii.inner.RawManifest() }

func (ii *imageIndex) Image(h v1.Hash) (v1.Image, error) {
i, err := ii.inner.Image(h)
if err != nil {
return nil, err
}
return Image(i, ii.c), nil
}

func (ii *imageIndex) ImageIndex(h v1.Hash) (v1.ImageIndex, error) {
idx, err := ii.inner.ImageIndex(h)
if err != nil {
return nil, err
}
return ImageIndex(idx, ii.c), nil
}
25 changes: 25 additions & 0 deletions pkg/v1/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/validate"
)
Expand All @@ -42,6 +43,30 @@ func TestImage(t *testing.T) {
}
}

func TestImageIndex(t *testing.T) {
// ImageIndex with child Image and ImageIndex manifests.
ii, err := random.Index(1024, 5, 2)
if err != nil {
t.Fatalf("random.Index: %v", err)
}
iiChild, err := random.Index(1024, 5, 2)
if err != nil {
t.Fatalf("random.Index: %v", err)
}
ii = mutate.AppendManifests(ii, mutate.IndexAddendum{Add: iiChild})

m := &memcache{map[v1.Hash]v1.Layer{}}
ii = ImageIndex(ii, m)

// Validate twice to hit the cache.
if err := validate.Index(ii); err != nil {
t.Errorf("Validate: %v", err)
}
if err := validate.Index(ii); err != nil {
t.Errorf("Validate: %v", err)
}
}

func TestLayersLazy(t *testing.T) {
img, err := random.Image(1024, 5)
if err != nil {
Expand Down