From a834725ef815ba983a9834c5a3da33a6c498ce3f Mon Sep 17 00:00:00 2001 From: HIHIA <283304489@qq.com> Date: Sat, 12 Nov 2022 20:36:09 +0800 Subject: [PATCH] saving container image optional Signed-off-by: HIHIA <283304489@qq.com> --- cmd/sealer/cmd/image/build.go | 21 ++++++++++++++------- pkg/define/options/options.go | 33 +++++++++++++++++---------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/cmd/sealer/cmd/image/build.go b/cmd/sealer/cmd/image/build.go index ecf85837d21..ee5c6043e36 100644 --- a/cmd/sealer/cmd/image/build.go +++ b/cmd/sealer/cmd/image/build.go @@ -92,6 +92,7 @@ func NewBuildCmd() *cobra.Command { buildCmd.Flags().StringSliceVar(&buildFlags.Annotations, "annotation", []string{}, "add annotations for image. Format like --annotation key=[value]") buildCmd.Flags().StringSliceVar(&buildFlags.Labels, "label", []string{getSealerLabel()}, "add labels for image. Format like --label key=[value]") buildCmd.Flags().BoolVar(&buildFlags.NoCache, "no-cache", false, "do not use existing cached images for building. Build from the start with a new set of cached layers.") + buildCmd.Flags().BoolVarP(&buildFlags.DownloadContainerImage, "download-container-image", "d", true, "save the container image generated during the build process.") supportedImageType := map[string]struct{}{v12.KubeInstaller: {}, v12.AppInstaller: {}} if _, ok := supportedImageType[buildFlags.ImageType]; !ok { @@ -160,13 +161,15 @@ func buildSealerImage() error { } defer func() { - for _, m := range []string{tempTag} { - // the above image is intermediate image, we need to remove it when the build ends. - if err := engine.RemoveImage(&bc.RemoveImageOptions{ - ImageNamesOrIDs: []string{m}, - Force: true, - }); err != nil { - logrus.Debugf("failed to remove image %s, you need to remove it manually: %v", m, err) + if !buildFlags.DownloadContainerImage { + for _, m := range []string{tempTag} { + // the above image is intermediate image, we need to remove it when the build ends. + if err := engine.RemoveImage(&bc.RemoveImageOptions{ + ImageNamesOrIDs: []string{m}, + Force: true, + }); err != nil { + logrus.Debugf("failed to remove image %s, you need to remove it manually: %v", m, err) + } } } }() @@ -229,6 +232,10 @@ func commitSingleImage(iid string, tag string, engine imageengine.Interface) err } func applyRegistryToImage(imageID, tag, manifest string, platform v1.Platform, engine imageengine.Interface) error { + if !buildFlags.DownloadContainerImage { + return nil + } + _os, arch, variant := platform.OS, platform.Architecture, platform.Variant // this temporary file is used to execute image pull, and save it to /registry. // engine.BuildRootfs will generate an image rootfs, and link the rootfs to temporary dir(temp sealer rootfs). diff --git a/pkg/define/options/options.go b/pkg/define/options/options.go index a0dc50cff9e..4afa9f02127 100644 --- a/pkg/define/options/options.go +++ b/pkg/define/options/options.go @@ -16,22 +16,23 @@ package options // BuildOptions should be out of buildah scope. type BuildOptions struct { - Kubefile string - ContextDir string - PullPolicy string - ImageType string - Manifest string - Tag string - BuildArgs []string - Platforms []string - Labels []string - Annotations []string - NoCache bool - Base bool - Tags []string - Platform string - ImageList string - ImageListWithAuth string + Kubefile string + ContextDir string + PullPolicy string + ImageType string + Manifest string + Tag string + BuildArgs []string + Platforms []string + Labels []string + Annotations []string + NoCache bool + Base bool + Tags []string + Platform string + ImageList string + ImageListWithAuth string + DownloadContainerImage bool } type FromOptions struct {