Skip to content

Commit a58f1d5

Browse files
authored
Propagate remote options through schema 1 copies (#1007)
The schema 1 copying code was very hacked together, but remote.Put simplifies things a bit. This extends some of the crane/gcrane options down through to legacy.CopySchema1.
1 parent 5a37827 commit a58f1d5

File tree

3 files changed

+7
-67
lines changed

3 files changed

+7
-67
lines changed

Diff for: internal/legacy/copy.go

+4-49
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ package legacy
1818
import (
1919
"bytes"
2020
"encoding/json"
21-
"fmt"
22-
"net/http"
23-
"net/url"
2421

25-
"github.com/google/go-containerregistry/pkg/authn"
26-
"github.com/google/go-containerregistry/pkg/logs"
2722
"github.com/google/go-containerregistry/pkg/name"
2823
"github.com/google/go-containerregistry/pkg/v1/remote"
29-
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
3024
)
3125

3226
// CopySchema1 allows `[g]crane cp` to work with old images without adding
3327
// full support for schema 1 images to this package.
34-
func CopySchema1(desc *remote.Descriptor, srcRef, dstRef name.Reference, srcAuth, dstAuth authn.Authenticator) error {
28+
func CopySchema1(desc *remote.Descriptor, srcRef, dstRef name.Reference, opts ...remote.Option) error {
3529
m := schema1{}
3630
if err := json.NewDecoder(bytes.NewReader(desc.Manifest)).Decode(&m); err != nil {
3731
return err
@@ -41,56 +35,17 @@ func CopySchema1(desc *remote.Descriptor, srcRef, dstRef name.Reference, srcAuth
4135
src := srcRef.Context().Digest(layer.BlobSum)
4236
dst := dstRef.Context().Digest(layer.BlobSum)
4337

44-
blob, err := remote.Layer(src, remote.WithAuth(srcAuth))
38+
blob, err := remote.Layer(src, opts...)
4539
if err != nil {
4640
return err
4741
}
4842

49-
if err := remote.WriteLayer(dst.Context(), blob, remote.WithAuth(dstAuth)); err != nil {
43+
if err := remote.WriteLayer(dst.Context(), blob, opts...); err != nil {
5044
return err
5145
}
5246
}
5347

54-
return putManifest(desc, dstRef, dstAuth)
55-
}
56-
57-
// TODO: perhaps expose this in remote?
58-
func putManifest(desc *remote.Descriptor, dstRef name.Reference, dstAuth authn.Authenticator) error {
59-
reg := dstRef.Context().Registry
60-
scopes := []string{dstRef.Scope(transport.PushScope)}
61-
62-
// TODO(jonjohnsonjr): Use NewWithContext.
63-
tr, err := transport.New(reg, dstAuth, http.DefaultTransport, scopes)
64-
if err != nil {
65-
return err
66-
}
67-
client := &http.Client{Transport: tr}
68-
69-
u := url.URL{
70-
Scheme: dstRef.Context().Registry.Scheme(),
71-
Host: dstRef.Context().RegistryStr(),
72-
Path: fmt.Sprintf("/v2/%s/manifests/%s", dstRef.Context().RepositoryStr(), dstRef.Identifier()),
73-
}
74-
75-
req, err := http.NewRequest(http.MethodPut, u.String(), bytes.NewBuffer(desc.Manifest))
76-
if err != nil {
77-
return err
78-
}
79-
req.Header.Set("Content-Type", string(desc.MediaType))
80-
81-
resp, err := client.Do(req)
82-
if err != nil {
83-
return err
84-
}
85-
defer resp.Body.Close()
86-
87-
if err := transport.CheckError(resp, http.StatusOK, http.StatusCreated, http.StatusAccepted); err != nil {
88-
return err
89-
}
90-
91-
// The image was successfully pushed!
92-
logs.Progress.Printf("%v: digest: %v size: %d", dstRef, desc.Digest, len(desc.Manifest))
93-
return nil
48+
return remote.Put(dstRef, desc, opts...)
9449
}
9550

9651
type fslayer struct {

Diff for: internal/legacy/copy_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222
"testing"
2323

24-
"github.com/google/go-containerregistry/pkg/authn"
2524
"github.com/google/go-containerregistry/pkg/name"
2625
"github.com/google/go-containerregistry/pkg/registry"
2726
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -87,11 +86,11 @@ func TestCopySchema1(t *testing.T) {
8786
},
8887
},
8988
}
90-
if err := putManifest(desc, dstRef, authn.Anonymous); err != nil {
89+
if err := remote.Put(dstRef, desc); err != nil {
9190
t.Fatal(err)
9291
}
9392

94-
if err := CopySchema1(desc, srcRef, dstRef, authn.Anonymous, authn.Anonymous); err != nil {
93+
if err := CopySchema1(desc, srcRef, dstRef); err != nil {
9594
t.Errorf("failed to copy schema 1: %v", err)
9695
}
9796
}

Diff for: pkg/crane/copy.go

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

2020
"github.com/google/go-containerregistry/internal/legacy"
21-
"github.com/google/go-containerregistry/pkg/authn"
2221
"github.com/google/go-containerregistry/pkg/logs"
2322
"github.com/google/go-containerregistry/pkg/name"
2423
"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -59,7 +58,7 @@ func Copy(src, dst string, opt ...Option) error {
5958
}
6059
case types.DockerManifestSchema1, types.DockerManifestSchema1Signed:
6160
// Handle schema 1 images separately.
62-
if err := copySchema1(desc, srcRef, dstRef); err != nil {
61+
if err := legacy.CopySchema1(desc, srcRef, dstRef, o.remote...); err != nil {
6362
return fmt.Errorf("failed to copy schema 1 image: %v", err)
6463
}
6564
default:
@@ -87,16 +86,3 @@ func copyIndex(desc *remote.Descriptor, dstRef name.Reference, o options) error
8786
}
8887
return remote.WriteIndex(dstRef, idx, o.remote...)
8988
}
90-
91-
func copySchema1(desc *remote.Descriptor, srcRef, dstRef name.Reference) error {
92-
srcAuth, err := authn.DefaultKeychain.Resolve(srcRef.Context().Registry)
93-
if err != nil {
94-
return err
95-
}
96-
dstAuth, err := authn.DefaultKeychain.Resolve(dstRef.Context().Registry)
97-
if err != nil {
98-
return err
99-
}
100-
101-
return legacy.CopySchema1(desc, srcRef, dstRef, srcAuth, dstAuth)
102-
}

0 commit comments

Comments
 (0)