Skip to content

Commit

Permalink
feat(stapel-to-buildah): implement 'from' stage
Browse files Browse the repository at this point in the history
* New stapel builder method introduced into ContainerRuntime, not fully implemented yet.
* pkg/build/stage/from implemented using new stapel builder.
* refactored all StageBuilder related implementations and moved to pkg/container_runtime/stage_builder.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Mar 29, 2022
1 parent 558a55e commit 7cc7d71
Show file tree
Hide file tree
Showing 41 changed files with 573 additions and 377 deletions.
2 changes: 1 addition & 1 deletion cmd/werf/common/conveyor_options.go
Expand Up @@ -66,7 +66,7 @@ func GetBuildOptions(commonCmdData *CmdData, giterminismManager giterminism_mana
buildOptions = build.BuildOptions{
SkipImageMetadataPublication: *commonCmdData.Dev,
CustomTagFuncList: customTagFuncList,
ImageBuildOptions: container_runtime.LegacyBuildOptions{
ImageBuildOptions: container_runtime.BuildOptions{
IntrospectAfterError: *commonCmdData.IntrospectAfterError,
IntrospectBeforeError: *commonCmdData.IntrospectBeforeError,
},
Expand Down
18 changes: 10 additions & 8 deletions pkg/build/build_phase.go
Expand Up @@ -36,7 +36,7 @@ type BuildPhaseOptions struct {
}

type BuildOptions struct {
ImageBuildOptions container_runtime.LegacyBuildOptions
ImageBuildOptions container_runtime.BuildOptions
IntrospectOptions

ReportPath string
Expand Down Expand Up @@ -628,10 +628,10 @@ func (phase *BuildPhase) prepareStageInstructions(ctx context.Context, img *Imag
for key, value := range serviceLabels {
labels = append(labels, fmt.Sprintf("%s=%v", key, value))
}
stageImage.StageBuilderAccessor.NativeDockerfileStageBuilder().AppendLabels(labels...)
stageImage.Builder.DockerfileStageBuilder().AppendLabels(labels...)

phase.Conveyor.AppendOnTerminateFunc(func() error {
return stageImage.StageBuilderAccessor.NativeDockerfileStageBuilder().Cleanup(ctx)
return stageImage.Builder.DockerfileStageBuilder().Cleanup(ctx)
})

default:
Expand Down Expand Up @@ -675,7 +675,7 @@ func (phase *BuildPhase) prepareStageInstructions(ctx context.Context, img *Imag
}

func (phase *BuildPhase) buildStage(ctx context.Context, img *Image, stg stage.Interface) error {
if !img.isDockerfileImage {
if !img.isDockerfileImage && phase.Conveyor.UseLegacyStapelBuilder(phase.Conveyor.ContainerRuntime) {
_, err := stapel.GetOrCreateContainer(ctx)
if err != nil {
return fmt.Errorf("get or create stapel container failed: %s", err)
Expand Down Expand Up @@ -724,11 +724,13 @@ func (phase *BuildPhase) atomicBuildStageImage(ctx context.Context, img *Image,
}

if err := logboek.Context(ctx).Streams().DoErrorWithTag(fmt.Sprintf("%s/%s", img.LogName(), stg.Name()), img.LogTagStyle(), func() error {
switch stg.Name() {
case "dockerfile":
return stageImage.StageBuilderAccessor.NativeDockerfileStageBuilder().Build(ctx)
switch {
case stg.Name() == "dockerfile":
return stageImage.Builder.DockerfileStageBuilder().Build(ctx)
case phase.Conveyor.UseLegacyStapelBuilder(phase.Conveyor.ContainerRuntime):
return stageImage.Builder.LegacyStapelStageBuilder().Build(ctx, phase.ImageBuildOptions)
default:
return stageImage.StageBuilderAccessor.LegacyStapelStageBuilder().Build(ctx, phase.ImageBuildOptions)
return stageImage.Builder.StapelStageBuilder().Build(ctx, phase.ImageBuildOptions)
}
}); err != nil {
return fmt.Errorf("failed to build image for stage %s with digest %s: %s", stg.Name(), stg.GetDigest(), err)
Expand Down
27 changes: 14 additions & 13 deletions pkg/build/builder/ansible.go
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/werf/logboek"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/container_runtime/stage_builder"
"github.com/werf/werf/pkg/stapel"
"github.com/werf/werf/pkg/util"
)
Expand Down Expand Up @@ -44,20 +45,20 @@ func (b *Ansible) IsBeforeSetupEmpty(ctx context.Context) bool {
}
func (b *Ansible) IsSetupEmpty(ctx context.Context) bool { return b.isEmptyStage(ctx, "Setup") }

func (b *Ansible) BeforeInstall(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(ctx, cr, stageBuilder, "BeforeInstall")
func (b *Ansible) BeforeInstall(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(ctx, cr, stageBuilder, useLegacyStapelBuilder, "BeforeInstall")
}

func (b *Ansible) Install(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(ctx, cr, stageBuilder, "Install")
func (b *Ansible) Install(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(ctx, cr, stageBuilder, useLegacyStapelBuilder, "Install")
}

func (b *Ansible) BeforeSetup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(ctx, cr, stageBuilder, "BeforeSetup")
func (b *Ansible) BeforeSetup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(ctx, cr, stageBuilder, useLegacyStapelBuilder, "BeforeSetup")
}

func (b *Ansible) Setup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(ctx, cr, stageBuilder, "Setup")
func (b *Ansible) Setup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(ctx, cr, stageBuilder, useLegacyStapelBuilder, "Setup")
}

func (b *Ansible) BeforeInstallChecksum(ctx context.Context) string {
Expand All @@ -73,11 +74,8 @@ func (b *Ansible) isEmptyStage(ctx context.Context, userStageName string) bool {
return b.stageChecksum(ctx, userStageName) == ""
}

func (b *Ansible) stage(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface, userStageName string) error {
if cr.HasContainerRootMountSupport() {
// TODO(stapel-to-buildah)
panic("not implemented")
} else {
func (b *Ansible) stage(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool, userStageName string) error {
if useLegacyStapelBuilder {
container := stageBuilder.LegacyStapelStageBuilder().BuilderContainer()

if len(b.stageTasks(userStageName)) == 0 {
Expand Down Expand Up @@ -136,6 +134,9 @@ func (b *Ansible) stage(ctx context.Context, cr container_runtime.ContainerRunti
container.AddServiceRunCommands(command)

return nil
} else {
// TODO(stapel-to-buildah)
panic("not implemented")
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/build/builder/builder.go
Expand Up @@ -5,17 +5,18 @@ import (
"os"

"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/container_runtime/stage_builder"
)

type Builder interface {
IsBeforeInstallEmpty(ctx context.Context) bool
IsInstallEmpty(ctx context.Context) bool
IsBeforeSetupEmpty(ctx context.Context) bool
IsSetupEmpty(ctx context.Context) bool
BeforeInstall(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error
Install(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error
BeforeSetup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error
Setup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error
BeforeInstall(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
Install(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
BeforeSetup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
Setup(ctx context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
BeforeInstallChecksum(ctx context.Context) string
InstallChecksum(ctx context.Context) string
BeforeSetupChecksum(ctx context.Context) string
Expand Down
27 changes: 14 additions & 13 deletions pkg/build/builder/shell.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/werf/logboek"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/container_runtime/stage_builder"
"github.com/werf/werf/pkg/stapel"
"github.com/werf/werf/pkg/util"
)
Expand All @@ -36,20 +37,20 @@ func (b *Shell) IsBeforeSetupEmpty(ctx context.Context) bool {
}
func (b *Shell) IsSetupEmpty(ctx context.Context) bool { return b.isEmptyStage(ctx, "Setup") }

func (b *Shell) BeforeInstall(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(cr, stageBuilder, "BeforeInstall")
func (b *Shell) BeforeInstall(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "BeforeInstall")
}

func (b *Shell) Install(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(cr, stageBuilder, "Install")
func (b *Shell) Install(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "Install")
}

func (b *Shell) BeforeSetup(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(cr, stageBuilder, "BeforeSetup")
func (b *Shell) BeforeSetup(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "BeforeSetup")
}

func (b *Shell) Setup(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface) error {
return b.stage(cr, stageBuilder, "Setup")
func (b *Shell) Setup(_ context.Context, cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "Setup")
}

func (b *Shell) BeforeInstallChecksum(ctx context.Context) string {
Expand All @@ -65,11 +66,8 @@ func (b *Shell) isEmptyStage(ctx context.Context, userStageName string) bool {
return b.stageChecksum(ctx, userStageName) == ""
}

func (b *Shell) stage(cr container_runtime.ContainerRuntime, stageBuilder StageBuilderAccessorInterface, userStageName string) error {
if cr.HasContainerRootMountSupport() {
// TODO(stapel-to-buildah)
panic("not implemented")
} else {
func (b *Shell) stage(cr container_runtime.ContainerRuntime, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool, userStageName string) error {
if useLegacyStapelBuilder {
container := stageBuilder.LegacyStapelStageBuilder().BuilderContainer()

stageHostTmpDir, err := b.stageHostTmpDir(userStageName)
Expand All @@ -91,6 +89,9 @@ func (b *Shell) stage(cr container_runtime.ContainerRuntime, stageBuilder StageB
container.AddServiceRunCommands(containerTmpScriptFilePath)

return nil
} else {
// TODO(stapel-to-buildah)
panic("not implemented")
}
}

Expand Down
123 changes: 0 additions & 123 deletions pkg/build/builder/stage_builder.go

This file was deleted.

6 changes: 5 additions & 1 deletion pkg/build/conveyor.go
Expand Up @@ -125,6 +125,10 @@ func (c *Conveyor) getServiceRWMutex(service string) *sync.RWMutex {
return rwMutex
}

func (c *Conveyor) UseLegacyStapelBuilder(cr container_runtime.ContainerRuntime) bool {
return !cr.HasStapelBuildSupport()
}

func (c *Conveyor) IsBaseImagesRepoIdsCacheExist(key string) bool {
c.getServiceRWMutex("BaseImagesRepoIdsCache").RLock()
defer c.getServiceRWMutex("BaseImagesRepoIdsCache").RUnlock()
Expand Down Expand Up @@ -688,7 +692,7 @@ func (c *Conveyor) GetOrCreateStageImage(fromImage *container_runtime.LegacyStag
}

i := container_runtime.NewLegacyStageImage(fromImage, name, c.ContainerRuntime)
img := stage.NewStageImage(c.ContainerRuntime, i)
img := stage.NewStageImage(c.ContainerRuntime, fromImage, i)

c.SetStageImage(img)
return img
Expand Down

0 comments on commit 7cc7d71

Please sign in to comment.