Skip to content

Commit

Permalink
[PRFix modify fetchReference]
Browse files Browse the repository at this point in the history
Signed-off-by: Zoey Li <[email protected]>
  • Loading branch information
lizMSFT committed Sep 8, 2022
1 parent 097dc7f commit defc485
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
14 changes: 7 additions & 7 deletions cmd/oras/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ type fetchBlobOptions struct {
func fetchCmd() *cobra.Command {
var opts fetchBlobOptions
cmd := &cobra.Command{
Use: "fetch <name@digest> [flags]",
Use: "fetch [flags] <name@digest>",
Short: "[Preview] Fetch a blob from a remote registry",
Long: `[Preview] Fetch a blob from a remote registry
** This command is in preview and under development. **
Example - Fetch the blob and save it to a local file:
oras blob fetch localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 --output blob.tar.gz
oras blob fetch --output blob.tar.gz localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5
Example - Fetch the blob and stdout the raw blob content:
oras blob fetch localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 --output -
oras blob fetch --output - localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5
Example - Fetch the blob and stdout the descriptor of a blob:
oras blob fetch localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 --descriptor
oras blob fetch --descriptor localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5
Example - Fetch the blob and save it to a local file and stdout the descriptor:
oras blob fetch localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 --output blob.tar.gz --descriptor
Example - Fetch the blob, save it to a local file and stdout the descriptor:
oras blob fetch --output blob.tar.gz --descriptor localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5
Example - Fetch blob from the insecure registry:
oras blob fetch localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 --insecure
oras blob fetch --insecure localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5
`,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down
25 changes: 13 additions & 12 deletions internal/cache/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,37 @@ type referenceTarget struct {
registry.ReferenceFetcher
}

// FetchReference fetches the content identified by the reference from the cache
// if exists, otherwise fetches the content from the remote and then cache the
// fetched content.
// FetchReference fetches the content identified by the reference from the
// remote and cache the fetched content.
// Cached content will only be read via Fetch, FetchReference will always fetch
// From origin.
func (t *referenceTarget) FetchReference(ctx context.Context, reference string) (ocispec.Descriptor, io.ReadCloser, error) {
desc, err := t.Resolve(ctx, reference)
target, rc, err := t.ReferenceFetcher.FetchReference(ctx, reference)
if err != nil {
return ocispec.Descriptor{}, nil, err
}

// skip caching if the content already exists in cache
exists, err := t.cache.Exists(ctx, desc)
exists, err := t.cache.Exists(ctx, target)
if err != nil {
return ocispec.Descriptor{}, nil, err
}
if exists {
err = rc.Close()
if err != nil {
return ocispec.Descriptor{}, nil, err
}

// get rc from the cache
rc, err := t.cache.Fetch(ctx, desc)
rc, err = t.cache.Fetch(ctx, target)
if err != nil {
return ocispec.Descriptor{}, nil, err
}
return desc, rc, nil
}

desc, rc, err := t.ReferenceFetcher.FetchReference(ctx, reference)
if err != nil {
return ocispec.Descriptor{}, nil, err
// no need to do tee'd push
return target, rc, nil
}

// Fetch from origin with caching
return desc, t.cacheReadCloser(ctx, rc, desc), nil
return target, t.cacheReadCloser(ctx, rc, target), nil
}
23 changes: 0 additions & 23 deletions internal/cache/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,6 @@ func TestProxy_fetchReference(t *testing.T) {
w.Write(blob)
atomic.AddInt64(&successCount, 1)
return
} else if r.Method == http.MethodHead &&
(r.URL.Path == fmt.Sprintf("/v2/%s/manifests/%s", repoName, tagName) ||
r.URL.Path == fmt.Sprintf("/v2/%s/manifests/%s", repoName, digest)) {
w.Header().Set("Content-Type", mediaType)
w.Header().Set("Docker-Content-Digest", digest.String())
w.Header().Set("Content-Length", strconv.Itoa(len([]byte(blob))))
atomic.AddInt64(&successCount, 1)
return
}
t.Errorf("unexpected access: %s %s", r.Method, r.URL)
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -223,21 +215,6 @@ func TestProxy_fetchReference(t *testing.T) {
if !bytes.Equal(got, blob) {
t.Errorf("ReferenceTarget.Fetch() = %v, want %v", got, blob)
}
if wantRequestCount += 2; requestCount != wantRequestCount {
t.Errorf("unexpected number of requests: %d, want %d", requestCount, wantRequestCount)
}
if wantSuccessCount += 2; successCount != wantSuccessCount {
t.Errorf("unexpected number of successful requests: %d, want %d", successCount, wantSuccessCount)
}

// second fetch reference, should get the content from the cache
gotDesc, _, err = p.(registry.ReferenceFetcher).FetchReference(ctx, repo.Reference.Reference)
if err != nil {
t.Fatal("ReferenceTarget.FetchReference() error =", err)
}
if !reflect.DeepEqual(gotDesc, desc) {
t.Fatalf("ReferenceTarget.FetchReference() got %v, want %v", gotDesc, desc)
}
if wantRequestCount++; requestCount != wantRequestCount {
t.Errorf("unexpected number of requests: %d, want %d", requestCount, wantRequestCount)
}
Expand Down

0 comments on commit defc485

Please sign in to comment.