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 Oct 29, 2024
1 parent dc3a2c6 commit 10dec6e
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 127 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/v2/pkg/config"
"github.com/werf/werf/v2/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 @@ -47,6 +47,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 @@ -95,10 +96,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 @@ -156,6 +153,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 @@ -286,12 +284,7 @@ func run(
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/v2/pkg/git_repo/gitdata"
"github.com/werf/werf/v2/pkg/image"
"github.com/werf/werf/v2/pkg/ssh_agent"
"github.com/werf/werf/v2/pkg/storage"
"github.com/werf/werf/v2/pkg/storage/lrumeta"
"github.com/werf/werf/v2/pkg/storage/manager"
"github.com/werf/werf/v2/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 @@ -30,6 +30,7 @@ import (
"github.com/werf/werf/v2/pkg/giterminism_manager"
"github.com/werf/werf/v2/pkg/image"
"github.com/werf/werf/v2/pkg/ssh_agent"
"github.com/werf/werf/v2/pkg/storage"
"github.com/werf/werf/v2/pkg/storage/lrumeta"
"github.com/werf/werf/v2/pkg/storage/manager"
"github.com/werf/werf/v2/pkg/tmp_manager"
Expand Down Expand Up @@ -94,10 +95,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 @@ -157,6 +154,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)

Expand Down Expand Up @@ -278,12 +278,7 @@ func run(
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 @@ -302,9 +297,22 @@ func run(
}

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 @@ -341,7 +349,7 @@ func run(

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 @@ -516,9 +524,12 @@ func run(
return fmt.Errorf("get HEAD commit time: %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: releaseNamespace,
Env: *commonCmdData.Environment,
IsStub: isStub,
DisableEnvStub: true,
StubImageNameList: stubImageNameList,
SetDockerConfigJsonValue: *commonCmdData.SetDockerConfigJsonValue,
DockerConfigPath: filepath.Dir(registryCredentialsPath),
CommitHash: headHash,
Expand Down
Loading

0 comments on commit 10dec6e

Please sign in to comment.