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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 images
import (
gocontext "context"
"errors"
"fmt"
"net/http/httptrace"
"os"
"sync"
"text/tabwriter"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
var pushCommand = cli.Command{
Name: "push",
Usage: "push an image to a remote",
ArgsUsage: "[flags] <remote> [<local>]",
Description: `Pushes an image reference from containerd.
All resources associated with the manifest reference will be pushed.
The ref is used to resolve to a locally existing image manifest.
The image manifest must exist before push. Creating a new image
manifest can be done through calculating the diff for layers,
creating the associated configuration, and creating the manifest
which references those resources.
`,
Flags: append(commands.RegistryFlags, cli.StringFlag{
Name: "manifest",
Usage: "digest of manifest",
}, cli.StringFlag{
Name: "manifest-type",
Usage: "media type of manifest digest",
Value: ocispec.MediaTypeImageManifest,
}, cli.StringSliceFlag{
Name: "platform",
Usage: "push content from a specific platform",
Value: &cli.StringSlice{},
}, cli.IntFlag{
Name: "max-concurrent-uploaded-layers",
Usage: "Set the max concurrent uploaded layers for each push",
}, cli.BoolFlag{
Name: "allow-non-distributable-blobs",
Usage: "Allow pushing blobs that are marked as non-distributable",
}),
Action: func(context *cli.Context) error {
var (
ref = context.Args().First()
local = context.Args().Get(1)
debug = context.GlobalBool("debug")
desc ocispec.Descriptor
)
if ref == "" {
return errors.New("please provide a remote image reference to push")
}
client, ctx, cancel, err := commands.NewClient(context)
if err != nil {
return err
}
defer cancel()
if manifest := context.String("manifest"); manifest != "" {
desc.Digest, err = digest.Parse(manifest)
if err != nil {
return fmt.Errorf("invalid manifest digest: %w", err)
}
desc.MediaType = context.String("manifest-type")
} else {
if local == "" {
local = ref
}
img, err := client.ImageService().Get(ctx, local)
if err != nil {
return fmt.Errorf("unable to resolve image to manifest: %w", err)
}
desc = img.Target
if pss := context.StringSlice("platform"); len(pss) == 1 {
p, err := platforms.Parse(pss[0])
if err != nil {
return fmt.Errorf("invalid platform %q: %w", pss[0], err)
}
cs := client.ContentStore()
if manifests, err := images.Children(ctx, cs, desc); err == nil && len(manifests) > 0 {
matcher := platforms.NewMatcher(p)
for _, manifest := range manifests {
if manifest.Platform != nil && matcher.Match(*manifest.Platform) {
if _, err := images.Children(ctx, cs, manifest); err != nil {
return fmt.Errorf("no matching manifest: %w", err)
}
desc = manifest
break
}
}
}
}
}
if context.Bool("http-trace") {
ctx = httptrace.WithClientTrace(ctx, commands.NewDebugClientTrace(ctx))
}
resolver, err := commands.GetResolver(ctx, context)
if err != nil {
return err
}
ongoing := newPushJobs(commands.PushTracker)
eg, ctx := errgroup.WithContext(ctx)
// used to notify the progress writer
doneCh := make(chan struct{})
eg.Go(func() error {
defer close(doneCh)
log.G(ctx).WithField("image", ref).WithField("digest", desc.Digest).Debug("pushing")
jobHandler := images.HandlerFunc(func(ctx gocontext.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if !context.Bool("allow-non-distributable-blobs") && images.IsNonDistributable(desc.MediaType) {
return nil, nil
}
ongoing.add(remotes.MakeRefKey(ctx, desc))
return nil, nil
})
handler := jobHandler
if !context.Bool("allow-non-distributable-blobs") {
handler = remotes.SkipNonDistributableBlobs(handler)
}
ropts := []containerd.RemoteOpt{
containerd.WithResolver(resolver),
containerd.WithImageHandler(handler),
}
if context.IsSet("max-concurrent-uploaded-layers") {
mcu := context.Int("max-concurrent-uploaded-layers")
ropts = append(ropts, containerd.WithMaxConcurrentUploadedLayers(mcu))
}
return client.Push(ctx, ref, desc, ropts...)
})
// don't show progress if debug mode is set
if !debug {
eg.Go(func() error {
var (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(os.Stdout)
start = time.Now()
done bool
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fw.Flush()
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)
content.Display(tw, ongoing.status(), start)
tw.Flush()
if done {
fw.Flush()
return nil
}
case <-doneCh:
done = true
case <-ctx.Done():
done = true // allow ui to update once more
}
}
})
}
return eg.Wait()
},
}
type pushjobs struct {
jobs map[string]struct{}
ordered []string
tracker docker.StatusTracker
mu sync.Mutex
}
func newPushJobs(tracker docker.StatusTracker) *pushjobs {
return &pushjobs{
jobs: make(map[string]struct{}),
tracker: tracker,
}
}
func (j *pushjobs) add(ref string) {
j.mu.Lock()
defer j.mu.Unlock()
if _, ok := j.jobs[ref]; ok {
return
}
j.ordered = append(j.ordered, ref)
j.jobs[ref] = struct{}{}
}
func (j *pushjobs) status() []content.StatusInfo {
j.mu.Lock()
defer j.mu.Unlock()
statuses := make([]content.StatusInfo, 0, len(j.jobs))
for _, name := range j.ordered {
si := content.StatusInfo{
Ref: name,
}
status, err := j.tracker.GetStatus(name)
if err != nil {
si.Status = "waiting"
} else {
si.Offset = status.Offset
si.Total = status.Total
si.StartedAt = status.StartedAt
si.UpdatedAt = status.UpdatedAt
if status.Offset >= status.Total {
if status.UploadUUID == "" {
si.Status = "done"
} else {
si.Status = "committing"
}
} else {
si.Status = "uploading"
}
}
statuses = append(statuses, si)
}
return statuses
}
|