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

fix: pushing based on temporary file store path #996

Merged
merged 16 commits into from
Jul 4, 2023
22 changes: 13 additions & 9 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/fileref"
"oras.land/oras/cmd/oras/internal/option"
ostore "oras.land/oras/internal/store"
)

type pushOptions struct {
Expand Down Expand Up @@ -139,6 +141,8 @@ func runPush(ctx context.Context, opts pushOptions) error {
return err
}
defer store.Close()
memStore := memory.New()
qweeah marked this conversation as resolved.
Show resolved Hide resolved
hybrid := ostore.NewReadOnlyHybrid(store, memStore, memStore)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
if opts.manifestConfigRef != "" {
path, cfgMediaType, err := fileref.Parse(opts.manifestConfigRef, oras.MediaTypeUnknownConfig)
if err != nil {
Expand All @@ -160,11 +164,11 @@ func runPush(ctx context.Context, opts pushOptions) error {
return err
}
pack := func() (ocispec.Descriptor, error) {
root, err := oras.Pack(ctx, store, opts.artifactType, descs, packOpts)
root, err := oras.Pack(ctx, memStore, opts.artifactType, descs, packOpts)
if err != nil {
return ocispec.Descriptor{}, err
}
if err = store.Tag(ctx, root, root.Digest.String()); err != nil {
if err = memStore.Tag(ctx, root, root.Digest.String()); err != nil {
return ocispec.Descriptor{}, err
}
return root, nil
Expand All @@ -177,12 +181,12 @@ func runPush(ctx context.Context, opts pushOptions) error {
}
copyOptions := oras.DefaultCopyOptions
copyOptions.Concurrency = opts.concurrency
updateDisplayOption(&copyOptions.CopyGraphOptions, store, opts.Verbose)
updateDisplayOption(&copyOptions.CopyGraphOptions, hybrid, opts.Verbose)
copy := func(root ocispec.Descriptor) error {
if tag := opts.Reference; tag == "" {
err = oras.CopyGraph(ctx, store, dst, root, copyOptions.CopyGraphOptions)
err = oras.CopyGraph(ctx, hybrid, dst, root, copyOptions.CopyGraphOptions)
} else {
_, err = oras.Copy(ctx, store, root.Digest.String(), dst, tag, copyOptions)
_, err = oras.Copy(ctx, hybrid, root.Digest.String(), dst, tag, copyOptions)
}
return err
}
Expand All @@ -195,7 +199,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
fmt.Println("Pushed", opts.AnnotatedReference())

if len(opts.extraRefs) != 0 {
contentBytes, err := content.FetchAll(ctx, store, root)
contentBytes, err := content.FetchAll(ctx, memStore, root)
if err != nil {
return err
}
Expand All @@ -209,10 +213,10 @@ func runPush(ctx context.Context, opts pushOptions) error {
fmt.Println("Digest:", root.Digest)

// Export manifest
return opts.ExportManifest(ctx, store, root)
return opts.ExportManifest(ctx, memStore, root)
}

func updateDisplayOption(opts *oras.CopyGraphOptions, store content.Fetcher, verbose bool) {
func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, verbose bool) {
committed := &sync.Map{}
opts.PreCopy = display.StatusPrinter("Uploading", verbose)
opts.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
Expand All @@ -221,7 +225,7 @@ func updateDisplayOption(opts *oras.CopyGraphOptions, store content.Fetcher, ver
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := display.PrintSuccessorStatus(ctx, desc, "Skipped ", store, committed, verbose); err != nil {
if err := display.PrintSuccessorStatus(ctx, desc, "Skipped ", fetcher, committed, verbose); err != nil {
return err
}
return display.PrintStatus(desc, "Uploaded ", verbose)
Expand Down
59 changes: 59 additions & 0 deletions internal/store/hybrid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cache
qweeah marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)

type hybrid struct {
oras.ReadOnlyTarget
combined []oras.Target
}

// NewReadOnlyHybrid generates a new hybrid storage.
func NewReadOnlyHybrid(provider oras.ReadOnlyTarget, combined ...oras.Target) oras.ReadOnlyTarget {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
return &hybrid{
ReadOnlyTarget: provider,
combined: combined,
}
}

// Fetch fetches the content from combined targets first, then from the provider.
func (h *hybrid) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) {
for _, c := range h.combined {
rc, err := c.Fetch(ctx, target)
if err == nil {
return rc, nil
}
}
return h.ReadOnlyTarget.Fetch(ctx, target)
}

// Resolve resolves the content from cache first, then from the provider.
func (h *hybrid) Resolve(ctx context.Context, ref string) (ocispec.Descriptor, error) {
for _, c := range h.combined {
desc, err := c.Resolve(ctx, ref)
if err == nil {
return desc, nil
}
}
return h.ReadOnlyTarget.Resolve(ctx, ref)
}