Skip to content

Commit

Permalink
feat(staged-dockerfile): move container backend instructions data int…
Browse files Browse the repository at this point in the history
…o dockerfile parser package

Embed instructions data into container backend instructions.

refs #2215

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Oct 12, 2022
1 parent 1f6ce1c commit 9500967
Show file tree
Hide file tree
Showing 36 changed files with 304 additions and 117 deletions.
3 changes: 2 additions & 1 deletion pkg/build/build_phase.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/werf/werf/pkg/container_backend"
backend_instruction "github.com/werf/werf/pkg/container_backend/instruction"
"github.com/werf/werf/pkg/docker_registry"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
"github.com/werf/werf/pkg/git_repo"
imagePkg "github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/stapel"
Expand Down Expand Up @@ -677,7 +678,7 @@ func (phase *BuildPhase) prepareStageInstructions(ctx context.Context, img *imag
return stageImage.Builder.DockerfileBuilder().Cleanup(ctx)
})
} else {
stageImage.Builder.DockerfileStageBuilder().AppendPostInstruction(backend_instruction.NewLabel(serviceLabels))
stageImage.Builder.DockerfileStageBuilder().AppendPostInstruction(backend_instruction.NewLabel(*dockerfile_instruction.NewLabel(serviceLabels)))
}

err := stg.PrepareImage(ctx, phase.Conveyor, phase.Conveyor.ContainerBackend, phase.StagesIterator.GetPrevBuiltImage(img, stg), stageImage)
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/image/dockerfile.go
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/werf/werf/pkg/config"
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/path_matcher"
"github.com/werf/werf/pkg/util"
)
Expand Down Expand Up @@ -82,7 +83,7 @@ func mapDockerfileToImagesSets(ctx context.Context, cfg *dockerfile.Dockerfile,
return nil, fmt.Errorf("unable to create image %q: %w", "test", err)
}

img.stages = append(img.stages, stage_instruction.NewRun(backend_instruction.NewRun([]string{"ls", "/"}), nil, false, &stage.BaseStageOptions{
img.stages = append(img.stages, stage_instruction.NewRun(backend_instruction.NewRun(*dockerfile_instruction.NewRun([]string{"ls", "/"})), nil, false, &stage.BaseStageOptions{
ImageName: img.Name,
ImageTmpDir: img.TmpDir,
ContainerWerfDir: img.ContainerWerfDir,
Expand Down
12 changes: 4 additions & 8 deletions pkg/container_backend/instruction/add.go
Expand Up @@ -6,25 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Add struct {
Src []string
Dst string
dockerfile_instruction.Add
}

func NewAdd(src []string, dst string) *Add {
return &Add{Src: src, Dst: dst}
func NewAdd(i dockerfile_instruction.Add) *Add {
return &Add{Add: i}
}

func (i *Add) UsesBuildContext() bool {
return true
}

func (i *Add) Name() string {
return "ADD"
}

func (i *Add) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
contextDir, err := buildContext.GetContextDir(ctx)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions pkg/container_backend/instruction/cmd.go
Expand Up @@ -6,26 +6,23 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Cmd struct {
Cmd []string
dockerfile_instruction.Cmd
}

func NewCmd(cmd []string) *Cmd {
return &Cmd{Cmd: cmd}
func NewCmd(i dockerfile_instruction.Cmd) *Cmd {
return &Cmd{Cmd: i}
}

func (i *Cmd) UsesBuildContext() bool {
return false
}

func (i *Cmd) Name() string {
return "CMD"
}

func (i *Cmd) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Cmd: i.Cmd}); err != nil {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Cmd: i.Cmd.Cmd}); err != nil {
return fmt.Errorf("error setting cmd %v for container %s: %w", i.Cmd, containerName, err)
}
return nil
Expand Down
13 changes: 4 additions & 9 deletions pkg/container_backend/instruction/copy.go
Expand Up @@ -6,26 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Copy struct {
From string
Src []string
Dst string
dockerfile_instruction.Copy
}

func NewCopy(from string, src []string, dst string) *Copy {
return &Copy{From: from, Src: src, Dst: dst}
func NewCopy(i dockerfile_instruction.Copy) *Copy {
return &Copy{Copy: i}
}

func (i *Copy) UsesBuildContext() bool {
return true
}

func (i *Copy) Name() string {
return "COPY"
}

func (i *Copy) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
contextDir, err := buildContext.GetContextDir(ctx)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions pkg/container_backend/instruction/entrypoint.go
Expand Up @@ -6,26 +6,23 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Entrypoint struct {
Entrypoint []string
dockerfile_instruction.Entrypoint
}

func NewEntrypoint(entrypoint []string) *Entrypoint {
return &Entrypoint{Entrypoint: entrypoint}
func NewEntrypoint(i dockerfile_instruction.Entrypoint) *Entrypoint {
return &Entrypoint{Entrypoint: i}
}

func (i *Entrypoint) UsesBuildContext() bool {
return false
}

func (i *Entrypoint) Name() string {
return "ENTRYPOINT"
}

func (i *Entrypoint) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Entrypoint: i.Entrypoint}); err != nil {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Entrypoint: i.Entrypoint.Entrypoint}); err != nil {
return fmt.Errorf("error setting entrypoint %v for container %s: %w", i.Entrypoint, containerName, err)
}
return nil
Expand Down
11 changes: 4 additions & 7 deletions pkg/container_backend/instruction/env.go
Expand Up @@ -6,24 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Env struct {
Envs map[string]string
dockerfile_instruction.Env
}

func NewEnv(envs map[string]string) *Env {
return &Env{Envs: envs}
func NewEnv(i dockerfile_instruction.Env) *Env {
return &Env{Env: i}
}

func (i *Env) UsesBuildContext() bool {
return false
}

func (i *Env) Name() string {
return "ENV"
}

func (i *Env) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Envs: i.Envs}); err != nil {
return fmt.Errorf("error setting envs %v for container %s: %w", i.Envs, containerName, err)
Expand Down
16 changes: 5 additions & 11 deletions pkg/container_backend/instruction/expose.go
Expand Up @@ -6,29 +6,23 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Expose struct {
Ports []string
dockerfile_instruction.Expose
}

func NewExpose(ports []string) *Expose {
return &Expose{Ports: ports}
func NewExpose(i dockerfile_instruction.Expose) *Expose {
return &Expose{Expose: i}
}

func (i *Expose) UsesBuildContext() bool {
return false
}

func (i *Expose) Name() string {
return "EXPOSE"
}

func (i *Expose) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{
CommonOpts: drvOpts,
Expose: i.Ports,
}); err != nil {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Expose: i.Ports}); err != nil {
return fmt.Errorf("error setting exposed ports %v for container %s: %w", i.Ports, containerName, err)
}
return nil
Expand Down
9 changes: 7 additions & 2 deletions pkg/container_backend/instruction/healthcheck.go
@@ -1,8 +1,11 @@
package instruction

import (
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Healthcheck struct {
Type HealthcheckType
Command string
dockerfile_instruction.Healthcheck
}

type HealthcheckType string
Expand All @@ -12,3 +15,5 @@ var (
HealthcheckTypeCmd HealthcheckType = "CMD"
HealthcheckTypeCmdShell HealthcheckType = "CMD-SHELL"
)

// TODO
11 changes: 4 additions & 7 deletions pkg/container_backend/instruction/label.go
Expand Up @@ -6,24 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Label struct {
Labels map[string]string
dockerfile_instruction.Label
}

func NewLabel(labels map[string]string) *Label {
return &Label{Labels: labels}
func NewLabel(i dockerfile_instruction.Label) *Label {
return &Label{Label: i}
}

func (i *Label) UsesBuildContext() bool {
return false
}

func (i *Label) Name() string {
return "LABEL"
}

func (i *Label) LabelsAsList() []string {
var labels []string
for k, v := range i.Labels {
Expand Down
11 changes: 4 additions & 7 deletions pkg/container_backend/instruction/on_build.go
Expand Up @@ -6,24 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type OnBuild struct {
Instruction string
dockerfile_instruction.OnBuild
}

func NewOnBuild(instruction string) *OnBuild {
return &OnBuild{Instruction: instruction}
func NewOnBuild(i dockerfile_instruction.OnBuild) *OnBuild {
return &OnBuild{OnBuild: i}
}

func (i *OnBuild) UsesBuildContext() bool {
return false
}

func (i *OnBuild) Name() string {
return "ONBUILD"
}

func (i *OnBuild) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, OnBuild: i.Instruction}); err != nil {
return fmt.Errorf("error setting onbuild %v for container %s: %w", i.Instruction, containerName, err)
Expand Down
11 changes: 4 additions & 7 deletions pkg/container_backend/instruction/run.go
Expand Up @@ -6,24 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Run struct {
Command []string
dockerfile_instruction.Run
}

func NewRun(command []string) *Run {
return &Run{Command: command}
func NewRun(i dockerfile_instruction.Run) *Run {
return &Run{Run: i}
}

func (i *Run) UsesBuildContext() bool {
return false
}

func (i *Run) Name() string {
return "RUN"
}

func (i *Run) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.RunCommand(ctx, containerName, i.Command, buildah.RunCommandOpts{
// FIXME(ilya-lesikov): should we suppress or not?
Expand Down
13 changes: 5 additions & 8 deletions pkg/container_backend/instruction/shell.go
Expand Up @@ -6,26 +6,23 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type Shell struct {
Shell []string
dockerfile_instruction.Shell
}

func NewShell(shell []string) *Shell {
return &Shell{Shell: shell}
func NewShell(i dockerfile_instruction.Shell) *Shell {
return &Shell{Shell: i}
}

func (i *Shell) UsesBuildContext() bool {
return false
}

func (i *Shell) Name() string {
return "SHELL"
}

func (i *Shell) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Shell: i.Shell}); err != nil {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, Shell: i.Shell.Shell}); err != nil {
return fmt.Errorf("error setting shell %v for container %s: %w", i.Shell, containerName, err)
}
return nil
Expand Down
11 changes: 4 additions & 7 deletions pkg/container_backend/instruction/stop_signal.go
Expand Up @@ -6,24 +6,21 @@ import (

"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/container_backend/build_context"
dockerfile_instruction "github.com/werf/werf/pkg/dockerfile/instruction"
)

type StopSignal struct {
Signal string
dockerfile_instruction.StopSignal
}

func NewStopSignal(signal string) *StopSignal {
return &StopSignal{Signal: signal}
func NewStopSignal(i dockerfile_instruction.StopSignal) *StopSignal {
return &StopSignal{StopSignal: i}
}

func (i *StopSignal) UsesBuildContext() bool {
return false
}

func (i *StopSignal) Name() string {
return "STOPSIGNAL"
}

func (i *StopSignal) Apply(ctx context.Context, containerName string, drv buildah.Buildah, drvOpts buildah.CommonOpts, buildContext *build_context.BuildContext) error {
if err := drv.Config(ctx, containerName, buildah.ConfigOpts{CommonOpts: drvOpts, StopSignal: i.Signal}); err != nil {
return fmt.Errorf("error setting stop signal %v for container %s: %w", i.Signal, containerName, err)
Expand Down

0 comments on commit 9500967

Please sign in to comment.