From 76f654d2ee871e16e1d787b741e9cdc133c8f82c Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Mon, 30 Jan 2023 18:10:30 +0300 Subject: [PATCH] fix(staged-dockerfile): fix multiple stages with the same name from multiple Dockerfiles Fix naming conflict, which occured when building multiple Dockerfile-s with stages with same names. Signed-off-by: Timofey Kirillov --- pkg/build/build_phase.go | 2 +- pkg/build/image/dockerfile.go | 10 ++++++---- pkg/build/stage/instruction/add.go | 2 +- pkg/build/stage/instruction/cmd.go | 2 +- pkg/build/stage/instruction/copy.go | 4 ++-- pkg/build/stage/instruction/entrypoint.go | 2 +- pkg/build/stage/instruction/env.go | 2 +- pkg/build/stage/instruction/expose.go | 2 +- pkg/build/stage/instruction/healthcheck.go | 2 +- pkg/build/stage/instruction/label.go | 2 +- pkg/build/stage/instruction/maintainer.go | 2 +- pkg/build/stage/instruction/on_build.go | 2 +- pkg/build/stage/instruction/run.go | 2 +- pkg/build/stage/instruction/shell.go | 2 +- pkg/build/stage/instruction/stop_signal.go | 2 +- pkg/build/stage/instruction/user.go | 2 +- pkg/build/stage/instruction/volume.go | 2 +- pkg/build/stage/instruction/workdir.go | 2 +- pkg/container_backend/instruction/add.go | 4 ++-- pkg/container_backend/instruction/cmd.go | 4 ++-- pkg/container_backend/instruction/copy.go | 4 ++-- .../instruction/entrypoint.go | 4 ++-- pkg/container_backend/instruction/env.go | 4 ++-- pkg/container_backend/instruction/expose.go | 4 ++-- .../instruction/healthcheck.go | 4 ++-- pkg/container_backend/instruction/label.go | 4 ++-- .../instruction/maintainer.go | 4 ++-- pkg/container_backend/instruction/on_build.go | 4 ++-- pkg/container_backend/instruction/run.go | 10 +++++----- pkg/container_backend/instruction/shell.go | 4 ++-- .../instruction/stop_signal.go | 4 ++-- pkg/container_backend/instruction/user.go | 4 ++-- pkg/container_backend/instruction/volume.go | 4 ++-- pkg/container_backend/instruction/workdir.go | 4 ++-- pkg/dockerfile/dockerfile.go | 6 ++++-- pkg/dockerfile/dockerfile_stage.go | 20 ++++++++++--------- .../frontend/buildkit_dockerfile.go | 15 +++++++++----- 37 files changed, 84 insertions(+), 73 deletions(-) diff --git a/pkg/build/build_phase.go b/pkg/build/build_phase.go index 0a15958fe4..454503930f 100644 --- a/pkg/build/build_phase.go +++ b/pkg/build/build_phase.go @@ -718,7 +718,7 @@ func (phase *BuildPhase) prepareStageInstructions(ctx context.Context, img *imag for k, v := range serviceLabels { stageImage.Builder.DockerfileStageBuilder().AppendPostInstruction( - backend_instruction.NewLabel(instructions.NewLabelCommand(k, v, true)), + backend_instruction.NewLabel(*instructions.NewLabelCommand(k, v, true)), ) } } diff --git a/pkg/build/image/dockerfile.go b/pkg/build/image/dockerfile.go index 304e4820fd..49f7b4c391 100644 --- a/pkg/build/image/dockerfile.go +++ b/pkg/build/image/dockerfile.go @@ -30,7 +30,9 @@ func MapDockerfileConfigToImagesSets(ctx context.Context, dockerfileImageConfig return nil, fmt.Errorf("unable to read dockerfile %s: %w", relDockerfilePath, err) } - d, err := frontend.ParseDockerfileWithBuildkit(dockerfileData, dockerfile.DockerfileOptions{ + dockerfileID := util.Sha256Hash(filepath.Clean(relDockerfilePath)) + + d, err := frontend.ParseDockerfileWithBuildkit(dockerfileID, dockerfileData, dockerfileImageConfig.Name, dockerfile.DockerfileOptions{ Target: dockerfileImageConfig.Target, BuildArgs: util.MapStringInterfaceToMapStringString(dockerfileImageConfig.Args), AddHost: dockerfileImageConfig.AddHost, @@ -109,14 +111,14 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile, IsDockerfileTargetStage: item.IsTargetStage, DockerfileImageConfig: dockerfileImageConfig, CommonImageOptions: opts, - BaseImageName: baseStg.WerfImageName(), + BaseImageName: baseStg.GetWerfImageName(), DockerfileExpanderFactory: stg.ExpanderFactory, }) if err != nil { return nil, fmt.Errorf("unable to map stage %s to werf image %q: %w", stg.LogName(), dockerfileImageConfig.Name, err) } - appendQueue(baseStg.WerfImageName(), baseStg, item.Level+1) + appendQueue(baseStg.GetWerfImageName(), baseStg, item.Level+1) } else { img, err = NewImage(ctx, item.WerfImageName, ImageFromRegistryAsBaseImage, ImageOptions{ IsDockerfileImage: true, @@ -185,7 +187,7 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile, img.stages = append(img.stages, stg) for _, dep := range instr.GetDependenciesByStageRef() { - appendQueue(dep.WerfImageName(), dep, item.Level+1) + appendQueue(dep.GetWerfImageName(), dep, item.Level+1) } } diff --git a/pkg/build/stage/instruction/add.go b/pkg/build/stage/instruction/add.go index 6ae43aac1b..350a1b416f 100644 --- a/pkg/build/stage/instruction/add.go +++ b/pkg/build/stage/instruction/add.go @@ -20,7 +20,7 @@ type Add struct { } func NewAdd(i *dockerfile.DockerfileStageInstruction[*instructions.AddCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Add { - return &Add{Base: NewBase(i, backend_instruction.NewAdd(i.Data), dependencies, hasPrevStage, opts)} + return &Add{Base: NewBase(i, backend_instruction.NewAdd(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Add) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/cmd.go b/pkg/build/stage/instruction/cmd.go index 77df54f362..b5aa8132d9 100644 --- a/pkg/build/stage/instruction/cmd.go +++ b/pkg/build/stage/instruction/cmd.go @@ -19,7 +19,7 @@ type Cmd struct { } func NewCmd(i *dockerfile.DockerfileStageInstruction[*instructions.CmdCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Cmd { - return &Cmd{Base: NewBase(i, backend_instruction.NewCmd(i.Data), dependencies, hasPrevStage, opts)} + return &Cmd{Base: NewBase(i, backend_instruction.NewCmd(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Cmd) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/copy.go b/pkg/build/stage/instruction/copy.go index 0762e444aa..7c3c391896 100644 --- a/pkg/build/stage/instruction/copy.go +++ b/pkg/build/stage/instruction/copy.go @@ -19,7 +19,7 @@ type Copy struct { } func NewCopy(i *dockerfile.DockerfileStageInstruction[*instructions.CopyCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Copy { - return &Copy{Base: NewBase(i, backend_instruction.NewCopy(i.Data), dependencies, hasPrevStage, opts)} + return &Copy{Base: NewBase(i, backend_instruction.NewCopy(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Copy) ExpandDependencies(ctx context.Context, c stage.Conveyor, baseEnv map[string]string) error { @@ -33,7 +33,7 @@ func (stg *Copy) ExpandInstruction(c stage.Conveyor, env map[string]string) erro if stg.instruction.Data.From != "" { if ds := stg.instruction.GetDependencyByStageRef(stg.instruction.Data.From); ds != nil { - depStageImageName := c.GetImageNameForLastImageStage(ds.WerfImageName()) + depStageImageName := c.GetImageNameForLastImageStage(ds.GetWerfImageName()) stg.backendInstruction.From = depStageImageName } } diff --git a/pkg/build/stage/instruction/entrypoint.go b/pkg/build/stage/instruction/entrypoint.go index 65bc45ecf4..84738cff19 100644 --- a/pkg/build/stage/instruction/entrypoint.go +++ b/pkg/build/stage/instruction/entrypoint.go @@ -19,7 +19,7 @@ type Entrypoint struct { } func NewEntrypoint(i *dockerfile.DockerfileStageInstruction[*instructions.EntrypointCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Entrypoint { - return &Entrypoint{Base: NewBase(i, backend_instruction.NewEntrypoint(i.Data), dependencies, hasPrevStage, opts)} + return &Entrypoint{Base: NewBase(i, backend_instruction.NewEntrypoint(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Entrypoint) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/env.go b/pkg/build/stage/instruction/env.go index 4cfca277c9..0fa89c59bd 100644 --- a/pkg/build/stage/instruction/env.go +++ b/pkg/build/stage/instruction/env.go @@ -18,7 +18,7 @@ type Env struct { } func NewEnv(i *dockerfile.DockerfileStageInstruction[*instructions.EnvCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Env { - return &Env{Base: NewBase(i, backend_instruction.NewEnv(i.Data), dependencies, hasPrevStage, opts)} + return &Env{Base: NewBase(i, backend_instruction.NewEnv(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Env) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/expose.go b/pkg/build/stage/instruction/expose.go index c65b694eb2..d09448db29 100644 --- a/pkg/build/stage/instruction/expose.go +++ b/pkg/build/stage/instruction/expose.go @@ -18,7 +18,7 @@ type Expose struct { } func NewExpose(i *dockerfile.DockerfileStageInstruction[*instructions.ExposeCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Expose { - return &Expose{Base: NewBase(i, backend_instruction.NewExpose(i.Data), dependencies, hasPrevStage, opts)} + return &Expose{Base: NewBase(i, backend_instruction.NewExpose(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Expose) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/healthcheck.go b/pkg/build/stage/instruction/healthcheck.go index d0f179eba3..07810e5498 100644 --- a/pkg/build/stage/instruction/healthcheck.go +++ b/pkg/build/stage/instruction/healthcheck.go @@ -19,7 +19,7 @@ type Healthcheck struct { } func NewHealthcheck(i *dockerfile.DockerfileStageInstruction[*instructions.HealthCheckCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Healthcheck { - return &Healthcheck{Base: NewBase(i, backend_instruction.NewHealthcheck(i.Data), dependencies, hasPrevStage, opts)} + return &Healthcheck{Base: NewBase(i, backend_instruction.NewHealthcheck(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Healthcheck) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/label.go b/pkg/build/stage/instruction/label.go index 989786612e..f8773977d4 100644 --- a/pkg/build/stage/instruction/label.go +++ b/pkg/build/stage/instruction/label.go @@ -18,7 +18,7 @@ type Label struct { } func NewLabel(i *dockerfile.DockerfileStageInstruction[*instructions.LabelCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Label { - return &Label{Base: NewBase(i, backend_instruction.NewLabel(i.Data), dependencies, hasPrevStage, opts)} + return &Label{Base: NewBase(i, backend_instruction.NewLabel(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Label) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/maintainer.go b/pkg/build/stage/instruction/maintainer.go index 5a60b687b2..2eacc5605c 100644 --- a/pkg/build/stage/instruction/maintainer.go +++ b/pkg/build/stage/instruction/maintainer.go @@ -18,7 +18,7 @@ type Maintainer struct { } func NewMaintainer(i *dockerfile.DockerfileStageInstruction[*instructions.MaintainerCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Maintainer { - return &Maintainer{Base: NewBase(i, backend_instruction.NewMaintainer(i.Data), dependencies, hasPrevStage, opts)} + return &Maintainer{Base: NewBase(i, backend_instruction.NewMaintainer(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Maintainer) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/on_build.go b/pkg/build/stage/instruction/on_build.go index 9b954aa478..ce6e2e7127 100644 --- a/pkg/build/stage/instruction/on_build.go +++ b/pkg/build/stage/instruction/on_build.go @@ -18,7 +18,7 @@ type OnBuild struct { } func NewOnBuild(i *dockerfile.DockerfileStageInstruction[*instructions.OnbuildCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *OnBuild { - return &OnBuild{Base: NewBase(i, backend_instruction.NewOnBuild(i.Data), dependencies, hasPrevStage, opts)} + return &OnBuild{Base: NewBase(i, backend_instruction.NewOnBuild(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *OnBuild) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/run.go b/pkg/build/stage/instruction/run.go index 2de208f095..4651222f76 100644 --- a/pkg/build/stage/instruction/run.go +++ b/pkg/build/stage/instruction/run.go @@ -20,7 +20,7 @@ type Run struct { } func NewRun(i *dockerfile.DockerfileStageInstruction[*instructions.RunCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Run { - return &Run{Base: NewBase(i, backend_instruction.NewRun(i.Data, nil), dependencies, hasPrevStage, opts)} + return &Run{Base: NewBase(i, backend_instruction.NewRun(*i.Data, nil), dependencies, hasPrevStage, opts)} } func (stg *Run) ExpandDependencies(ctx context.Context, c stage.Conveyor, baseEnv map[string]string) error { diff --git a/pkg/build/stage/instruction/shell.go b/pkg/build/stage/instruction/shell.go index 416df205a0..8ea739133e 100644 --- a/pkg/build/stage/instruction/shell.go +++ b/pkg/build/stage/instruction/shell.go @@ -18,7 +18,7 @@ type Shell struct { } func NewShell(i *dockerfile.DockerfileStageInstruction[*instructions.ShellCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Shell { - return &Shell{Base: NewBase(i, backend_instruction.NewShell(i.Data), dependencies, hasPrevStage, opts)} + return &Shell{Base: NewBase(i, backend_instruction.NewShell(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Shell) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/stop_signal.go b/pkg/build/stage/instruction/stop_signal.go index 8c084d08fb..ab36f9b1d2 100644 --- a/pkg/build/stage/instruction/stop_signal.go +++ b/pkg/build/stage/instruction/stop_signal.go @@ -18,7 +18,7 @@ type StopSignal struct { } func NewStopSignal(i *dockerfile.DockerfileStageInstruction[*instructions.StopSignalCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *StopSignal { - return &StopSignal{Base: NewBase(i, backend_instruction.NewStopSignal(i.Data), dependencies, hasPrevStage, opts)} + return &StopSignal{Base: NewBase(i, backend_instruction.NewStopSignal(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *StopSignal) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/user.go b/pkg/build/stage/instruction/user.go index b27f913b11..5532534631 100644 --- a/pkg/build/stage/instruction/user.go +++ b/pkg/build/stage/instruction/user.go @@ -18,7 +18,7 @@ type User struct { } func NewUser(i *dockerfile.DockerfileStageInstruction[*instructions.UserCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *User { - return &User{Base: NewBase(i, backend_instruction.NewUser(i.Data), dependencies, hasPrevStage, opts)} + return &User{Base: NewBase(i, backend_instruction.NewUser(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *User) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/volume.go b/pkg/build/stage/instruction/volume.go index 6123a8daa2..e3f6ab6e9b 100644 --- a/pkg/build/stage/instruction/volume.go +++ b/pkg/build/stage/instruction/volume.go @@ -18,7 +18,7 @@ type Volume struct { } func NewVolume(i *dockerfile.DockerfileStageInstruction[*instructions.VolumeCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Volume { - return &Volume{Base: NewBase(i, backend_instruction.NewVolume(i.Data), dependencies, hasPrevStage, opts)} + return &Volume{Base: NewBase(i, backend_instruction.NewVolume(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Volume) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/build/stage/instruction/workdir.go b/pkg/build/stage/instruction/workdir.go index 7ae9623cc9..01e4c0a5ef 100644 --- a/pkg/build/stage/instruction/workdir.go +++ b/pkg/build/stage/instruction/workdir.go @@ -18,7 +18,7 @@ type Workdir struct { } func NewWorkdir(i *dockerfile.DockerfileStageInstruction[*instructions.WorkdirCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Workdir { - return &Workdir{Base: NewBase(i, backend_instruction.NewWorkdir(i.Data), dependencies, hasPrevStage, opts)} + return &Workdir{Base: NewBase(i, backend_instruction.NewWorkdir(*i.Data), dependencies, hasPrevStage, opts)} } func (stg *Workdir) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) { diff --git a/pkg/container_backend/instruction/add.go b/pkg/container_backend/instruction/add.go index 87ee61c350..6948829a69 100644 --- a/pkg/container_backend/instruction/add.go +++ b/pkg/container_backend/instruction/add.go @@ -11,10 +11,10 @@ import ( ) type Add struct { - *instructions.AddCommand + instructions.AddCommand } -func NewAdd(i *instructions.AddCommand) *Add { +func NewAdd(i instructions.AddCommand) *Add { return &Add{AddCommand: i} } diff --git a/pkg/container_backend/instruction/cmd.go b/pkg/container_backend/instruction/cmd.go index b351ee7c3e..4af2d6fb0b 100644 --- a/pkg/container_backend/instruction/cmd.go +++ b/pkg/container_backend/instruction/cmd.go @@ -11,10 +11,10 @@ import ( ) type Cmd struct { - *instructions.CmdCommand + instructions.CmdCommand } -func NewCmd(i *instructions.CmdCommand) *Cmd { +func NewCmd(i instructions.CmdCommand) *Cmd { return &Cmd{CmdCommand: i} } diff --git a/pkg/container_backend/instruction/copy.go b/pkg/container_backend/instruction/copy.go index 288867591a..b6ada10e1e 100644 --- a/pkg/container_backend/instruction/copy.go +++ b/pkg/container_backend/instruction/copy.go @@ -11,10 +11,10 @@ import ( ) type Copy struct { - *instructions.CopyCommand + instructions.CopyCommand } -func NewCopy(i *instructions.CopyCommand) *Copy { +func NewCopy(i instructions.CopyCommand) *Copy { return &Copy{CopyCommand: i} } diff --git a/pkg/container_backend/instruction/entrypoint.go b/pkg/container_backend/instruction/entrypoint.go index 5cc37b17db..12ec4b8273 100644 --- a/pkg/container_backend/instruction/entrypoint.go +++ b/pkg/container_backend/instruction/entrypoint.go @@ -11,10 +11,10 @@ import ( ) type Entrypoint struct { - *instructions.EntrypointCommand + instructions.EntrypointCommand } -func NewEntrypoint(i *instructions.EntrypointCommand) *Entrypoint { +func NewEntrypoint(i instructions.EntrypointCommand) *Entrypoint { return &Entrypoint{EntrypointCommand: i} } diff --git a/pkg/container_backend/instruction/env.go b/pkg/container_backend/instruction/env.go index cc3744272a..bf0b16b284 100644 --- a/pkg/container_backend/instruction/env.go +++ b/pkg/container_backend/instruction/env.go @@ -11,10 +11,10 @@ import ( ) type Env struct { - *instructions.EnvCommand + instructions.EnvCommand } -func NewEnv(i *instructions.EnvCommand) *Env { +func NewEnv(i instructions.EnvCommand) *Env { return &Env{EnvCommand: i} } diff --git a/pkg/container_backend/instruction/expose.go b/pkg/container_backend/instruction/expose.go index d514b8fbf9..878d2610d3 100644 --- a/pkg/container_backend/instruction/expose.go +++ b/pkg/container_backend/instruction/expose.go @@ -11,10 +11,10 @@ import ( ) type Expose struct { - *instructions.ExposeCommand + instructions.ExposeCommand } -func NewExpose(i *instructions.ExposeCommand) *Expose { +func NewExpose(i instructions.ExposeCommand) *Expose { return &Expose{ExposeCommand: i} } diff --git a/pkg/container_backend/instruction/healthcheck.go b/pkg/container_backend/instruction/healthcheck.go index 2db8f6b96b..f1f49e4b68 100644 --- a/pkg/container_backend/instruction/healthcheck.go +++ b/pkg/container_backend/instruction/healthcheck.go @@ -12,10 +12,10 @@ import ( ) type Healthcheck struct { - *instructions.HealthCheckCommand + instructions.HealthCheckCommand } -func NewHealthcheck(i *instructions.HealthCheckCommand) *Healthcheck { +func NewHealthcheck(i instructions.HealthCheckCommand) *Healthcheck { return &Healthcheck{HealthCheckCommand: i} } diff --git a/pkg/container_backend/instruction/label.go b/pkg/container_backend/instruction/label.go index 92b08d23fa..a51e561d2b 100644 --- a/pkg/container_backend/instruction/label.go +++ b/pkg/container_backend/instruction/label.go @@ -11,10 +11,10 @@ import ( ) type Label struct { - *instructions.LabelCommand + instructions.LabelCommand } -func NewLabel(i *instructions.LabelCommand) *Label { +func NewLabel(i instructions.LabelCommand) *Label { return &Label{LabelCommand: i} } diff --git a/pkg/container_backend/instruction/maintainer.go b/pkg/container_backend/instruction/maintainer.go index b9280db0ff..01bd18f5fa 100644 --- a/pkg/container_backend/instruction/maintainer.go +++ b/pkg/container_backend/instruction/maintainer.go @@ -11,10 +11,10 @@ import ( ) type Maintainer struct { - *instructions.MaintainerCommand + instructions.MaintainerCommand } -func NewMaintainer(i *instructions.MaintainerCommand) *Maintainer { +func NewMaintainer(i instructions.MaintainerCommand) *Maintainer { return &Maintainer{MaintainerCommand: i} } diff --git a/pkg/container_backend/instruction/on_build.go b/pkg/container_backend/instruction/on_build.go index b526f72f09..dca27d9805 100644 --- a/pkg/container_backend/instruction/on_build.go +++ b/pkg/container_backend/instruction/on_build.go @@ -11,10 +11,10 @@ import ( ) type OnBuild struct { - *instructions.OnbuildCommand + instructions.OnbuildCommand } -func NewOnBuild(i *instructions.OnbuildCommand) *OnBuild { +func NewOnBuild(i instructions.OnbuildCommand) *OnBuild { return &OnBuild{OnbuildCommand: i} } diff --git a/pkg/container_backend/instruction/run.go b/pkg/container_backend/instruction/run.go index dd6fa567a5..a6ef925aa6 100644 --- a/pkg/container_backend/instruction/run.go +++ b/pkg/container_backend/instruction/run.go @@ -13,11 +13,11 @@ import ( ) type Run struct { - *instructions.RunCommand + instructions.RunCommand Envs []string } -func NewRun(i *instructions.RunCommand, envs []string) *Run { +func NewRun(i instructions.RunCommand, envs []string) *Run { return &Run{RunCommand: i, Envs: envs} } @@ -32,15 +32,15 @@ func (i *Run) UsesBuildContext() bool { } func (i *Run) GetMounts() []*instructions.Mount { - return instructions.GetMounts(i.RunCommand) + return instructions.GetMounts(&i.RunCommand) } func (i *Run) GetSecurity() string { - return instructions.GetSecurity(i.RunCommand) + return instructions.GetSecurity(&i.RunCommand) } func (i *Run) GetNetwork() string { - return instructions.GetNetwork(i.RunCommand) + return instructions.GetNetwork(&i.RunCommand) } func (i *Run) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContextArchive container_backend.BuildContextArchiver) error { diff --git a/pkg/container_backend/instruction/shell.go b/pkg/container_backend/instruction/shell.go index 31eef5d2fd..202f6d797d 100644 --- a/pkg/container_backend/instruction/shell.go +++ b/pkg/container_backend/instruction/shell.go @@ -11,10 +11,10 @@ import ( ) type Shell struct { - *instructions.ShellCommand + instructions.ShellCommand } -func NewShell(i *instructions.ShellCommand) *Shell { +func NewShell(i instructions.ShellCommand) *Shell { return &Shell{ShellCommand: i} } diff --git a/pkg/container_backend/instruction/stop_signal.go b/pkg/container_backend/instruction/stop_signal.go index e99bf502cc..f0b2812552 100644 --- a/pkg/container_backend/instruction/stop_signal.go +++ b/pkg/container_backend/instruction/stop_signal.go @@ -11,10 +11,10 @@ import ( ) type StopSignal struct { - *instructions.StopSignalCommand + instructions.StopSignalCommand } -func NewStopSignal(i *instructions.StopSignalCommand) *StopSignal { +func NewStopSignal(i instructions.StopSignalCommand) *StopSignal { return &StopSignal{StopSignalCommand: i} } diff --git a/pkg/container_backend/instruction/user.go b/pkg/container_backend/instruction/user.go index 356549cd00..7bbaf6f4df 100644 --- a/pkg/container_backend/instruction/user.go +++ b/pkg/container_backend/instruction/user.go @@ -11,10 +11,10 @@ import ( ) type User struct { - *instructions.UserCommand + instructions.UserCommand } -func NewUser(i *instructions.UserCommand) *User { +func NewUser(i instructions.UserCommand) *User { return &User{UserCommand: i} } diff --git a/pkg/container_backend/instruction/volume.go b/pkg/container_backend/instruction/volume.go index d039f2d87b..328de7990d 100644 --- a/pkg/container_backend/instruction/volume.go +++ b/pkg/container_backend/instruction/volume.go @@ -11,10 +11,10 @@ import ( ) type Volume struct { - *instructions.VolumeCommand + instructions.VolumeCommand } -func NewVolume(i *instructions.VolumeCommand) *Volume { +func NewVolume(i instructions.VolumeCommand) *Volume { return &Volume{VolumeCommand: i} } diff --git a/pkg/container_backend/instruction/workdir.go b/pkg/container_backend/instruction/workdir.go index 2f9c17c79f..db1effc24d 100644 --- a/pkg/container_backend/instruction/workdir.go +++ b/pkg/container_backend/instruction/workdir.go @@ -11,10 +11,10 @@ import ( ) type Workdir struct { - *instructions.WorkdirCommand + instructions.WorkdirCommand } -func NewWorkdir(i *instructions.WorkdirCommand) *Workdir { +func NewWorkdir(i instructions.WorkdirCommand) *Workdir { return &Workdir{WorkdirCommand: i} } diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index f314b63c6b..71577957cf 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -13,8 +13,9 @@ type DockerfileOptions struct { SSH string } -func NewDockerfile(stages []*DockerfileStage, opts DockerfileOptions) *Dockerfile { +func NewDockerfile(dockerfileID string, stages []*DockerfileStage, opts DockerfileOptions) *Dockerfile { return &Dockerfile{ + DockerfileID: dockerfileID, DockerfileOptions: opts, Stages: stages, } @@ -23,7 +24,8 @@ func NewDockerfile(stages []*DockerfileStage, opts DockerfileOptions) *Dockerfil type Dockerfile struct { DockerfileOptions - Stages []*DockerfileStage + DockerfileID string + Stages []*DockerfileStage } func (df *Dockerfile) GetTargetStage() (*DockerfileStage, error) { diff --git a/pkg/dockerfile/dockerfile_stage.go b/pkg/dockerfile/dockerfile_stage.go index 6cc781a07a..5d763e90a9 100644 --- a/pkg/dockerfile/dockerfile_stage.go +++ b/pkg/dockerfile/dockerfile_stage.go @@ -8,11 +8,12 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/instructions" ) -func NewDockerfileStage(index int, baseName, stageName string, instructions []DockerfileStageInstructionInterface, platform string, expanderFactory ExpanderFactory) *DockerfileStage { +func NewDockerfileStage(index int, baseName, stageName, werfImageName string, instructions []DockerfileStageInstructionInterface, platform string, expanderFactory ExpanderFactory) *DockerfileStage { return &DockerfileStage{ ExpanderFactory: expanderFactory, BaseName: baseName, StageName: stageName, + WerfImageName: werfImageName, Instructions: instructions, Platform: platform, } @@ -24,11 +25,12 @@ type DockerfileStage struct { BaseStage *DockerfileStage ExpanderFactory ExpanderFactory - BaseName string - Index int - StageName string - Platform string - Instructions []DockerfileStageInstructionInterface + BaseName string + Index int + StageName string + WerfImageName string + Platform string + Instructions []DockerfileStageInstructionInterface } func (stage *DockerfileStage) AppendDependencyStage(dep *DockerfileStage) { @@ -40,11 +42,11 @@ func (stage *DockerfileStage) AppendDependencyStage(dep *DockerfileStage) { stage.Dependencies = append(stage.Dependencies, dep) } -func (stage *DockerfileStage) WerfImageName() string { +func (stage *DockerfileStage) GetWerfImageName() string { if stage.HasStageName() { - return fmt.Sprintf("stage/%s", stage.StageName) + return fmt.Sprintf("%s/stage/%s", stage.WerfImageName, stage.StageName) } else { - return fmt.Sprintf("stage%d", stage.Index) + return fmt.Sprintf("%s/stage%d", stage.WerfImageName, stage.Index) } } diff --git a/pkg/dockerfile/frontend/buildkit_dockerfile.go b/pkg/dockerfile/frontend/buildkit_dockerfile.go index 4b5cacde1c..13f01add18 100644 --- a/pkg/dockerfile/frontend/buildkit_dockerfile.go +++ b/pkg/dockerfile/frontend/buildkit_dockerfile.go @@ -12,7 +12,7 @@ import ( "github.com/werf/werf/pkg/dockerfile" ) -func ParseDockerfileWithBuildkit(dockerfileBytes []byte, opts dockerfile.DockerfileOptions) (*dockerfile.Dockerfile, error) { +func ParseDockerfileWithBuildkit(dockerfileID string, dockerfileBytes []byte, werfImageName string, opts dockerfile.DockerfileOptions) (*dockerfile.Dockerfile, error) { p, err := parser.Parse(bytes.NewReader(dockerfileBytes)) if err != nil { return nil, fmt.Errorf("parsing dockerfile data: %w", err) @@ -43,7 +43,12 @@ func ParseDockerfileWithBuildkit(dockerfileBytes []byte, opts dockerfile.Dockerf // TODO(staged-dockerfile): support meta-args expansion for dockerStage.Platform - if stage, err := NewDockerfileStageFromBuildkitStage(i, dockerStage, expanderFactory, metaArgs, opts.BuildArgs, opts.DependenciesArgsKeys); err != nil { + // Check stage not already created for another dockerfile? + // Filepath = dockerfileID — should be generated at this stage, deduplicate stages building at a higher level. + // Same underhood stage could be printed several times for each werf-image-target-name. + // /stage || /stage/ + + if stage, err := NewDockerfileStageFromBuildkitStage(i, werfImageName, dockerStage, expanderFactory, metaArgs, opts.BuildArgs, opts.DependenciesArgsKeys); err != nil { return nil, fmt.Errorf("error converting buildkit stage to dockerfile stage: %w", err) } else { stages = append(stages, stage) @@ -52,14 +57,14 @@ func ParseDockerfileWithBuildkit(dockerfileBytes []byte, opts dockerfile.Dockerf dockerfile.SetupDockerfileStagesDependencies(stages) - d := dockerfile.NewDockerfile(stages, opts) + d := dockerfile.NewDockerfile(dockerfileID, stages, opts) for _, stage := range d.Stages { stage.Dockerfile = d } return d, nil } -func NewDockerfileStageFromBuildkitStage(index int, stage instructions.Stage, expanderFactory *ShlexExpanderFactory, metaArgs, buildArgs map[string]string, dependenciesArgsKeys []string) (*dockerfile.DockerfileStage, error) { +func NewDockerfileStageFromBuildkitStage(index int, werfImageName string, stage instructions.Stage, expanderFactory *ShlexExpanderFactory, metaArgs, buildArgs map[string]string, dependenciesArgsKeys []string) (*dockerfile.DockerfileStage, error) { var stageInstructions []dockerfile.DockerfileStageInstructionInterface env := make(map[string]string) @@ -199,7 +204,7 @@ func NewDockerfileStageFromBuildkitStage(index int, stage instructions.Stage, ex stageInstructions = append(stageInstructions, i) } - return dockerfile.NewDockerfileStage(index, stage.BaseName, stage.Name, stageInstructions, stage.Platform, expanderFactory), nil + return dockerfile.NewDockerfileStage(index, stage.BaseName, stage.Name, werfImageName, stageInstructions, stage.Platform, expanderFactory), nil } func createAndExpandInstruction[T dockerfile.InstructionDataInterface](data T, expanderFactory dockerfile.ExpanderFactory, env map[string]string) (*dockerfile.DockerfileStageInstruction[T], error) {