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: support mounting blobs during copy #1242

Merged
merged 19 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
7 changes: 7 additions & 0 deletions cmd/oras/internal/display/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func NewTarget(t oras.GraphTarget, actionPrompt, donePrompt string, tty *os.File
return gt, nil
}

// Mount makes graphTarget implement oras.Mounter.
qweeah marked this conversation as resolved.
Show resolved Hide resolved
func (t *graphTarget) Mount(ctx context.Context, desc ocispec.Descriptor, fromRepo string, getContent func() (io.ReadCloser, error)) error {
// should not be called if the underlying GraphTarget is not a mounter
mounter := t.GraphTarget.(registry.Mounter)
return mounter.Mount(ctx, desc, fromRepo, getContent)
}

// Push pushes the content to the base oras.GraphTarget with tracking.
func (t *graphTarget) Push(ctx context.Context, expected ocispec.Descriptor, content io.Reader) error {
r, err := managedReader(content, expected, t.manager, t.actionPrompt, t.donePrompt)
Expand Down
34 changes: 4 additions & 30 deletions cmd/oras/internal/display/track/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/display/console/testutils"
)

Expand Down Expand Up @@ -79,34 +80,7 @@ func Test_referenceGraphTarget_PushReference(t *testing.T) {
}
}

func Test_referenceGraphTarget_Prompt(t *testing.T) {
// prepare
pty, device, err := testutils.NewPty()
if err != nil {
t.Fatal(err)
}
defer device.Close()
content := []byte("test")
desc := ocispec.Descriptor{
MediaType: "application/octet-stream",
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}
// test
prompt := "prompt"
target, err := NewTarget(memory.New(), "action", "done", device)
if err != nil {
t.Fatal(err)
}
m := target.(*graphTarget).manager
if err := target.Prompt(desc, prompt); err != nil {
t.Fatal(err)
}
if err := m.Close(); err != nil {
t.Fatal(err)
}
// validate
if err = testutils.MatchPty(pty, device, prompt, desc.MediaType, "100.00%", desc.Digest.String()); err != nil {
t.Fatal(err)
}
func Test_referenceGraphTarget_Mount(t *testing.T) {
target := graphTarget{GraphTarget: &remote.Repository{}}
_ = target.Mount(context.Background(), ocispec.Descriptor{}, "", nil)
}
18 changes: 17 additions & 1 deletion cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/display"
Expand Down Expand Up @@ -162,8 +163,15 @@
promptCopying = "Copying"
promptCopied = "Copied "
promptSkipped = "Skipped"
promptMounted = "Mounted"
)

srcRepo, srcIsRemote := src.(*remote.Repository)
dstRepo, dstIsRemote := dst.(*remote.Repository)
if srcIsRemote && dstIsRemote && srcRepo.Reference.Registry == dstRepo.Reference.Registry {
extendedCopyOptions.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
return []string{srcRepo.Reference.Repository}, nil
}
}
if opts.TTY == nil {
// none TTY output
extendedCopyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
Expand All @@ -180,6 +188,10 @@
}
return display.PrintStatus(desc, promptCopied, opts.Verbose)
}
extendedCopyOptions.OnMounted = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintStatus(desc, promptMounted, opts.Verbose)
}
} else {
// TTY output
tracked, err := track.NewTarget(dst, promptCopying, promptCopied, opts.TTY)
Expand All @@ -198,6 +210,10 @@
return tracked.Prompt(desc, promptSkipped)
})
}
extendedCopyOptions.OnMounted = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return tracked.Prompt(desc, promptMounted)
}

Check warning on line 216 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L214-L216

Added lines #L214 - L216 were not covered by tests
}

var desc ocispec.Descriptor
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
golang.org/x/sync v0.6.0
golang.org/x/term v0.16.0
gopkg.in/yaml.v3 v3.0.1
oras.land/oras-go/v2 v2.3.1-0.20231227022511-1d9ad6c409b3
oras.land/oras-go/v2 v2.3.1-0.20240111050452-3b1dd0e97000
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
oras.land/oras-go/v2 v2.3.1-0.20231227022511-1d9ad6c409b3 h1:cF1ErV93cAw4i5/MNF4pW6ULHa27ig3oYGlxouzymY8=
oras.land/oras-go/v2 v2.3.1-0.20231227022511-1d9ad6c409b3/go.mod h1:W2rj9/rGtsTh9lmU3S0uq3iwvR6wNvUuD8nmOFmWBUY=
oras.land/oras-go/v2 v2.3.1-0.20240111050452-3b1dd0e97000 h1:tCh6spxJeREZx+phlJMk76/gcK3V02g56siet4NP180=
oras.land/oras-go/v2 v2.3.1-0.20240111050452-3b1dd0e97000/go.mod h1:JE7nRv2IHCiuu7kbH3UmDCp7bYD38yfnt1sl0lkLrwk=
1 change: 1 addition & 0 deletions test/e2e/internal/utils/match/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func newStateMachine(cmd string) *stateMachine {
sm.addPath("Copying", "Copied")
sm.addPath("Skipped")
sm.addPath("Exists")
sm.addPath("Mounted")
default:
ginkgo.Fail("Unrecognized cmd name " + cmd)
}
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/suite/command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ var _ = Describe("1.1 registry users:", func() {

var _ = Describe("OCI spec 1.0 registry users:", func() {
When("running `cp`", func() {
It("should copy an image artifact with mounting", func() {
repo := cpTestRepo("1.0-mount")
src := RegistryRef(FallbackHost, ArtifactRepo, foobar.Tag)
dst := RegistryRef(FallbackHost, repo, "")
out := ORAS("cp", src, dst, "-v").Exec()
Expect(out).Should(gbytes.Say("Mounted fcde2b2edba5 bar"))
CompareRef(src, RegistryRef(FallbackHost, repo, foobar.Digest))
})

It("should copy an image artifact and its referrers from a registry to a fallback registry", func() {
repo := cpTestRepo("to-fallback")
stateKeys := append(append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageReferrerConfigStateKeys[0]), foobar.ImageReferrersStateKeys...)
Expand Down
Loading