Skip to content
Permalink
Browse files
fix(buildah): buildah Dockerfile builder was not using layers cache
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed May 31, 2022
1 parent 3f32bf0 commit 8d9326d7c8f325efce01765c64f8e907deaa6069
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
@@ -183,19 +183,23 @@ func (b *NativeBuildah) Push(ctx context.Context, ref string, opts PushOpts) err

func (b *NativeBuildah) BuildFromDockerfile(ctx context.Context, dockerfile []byte, opts BuildFromDockerfileOpts) (string, error) {
buildOpts := define.BuildOptions{
Isolation: define.Isolation(b.Isolation),
Runtime: DefaultRuntime,
Args: opts.BuildArgs,
SignaturePolicyPath: b.SignaturePolicyPath,
ReportWriter: opts.LogWriter,
OutputFormat: buildah.Dockerv2ImageManifest,
SystemContext: &b.DefaultSystemContext,
ConfigureNetwork: define.NetworkEnabled,
CommonBuildOpts: &b.DefaultCommonBuildOptions,
Target: opts.Target,
MaxPullPushRetries: MaxPullPushRetries,
PullPushRetryDelay: PullPushRetryDelay,
Platforms: b.platforms,
Isolation: define.Isolation(b.Isolation),
Runtime: DefaultRuntime,
Args: opts.BuildArgs,
SignaturePolicyPath: b.SignaturePolicyPath,
ReportWriter: opts.LogWriter,
OutputFormat: buildah.Dockerv2ImageManifest,
SystemContext: &b.DefaultSystemContext,
ConfigureNetwork: define.NetworkEnabled,
CommonBuildOpts: &b.DefaultCommonBuildOptions,
Target: opts.Target,
MaxPullPushRetries: MaxPullPushRetries,
PullPushRetryDelay: PullPushRetryDelay,
Platforms: b.platforms,
Layers: true,
RemoveIntermediateCtrs: true,
ForceRmIntermediateCtrs: false,
NoCache: false,
}

errLog := &bytes.Buffer{}
@@ -350,8 +354,8 @@ func (b *NativeBuildah) Rm(ctx context.Context, ref string, opts RmOpts) error {

func (b *NativeBuildah) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
_, rmiErrors := b.Runtime.RemoveImages(ctx, []string{ref}, &libimage.RemoveImagesOptions{
Force: opts.Force,
Filters: []string{"readonly=false", "intermediate=false", "dangling=true"},
Force: opts.Force,
//Filters: []string{"readonly=false", "intermediate=false", "dangling=true"},
})

var multiErr *multierror.Error
@@ -559,6 +559,10 @@ func (runtime *BuildahBackend) BuildDockerfile(ctx context.Context, dockerfile [
})
}

func (runtime *BuildahBackend) ShouldCleanupDockerfileImage() bool {
return false
}

func (runtime *BuildahBackend) RefreshImageObject(ctx context.Context, img LegacyImageInterface) error {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
@@ -78,6 +78,12 @@ func (runtime *DockerServerBackend) BuildDockerfile(ctx context.Context, _ []byt
return tempID, docker.CliBuild_LiveOutputWithCustomIn(ctx, opts.ContextTar, cliArgs...)
}

// ShouldCleanupDockerfileImage for docker-server backend we should cleanup image built from dockerfrom tagged with tempID
// which is implementation detail of the BuildDockerfile.
func (runtime *DockerServerBackend) ShouldCleanupDockerfileImage() bool {
return true
}

func (runtime *DockerServerBackend) GetImageInfo(ctx context.Context, ref string, opts GetImageInfoOpts) (*image.Info, error) {
inspect, err := docker.ImageInspect(ctx, ref)
if client.IsErrNotFound(err) {
@@ -58,6 +58,7 @@ type ContainerBackend interface {
String() string

// Legacy
ShouldCleanupDockerfileImage() bool
RefreshImageObject(ctx context.Context, img LegacyImageInterface) error
PullImageFromRegistry(ctx context.Context, img LegacyImageInterface) error
RenameImage(ctx context.Context, img LegacyImageInterface, newImageName string, removeOldName bool) error
@@ -19,6 +19,10 @@ func (runtime *PerfCheckContainerBackend) HasStapelBuildSupport() bool {
return runtime.ContainerBackend.HasStapelBuildSupport()
}

func (runtime *PerfCheckContainerBackend) ShouldCleanupDockerfileImage() bool {
return runtime.ContainerBackend.ShouldCleanupDockerfileImage()
}

func (runtime *PerfCheckContainerBackend) GetImageInfo(ctx context.Context, ref string, opts GetImageInfoOpts) (resImg *image.Info, resErr error) {
logboek.Context(ctx).Default().LogProcess("ContainerBackend.GetImageInfo %q", ref).
Do(func() {
@@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/werf/logboek"

"github.com/werf/werf/pkg/container_backend"
)

@@ -60,13 +62,17 @@ func (b *DockerfileStageBuilder) Build(ctx context.Context) error {
if err != nil {
return fmt.Errorf("error building dockerfile with %s: %w", b.ContainerBackend.String(), err)
}

b.Image.SetBuiltID(builtID)

return nil
}

func (b *DockerfileStageBuilder) Cleanup(ctx context.Context) error {
if !b.ContainerBackend.ShouldCleanupDockerfileImage() {
return nil
}

logboek.Context(ctx).Info().LogF("Cleanup built dockerfile image %q\n", b.Image.BuiltID())
if err := b.ContainerBackend.Rmi(ctx, b.Image.BuiltID(), container_backend.RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove built dockerfile image %q: %w", b.Image.BuiltID(), err)
}

0 comments on commit 8d9326d

Please sign in to comment.