/
pusher.go
174 lines (153 loc) · 5.15 KB
/
pusher.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You
* may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package ecr
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/reference"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
var (
errLayerNotFound = errors.New("ecr: layer not found")
)
// ecrPusher implements the containerd remotes.Pusher interface and can be used
// to push images to Amazon ECR.
type ecrPusher struct {
ecrBase
tracker docker.StatusTracker
}
var _ remotes.Pusher = (*ecrPusher)(nil)
func (p ecrPusher) Push(ctx context.Context, desc ocispec.Descriptor) (content.Writer, error) {
ctx = log.WithLogger(ctx, log.G(ctx).WithField("desc", desc))
log.G(ctx).Debug("ecr.push")
switch desc.MediaType {
case
images.MediaTypeDockerSchema1Manifest,
images.MediaTypeDockerSchema2Manifest,
images.MediaTypeDockerSchema2ManifestList,
ocispec.MediaTypeImageIndex,
ocispec.MediaTypeImageManifest:
return p.pushManifest(ctx, desc)
default:
return p.pushBlob(ctx, desc)
}
}
func (p ecrPusher) pushManifest(ctx context.Context, desc ocispec.Descriptor) (content.Writer, error) {
log.G(ctx).Debug("ecr.pusher.manifest")
exists, err := p.checkManifestExistence(ctx, desc)
if err != nil {
log.G(ctx).WithError(err).
Error("ecr.pusher.manifest: failed to check existence")
return nil, err
}
if exists {
log.G(ctx).Debug("ecr.pusher.manifest: content already on remote")
p.markStatusExists(ctx, desc)
return nil, fmt.Errorf("content %v on remote: %w", desc.Digest, errdefs.ErrAlreadyExists)
}
ref := p.markStatusStarted(ctx, desc)
return &manifestWriter{
ctx: ctx,
base: &p.ecrBase,
desc: desc,
tracker: p.tracker,
ref: ref,
}, nil
}
func (p ecrPusher) checkManifestExistence(ctx context.Context, desc ocispec.Descriptor) (bool, error) {
image, err := p.getImageByDescriptor(ctx, desc)
if err != nil {
if err == errImageNotFound {
return false, nil
}
return false, err
}
if image == nil {
return false, errors.New("ecr.pusher.manifest: unexpected nil image")
}
found := desc.Digest.String() == aws.StringValue(image.ImageId.ImageDigest)
return found, nil
}
func (p ecrPusher) pushBlob(ctx context.Context, desc ocispec.Descriptor) (content.Writer, error) {
log.G(ctx).Debug("ecr.pusher.blob")
exists, err := p.checkBlobExistence(ctx, desc)
if err != nil {
log.G(ctx).WithError(err).
Error("ecr.pusher.blob: failed to check existence")
return nil, err
}
if exists {
log.G(ctx).Debug("ecr.pusher.blob: content already on remote")
p.markStatusExists(ctx, desc)
return nil, fmt.Errorf("content %v on remote: %w", desc.Digest, errdefs.ErrAlreadyExists)
}
ref := p.markStatusStarted(ctx, desc)
return newLayerWriter(&p.ecrBase, p.tracker, ref, desc)
}
func (p ecrPusher) checkBlobExistence(ctx context.Context, desc ocispec.Descriptor) (bool, error) {
batchCheckLayerAvailabilityInput := &ecr.BatchCheckLayerAvailabilityInput{
RegistryId: aws.String(p.ecrSpec.Registry()),
RepositoryName: aws.String(p.ecrSpec.Repository),
LayerDigests: []*string{aws.String(desc.Digest.String())},
}
batchCheckLayerAvailabilityOutput, err := p.client.BatchCheckLayerAvailabilityWithContext(ctx, batchCheckLayerAvailabilityInput)
if err != nil {
log.G(ctx).WithError(err).Error("ecr.pusher.blob: failed to check availability")
return false, err
}
log.G(ctx).
WithField("batchCheckLayerAvailability", batchCheckLayerAvailabilityOutput).
Debug("ecr.pusher.blob")
if len(batchCheckLayerAvailabilityOutput.Layers) == 0 {
if len(batchCheckLayerAvailabilityOutput.Failures) > 0 {
return false, errLayerNotFound
}
return false, reference.ErrInvalid
}
layer := batchCheckLayerAvailabilityOutput.Layers[0]
return aws.StringValue(layer.LayerAvailability) == ecr.LayerAvailabilityAvailable, nil
}
func (p ecrPusher) markStatusExists(ctx context.Context, desc ocispec.Descriptor) string {
ref := remotes.MakeRefKey(ctx, desc)
p.tracker.SetStatus(ref, docker.Status{
Status: content.Status{
Ref: ref,
UpdatedAt: time.Now(),
},
})
return ref
}
func (p ecrPusher) markStatusStarted(ctx context.Context, desc ocispec.Descriptor) string {
ref := remotes.MakeRefKey(ctx, desc)
p.tracker.SetStatus(ref, docker.Status{
Status: content.Status{
Ref: ref,
Total: desc.Size,
Expected: desc.Digest,
StartedAt: time.Now(),
},
})
return ref
}