forked from containerd/nerdctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.go
More file actions
119 lines (104 loc) · 3.84 KB
/
Copy pathpush.go
File metadata and controls
119 lines (104 loc) · 3.84 KB
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
/*
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 main
import (
"context"
"github.com/containerd/containerd/images/converter"
"github.com/containerd/containerd/platforms"
refdocker "github.com/containerd/containerd/reference/docker"
"github.com/containerd/containerd/remotes"
"github.com/containerd/nerdctl/pkg/imgutil"
"github.com/containerd/nerdctl/pkg/imgutil/dockerconfigresolver"
"github.com/containerd/nerdctl/pkg/imgutil/push"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var pushCommand = &cli.Command{
Name: "push",
Usage: "Push an image or a repository to a registry",
ArgsUsage: "NAME[:TAG]",
Action: pushAction,
BashComplete: pushBashComplete,
Flags: []cli.Flag{},
}
func pushAction(clicontext *cli.Context) error {
if clicontext.NArg() != 1 {
return errors.New("image name needs to be specified")
}
rawRef := clicontext.Args().First()
named, err := refdocker.ParseDockerRef(rawRef)
if err != nil {
return err
}
ref := named.String()
refDomain := refdocker.Domain(named)
insecure := clicontext.Bool("insecure-registry")
client, ctx, cancel, err := newClient(clicontext)
if err != nil {
return err
}
defer cancel()
// Push fails with "400 Bad Request" when the manifest is multi-platform but we do not locally have multi-platform blobs.
// So we create a tmp single-platform image to avoid the error.
// TODO: support pushing multi-platform
singlePlatform := platforms.DefaultStrict()
singlePlatformRef := ref + "-tmp-single"
singlePlatformImg, err := converter.Convert(ctx, client, singlePlatformRef, ref,
converter.WithPlatform(singlePlatform))
if err != nil {
return errors.Wrapf(err, "failed to create a tmp single-platform image %q", singlePlatformRef)
}
defer client.ImageService().Delete(context.TODO(), singlePlatformImg.Name)
logrus.Infof("pushing as a single-platform image (%s, %s)", singlePlatformImg.Target.MediaType, singlePlatformImg.Target.Digest)
pushFunc := func(r remotes.Resolver) error {
return push.Push(ctx, client, r, clicontext.App.Writer, singlePlatformRef, ref, singlePlatform)
}
var dOpts []dockerconfigresolver.Opt
if insecure {
logrus.Warnf("skipping verifying HTTPS certs for %q", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithSkipVerifyCerts(true))
}
resolver, err := dockerconfigresolver.New(refDomain, dOpts...)
if err != nil {
return err
}
if err = pushFunc(resolver); err != nil {
if !imgutil.IsErrHTTPResponseToHTTPSClient(err) {
return err
}
if insecure {
logrus.WithError(err).Warnf("server %q does not seem to support HTTPS, falling back to plain HTTP", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithPlainHTTP(true))
resolver, err = dockerconfigresolver.New(refDomain, dOpts...)
if err != nil {
return err
}
return pushFunc(resolver)
} else {
logrus.WithError(err).Errorf("server %q does not seem to support HTTPS", refDomain)
logrus.Info("Hint: you may want to try --insecure-registry to allow plain HTTP (if you are in a trusted network)")
return err
}
}
return nil
}
func pushBashComplete(clicontext *cli.Context) {
coco := parseCompletionContext(clicontext)
if coco.boring || coco.flagTakesValue {
defaultBashComplete(clicontext)
return
}
// show image names
bashCompleteImageNames(clicontext)
}