Skip to content

Commit 2d2b853

Browse files
committed
sourcesink
1 parent 4c4c94c commit 2d2b853

File tree

15 files changed

+144
-142
lines changed

15 files changed

+144
-142
lines changed

cmd/crane/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
7272
}
7373
if uselocal != "" {
7474
p, _ := layout.FromPath(uselocal)
75-
options = append(options, crane.WithPuller(layout.NewPuller(p)), crane.WithPusher(layout.NewPusher(p)))
75+
options = append(options, crane.WithSource(layout.NewSource(p)), crane.WithSink(layout.NewSink(p)))
7676
}
7777
if Version != "" {
7878
binary := "crane"

pkg/crane/delete.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919

2020
"github.com/google/go-containerregistry/pkg/name"
21-
"github.com/google/go-containerregistry/pkg/v1/remote"
2221
)
2322

2423
// Delete deletes the remote reference at src.
@@ -29,5 +28,5 @@ func Delete(src string, opt ...Option) error {
2928
return fmt.Errorf("parsing reference %q: %w", src, err)
3029
}
3130

32-
return remote.Delete(ref, o.Remote...)
31+
return o.sink.Delete(o.ctx, ref)
3332
}

pkg/crane/get.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func getArtifact(r string, opt ...Option) (partial.Artifact, error) {
3838
if err != nil {
3939
return nil, fmt.Errorf("parsing reference %q: %w", r, err)
4040
}
41-
return remote.Artifact(ref, o.Remote...)
41+
return o.source.Artifact(o.ctx, ref)
4242
}
4343

4444
// Get calls remote.Get and returns an uninterpreted response.
@@ -58,5 +58,5 @@ func Head(r string, opt ...Option) (*v1.Descriptor, error) {
5858
if err != nil {
5959
return nil, err
6060
}
61-
return remote.Head(ref, o.Remote...)
61+
return o.source.Head(o.ctx, ref)
6262
}

pkg/crane/options.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/google/go-containerregistry/pkg/name"
2424
v1 "github.com/google/go-containerregistry/pkg/v1"
2525
"github.com/google/go-containerregistry/pkg/v1/remote"
26+
"github.com/google/go-containerregistry/pkg/v1/sourcesink"
2627
)
2728

2829
// Options hold the options that crane uses when calling other packages.
@@ -38,6 +39,9 @@ type Options struct {
3839
jobs int
3940
noclobber bool
4041
ctx context.Context
42+
43+
sink sourcesink.Sink
44+
source sourcesink.Source
4145
}
4246

4347
// GetOptions exposes the underlying []remote.Option, []name.Option, and
@@ -62,6 +66,14 @@ func makeOptions(opts ...Option) Options {
6266
o(&opt)
6367
}
6468

69+
// By default use remote source and sink
70+
if opt.sink == nil {
71+
opt.sink, _ = remote.NewPusher(opt.Remote...)
72+
}
73+
if opt.source == nil {
74+
opt.source, _ = remote.NewPuller(opt.Remote...)
75+
}
76+
6577
// Allow for untrusted certificates if the user
6678
// passed Insecure but no custom transport.
6779
if opt.insecure && opt.Transport == nil {
@@ -177,16 +189,16 @@ func WithNoClobber(noclobber bool) Option {
177189
}
178190
}
179191

180-
// WithPuller sets the puller for remote
181-
func WithPuller(puller remote.Puller) Option {
192+
// WithSink sets the sink
193+
func WithSink(sink sourcesink.Sink) Option {
182194
return func(o *Options) {
183-
o.Remote = append(o.Remote, remote.WithPuller(puller))
195+
o.sink = sink
184196
}
185197
}
186198

187-
// WithPuller sets the puller for remote
188-
func WithPusher(pusher remote.Pusher) Option {
199+
// WithSource sets the source
200+
func WithSource(source sourcesink.Source) Option {
189201
return func(o *Options) {
190-
o.Remote = append(o.Remote, remote.WithPusher(pusher))
202+
o.source = source
191203
}
192204
}

pkg/crane/push.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
"github.com/google/go-containerregistry/pkg/name"
2121
v1 "github.com/google/go-containerregistry/pkg/v1"
22-
"github.com/google/go-containerregistry/pkg/v1/remote"
2322
"github.com/google/go-containerregistry/pkg/v1/tarball"
2423
)
2524

@@ -50,7 +49,7 @@ func Push(img v1.Image, dst string, opt ...Option) error {
5049
if err != nil {
5150
return fmt.Errorf("parsing reference %q: %w", dst, err)
5251
}
53-
return remote.Write(tag, img, o.Remote...)
52+
return o.sink.Push(o.ctx, tag, img)
5453
}
5554

5655
// Upload pushes the v1.Layer to a given repo.
@@ -60,6 +59,5 @@ func Upload(layer v1.Layer, repo string, opt ...Option) error {
6059
if err != nil {
6160
return fmt.Errorf("parsing repo %q: %w", repo, err)
6261
}
63-
64-
return remote.WriteLayer(ref, layer, o.Remote...)
62+
return o.sink.Upload(o.ctx, ref, layer)
6563
}

pkg/v1/layout/pusher.go renamed to pkg/v1/layout/sink.go

+14-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/google/go-containerregistry/pkg/name"
2525
v1 "github.com/google/go-containerregistry/pkg/v1"
2626
"github.com/google/go-containerregistry/pkg/v1/partial"
27-
"github.com/google/go-containerregistry/pkg/v1/remote"
27+
"github.com/google/go-containerregistry/pkg/v1/sourcesink"
2828
"github.com/google/go-containerregistry/pkg/v1/stream"
2929
"github.com/google/go-containerregistry/pkg/v1/types"
3030
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
@@ -89,18 +89,16 @@ func unpackTaggable(t partial.WithRawManifest) ([]byte, *v1.Descriptor, error) {
8989
}, nil
9090
}
9191

92-
// Use partial.Artifact to unpack taggable.
93-
// Duplication is not a concern here.
94-
type pusher struct {
92+
type sink struct {
9593
path Path
9694
}
9795

9896
// Delete implements remote.Pusher.
99-
func (lp *pusher) Delete(_ context.Context, _ name.Reference) error {
97+
func (lp *sink) Delete(_ context.Context, _ name.Reference) error {
10098
return errors.New("unsupported operation")
10199
}
102100

103-
func (lp *pusher) writeLayer(l v1.Layer) error {
101+
func (lp *sink) writeLayer(l v1.Layer) error {
104102
dg, err := l.Digest()
105103
if err != nil {
106104
return err
@@ -116,7 +114,7 @@ func (lp *pusher) writeLayer(l v1.Layer) error {
116114
return nil
117115
}
118116

119-
func (lp *pusher) writeLayers(pctx context.Context, img v1.Image) error {
117+
func (lp *sink) writeLayers(pctx context.Context, img v1.Image) error {
120118
ls, err := img.Layers()
121119
if err != nil {
122120
return err
@@ -155,7 +153,7 @@ func (lp *pusher) writeLayers(pctx context.Context, img v1.Image) error {
155153
return g.Wait()
156154
}
157155

158-
func (lp *pusher) writeChildren(pctx context.Context, idx v1.ImageIndex) error {
156+
func (lp *sink) writeChildren(pctx context.Context, idx v1.ImageIndex) error {
159157
children, err := partial.Manifests(idx)
160158
if err != nil {
161159
return err
@@ -173,7 +171,7 @@ func (lp *pusher) writeChildren(pctx context.Context, idx v1.ImageIndex) error {
173171
return g.Wait()
174172
}
175173

176-
func (lp *pusher) writeDeps(ctx context.Context, m partial.Artifact) error {
174+
func (lp *sink) writeDeps(ctx context.Context, m partial.Artifact) error {
177175
if img, ok := m.(v1.Image); ok {
178176
return lp.writeLayers(ctx, img)
179177
}
@@ -186,7 +184,7 @@ func (lp *pusher) writeDeps(ctx context.Context, m partial.Artifact) error {
186184
return nil
187185
}
188186

189-
func (lp *pusher) writeManifest(ctx context.Context, t partial.WithRawManifest) error {
187+
func (lp *sink) writeManifest(ctx context.Context, t partial.WithRawManifest) error {
190188
m, err := taggableToManifest(t)
191189
if err != nil {
192190
return err
@@ -220,7 +218,7 @@ func (lp *pusher) writeManifest(ctx context.Context, t partial.WithRawManifest)
220218
return nil
221219
}
222220

223-
func (lp *pusher) writeChild(ctx context.Context, child partial.Describable, g *errgroup.Group) error {
221+
func (lp *sink) writeChild(ctx context.Context, child partial.Describable, g *errgroup.Group) error {
224222
switch child := child.(type) {
225223
case v1.ImageIndex:
226224
// For recursive index, we want to do a depth-first launching of goroutines
@@ -244,7 +242,7 @@ func (lp *pusher) writeChild(ctx context.Context, child partial.Describable, g *
244242
}
245243

246244
// Push implements remote.Pusher.
247-
func (lp *pusher) Push(ctx context.Context, ref name.Reference, t partial.WithRawManifest) error {
245+
func (lp *sink) Push(ctx context.Context, ref name.Reference, t partial.WithRawManifest) error {
248246
err := lp.writeManifest(ctx, t)
249247
if err != nil {
250248
return err
@@ -264,7 +262,7 @@ func (lp *pusher) Push(ctx context.Context, ref name.Reference, t partial.WithRa
264262
}
265263

266264
// Upload implements remote.Pusher.
267-
func (lp *pusher) Upload(_ context.Context, _ name.Repository, l v1.Layer) error {
265+
func (lp *sink) Upload(_ context.Context, _ name.Repository, l v1.Layer) error {
268266
digest, err := l.Digest()
269267
if err != nil {
270268
return err
@@ -276,10 +274,10 @@ func (lp *pusher) Upload(_ context.Context, _ name.Repository, l v1.Layer) error
276274
return lp.path.WriteBlob(digest, rc)
277275
}
278276

279-
func NewPusher(path Path) remote.Pusher {
280-
return &pusher{
277+
func NewSink(path Path) sourcesink.Sink {
278+
return &sink{
281279
path,
282280
}
283281
}
284282

285-
var _ remote.Pusher = (*pusher)(nil)
283+
var _ sourcesink.Sink = (*sink)(nil)

pkg/v1/layout/pusher_test.go renamed to pkg/v1/layout/sink_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ func TestCanPushRandomImage(t *testing.T) {
110110
t.Fatalf("random.Image() = %v", err)
111111
}
112112
path := mustOCILayout(t)
113-
pusher := NewPusher(path)
113+
sink := NewSink(path)
114114
ref := name.MustParseReference("local.random/image:latest")
115-
err = pusher.Push(context.TODO(), ref, img)
115+
err = sink.Push(context.TODO(), ref, img)
116116
if err != nil {
117-
t.Errorf("pusher.Push() = %v", err)
117+
t.Errorf("sink.Push() = %v", err)
118118
}
119119
mustHaveManifest(t, path, "local.random/image:latest")
120120
mustHaveBlobs(t, path, enumerateImageBlobs(t, img))
@@ -126,11 +126,11 @@ func TestCanPushRandomImageIndex(t *testing.T) {
126126
t.Fatalf("random.Index() = %v", err)
127127
}
128128
path := mustOCILayout(t)
129-
pusher := NewPusher(path)
129+
sink := NewSink(path)
130130
ref := name.MustParseReference("local.random/index:latest")
131-
err = pusher.Push(context.TODO(), ref, idx)
131+
err = sink.Push(context.TODO(), ref, idx)
132132
if err != nil {
133-
t.Errorf("pusher.Push() = %v", err)
133+
t.Errorf("sink.Push() = %v", err)
134134
}
135135
mustHaveManifest(t, path, "local.random/index:latest")
136136
mustHaveBlobs(t, path, enumerateImageIndexBlobs(t, idx))
@@ -147,12 +147,12 @@ func TestCanPushImageIndex(t *testing.T) {
147147
}
148148

149149
path := mustOCILayout(t)
150-
pusher := NewPusher(path)
150+
sink := NewSink(path)
151151
ref := name.MustParseReference("local.repo/index:latest")
152152

153-
err = pusher.Push(context.TODO(), ref, img)
153+
err = sink.Push(context.TODO(), ref, img)
154154
if err != nil {
155-
t.Errorf("pusher.Push() = %v", err)
155+
t.Errorf("sink.Push() = %v", err)
156156
}
157157
mustHaveManifest(t, path, "local.repo/index:latest")
158158
mustHaveBlobs(t, path, enumerateImageBlobs(t, img))
@@ -169,12 +169,12 @@ func TestCanPushImage(t *testing.T) {
169169
}
170170

171171
path := mustOCILayout(t)
172-
pusher := NewPusher(path)
172+
sink := NewSink(path)
173173
ref := name.MustParseReference("local.repo/index:latest")
174174

175-
err = pusher.Push(context.TODO(), ref, img)
175+
err = sink.Push(context.TODO(), ref, img)
176176
if err != nil {
177-
t.Errorf("pusher.Push() = %v", err)
177+
t.Errorf("sink.Push() = %v", err)
178178
}
179179
mustHaveManifest(t, path, "local.repo/index:latest")
180180
mustHaveBlobs(t, path, enumerateImageBlobs(t, img))
@@ -191,11 +191,11 @@ func TestCanPushImageWithLatestTag(t *testing.T) {
191191
}
192192

193193
path := mustOCILayout(t)
194-
pusher := NewPusher(path)
194+
sink := NewSink(path)
195195

196-
err = pusher.Push(context.TODO(), name.MustParseReference("reg.local.repo/index"), img)
196+
err = sink.Push(context.TODO(), name.MustParseReference("reg.local.repo/index"), img)
197197
if err != nil {
198-
t.Errorf("pusher.Push() = %v", err)
198+
t.Errorf("sink.Push() = %v", err)
199199
}
200200
mustHaveManifest(t, path, "reg.local.repo/index:latest")
201201
mustHaveBlobs(t, path, enumerateImageBlobs(t, img))

pkg/v1/layout/puller.go renamed to pkg/v1/layout/source.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ import (
2323

2424
"github.com/google/go-containerregistry/pkg/v1/partial"
2525
"github.com/google/go-containerregistry/pkg/v1/remote"
26+
"github.com/google/go-containerregistry/pkg/v1/sourcesink"
2627
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
2728
)
2829

29-
type puller struct {
30+
type source struct {
3031
path Path
3132
}
3233

33-
func NewPuller(path Path) remote.Puller {
34-
return &puller{
34+
func NewSource(path Path) sourcesink.Source {
35+
return &source{
3536
path,
3637
}
3738
}
3839

39-
var _ remote.Puller = (*puller)(nil)
40+
var _ sourcesink.Source = (*source)(nil)
4041

4142
// Artifact implements remote.Puller.
42-
func (p *puller) getDescriptor(ref name.Reference) (*v1.Descriptor, error) {
43+
func (p *source) getDescriptor(ref name.Reference) (*v1.Descriptor, error) {
4344
idx, err := p.path.ImageIndex()
4445
if err != nil {
4546
return nil, err
@@ -61,7 +62,7 @@ func (p *puller) getDescriptor(ref name.Reference) (*v1.Descriptor, error) {
6162
}
6263

6364
// Artifact implements remote.Puller.
64-
func (p *puller) Artifact(_ context.Context, ref name.Reference) (partial.Artifact, error) {
65+
func (p *source) Artifact(_ context.Context, ref name.Reference) (partial.Artifact, error) {
6566
desc, err := p.getDescriptor(ref)
6667
if err != nil {
6768
return nil, err
@@ -81,12 +82,12 @@ func (p *puller) Artifact(_ context.Context, ref name.Reference) (partial.Artifa
8182
}
8283

8384
// Head implements remote.Puller.
84-
func (p *puller) Head(_ context.Context, ref name.Reference) (*v1.Descriptor, error) {
85+
func (p *source) Head(_ context.Context, ref name.Reference) (*v1.Descriptor, error) {
8586
return p.getDescriptor(ref)
8687
}
8788

8889
// Layer implements remote.Puller.
89-
func (p *puller) Layer(_ context.Context, ref name.Digest) (v1.Layer, error) {
90+
func (p *source) Layer(_ context.Context, ref name.Digest) (v1.Layer, error) {
9091
h, err := v1.NewHash(ref.Identifier())
9192
if err != nil {
9293
return nil, err
@@ -102,31 +103,31 @@ func (p *puller) Layer(_ context.Context, ref name.Digest) (v1.Layer, error) {
102103
}
103104

104105
// List implements remote.Puller.
105-
func (*puller) List(_ context.Context, _ name.Repository) ([]string, error) {
106+
func (*source) List(_ context.Context, _ name.Repository) ([]string, error) {
106107
return nil, fmt.Errorf("unsupported operation")
107108
}
108109

109110
// Get implements remote.Puller.
110-
func (*puller) Get(_ context.Context, _ name.Reference) (*remote.Descriptor, error) {
111+
func (*source) Get(_ context.Context, _ name.Reference) (*remote.Descriptor, error) {
111112
return nil, fmt.Errorf("unsupported operation")
112113
}
113114

114115
// Lister implements remote.Puller.
115-
func (*puller) Lister(_ context.Context, _ name.Repository) (*remote.Lister, error) {
116+
func (*source) Lister(_ context.Context, _ name.Repository) (*remote.Lister, error) {
116117
return nil, fmt.Errorf("unsupported operation")
117118
}
118119

119120
// Catalogger implements remote.Puller.
120-
func (*puller) Catalogger(_ context.Context, _ name.Registry) (*remote.Catalogger, error) {
121+
func (*source) Catalogger(_ context.Context, _ name.Registry) (*remote.Catalogger, error) {
121122
return nil, fmt.Errorf("unsupported operation")
122123
}
123124

124125
// Catalog implements remote.Puller.
125-
func (*puller) Catalog(_ context.Context, _ name.Registry) ([]string, error) {
126+
func (*source) Catalog(_ context.Context, _ name.Registry) ([]string, error) {
126127
return nil, fmt.Errorf("unsupported operation")
127128
}
128129

129130
// Referrers implements remote.Puller.
130-
func (*puller) Referrers(_ context.Context, _ name.Digest, _ map[string]string) (v1.ImageIndex, error) {
131+
func (*source) Referrers(_ context.Context, _ name.Digest, _ map[string]string) (v1.ImageIndex, error) {
131132
return nil, fmt.Errorf("unsupported operation")
132133
}

0 commit comments

Comments
 (0)