This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
forked from regclient/regclient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blob.go
137 lines (128 loc) · 4.41 KB
/
blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package regclient
import (
"bytes"
"context"
"io"
"github.com/regclient/regclient/types"
"github.com/regclient/regclient/types/blob"
"github.com/regclient/regclient/types/ref"
"github.com/sirupsen/logrus"
)
// BlobCopy copies a blob between two locations
// If the blob already exists in the target, the copy is skipped
// A server side cross repository blob mount is attempted
func (rc *RegClient) BlobCopy(ctx context.Context, refSrc ref.Ref, refTgt ref.Ref, d types.Descriptor) error {
tDesc := d
tDesc.URLs = []string{} // ignore URLs when pushing to target
// for the same repository, there's nothing to copy
if ref.EqualRepository(refSrc, refTgt) {
rc.log.WithFields(logrus.Fields{
"src": refTgt.Reference,
"tgt": refTgt.Reference,
"digest": d.Digest,
}).Debug("Blob copy skipped, same repo")
return nil
}
// check if layer already exists
if _, err := rc.BlobHead(ctx, refTgt, tDesc); err == nil {
rc.log.WithFields(logrus.Fields{
"tgt": refTgt.Reference,
"digest": d,
}).Debug("Blob copy skipped, already exists")
return nil
}
// try mounting blob from the source repo is the registry is the same
if ref.EqualRegistry(refSrc, refTgt) {
err := rc.BlobMount(ctx, refSrc, refTgt, d)
if err == nil {
rc.log.WithFields(logrus.Fields{
"src": refTgt.Reference,
"tgt": refTgt.Reference,
"digest": d,
}).Debug("Blob copy performed server side with registry mount")
return nil
}
rc.log.WithFields(logrus.Fields{
"err": err,
"src": refSrc.Reference,
"tgt": refTgt.Reference,
}).Warn("Failed to mount blob")
}
// fast options failed, download layer from source and push to target
blobIO, err := rc.BlobGet(ctx, refSrc, d)
if err != nil {
rc.log.WithFields(logrus.Fields{
"err": err,
"src": refSrc.Reference,
"digest": d,
}).Warn("Failed to retrieve blob")
return err
}
defer blobIO.Close()
if _, err := rc.BlobPut(ctx, refTgt, blobIO.GetDescriptor(), blobIO); err != nil {
rc.log.WithFields(logrus.Fields{
"err": err,
"src": refSrc.Reference,
"tgt": refTgt.Reference,
}).Warn("Failed to push blob")
return err
}
return nil
}
// BlobDelete removes a blob from the registry
// This method should only be used to repair a damaged registry
// Typically a server side garbage collection should be used to purge unused blobs
func (rc *RegClient) BlobDelete(ctx context.Context, r ref.Ref, d types.Descriptor) error {
schemeAPI, err := rc.schemeGet(r.Scheme)
if err != nil {
return err
}
return schemeAPI.BlobDelete(ctx, r, d)
}
// BlobGet retrieves a blob, returning a reader
func (rc *RegClient) BlobGet(ctx context.Context, r ref.Ref, d types.Descriptor) (blob.Reader, error) {
data, err := d.GetData()
if err == nil {
return blob.NewReader(blob.WithDesc(d), blob.WithRef(r), blob.WithReader(bytes.NewReader(data))), nil
}
schemeAPI, err := rc.schemeGet(r.Scheme)
if err != nil {
return nil, err
}
return schemeAPI.BlobGet(ctx, r, d)
}
// BlobGetOCIConfig retrieves an OCI config from a blob, automatically extracting the JSON
func (rc *RegClient) BlobGetOCIConfig(ctx context.Context, ref ref.Ref, d types.Descriptor) (blob.OCIConfig, error) {
b, err := rc.BlobGet(ctx, ref, d)
if err != nil {
return nil, err
}
return b.ToOCIConfig()
}
// BlobHead is used to verify if a blob exists and is accessible
func (rc *RegClient) BlobHead(ctx context.Context, r ref.Ref, d types.Descriptor) (blob.Reader, error) {
schemeAPI, err := rc.schemeGet(r.Scheme)
if err != nil {
return nil, err
}
return schemeAPI.BlobHead(ctx, r, d)
}
// BlobMount attempts to perform a server side copy/mount of the blob between repositories
func (rc *RegClient) BlobMount(ctx context.Context, refSrc ref.Ref, refTgt ref.Ref, d types.Descriptor) error {
schemeAPI, err := rc.schemeGet(refSrc.Scheme)
if err != nil {
return err
}
return schemeAPI.BlobMount(ctx, refSrc, refTgt, d)
}
// BlobPut uploads a blob to a repository.
// This will attempt an anonymous blob mount first which some registries may support.
// It will then try doing a full put of the blob without chunking (most widely supported).
// If the full put fails, it will fall back to a chunked upload (useful for flaky networks).
func (rc *RegClient) BlobPut(ctx context.Context, ref ref.Ref, d types.Descriptor, rdr io.Reader) (types.Descriptor, error) {
schemeAPI, err := rc.schemeGet(ref.Scheme)
if err != nil {
return types.Descriptor{}, err
}
return schemeAPI.BlobPut(ctx, ref, d, rdr)
}