Skip to content

Commit

Permalink
fix(deploy): ensure werf commands run consistently without images or …
Browse files Browse the repository at this point in the history
…with stubs

This fix standardizes werf command behavior in cases where images are not used or should be replaced by stubs:
* werf converge can now be executed --without-images.
* werf plan now supports both --without-images and --stub-tags options.

Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
alexey-igrychev committed Nov 15, 2024
1 parent 5ab0315 commit ae00ef0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 97 deletions.
12 changes: 0 additions & 12 deletions cmd/werf/common/deploy_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package common
import (
"fmt"
"strings"

"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/image"
)

func GetUserExtraAnnotations(cmdData *CmdData) (map[string]string, error) {
Expand Down Expand Up @@ -49,12 +46,3 @@ func KeyValueArrayToMap(pairs []string, sep string) (map[string]string, error) {

return keyValueMap, nil
}

func StubImageInfoGetters(imagesToProcess config.ImagesToProcess) []*image.InfoGetter {
var getters []*image.InfoGetter
for _, imageName := range imagesToProcess.FinalImageNameList {
getters = append(getters, image.NewInfoGetter(imageName, fmt.Sprintf("%s:%s", StubRepoAddress, StubTag), image.InfoGetterOptions{}))
}

return getters
}
13 changes: 3 additions & 10 deletions cmd/werf/converge/converge.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var cmdData struct {

var commonCmdData common.CmdData

// TODO: support specific images in v3 by default.
func isSpecificImagesEnabled() bool {
return util.GetBoolEnvironmentDefaultFalse("WERF_CONVERGE_ENABLE_IMAGES_PARAMS")
}
Expand Down Expand Up @@ -125,10 +126,6 @@ werf converge --repo registry.mydomain.com/web --env production`,
},
})

if isSpecificImagesEnabled() {
commonCmdData.SetupWithoutImages(cmd)
}

common.SetupDir(&commonCmdData, cmd)
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -186,6 +183,7 @@ werf converge --repo registry.mydomain.com/web --env production`,
commonCmdData.SetupDisableDefaultValues(cmd)
commonCmdData.SetupDisableDefaultSecretValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)
commonCmdData.SetupWithoutImages(cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
Expand Down Expand Up @@ -316,12 +314,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return fmt.Errorf("unable to load werf config: %w", err)
}

var withoutImages bool
if isSpecificImagesEnabled() {
withoutImages = *commonCmdData.WithoutImages
}

imagesToProcess, err := config.NewImagesToProcess(werfConfig, imageNameListFromArgs, true, withoutImages)
imagesToProcess, err := config.NewImagesToProcess(werfConfig, imageNameListFromArgs, true, *commonCmdData.WithoutImages)
if err != nil {
return err
}
Expand Down
36 changes: 23 additions & 13 deletions cmd/werf/helm/get_autogenerated_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
"github.com/werf/werf/pkg/tmp_manager"
Expand Down Expand Up @@ -173,23 +174,29 @@ func runGetServiceValues(ctx context.Context, imageNameListFromArgs []string) er
return err
}

var imagesRepository string
var imagesInfoGetters []*image.InfoGetter
var imagesRepository string
var isStub bool
var stubImageNameList []string

addr, err := getAutogeneratedValuedCmdData.Repo.GetAddress()
if err != nil {
return err
}

if *getAutogeneratedValuedCmdData.StubTags {
imagesInfoGetters = common.StubImageInfoGetters(imagesToProcess)
imagesRepository = common.StubRepoAddress
} else if !imagesToProcess.WithoutImages {
switch {
case imagesToProcess.WithoutImages:
case *getAutogeneratedValuedCmdData.StubTags || addr == storage.LocalStorageAddress:
imagesRepository = "REPO"
isStub = true
stubImageNameList = append(stubImageNameList, imagesToProcess.FinalImageNameList...)
default:
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %w", err)
}
defer tmp_manager.ReleaseProjectDir(projectTmpDir)

_, err = getAutogeneratedValuedCmdData.Repo.GetAddress()
if err != nil {
return fmt.Errorf("%w (use --stub-tags option to get service values without real tags)", err)
}
stagesStorage, err := common.GetStagesStorage(ctx, containerBackend, &getAutogeneratedValuedCmdData)
if err != nil {
return err
Expand Down Expand Up @@ -262,10 +269,13 @@ func runGetServiceValues(ctx context.Context, imageNameListFromArgs []string) er
}

serviceValues, err := helpers.GetServiceValues(ctx, projectName, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Namespace: namespace,
Env: environment,
CommitHash: headHash,
CommitDate: headTime,
Namespace: namespace,
Env: environment,
IsStub: isStub,
DisableEnvStub: true,
StubImageNameList: stubImageNameList,
CommitHash: headHash,
CommitDate: headTime,
})
if err != nil {
return fmt.Errorf("error creating service values: %w", err)
Expand Down
39 changes: 25 additions & 14 deletions cmd/werf/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/werf/werf/pkg/giterminism_manager"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
"github.com/werf/werf/pkg/tmp_manager"
Expand Down Expand Up @@ -115,10 +116,6 @@ werf plan --repo registry.mydomain.com/web --env production`,
},
})

if isSpecificImagesEnabled() {
commonCmdData.SetupWithoutImages(cmd)
}

common.SetupDir(&commonCmdData, cmd)
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
Expand Down Expand Up @@ -177,6 +174,9 @@ werf plan --repo registry.mydomain.com/web --env production`,
commonCmdData.SetupDisableDefaultSecretValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)

commonCmdData.SetupWithoutImages(cmd)
common.SetupStubTags(&commonCmdData, cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
Expand Down Expand Up @@ -301,12 +301,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return fmt.Errorf("unable to load werf config: %w", err)
}

var withoutImages bool
if isSpecificImagesEnabled() {
withoutImages = *commonCmdData.WithoutImages
}

imagesToProcess, err := config.NewImagesToProcess(werfConfig, imageNameListFromArgs, true, withoutImages)
imagesToProcess, err := config.NewImagesToProcess(werfConfig, imageNameListFromArgs, true, *commonCmdData.WithoutImages)
if err != nil {
return err
}
Expand All @@ -332,9 +327,22 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}

var imagesInfoGetters []*image.InfoGetter
var imagesRepo string
var imagesRepository string
var isStub bool
var stubImageNameList []string

addr, err := commonCmdData.Repo.GetAddress()
if err != nil {
return err
}

if !imagesToProcess.WithoutImages {
switch {
case imagesToProcess.WithoutImages:
case *commonCmdData.StubTags || addr == storage.LocalStorageAddress:
imagesRepository = "REPO"
isStub = true
stubImageNameList = append(stubImageNameList, imagesToProcess.FinalImageNameList...)
default:
stagesStorage, err := common.GetStagesStorage(ctx, containerBackend, &commonCmdData)
if err != nil {
return err
Expand Down Expand Up @@ -367,7 +375,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken

storageManager := manager.NewStorageManager(projectName, stagesStorage, finalStagesStorage, secondaryStagesStorageList, cacheStagesStorageList, storageLockManager)

imagesRepo = storageManager.GetServiceValuesRepo()
imagesRepository = storageManager.GetServiceValuesRepo()

conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
Expand Down Expand Up @@ -473,9 +481,12 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return fmt.Errorf("getting HEAD commit time failed: %w", err)
}

if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepo, imagesInfoGetters, helpers.ServiceValuesOptions{
if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Namespace: namespace,
Env: *commonCmdData.Environment,
IsStub: isStub,
DisableEnvStub: true,
StubImageNameList: stubImageNameList,
SetDockerConfigJsonValue: *commonCmdData.SetDockerConfigJsonValue,
DockerConfigPath: *commonCmdData.DockerConfig,
CommitHash: headHash,
Expand Down
96 changes: 48 additions & 48 deletions cmd/werf/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,26 @@ func runRender(ctx context.Context, imageNameListFromArgs []string) error {
logboek.LogOptionalLn()

var imagesInfoGetters []*image.InfoGetter
var imagesRepo string
var imagesRepository string
var isStub bool
var stubImageNameList []string

if !imagesToProcess.WithoutImages {
addr, err := commonCmdData.Repo.GetAddress()
if err != nil {
addr, err := commonCmdData.Repo.GetAddress()
if err != nil {
return err
}

switch {
case imagesToProcess.WithoutImages:
case *commonCmdData.StubTags || addr == storage.LocalStorageAddress:
imagesRepository = "REPO"
isStub = true
stubImageNameList = append(stubImageNameList, imagesToProcess.FinalImageNameList...)
default:
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

if !*commonCmdData.StubTags && addr != storage.LocalStorageAddress {
if err := common.DockerRegistryInit(ctx, &commonCmdData, registryMirrors); err != nil {
return err
}

stagesStorage, err := common.GetStagesStorage(ctx, containerBackend, &commonCmdData)
if err != nil {
return err
Expand Down Expand Up @@ -320,56 +325,51 @@ func runRender(ctx context.Context, imageNameListFromArgs []string) error {
return err
}

storageManager := manager.NewStorageManager(projectName, stagesStorage, finalStagesStorage, secondaryStagesStorageList, cacheStagesStorageList, storageLockManager)
storageManager := manager.NewStorageManager(projectName, stagesStorage, finalStagesStorage, secondaryStagesStorageList, cacheStagesStorageList, storageLockManager)

imagesRepo = storageManager.GetServiceValuesRepo()
imagesRepository = storageManager.GetServiceValuesRepo()

conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}
conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}

// Override default behaviour:
// Print build logs on error by default.
// Always print logs if --log-verbose is specified (level.Info).
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
conveyorOptions.DeferBuildLog = !isVerbose

conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, giterminismManager, giterminismManager.ProjectDir(), projectTmpDir, ssh_agent.SSHAuthSock, containerBackend, storageManager, storageLockManager, conveyorOptions)
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, imagesToProcess)
if err != nil {
return err
}

if err := c.ShouldBeBuilt(ctx, shouldBeBuiltOptions); err != nil {
return err
}
} else {
if err := c.Build(ctx, buildOptions); err != nil {
return err
}
}
// Override default behaviour:
// Print build logs on error by default.
// Always print logs if --log-verbose is specified (level.Info).
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
conveyorOptions.DeferBuildLog = !isVerbose

conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, giterminismManager, giterminismManager.ProjectDir(), projectTmpDir, ssh_agent.SSHAuthSock, containerBackend, storageManager, storageLockManager, conveyorOptions)
defer conveyorWithRetry.Terminate()

imagesInfoGetters, err = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})
if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, imagesToProcess)
if err != nil {
return err
}

return nil
}); err != nil {
if err := c.ShouldBeBuilt(ctx, shouldBeBuiltOptions); err != nil {
return err
}
} else {
if err := c.Build(ctx, buildOptions); err != nil {
return err
}
}

imagesInfoGetters, err = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})
if err != nil {
return err
}

logboek.LogOptionalLn()
} else {
imagesRepo = "REPO"
isStub = true
stubImageNameList = append(stubImageNameList, imagesToProcess.FinalImageNameList...)
return nil
}); err != nil {
return err
}

logboek.LogOptionalLn()
}

secretsManager := secrets_manager.NewSecretsManager(secrets_manager.SecretsManagerOptions{DisableSecretsDecryption: *commonCmdData.IgnoreSecretKey})
Expand Down Expand Up @@ -406,7 +406,7 @@ func runRender(ctx context.Context, imageNameListFromArgs []string) error {
return fmt.Errorf("getting HEAD commit time failed: %w", err)
}

if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepo, imagesInfoGetters, helpers.ServiceValuesOptions{
if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Namespace: namespace,
Env: *commonCmdData.Environment,
IsStub: isStub,
Expand Down
4 changes: 4 additions & 0 deletions docs/_includes/reference/cli/werf_converge.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,9 @@ werf converge --repo registry.mydomain.com/web --env production
--virtual-merge=false
Enable virtual/ephemeral merge commit mode when building current application state
($WERF_VIRTUAL_MERGE by default)
--without-images=false
Disable building of images defined in the werf.yaml (if any) and usage of such images
in the .helm/templates ($WERF_WITHOUT_IMAGES or false by default — e.g. enable all
images defined in the werf.yaml by default)
```
6 changes: 6 additions & 0 deletions docs/_includes/reference/cli/werf_plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ werf plan --repo registry.mydomain.com/web --env production
--status-progress-period=5
Status progress period in seconds. Set -1 to stop showing status progress. Defaults to
$WERF_STATUS_PROGRESS_PERIOD_SECONDS or 5 seconds
--stub-tags=false
Use stubs instead of real tags (default $WERF_STUB_TAGS)
-S, --synchronization=''
Address of synchronizer for multiple werf processes to work with a single repo.

Expand Down Expand Up @@ -394,5 +396,9 @@ werf plan --repo registry.mydomain.com/web --env production
--virtual-merge=false
Enable virtual/ephemeral merge commit mode when building current application state
($WERF_VIRTUAL_MERGE by default)
--without-images=false
Disable building of images defined in the werf.yaml (if any) and usage of such images
in the .helm/templates ($WERF_WITHOUT_IMAGES or false by default — e.g. enable all
images defined in the werf.yaml by default)
```

0 comments on commit ae00ef0

Please sign in to comment.