Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/build/build_phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func (phase *BuildPhase) scanOptionsForImage(img *image.Image) scanner.ScanOptio
return scanOpts
}

if len(scanOpts.Commands) == 0 {
return scanOpts
}

catalogers := managedinput.ToCatalogers(stapelConfig.ImageBaseConfig().Packages)
for i := range scanOpts.Commands {
scanOpts.Commands[i].Catalogers = catalogers
Expand Down
8 changes: 8 additions & 0 deletions pkg/build/builder/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (b *Ansible) BeforeSetupChecksum(ctx context.Context) string {
}
func (b *Ansible) SetupChecksum(ctx context.Context) string { return b.stageChecksum(ctx, "Setup") }

func (b *Ansible) IsPackagesEmpty(_ context.Context) bool { return true }

func (b *Ansible) Packages(_ context.Context, _ container_backend.ContainerBackend, _ stage_builder.StageBuilderInterface, _ bool) error {
return nil
}

func (b *Ansible) PackagesChecksum(_ context.Context) string { return "" }

func (b *Ansible) isEmptyStage(ctx context.Context, userStageName string) bool {
return b.stageChecksum(ctx, userStageName) == ""
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/build/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ type Builder interface {
IsInstallEmpty(ctx context.Context) bool
IsBeforeSetupEmpty(ctx context.Context) bool
IsSetupEmpty(ctx context.Context) bool
IsPackagesEmpty(ctx context.Context) bool
BeforeInstall(ctx context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
Install(ctx context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
BeforeSetup(ctx context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
Setup(ctx context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
Packages(ctx context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error
BeforeInstallChecksum(ctx context.Context) string
InstallChecksum(ctx context.Context) string
BeforeSetupChecksum(ctx context.Context) string
SetupChecksum(ctx context.Context) string
PackagesChecksum(ctx context.Context) string
}

type Container interface {
Expand Down
9 changes: 8 additions & 1 deletion pkg/build/builder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (b *Shell) IsBeforeSetupEmpty(ctx context.Context) bool {
}
func (b *Shell) IsSetupEmpty(ctx context.Context) bool { return b.isEmptyStage(ctx, "Setup") }

func (b *Shell) IsPackagesEmpty(ctx context.Context) bool { return b.isEmptyStage(ctx, "Packages") }

func (b *Shell) BeforeInstall(_ context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "BeforeInstall")
}
Expand All @@ -56,14 +58,19 @@ func (b *Shell) Setup(_ context.Context, cr container_backend.ContainerBackend,
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "Setup")
}

func (b *Shell) Packages(_ context.Context, cr container_backend.ContainerBackend, stageBuilder stage_builder.StageBuilderInterface, useLegacyStapelBuilder bool) error {
return b.stage(cr, stageBuilder, useLegacyStapelBuilder, "Packages")
}

func (b *Shell) BeforeInstallChecksum(ctx context.Context) string {
return b.stageChecksum(ctx, "BeforeInstall")
}
func (b *Shell) InstallChecksum(ctx context.Context) string { return b.stageChecksum(ctx, "Install") }
func (b *Shell) BeforeSetupChecksum(ctx context.Context) string {
return b.stageChecksum(ctx, "BeforeSetup")
}
func (b *Shell) SetupChecksum(ctx context.Context) string { return b.stageChecksum(ctx, "Setup") }
func (b *Shell) SetupChecksum(ctx context.Context) string { return b.stageChecksum(ctx, "Setup") }
func (b *Shell) PackagesChecksum(ctx context.Context) string { return b.stageChecksum(ctx, "Packages") }

func (b *Shell) isEmptyStage(ctx context.Context, userStageName string) bool {
return b.stageChecksum(ctx, userStageName) == ""
Expand Down
1 change: 1 addition & 0 deletions pkg/build/image/image_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ func stageDependenciesToMap(sd *config.StageDependencies) map[stage.StageName][]
stage.Install: sd.Install,
stage.BeforeSetup: sd.BeforeSetup,
stage.Setup: sd.Setup,
stage.Packages: sd.Packages,
}

return result
Expand Down
27 changes: 27 additions & 0 deletions pkg/build/image/stapel.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func initStages(ctx context.Context, image *Image, metaConfig *config.Meta, stap
stages = append(stages, stage.NewGitArchiveStage(gitArchiveStageOptions, baseStageOptions))
}

stages = appendIfExist(ctx, stages, stage.GeneratePackagesStage(ctx, imageBaseConfig, gitPatchStageOptions, baseStageOptions))

stages = appendIfExist(ctx, stages, stage.GenerateInstallStage(ctx, imageBaseConfig, gitPatchStageOptions, baseStageOptions))
stages = appendIfExist(ctx, stages, stage.GenerateDependenciesAfterInstallStage(imageBaseConfig, baseStageOptions))
stages = appendIfExist(ctx, stages, stage.GenerateBeforeSetupStage(ctx, imageBaseConfig, gitPatchStageOptions, baseStageOptions))
Expand Down Expand Up @@ -149,11 +151,36 @@ func initStages(ctx context.Context, image *Image, metaConfig *config.Meta, stap
}
}

sbomEnabled := metaConfig.Build.Sbom != nil && metaConfig.Build.Sbom.Enable
if sbomEnabled {
hasShellStages := false
for _, s := range stages {
if stageHasNetworkAccess(s) {
continue
}
if no, ok := s.(interface{ SetNetworkOverride(string) }); ok {
no.SetNetworkOverride("none")
hasShellStages = true
}
}

if hasShellStages {
logboek.Context(ctx).Warn().LogLn("Network is disabled for shell stages (build.sbom.enable is true). Declare dependencies via 'packages' directive.")
}
}

image.SetStages(stages)

return nil
}

func stageHasNetworkAccess(s stage.Interface) bool {
if nn, ok := s.(interface{ NeedsNetwork() bool }); ok {
return nn.NeedsNetwork()
}
return false
}

// TODO(v3): make this a hard error instead of a warning.
func warnStageDependenciesWithoutInstructions(ctx context.Context, imageBaseConfig *config.StapelImageBase, gitMappings []*stage.GitMapping) {
for _, gitMapping := range gitMappings {
Expand Down
24 changes: 21 additions & 3 deletions pkg/build/stage/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
DependenciesBeforeSetup StageName = "dependenciesBeforeSetup"
Setup StageName = "setup"
DependenciesAfterSetup StageName = "dependenciesAfterSetup"
Packages StageName = "packages"
GitCache StageName = "gitCache"
GitLatestPatch StageName = "gitLatestPatch"
DockerInstructions StageName = "dockerInstructions"
Expand Down Expand Up @@ -88,6 +89,7 @@ type BaseStageOptions struct {
ContainerWerfDir string
ProjectName string
Network string
NeedsNetwork bool
}

const disableGitCommitAncestryCheckEnv = "WERF_DISABLE_GIT_COMMIT_ANCESTRY_CHECK"
Expand Down Expand Up @@ -120,6 +122,7 @@ func NewBaseStage(name StageName, options *BaseStageOptions) *BaseStage {
s.containerWerfDir = options.ContainerWerfDir
s.projectName = options.ProjectName
s.network = options.Network
s.needsNetwork = options.NeedsNetwork
s.meta = &StageMeta{}
return s
}
Expand All @@ -138,6 +141,8 @@ type BaseStage struct {
configMounts []*config.Mount
projectName string
network string
networkOverride string
needsNetwork bool
meta *StageMeta
}

Expand All @@ -151,6 +156,14 @@ func (s *BaseStage) IsBuildable() bool {
return true
}

func (s *BaseStage) SetNetworkOverride(network string) {
s.networkOverride = network
}

func (s *BaseStage) NeedsNetwork() bool {
return s.needsNetwork
}

func (s *BaseStage) IsMutable() bool {
return false
}
Expand Down Expand Up @@ -344,11 +357,16 @@ func (s *BaseStage) PrepareImage(ctx context.Context, c Conveyor, cb container_b

s.addProjectRepoCommitLabel(ctx, c, cb, stageImage)

if s.network != "" {
network := s.network
if s.networkOverride != "" {
network = s.networkOverride
}

if network != "" {
if c.UseLegacyStapelBuilder(cb) {
stageImage.Builder.LegacyStapelStageBuilder().Container().RunOptions().AddNetwork(s.network)
stageImage.Builder.LegacyStapelStageBuilder().Container().RunOptions().AddNetwork(network)
} else {
stageImage.Builder.StapelStageBuilder().SetNetwork(s.network)
stageImage.Builder.StapelStageBuilder().SetNetwork(network)
}
}

Expand Down
52 changes: 52 additions & 0 deletions pkg/build/stage/packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package stage

import (
"context"

"github.com/werf/common-go/pkg/util"
"github.com/werf/werf/v2/pkg/build/builder"
"github.com/werf/werf/v2/pkg/config"
"github.com/werf/werf/v2/pkg/container_backend"
)

type PackagesStage struct {
*UserWithGitPatchStage
}

func GeneratePackagesStage(ctx context.Context, imageBaseConfig *config.StapelImageBase, gitPatchStageOptions *NewGitPatchStageOptions, baseStageOptions *BaseStageOptions) *PackagesStage {
b := getBuilder(imageBaseConfig, baseStageOptions)
if b != nil && !b.IsPackagesEmpty(ctx) {
return newPackagesStage(b, gitPatchStageOptions, baseStageOptions)
}

return nil
}

func newPackagesStage(builder builder.Builder, gitPatchStageOptions *NewGitPatchStageOptions, baseStageOptions *BaseStageOptions) *PackagesStage {
opts := *baseStageOptions
opts.NeedsNetwork = true
s := &PackagesStage{}
s.UserWithGitPatchStage = newUserWithGitPatchStage(builder, Packages, gitPatchStageOptions, &opts)
return s
}

func (s *PackagesStage) GetDependencies(ctx context.Context, c Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
stageDependenciesChecksum, err := s.getStageDependenciesChecksum(ctx, c, Packages)
if err != nil {
return "", err
}

return util.Sha256Hash(s.builder.PackagesChecksum(ctx), stageDependenciesChecksum), nil
}

func (s *PackagesStage) PrepareImage(ctx context.Context, c Conveyor, cb container_backend.ContainerBackend, prevBuiltImage, stageImage *StageImage, buildContextArchive container_backend.BuildContextArchiver) error {
if err := s.UserWithGitPatchStage.PrepareImage(ctx, c, cb, prevBuiltImage, stageImage, nil); err != nil {
return err
}

if err := s.builder.Packages(ctx, cb, stageImage.Builder, c.UseLegacyStapelBuilder(cb)); err != nil {
return err
}

return nil
}
Loading
Loading