Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sealer build supports lite mode option #2251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions cmd/sealer/cmd/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ 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().StringVar(&buildFlags.BuildMode, "build-mode", options.WithAllMode, "whether to download container image during the build process. default is `all`.")

supportedImageType := map[string]struct{}{v12.KubeInstaller: {}, v12.AppInstaller: {}}
if _, ok := supportedImageType[buildFlags.ImageType]; !ok {
logrus.Fatalf("image type %s is not supported", buildFlags.ImageType)
}

supportedBuildModeType := map[string]struct{}{options.WithAllMode: {}, options.WithLiteMode: {}}
if _, ok := supportedBuildModeType[buildFlags.BuildMode]; !ok {
logrus.Fatalf("build mode type %s is not supported in %s", buildFlags.BuildMode, options.SupportedBuildModes)
}

return buildCmd
}

Expand Down Expand Up @@ -172,6 +178,7 @@ func buildSingleSealerImage(engine imageengine.Interface, imageName string, mani
defer func() {
_ = os.Remove(dockerfilePath)
}()
buildFlags.DockerFilePath = dockerfilePath

// set the image extension to oci image annotation
imageExtension := buildImageExtensionOnResult(result, buildFlags.ImageType)
Expand All @@ -180,18 +187,40 @@ func buildSingleSealerImage(engine imageengine.Interface, imageName string, mani
return errors.Wrap(err, "failed to marshal image extension")
}

// add annotations to image. Store some sealer specific information
buildFlags.Annotations = append(buildFlags.Annotations, fmt.Sprintf("%s=%s", v12.SealerImageExtension, string(iejson)))
buildFlags.Platforms = []string{platformStr}

// if it is lite mode, just build it and return
if buildFlags.BuildMode == options.WithLiteMode {
// build single platform image
if manifest == "" {
_, err = engine.Build(&buildFlags)
if err != nil {
return fmt.Errorf("failed to build sealer image with lite mode: %+v", err)
}

return nil
}

// build multi-platform image
buildFlags.Tag = ""
buildFlags.NoCache = true
iid, err := engine.Build(&buildFlags)
if err != nil {
return fmt.Errorf("failed to build sealer image with all mode: %+v", err)
}

return engine.AddToManifest(manifest, []string{iid}, &options.ManifestAddOpts{})
}

var (
randomStr = getRandomString(8)
// use temp tag to do temp image build, because after build,
// we need to download some container data loaded from rootfs to it.
tempTag = imageName + randomStr
tempTag = imageName + getRandomString(8)
)

buildFlags.Tag = tempTag
// add annotations to image. Store some sealer specific information
buildFlags.DockerFilePath = dockerfilePath
buildFlags.Annotations = append(buildFlags.Annotations, fmt.Sprintf("%s=%s", v12.SealerImageExtension, string(iejson)))
buildFlags.Platforms = []string{platformStr}
iid, err := engine.Build(&buildFlags)
if err != nil {
return errors.Errorf("error in building image, %v", err)
Expand Down
14 changes: 14 additions & 0 deletions pkg/define/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

package options

const (
WithLiteMode = "lite"
WithAllMode = "all"
)

var SupportedBuildModes = []string{
WithLiteMode,
WithAllMode,
}

// BuildOptions should be out of buildah scope.
type BuildOptions struct {
Kubefile string
Expand All @@ -32,6 +42,10 @@ type BuildOptions struct {
ImageList string
ImageListWithAuth string
IgnoredImageList string

//BuildMode means whether to download container image during the build process
// default value is download all container images.
BuildMode string
}

type FromOptions struct {
Expand Down