Skip to content

Commit

Permalink
Merge pull request #5063 from werf/feat-staged-dockerfile-all-dockerf…
Browse files Browse the repository at this point in the history
…ile-options-and-instructions

feat(staged-dockerfile): all dockerfile instructions with options
  • Loading branch information
ilya-lesikov committed Oct 26, 2022
2 parents b14b793 + 2a55266 commit 45e7382
Show file tree
Hide file tree
Showing 24 changed files with 724 additions and 138 deletions.
2 changes: 1 addition & 1 deletion cmd/buildah-test/main.go
Expand Up @@ -47,7 +47,7 @@ echo STOP
defer os.RemoveAll("/tmp/build_stage.sh")

if err := b.RunCommand(ctx, "mycontainer", []string{"/.werf/build_stage.sh"}, buildah.RunCommandOpts{
Mounts: []specs.Mount{
LegacyMounts: []specs.Mount{
{
Type: "bind",
Source: "/tmp/build_stage.sh",
Expand Down
36 changes: 23 additions & 13 deletions pkg/build/image/dockerfile.go
Expand Up @@ -65,36 +65,43 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,
}

queue := []struct {
Stage *dockerfile.DockerfileStage
Level int
WerfImageName string
Stage *dockerfile.DockerfileStage
Level int
}{
{Stage: targetStage, Level: 0},
{WerfImageName: dockerfileImageConfig.Name, Stage: targetStage, Level: 0},
}

appendQueue := func(stage *dockerfile.DockerfileStage, level int) {
appendQueue := func(werfImageName string, stage *dockerfile.DockerfileStage, level int) {
queue = append(queue, struct {
Stage *dockerfile.DockerfileStage
Level int
}{Stage: stage, Level: level})
WerfImageName string
Stage *dockerfile.DockerfileStage
Level int
}{WerfImageName: werfImageName, Stage: stage, Level: level})
}

for len(queue) > 0 {
item := queue[0]
queue = queue[1:]

appendImageToCurrentSet := func(img *Image) {
appendImageToCurrentSet := func(newImg *Image) {
if item.Level == len(ret) {
ret = append([][]*Image{nil}, ret...)
}
ret[len(ret)-item.Level-1] = append(ret[len(ret)-item.Level-1], img)
for _, img := range ret[len(ret)-item.Level-1] {
if img.Name == newImg.Name {
return
}
}
ret[len(ret)-item.Level-1] = append(ret[len(ret)-item.Level-1], newImg)
}

stg := item.Stage

var img *Image
var err error
if baseStg := cfg.FindStage(stg.BaseName); baseStg != nil {
img, err = NewImage(ctx, dockerfileImageConfig.Name, StageAsBaseImage, ImageOptions{
img, err = NewImage(ctx, item.WerfImageName, StageAsBaseImage, ImageOptions{
IsDockerfileImage: true,
DockerfileImageConfig: dockerfileImageConfig,
CommonImageOptions: opts,
Expand All @@ -104,9 +111,9 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,
return nil, fmt.Errorf("unable to map stage %s to werf image %q: %w", stg.LogName(), dockerfileImageConfig.Name, err)
}

appendQueue(baseStg, item.Level+1)
appendQueue(baseStg.WerfImageName(), baseStg, item.Level+1)
} else {
img, err = NewImage(ctx, dockerfileImageConfig.Name, ImageFromRegistryAsBaseImage, ImageOptions{
img, err = NewImage(ctx, item.WerfImageName, ImageFromRegistryAsBaseImage, ImageOptions{
IsDockerfileImage: true,
DockerfileImageConfig: dockerfileImageConfig,
CommonImageOptions: opts,
Expand All @@ -129,6 +136,9 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,

var stg stage.Interface
switch typedInstr := any(instr).(type) {
case *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Arg]:
// TODO(staged-dockerfile): support build-args at this level or dockerfile pkg level (?)
continue
case *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Add]:
stg = stage_instruction.NewAdd(stageName, typedInstr, dockerfileImageConfig.Dependencies, !isFirstStage, baseStageOptions)
case *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Cmd]:
Expand Down Expand Up @@ -168,7 +178,7 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,
img.stages = append(img.stages, stg)

for _, dep := range instr.GetDependenciesByStageRef() {
appendQueue(dep, item.Level+1)
appendQueue(dep.WerfImageName(), dep, item.Level+1)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/build/stage/instruction/healthcheck.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
backend_instruction "github.com/werf/werf/pkg/container_backend/instruction"
"github.com/werf/werf/pkg/dockerfile"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/util"
Expand All @@ -17,8 +18,7 @@ type Healthcheck struct {
}

func NewHealthcheck(name stage.StageName, i *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Healthcheck], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Healthcheck {
// FIXME(staged-dockerfile): construct backend instruction
return &Healthcheck{Base: NewBase(name, i, nil, dependencies, hasPrevStage, opts)}
return &Healthcheck{Base: NewBase(name, i, backend_instruction.NewHealthcheck(*i.Data), dependencies, hasPrevStage, opts)}
}

func (stage *Healthcheck) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/stage/instruction/maintainer.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
backend_instruction "github.com/werf/werf/pkg/container_backend/instruction"
"github.com/werf/werf/pkg/dockerfile"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/util"
Expand All @@ -16,8 +17,7 @@ type Maintainer struct {
}

func NewMaintainer(name stage.StageName, i *dockerfile.DockerfileStageInstruction[*dockerfile_instruction.Maintainer], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Maintainer {
// FIXME(staged-dockerfile): no Maintainer instruction
return &Maintainer{Base: NewBase(name, i, nil, dependencies, hasPrevStage, opts)}
return &Maintainer{Base: NewBase(name, i, backend_instruction.NewMaintainer(*i.Data), dependencies, hasPrevStage, opts)}
}

func (stage *Maintainer) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
Expand Down
56 changes: 39 additions & 17 deletions pkg/buildah/common.go
Expand Up @@ -10,9 +10,11 @@ import (
"runtime"
"strings"

"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/werf/werf/pkg/buildah/thirdparty"
"github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
)
Expand Down Expand Up @@ -44,6 +46,7 @@ type BuildFromCommandsOpts struct {

type BuildFromDockerfileOpts struct {
CommonOpts

ContextDir string
BuildArgs map[string]string
Target string
Expand All @@ -58,46 +61,65 @@ type RunMount struct {

type RunCommandOpts struct {
CommonOpts
WorkingDir string
User string
Args []string
Mounts []specs.Mount

ContextDir string
PrependShell bool
Shell []string
AddCapabilities []string
DropCapabilities []string
NetworkType instruction.NetworkType
WorkingDir string
User string
Envs []string
Mounts []instructions.Mount
LegacyMounts []specs.Mount // TODO(ilya-lesikov): migrate to Mounts
}

type RmiOpts struct {
CommonOpts

Force bool
}

type CommitOpts struct {
CommonOpts

Image string
}

type ConfigOpts struct {
CommonOpts
Labels []string
Volumes []string
Expose []string
Envs map[string]string
Cmd []string
Entrypoint []string
User string
Workdir string
Healthcheck string
OnBuild string
StopSignal string
Shell []string

Labels []string
Volumes []string
Expose []string
Envs map[string]string
Cmd []string
CmdPrependShell bool
Entrypoint []string
EntrypointPrependShell bool
User string
Workdir string
Healthcheck *thirdparty.HealthConfig
OnBuild string
StopSignal string
Shell []string
Maintainer string
}

type CopyOpts struct {
CommonOpts
From string

Chown string
Chmod string
}

type AddOpts struct {
CommonOpts

ContextDir string
Chown string
Chmod string
}

type (
Expand Down

0 comments on commit 45e7382

Please sign in to comment.