2015-11-18 22:18:44 +00:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
2015-12-14 23:44:45 +00:00
|
|
|
"errors"
|
2015-11-18 22:18:44 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-11-14 00:59:01 +00:00
|
|
|
"sync"
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-12-11 00:27:58 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2016-01-14 03:34:27 +00:00
|
|
|
distreference "github.com/docker/distribution/reference"
|
2015-12-11 00:27:58 +00:00
|
|
|
"github.com/docker/distribution/registry/client"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/distribution/metadata"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/distribution/xfer"
|
2015-12-14 23:44:45 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-12-04 21:55:15 +00:00
|
|
|
"github.com/docker/docker/reference"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-12-21 23:02:44 +00:00
|
|
|
// PushResult contains the tag, manifest digest, and manifest size from the
|
|
|
|
// push. It's used to signal this information to the trust code in the client
|
|
|
|
// so it can sign the manifest if necessary.
|
|
|
|
type PushResult struct {
|
|
|
|
Tag string
|
|
|
|
Digest digest.Digest
|
|
|
|
Size int
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:44 +00:00
|
|
|
type v2Pusher struct {
|
2016-01-14 03:34:27 +00:00
|
|
|
v2MetadataService *metadata.V2MetadataService
|
|
|
|
ref reference.Named
|
|
|
|
endpoint registry.APIEndpoint
|
|
|
|
repoInfo *registry.RepositoryInfo
|
|
|
|
config *ImagePushConfig
|
|
|
|
repo distribution.Repository
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-03-01 18:56:05 +00:00
|
|
|
// pushState is state built by the Upload functions.
|
2015-12-11 00:27:58 +00:00
|
|
|
pushState pushState
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
type pushState struct {
|
2015-11-14 00:59:01 +00:00
|
|
|
sync.Mutex
|
2015-12-11 00:27:58 +00:00
|
|
|
// remoteLayers is the set of layers known to exist on the remote side.
|
|
|
|
// This avoids redundant queries when pushing multiple tags that
|
|
|
|
// involve the same layers. It is also used to fill in digest and size
|
|
|
|
// information when building the manifest.
|
|
|
|
remoteLayers map[layer.DiffID]distribution.Descriptor
|
|
|
|
// confirmedV2 is set to true if we confirm we're talking to a v2
|
|
|
|
// registry. This is used to limit fallbacks to the v1 protocol.
|
|
|
|
confirmedV2 bool
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
func (p *v2Pusher) Push(ctx context.Context) (err error) {
|
2015-12-11 00:27:58 +00:00
|
|
|
p.pushState.remoteLayers = make(map[layer.DiffID]distribution.Descriptor)
|
|
|
|
|
|
|
|
p.repo, p.pushState.confirmedV2, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "push", "pull")
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error getting v2 registry: %v", err)
|
2016-02-11 23:45:29 +00:00
|
|
|
return err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
if err = p.pushV2Repository(ctx); err != nil {
|
2016-02-11 22:08:49 +00:00
|
|
|
if continueOnError(err) {
|
2016-02-11 23:45:29 +00:00
|
|
|
return fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: p.pushState.confirmedV2,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
2015-12-04 21:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Pusher) pushV2Repository(ctx context.Context) (err error) {
|
2015-12-14 23:44:45 +00:00
|
|
|
if namedTagged, isNamedTagged := p.ref.(reference.NamedTagged); isNamedTagged {
|
2015-12-04 21:55:15 +00:00
|
|
|
imageID, err := p.config.ReferenceStore.Get(p.ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-04 21:42:33 +00:00
|
|
|
return fmt.Errorf("tag does not exist: %s", p.ref.String())
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
return p.pushV2Tag(ctx, namedTagged, imageID)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-12-14 23:44:45 +00:00
|
|
|
|
|
|
|
if !reference.IsNameOnly(p.ref) {
|
|
|
|
return errors.New("cannot push a digest reference")
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
// Pull all tags
|
|
|
|
pushed := 0
|
|
|
|
for _, association := range p.config.ReferenceStore.ReferencesByName(p.ref) {
|
|
|
|
if namedTagged, isNamedTagged := association.Ref.(reference.NamedTagged); isNamedTagged {
|
|
|
|
pushed++
|
|
|
|
if err := p.pushV2Tag(ctx, namedTagged, association.ImageID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
if pushed == 0 {
|
|
|
|
return fmt.Errorf("no tags to push for %s", p.repoInfo.Name())
|
|
|
|
}
|
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
return nil
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
func (p *v2Pusher) pushV2Tag(ctx context.Context, ref reference.NamedTagged, imageID image.ID) error {
|
2015-11-18 22:18:44 +00:00
|
|
|
logrus.Debugf("Pushing repository: %s", ref.String())
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
img, err := p.config.ImageStore.Get(imageID)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not find image from tag %s: %v", ref.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var l layer.Layer
|
|
|
|
|
|
|
|
topLayerID := img.RootFS.ChainID()
|
|
|
|
if topLayerID == "" {
|
|
|
|
l = layer.EmptyLayer
|
|
|
|
} else {
|
|
|
|
l, err = p.config.LayerStore.Get(topLayerID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get top layer from image: %v", err)
|
|
|
|
}
|
|
|
|
defer layer.ReleaseAndLog(p.config.LayerStore, l)
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
var descriptors []xfer.UploadDescriptor
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
descriptorTemplate := v2PushDescriptor{
|
2016-01-14 03:34:27 +00:00
|
|
|
v2MetadataService: p.v2MetadataService,
|
|
|
|
repoInfo: p.repoInfo,
|
2016-06-18 05:27:03 +00:00
|
|
|
ref: p.ref,
|
2016-01-14 03:34:27 +00:00
|
|
|
repo: p.repo,
|
|
|
|
pushState: &p.pushState,
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
// Loop bounds condition is to avoid pushing the base layer on Windows.
|
2015-11-18 22:18:44 +00:00
|
|
|
for i := 0; i < len(img.RootFS.DiffIDs); i++ {
|
2015-12-04 21:42:33 +00:00
|
|
|
descriptor := descriptorTemplate
|
|
|
|
descriptor.layer = l
|
|
|
|
descriptors = append(descriptors, &descriptor)
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
l = l.Parent()
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
if err := p.config.UploadManager.Upload(ctx, descriptors, p.config.ProgressOutput); err != nil {
|
2015-11-14 00:59:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
// Try schema2 first
|
|
|
|
builder := schema2.NewManifestBuilder(p.repo.Blobs(ctx), img.RawJSON())
|
|
|
|
manifest, err := manifestFromBuilder(ctx, builder, descriptors)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
manSvc, err := p.repo.Manifests(ctx)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-14 23:44:45 +00:00
|
|
|
|
2016-04-12 00:29:17 +00:00
|
|
|
putOptions := []distribution.ManifestServiceOption{distribution.WithTag(ref.Tag())}
|
2015-12-14 23:44:45 +00:00
|
|
|
if _, err = manSvc.Put(ctx, manifest, putOptions...); err != nil {
|
|
|
|
logrus.Warnf("failed to upload schema2 manifest: %v - falling back to schema1", err)
|
|
|
|
|
2016-01-26 19:20:14 +00:00
|
|
|
manifestRef, err := distreference.WithTag(p.repo.Named(), ref.Tag())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
builder = schema1.NewConfigManifestBuilder(p.repo.Blobs(ctx), p.config.TrustKey, manifestRef, img.RawJSON())
|
2015-12-14 23:44:45 +00:00
|
|
|
manifest, err = manifestFromBuilder(ctx, builder, descriptors)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = manSvc.Put(ctx, manifest, putOptions...); err != nil {
|
|
|
|
return err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
var canonicalManifest []byte
|
|
|
|
|
|
|
|
switch v := manifest.(type) {
|
|
|
|
case *schema1.SignedManifest:
|
|
|
|
canonicalManifest = v.Canonical
|
|
|
|
case *schema2.DeserializedManifest:
|
|
|
|
_, canonicalManifest, err = v.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-12-11 00:27:58 +00:00
|
|
|
|
2015-12-14 23:44:45 +00:00
|
|
|
manifestDigest := digest.FromBytes(canonicalManifest)
|
|
|
|
progress.Messagef(p.config.ProgressOutput, "", "%s: digest: %s size: %d", ref.Tag(), manifestDigest, len(canonicalManifest))
|
|
|
|
// Signal digest to the trust client so it can sign the
|
|
|
|
// push, if appropriate.
|
|
|
|
progress.Aux(p.config.ProgressOutput, PushResult{Tag: ref.Tag(), Digest: manifestDigest, Size: len(canonicalManifest)})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func manifestFromBuilder(ctx context.Context, builder distribution.ManifestBuilder, descriptors []xfer.UploadDescriptor) (distribution.Manifest, error) {
|
|
|
|
// descriptors is in reverse order; iterate backwards to get references
|
|
|
|
// appended in the right order.
|
|
|
|
for i := len(descriptors) - 1; i >= 0; i-- {
|
|
|
|
if err := builder.AppendReference(descriptors[i].(*v2PushDescriptor)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-11 00:27:58 +00:00
|
|
|
}
|
2015-12-14 23:44:45 +00:00
|
|
|
|
|
|
|
return builder.Build(ctx)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
type v2PushDescriptor struct {
|
2016-01-14 03:34:27 +00:00
|
|
|
layer layer.Layer
|
|
|
|
v2MetadataService *metadata.V2MetadataService
|
|
|
|
repoInfo reference.Named
|
2016-06-18 05:27:03 +00:00
|
|
|
ref reference.Named
|
2016-01-14 03:34:27 +00:00
|
|
|
repo distribution.Repository
|
|
|
|
pushState *pushState
|
2016-03-01 18:56:05 +00:00
|
|
|
remoteDescriptor distribution.Descriptor
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *v2PushDescriptor) Key() string {
|
2016-06-18 05:27:03 +00:00
|
|
|
return "v2push:" + pd.ref.FullName() + " " + pd.layer.DiffID().String()
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *v2PushDescriptor) ID() string {
|
|
|
|
return stringid.TruncateID(pd.layer.DiffID().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *v2PushDescriptor) DiffID() layer.DiffID {
|
|
|
|
return pd.layer.DiffID()
|
|
|
|
}
|
|
|
|
|
2016-03-01 18:56:05 +00:00
|
|
|
func (pd *v2PushDescriptor) Upload(ctx context.Context, progressOutput progress.Output) (distribution.Descriptor, error) {
|
2016-06-07 00:49:34 +00:00
|
|
|
if fs, ok := pd.layer.(distribution.Describable); ok {
|
|
|
|
if d := fs.Descriptor(); len(d.URLs) > 0 {
|
2016-05-26 02:11:51 +00:00
|
|
|
progress.Update(progressOutput, pd.ID(), "Skipped foreign layer")
|
2016-06-07 00:49:34 +00:00
|
|
|
return d, nil
|
2016-05-26 02:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
diffID := pd.DiffID()
|
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
pd.pushState.Lock()
|
2016-03-01 18:56:05 +00:00
|
|
|
if descriptor, ok := pd.pushState.remoteLayers[diffID]; ok {
|
2015-12-11 00:27:58 +00:00
|
|
|
// it is already known that the push is not needed and
|
|
|
|
// therefore doing a stat is unnecessary
|
|
|
|
pd.pushState.Unlock()
|
|
|
|
progress.Update(progressOutput, pd.ID(), "Layer already exists")
|
2016-03-01 18:56:05 +00:00
|
|
|
return descriptor, nil
|
2015-12-11 00:27:58 +00:00
|
|
|
}
|
|
|
|
pd.pushState.Unlock()
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-01-14 03:34:27 +00:00
|
|
|
// Do we have any metadata associated with this layer's DiffID?
|
|
|
|
v2Metadata, err := pd.v2MetadataService.GetMetadata(diffID)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err == nil {
|
2016-01-14 03:34:27 +00:00
|
|
|
descriptor, exists, err := layerAlreadyExists(ctx, v2Metadata, pd.repoInfo, pd.repo, pd.pushState)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-11-14 00:59:01 +00:00
|
|
|
progress.Update(progressOutput, pd.ID(), "Image push failed")
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, retryOnError(err)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
if exists {
|
2015-11-14 00:59:01 +00:00
|
|
|
progress.Update(progressOutput, pd.ID(), "Layer already exists")
|
2015-12-11 00:27:58 +00:00
|
|
|
pd.pushState.Lock()
|
|
|
|
pd.pushState.remoteLayers[diffID] = descriptor
|
|
|
|
pd.pushState.Unlock()
|
2016-03-01 18:56:05 +00:00
|
|
|
return descriptor, nil
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
logrus.Debugf("Pushing layer: %s", diffID)
|
|
|
|
|
2015-11-18 22:18:44 +00:00
|
|
|
// if digest was empty or not saved, or if blob does not exist on the remote repository,
|
|
|
|
// then push the blob.
|
2015-11-14 00:59:01 +00:00
|
|
|
bs := pd.repo.Blobs(ctx)
|
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
var layerUpload distribution.BlobWriter
|
|
|
|
mountAttemptsRemaining := 3
|
|
|
|
|
|
|
|
// Attempt to find another repository in the same registry to mount the layer
|
|
|
|
// from to avoid an unnecessary upload.
|
|
|
|
// Note: metadata is stored from oldest to newest, so we iterate through this
|
|
|
|
// slice in reverse to maximize our chances of the blob still existing in the
|
|
|
|
// remote repository.
|
|
|
|
for i := len(v2Metadata) - 1; i >= 0 && mountAttemptsRemaining > 0; i-- {
|
|
|
|
mountFrom := v2Metadata[i]
|
|
|
|
|
|
|
|
sourceRepo, err := reference.ParseNamed(mountFrom.SourceRepository)
|
2016-01-05 22:17:42 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-22 19:27:17 +00:00
|
|
|
if pd.repoInfo.Hostname() != sourceRepo.Hostname() {
|
|
|
|
// don't mount blobs from another registry
|
|
|
|
continue
|
2016-01-14 03:34:27 +00:00
|
|
|
}
|
2016-01-05 22:17:42 +00:00
|
|
|
|
2016-01-14 03:34:27 +00:00
|
|
|
namedRef, err := reference.WithName(mountFrom.SourceRepository)
|
|
|
|
if err != nil {
|
2016-02-22 19:27:17 +00:00
|
|
|
continue
|
2016-01-14 03:34:27 +00:00
|
|
|
}
|
2016-01-05 22:17:42 +00:00
|
|
|
|
2016-01-14 03:34:27 +00:00
|
|
|
// TODO (brianbland): We need to construct a reference where the Name is
|
|
|
|
// only the full remote name, so clean this up when distribution has a
|
|
|
|
// richer reference package
|
|
|
|
remoteRef, err := distreference.WithName(namedRef.RemoteName())
|
|
|
|
if err != nil {
|
2016-02-22 19:27:17 +00:00
|
|
|
continue
|
2016-01-14 03:34:27 +00:00
|
|
|
}
|
2016-01-05 22:17:42 +00:00
|
|
|
|
2016-01-14 03:34:27 +00:00
|
|
|
canonicalRef, err := distreference.WithDigest(remoteRef, mountFrom.Digest)
|
|
|
|
if err != nil {
|
2016-02-22 19:27:17 +00:00
|
|
|
continue
|
2016-01-05 22:17:42 +00:00
|
|
|
}
|
2016-01-14 03:34:27 +00:00
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
logrus.Debugf("attempting to mount layer %s (%s) from %s", diffID, mountFrom.Digest, sourceRepo.FullName())
|
2016-01-05 22:17:42 +00:00
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
layerUpload, err = bs.Create(ctx, client.WithMountFrom(canonicalRef))
|
|
|
|
switch err := err.(type) {
|
|
|
|
case distribution.ErrBlobMounted:
|
|
|
|
progress.Updatef(progressOutput, pd.ID(), "Mounted from %s", err.From.Name())
|
2016-01-14 03:34:27 +00:00
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
err.Descriptor.MediaType = schema2.MediaTypeLayer
|
2016-01-20 19:39:32 +00:00
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
pd.pushState.Lock()
|
|
|
|
pd.pushState.confirmedV2 = true
|
|
|
|
pd.pushState.remoteLayers[diffID] = err.Descriptor
|
|
|
|
pd.pushState.Unlock()
|
2016-01-14 03:34:27 +00:00
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
// Cache mapping from this layer's DiffID to the blobsum
|
|
|
|
if err := pd.v2MetadataService.Add(diffID, metadata.V2Metadata{Digest: mountFrom.Digest, SourceRepository: pd.repoInfo.FullName()}); err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, xfer.DoNotRetry{Err: err}
|
2016-02-22 19:27:17 +00:00
|
|
|
}
|
2016-03-01 18:56:05 +00:00
|
|
|
return err.Descriptor, nil
|
2016-02-22 19:27:17 +00:00
|
|
|
case nil:
|
|
|
|
// blob upload session created successfully, so begin the upload
|
|
|
|
mountAttemptsRemaining = 0
|
|
|
|
default:
|
|
|
|
// unable to mount layer from this repository, so this source mapping is no longer valid
|
|
|
|
logrus.Debugf("unassociating layer %s (%s) with %s", diffID, mountFrom.Digest, mountFrom.SourceRepository)
|
|
|
|
pd.v2MetadataService.Remove(mountFrom)
|
|
|
|
mountAttemptsRemaining--
|
2016-01-14 03:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 19:27:17 +00:00
|
|
|
if layerUpload == nil {
|
|
|
|
layerUpload, err = bs.Create(ctx)
|
|
|
|
if err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, retryOnError(err)
|
2016-02-22 19:27:17 +00:00
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
defer layerUpload.Close()
|
|
|
|
|
|
|
|
arch, err := pd.layer.TarStream()
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, xfer.DoNotRetry{Err: err}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
|
|
|
|
// don't care if this fails; best effort
|
|
|
|
size, _ := pd.layer.DiffSize()
|
|
|
|
|
|
|
|
reader := progress.NewProgressReader(ioutils.NewCancelReadCloser(ctx, arch), progressOutput, size, pd.ID(), "Pushing")
|
2016-01-29 22:34:50 +00:00
|
|
|
compressedReader, compressionDone := compress(reader)
|
|
|
|
defer func() {
|
|
|
|
reader.Close()
|
|
|
|
<-compressionDone
|
|
|
|
}()
|
2015-11-14 00:59:01 +00:00
|
|
|
|
|
|
|
digester := digest.Canonical.New()
|
|
|
|
tee := io.TeeReader(compressedReader, digester.Hash())
|
|
|
|
|
|
|
|
nn, err := layerUpload.ReadFrom(tee)
|
|
|
|
compressedReader.Close()
|
|
|
|
if err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, retryOnError(err)
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pushDigest := digester.Digest()
|
|
|
|
if _, err := layerUpload.Commit(ctx, distribution.Descriptor{Digest: pushDigest}); err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, retryOnError(err)
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("uploaded layer %s (%s), %d bytes", diffID, pushDigest, nn)
|
|
|
|
progress.Update(progressOutput, pd.ID(), "Pushed")
|
|
|
|
|
2015-11-18 22:18:44 +00:00
|
|
|
// Cache mapping from this layer's DiffID to the blobsum
|
2016-01-14 03:34:27 +00:00
|
|
|
if err := pd.v2MetadataService.Add(diffID, metadata.V2Metadata{Digest: pushDigest, SourceRepository: pd.repoInfo.FullName()}); err != nil {
|
2016-03-01 18:56:05 +00:00
|
|
|
return distribution.Descriptor{}, xfer.DoNotRetry{Err: err}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
pd.pushState.Lock()
|
|
|
|
|
2016-02-11 23:21:52 +00:00
|
|
|
// If Commit succeeded, that's an indication that the remote registry
|
2015-12-11 00:27:58 +00:00
|
|
|
// speaks the v2 protocol.
|
|
|
|
pd.pushState.confirmedV2 = true
|
|
|
|
|
2016-03-01 18:56:05 +00:00
|
|
|
descriptor := distribution.Descriptor{
|
2015-12-11 00:27:58 +00:00
|
|
|
Digest: pushDigest,
|
|
|
|
MediaType: schema2.MediaTypeLayer,
|
|
|
|
Size: nn,
|
|
|
|
}
|
2016-03-01 18:56:05 +00:00
|
|
|
pd.pushState.remoteLayers[diffID] = descriptor
|
2015-12-11 00:27:58 +00:00
|
|
|
|
|
|
|
pd.pushState.Unlock()
|
|
|
|
|
2016-03-01 18:56:05 +00:00
|
|
|
return descriptor, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *v2PushDescriptor) SetRemoteDescriptor(descriptor distribution.Descriptor) {
|
|
|
|
pd.remoteDescriptor = descriptor
|
2015-12-11 00:27:58 +00:00
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
|
2015-12-11 00:27:58 +00:00
|
|
|
func (pd *v2PushDescriptor) Descriptor() distribution.Descriptor {
|
2016-03-01 18:56:05 +00:00
|
|
|
return pd.remoteDescriptor
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 03:34:27 +00:00
|
|
|
// layerAlreadyExists checks if the registry already know about any of the
|
|
|
|
// metadata passed in the "metadata" slice. If it finds one that the registry
|
2015-11-18 22:18:44 +00:00
|
|
|
// knows about, it returns the known digest and "true".
|
2016-01-14 03:34:27 +00:00
|
|
|
func layerAlreadyExists(ctx context.Context, metadata []metadata.V2Metadata, repoInfo reference.Named, repo distribution.Repository, pushState *pushState) (distribution.Descriptor, bool, error) {
|
|
|
|
for _, meta := range metadata {
|
2016-01-05 22:17:42 +00:00
|
|
|
// Only check blobsums that are known to this repository or have an unknown source
|
2016-01-14 03:34:27 +00:00
|
|
|
if meta.SourceRepository != "" && meta.SourceRepository != repoInfo.FullName() {
|
2016-01-05 22:17:42 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-01-14 03:34:27 +00:00
|
|
|
descriptor, err := repo.Blobs(ctx).Stat(ctx, meta.Digest)
|
2015-11-18 22:18:44 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2015-12-11 00:27:58 +00:00
|
|
|
descriptor.MediaType = schema2.MediaTypeLayer
|
|
|
|
return descriptor, true, nil
|
2015-11-18 22:18:44 +00:00
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
// nop
|
|
|
|
default:
|
2015-12-11 00:27:58 +00:00
|
|
|
return distribution.Descriptor{}, false, err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-11 00:27:58 +00:00
|
|
|
return distribution.Descriptor{}, false, nil
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|