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

refactor: use read only target to replace oras.Target for the target storage with caching #538

Merged
merged 1 commit into from
Sep 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
2 changes: 1 addition & 1 deletion cmd/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runPull(opts pullOptions) error {
if repo.Reference.Reference == "" {
return errors.NewErrInvalidReference(repo.Reference)
}
var src oras.Target = repo
var src oras.ReadOnlyTarget = repo
if opts.cacheRoot != "" {
ociStore, err := oci.New(opts.cacheRoot)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/cache/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func (fn closer) Close() error {

// Cache target struct.
type target struct {
oras.Target
oras.ReadOnlyTarget
cache content.Storage
}

// New generates a new target storage with caching.
func New(source oras.Target, cache content.Storage) oras.Target {
func New(source oras.ReadOnlyTarget, cache content.Storage) oras.ReadOnlyTarget {
t := &target{
Target: source,
cache: cache,
ReadOnlyTarget: source,
cache: cache,
}
if refFetcher, ok := source.(registry.ReferenceFetcher); ok {
return &referenceTarget{
Expand All @@ -61,7 +61,7 @@ func (t *target) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadC
return rc, nil
}

if rc, err = t.Target.Fetch(ctx, target); err != nil {
if rc, err = t.ReadOnlyTarget.Fetch(ctx, target); err != nil {
return nil, err
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func (t *target) Exists(ctx context.Context, desc ocispec.Descriptor) (bool, err
if err == nil && exists {
return true, nil
}
return t.Target.Exists(ctx, desc)
return t.ReadOnlyTarget.Exists(ctx, desc)
}

// Cache referenceTarget struct.
Expand Down
33 changes: 25 additions & 8 deletions internal/cache/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/registry"
Expand All @@ -45,7 +46,17 @@ func TestProxy_fetchCache(t *testing.T) {
Size: int64(len(blob)),
}

p := New(memory.New(), memory.New())
target := memory.New()
p := struct {
oras.Target
oras.ReadOnlyTarget
cache content.Storage
}{
target,
target,
memory.New(),
}

ctx := context.Background()

err := p.Push(ctx, desc, bytes.NewReader(blob))
Expand All @@ -54,14 +65,14 @@ func TestProxy_fetchCache(t *testing.T) {
}

// first fetch
exists, err := p.Exists(ctx, desc)
exists, err := p.Target.Exists(ctx, desc)
if err != nil {
t.Fatal("Proxy.Exists() error =", err)
}
if !exists {
t.Errorf("Proxy.Exists() = %v, want %v", exists, true)
}
got, err := content.FetchAll(ctx, p, desc)
got, err := content.FetchAll(ctx, p.Target, desc)
if err != nil {
t.Fatal("Proxy.Fetch() error =", err)
}
Expand All @@ -71,16 +82,16 @@ func TestProxy_fetchCache(t *testing.T) {

// repeated fetch should not touch base CAS
// nil base will generate panic if the base CAS is touched
p.(*target).Target = nil
p.Target = nil

exists, err = p.Exists(ctx, desc)
exists, err = p.ReadOnlyTarget.Exists(ctx, desc)
if err != nil {
t.Fatal("Proxy.Exists() error =", err)
}
if !exists {
t.Errorf("Proxy.Exists() = %v, want %v", exists, true)
}
got, err = content.FetchAll(ctx, p, desc)
got, err = content.FetchAll(ctx, p.ReadOnlyTarget, desc)
if err != nil {
t.Fatal("Proxy.Fetch() error =", err)
}
Expand All @@ -97,7 +108,13 @@ func TestProxy_pushPassThrough(t *testing.T) {
Size: int64(len(blob)),
}

p := New(memory.New(), memory.New())
p := struct {
oras.Target
cache content.Storage
}{
memory.New(),
memory.New(),
}
ctx := context.Background()

// before push
Expand Down Expand Up @@ -206,7 +223,7 @@ func TestProxy_fetchReference(t *testing.T) {
}

// repeated fetch should not touch base CAS
p.(*referenceTarget).Target = nil
p.(*referenceTarget).ReadOnlyTarget = nil
got, err = content.FetchAll(ctx, p, desc)
if err != nil {
t.Fatal("ReferenceTarget.Fetch() error =", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/cas/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// FetchDescriptor fetches a minimal descriptor of reference from target.
// If platform flag not empty, will fetch the specified platform.
func FetchDescriptor(ctx context.Context, target oras.Target, reference string, p *ocispec.Platform) ([]byte, error) {
func FetchDescriptor(ctx context.Context, target oras.ReadOnlyTarget, reference string, p *ocispec.Platform) ([]byte, error) {
desc, err := oras.Resolve(ctx, target, reference, oras.ResolveOptions{TargetPlatform: p})
if err != nil {
return nil, err
Expand All @@ -43,7 +43,7 @@ func FetchDescriptor(ctx context.Context, target oras.Target, reference string,

// FetchManifest fetches the manifest content of reference from target.
// If platform flag not empty, will fetch the specified platform.
func FetchManifest(ctx context.Context, target oras.Target, reference string, p *ocispec.Platform) ([]byte, error) {
func FetchManifest(ctx context.Context, target oras.ReadOnlyTarget, reference string, p *ocispec.Platform) ([]byte, error) {
// TODO: improve implementation once oras-go#102 is resolved
if p == nil {
if rf, ok := target.(registry.ReferenceFetcher); ok {
Expand Down