Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(staged-dockerfile): refactored container backend dockerfile builder
* Unified interface of instructions. * No switch-case by instruction type, use polymorphism instead. * Introduced BuildContext to keep unpacked context tar between executing multiple instructions. * Renaming of primitives and some packages. Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
- Loading branch information
1 parent
62b2181
commit a210944
Showing
28 changed files
with
632 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uild/stage/dockerfile_instruction/base.go → pkg/build/stage/instruction/base.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package dockerfile_instruction | ||
package instruction | ||
|
||
import ( | ||
"github.com/werf/werf/pkg/build/stage" | ||
|
2 changes: 1 addition & 1 deletion
2
...ile_instruction/dockerfile_instruction.go → pkg/build/stage/instruction/instruction.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package dockerfile_instruction | ||
package instruction | ||
|
||
import "github.com/werf/werf/pkg/build/stage" | ||
|
||
|
15 changes: 8 additions & 7 deletions
15
...build/stage/dockerfile_instruction/run.go → pkg/build/stage/instruction/run.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
package dockerfile_instruction | ||
package instruction | ||
|
||
import ( | ||
"context" | ||
|
||
"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/util" | ||
) | ||
|
||
type Run struct { | ||
*Base | ||
instruction *container_backend.InstructionRun | ||
instruction *backend_instruction.Run | ||
} | ||
|
||
func NewRun(instruction *container_backend.InstructionRun, dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Run { | ||
func NewRun(i *backend_instruction.Run, dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Run { | ||
return &Run{ | ||
Base: NewBase("RUN", dependencies, hasPrevStage, opts), | ||
instruction: instruction, | ||
Base: NewBase(InstructionRun, dependencies, hasPrevStage, opts), | ||
instruction: i, | ||
} | ||
} | ||
|
||
func (stage *Run) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage) (string, error) { | ||
return util.Sha256Hash(stage.instruction.Command...), nil | ||
return util.Sha256Hash(append([]string{string(InstructionRun)}, stage.instruction.Command...)...), nil | ||
} | ||
|
||
func (stage *Run) PrepareImage(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *stage.StageImage) error { | ||
stageImage.Builder.DockerfileStageBuilder().AppendMainCommands(stage.instruction) | ||
stageImage.Builder.DockerfileStageBuilder().AppendInstruction(stage.instruction) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package build_context | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/werf/werf/pkg/util" | ||
) | ||
|
||
type BuildContext struct { | ||
ContextTarReader io.ReadCloser | ||
TmpDir string | ||
|
||
contextTmpDir string | ||
} | ||
|
||
func NewBuildContext(tmpDir string, contextTarReader io.ReadCloser) *BuildContext { | ||
return &BuildContext{TmpDir: tmpDir, ContextTarReader: contextTarReader} | ||
} | ||
|
||
func (c *BuildContext) GetContextDir(ctx context.Context) (string, error) { | ||
if c.contextTmpDir != "" { | ||
return c.contextTmpDir, nil | ||
} | ||
|
||
contextTmpDir, err := ioutil.TempDir(c.TmpDir, "context") | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create context tmp dir: %w", err) | ||
} | ||
|
||
if err := util.ExtractTar(c.ContextTarReader, contextTmpDir, util.ExtractTarOptions{}); err != nil { | ||
return "", fmt.Errorf("unable to extract context tar to tmp context dir: %w", err) | ||
} | ||
if err := c.ContextTarReader.Close(); err != nil { | ||
return "", fmt.Errorf("error closing context tar: %w", err) | ||
} | ||
|
||
c.contextTmpDir = contextTmpDir | ||
|
||
return c.contextTmpDir, nil | ||
} | ||
|
||
func (c *BuildContext) Terminate() error { | ||
if c.contextTmpDir == "" { | ||
return nil | ||
} | ||
if err := os.RemoveAll(c.contextTmpDir); err != nil { | ||
return fmt.Errorf("unable to remove dir %q: %w", c.contextTmpDir, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.