/
base.go
64 lines (52 loc) · 2.75 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package instruction
import (
"context"
"fmt"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/dockerfile"
)
type Base[T dockerfile.InstructionDataInterface, BT container_backend.InstructionInterface] struct {
*stage.BaseStage
instruction *dockerfile.DockerfileStageInstruction[T]
backendInstruction BT
dependencies []*config.Dependency
hasPrevStage bool
}
func NewBase[T dockerfile.InstructionDataInterface, BT container_backend.InstructionInterface](instruction *dockerfile.DockerfileStageInstruction[T], backendInstruction BT, dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Base[T, BT] {
return &Base[T, BT]{
BaseStage: stage.NewBaseStage(stage.StageName(instruction.Data.Name()), opts),
instruction: instruction,
backendInstruction: backendInstruction,
dependencies: dependencies,
hasPrevStage: hasPrevStage,
}
}
func (stg *Base[T, BT]) HasPrevStage() bool {
return stg.hasPrevStage
}
func (stg *Base[T, BT]) IsStapelStage() bool {
return false
}
func (stg *Base[T, BT]) UsesBuildContext() bool {
return stg.backendInstruction.UsesBuildContext()
}
func (stg *Base[T, BT]) getDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver, expander InstructionExpander) ([]string, error) {
if err := expander.ExpandInstruction(ctx, c, cb, prevImage, prevBuiltImage, buildContextArchive); err != nil {
return nil, fmt.Errorf("unable to expand instruction %q: %w", stg.instruction.Data.Name(), err)
}
return nil, nil
}
func (stg *Base[T, BT]) PrepareImage(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) error {
stageImage.Builder.DockerfileStageBuilder().AppendInstruction(stg.backendInstruction)
return nil
}
func (stg *Base[T, BT]) ExpandInstruction(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) error {
dependenciesArgs := stage.ResolveDependenciesArgs(stg.dependencies, c)
// NOTE: do not skip unset envs during second stage expansion
return stg.instruction.Expand(dependenciesArgs, dockerfile.ExpandOptions{SkipUnsetEnv: false})
}
type InstructionExpander interface {
ExpandInstruction(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) error
}