Skip to content
Permalink
Browse files
fix(staged-dockerfile): fix multiple stages with the same name from m…
…ultiple Dockerfiles

Fix naming conflict, which occured when building multiple Dockerfile-s with stages with same names.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jan 30, 2023
1 parent b844a5f commit 76f654d
Show file tree
Hide file tree
Showing 37 changed files with 84 additions and 73 deletions.
@@ -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)),
)
}
}
@@ -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)
}
}

@@ -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) {
@@ -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) {
@@ -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
}
}
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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 {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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}
}

@@ -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 {
@@ -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}
}

@@ -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}
}

0 comments on commit 76f654d

Please sign in to comment.