-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
go
committed
Sep 27, 2024
1 parent
49a7103
commit a6ec2ce
Showing
6 changed files
with
221 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
commonFlag "github.com/containers/common/pkg/flag" | ||
"github.com/containers/common/pkg/retry" | ||
"github.com/containers/image/v5/copy" | ||
"github.com/containers/image/v5/types" | ||
) | ||
|
||
type copyOptions struct { | ||
global *globalOptions | ||
deprecatedTLSVerify *deprecatedTLSVerifyOption | ||
srcImage *imageOptions | ||
destImage *imageDestOptions | ||
retryOpts *retry.Options | ||
format commonFlag.OptionalString // Force conversion of the image to a specified format | ||
quiet bool // Suppress output information when copying images | ||
} | ||
|
||
type buildImageRefer func(string) (types.ImageReference, *types.SystemContext, error) | ||
|
||
func (opts *copyOptions) execCopy(args []string, stdout io.Writer, s buildImageRefer, d buildImageRefer) (retErr error) { | ||
if len(args) != 1 { | ||
return errorShouldDisplayUsage{errors.New("image is required")} | ||
} | ||
opts.deprecatedTLSVerify.warnIfUsed([]string{"--src-tls-verify", "--dest-tls-verify"}) | ||
imageName := args[0] | ||
|
||
policyContext, err := opts.global.getPolicyContext() | ||
if err != nil { | ||
return fmt.Errorf("error loading trust policy: %v", err) | ||
} | ||
defer func() { | ||
if err := policyContext.Destroy(); err != nil { | ||
retErr = noteCloseFailure(retErr, "tearing down policy context", err) | ||
} | ||
}() | ||
|
||
srcRef, sourceCtx, err := s(imageName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
destRef, destCtx, err := d(imageName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var manifestType string | ||
if opts.format.Present() { | ||
manifestType, err = parseManifestFormat(opts.format.Value()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
ctx, cancel := opts.global.commandTimeoutContext() | ||
defer cancel() | ||
|
||
if opts.quiet { | ||
stdout = nil | ||
} | ||
|
||
opts.destImage.warnAboutIneffectiveOptions(destRef.Transport()) | ||
|
||
return retry.IfNecessary(ctx, func() error { | ||
_, err := copy.Image(ctx, policyContext, destRef, srcRef, ©.Options{ | ||
ReportWriter: stdout, | ||
SourceCtx: sourceCtx, | ||
DestinationCtx: destCtx, | ||
ForceManifestMIMEType: manifestType, | ||
ImageListSelection: copy.CopySystemImage, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, opts.retryOpts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
commonFlag "github.com/containers/common/pkg/flag" | ||
"github.com/containers/image/v5/transports" | ||
"github.com/containers/image/v5/transports/alltransports" | ||
"github.com/containers/image/v5/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type pushOptions struct { | ||
*copyOptions | ||
destTag string | ||
} | ||
|
||
func push(global *globalOptions) *cobra.Command { | ||
sharedFlags, sharedOpts := sharedImageFlags() | ||
deprecatedTLSVerifyFlags, deprecatedTLSVerifyOpt := deprecatedTLSVerifyFlags() | ||
srcFlags, srcOpts := imageFlags(global, sharedOpts, deprecatedTLSVerifyOpt, "src-", "screds") | ||
destFlags, destOpts := imageDestFlags(global, sharedOpts, deprecatedTLSVerifyOpt, "dest-", "dcreds") | ||
retryFlags, retryOpts := retryFlags() | ||
opts := pushOptions{ | ||
copyOptions: ©Options{ | ||
global: global, | ||
deprecatedTLSVerify: deprecatedTLSVerifyOpt, | ||
srcImage: srcOpts, | ||
destImage: destOpts, | ||
retryOpts: retryOpts, | ||
}, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "push [command options] IMAGE ", | ||
Short: "push an image", | ||
Long: fmt.Sprintf(`Container "IMAGE-NAME" uses a "transport":"details" format. | ||
Supported transports: | ||
%s | ||
See skopeo(1) section "IMAGE NAMES" for the expected format | ||
`, strings.Join(transports.ListNames(), ", ")), | ||
RunE: commandAction(opts.run), | ||
Example: `gopull push redis | ||
gopull push redis -t example.harbor.org/redis:v1 | ||
`, | ||
ValidArgsFunction: autocompleteSupportedTransports, | ||
} | ||
adjustUsage(cmd) | ||
flags := cmd.Flags() | ||
flags.AddFlagSet(&sharedFlags) | ||
flags.AddFlagSet(&deprecatedTLSVerifyFlags) | ||
flags.AddFlagSet(&srcFlags) | ||
flags.AddFlagSet(&destFlags) | ||
flags.AddFlagSet(&retryFlags) | ||
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress output information when copying images") | ||
flags.VarP(commonFlag.NewOptionalStringValue(&opts.format), "format", "f", `MANIFEST TYPE (oci, v2s1, or v2s2) to use in the destination (default is manifest type of source, with fallbacks)`) | ||
flags.StringVarP(&opts.destTag, "--tag", "t", "", "Push destination") | ||
return cmd | ||
} | ||
|
||
func (opts *pushOptions) run(args []string, stdout io.Writer) error { | ||
return opts.execCopy(args, stdout, opts.buildSrcRef, opts.buildDestRef) | ||
} | ||
|
||
func (opts *pushOptions) buildSrcRef(imageName string) (types.ImageReference, *types.SystemContext, error) { | ||
|
||
srcRef, err := alltransports.ParseImageName("docker-daemon:" + imageName) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("invalid source name %s: %v", imageName, err) | ||
} | ||
sourceCtx, err := opts.srcImage.newSystemContext() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return srcRef, sourceCtx, nil | ||
} | ||
|
||
func (opts *pushOptions) buildDestRef(imageName string) (types.ImageReference, *types.SystemContext, error) { | ||
|
||
dest := "docker://" + imageName | ||
if opts.destTag != "" { | ||
dest = "docker://" + opts.destTag | ||
} | ||
|
||
destRef, err := alltransports.ParseImageName(dest) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("invalid destination name %s: %v", dest, err) | ||
} | ||
|
||
destCtx, err := opts.destImage.newSystemContext() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return destRef, destCtx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters