Skip to content

Commit

Permalink
fix(custom-tags): support custom tags for --final-repo images
Browse files Browse the repository at this point in the history
* Tag only images from the final repo in the case when --final-repo param has been specified.
* Set custom tag (or check should-be-built) after copying images into the final repo.
* Refactor: use CustomTagFunc in the image.InfoGetter, do not use it in the service options getter.
* Add simple test for image.InfoGetter.

Fixes #4515

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 10, 2022
1 parent 92231f5 commit e785c87
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 114 deletions.
18 changes: 8 additions & 10 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -251,6 +251,10 @@ func runExport(ctx context.Context) error {
if err != nil {
return err
}
useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -280,7 +284,7 @@ func runExport(ctx context.Context) error {
}
}

imagesInfoGetters = c.GetImageInfoGetters()
imagesInfoGetters = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})

return nil
}); err != nil {
Expand Down Expand Up @@ -308,11 +312,6 @@ func runExport(ctx context.Context) error {
return err
}

useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return fmt.Errorf("getting HEAD commit hash failed: %w", err)
Expand All @@ -324,10 +323,9 @@ func runExport(ctx context.Context) error {
}

if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Env: *commonCmdData.Environment,
CustomTagFunc: useCustomTagFunc,
CommitHash: headHash,
CommitDate: headTime,
Env: *commonCmdData.Environment,
CommitHash: headHash,
CommitDate: headTime,
}); err != nil {
return fmt.Errorf("error creating service values: %w", err)
} else {
Expand Down
18 changes: 8 additions & 10 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -277,6 +277,10 @@ func runPublish(ctx context.Context) error {
if err != nil {
return err
}
useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -306,7 +310,7 @@ func runPublish(ctx context.Context) error {
}
}

imagesInfoGetters = c.GetImageInfoGetters()
imagesInfoGetters = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})

return nil
}); err != nil {
Expand Down Expand Up @@ -339,11 +343,6 @@ func runPublish(ctx context.Context) error {
return err
}

useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return fmt.Errorf("getting HEAD commit hash failed: %w", err)
Expand All @@ -355,10 +354,9 @@ func runPublish(ctx context.Context) error {
}

if vals, err := helpers.GetServiceValues(ctx, werfConfig.Meta.Project, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Env: *commonCmdData.Environment,
CustomTagFunc: useCustomTagFunc,
CommitHash: headHash,
CommitDate: headTime,
Env: *commonCmdData.Environment,
CommitHash: headHash,
CommitDate: headTime,
}); err != nil {
return fmt.Errorf("error creating service values: %w", err)
} else {
Expand Down
7 changes: 4 additions & 3 deletions cmd/werf/common/conveyor_options.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/giterminism_manager"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/slug"
"github.com/werf/werf/pkg/storage"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func GetBuildOptions(commonCmdData *CmdData, giterminismManager giterminism_mana
return buildOptions, nil
}

func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) ([]build.CustomTagFunc, error) {
func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) ([]image.CustomTagFunc, error) {
tagOptionValues := getCustomTagOptionValues(commonCmdData)
if len(tagOptionValues) == 0 {
return nil, nil
Expand All @@ -101,7 +102,7 @@ func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism
"image_content_based_tag": func() string { return "%[4]s" },
})

var tagFuncList []build.CustomTagFunc
var tagFuncList []image.CustomTagFunc
for _, optionValue := range tagOptionValues {
tmpl, err := tmpl.Parse(optionValue)
if err != nil {
Expand Down Expand Up @@ -145,7 +146,7 @@ func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism
return tagFuncList, nil
}

func GetUseCustomTagFunc(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) (build.CustomTagFunc, error) {
func GetUseCustomTagFunc(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) (image.CustomTagFunc, error) {
customTagFuncList, err := getCustomTagFuncList(commonCmdData, giterminismManager, werfConfig)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/common/deploy_params.go
Expand Up @@ -54,7 +54,7 @@ func StubImageInfoGetters(werfConfig *config.WerfConfig) (list []*image.InfoGett
}

for _, imageName := range imagesNames {
list = append(list, image.NewInfoGetter(imageName, fmt.Sprintf("%s:%s", StubRepoAddress, StubTag), StubTag))
list = append(list, image.NewInfoGetter(imageName, fmt.Sprintf("%s:%s", StubRepoAddress, StubTag), image.InfoGetterOptions{}))
}

return list
Expand Down
12 changes: 5 additions & 7 deletions cmd/werf/converge/converge.go
Expand Up @@ -297,6 +297,10 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
if err != nil {
return err
}
useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -326,7 +330,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}
}

imagesInfoGetters = c.GetImageInfoGetters()
imagesInfoGetters = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})

return nil
}); err != nil {
Expand Down Expand Up @@ -390,11 +394,6 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return err
}

useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return fmt.Errorf("getting HEAD commit hash failed: %w", err)
Expand All @@ -410,7 +409,6 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
Env: *commonCmdData.Environment,
SetDockerConfigJsonValue: *commonCmdData.SetDockerConfigJsonValue,
DockerConfigPath: *commonCmdData.DockerConfig,
CustomTagFunc: useCustomTagFunc,
CommitHash: headHash,
CommitDate: headTime,
}); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/werf/export/export.go
Expand Up @@ -234,7 +234,7 @@ func run(ctx context.Context, imagesToProcess, tagTemplateList []string) error {
})
}

func getTagFuncList(imageNameList, tagTemplateList []string) ([]build.ExportTagFunc, error) {
func getTagFuncList(imageNameList, tagTemplateList []string) ([]image.ExportTagFunc, error) {
templateName := "--tag"
tmpl := template.New(templateName).Delims("%", "%")
tmpl = tmpl.Funcs(map[string]interface{}{
Expand All @@ -244,7 +244,7 @@ func getTagFuncList(imageNameList, tagTemplateList []string) ([]build.ExportTagF
"image_content_based_tag": func() string { return "%[4]s" },
})

var tagFuncList []build.ExportTagFunc
var tagFuncList []image.ExportTagFunc
for _, tagTemplate := range tagTemplateList {
tagFunc, err := getExportTagFunc(tmpl, templateName, imageNameList, tagTemplate)
if err != nil {
Expand All @@ -257,7 +257,7 @@ func getTagFuncList(imageNameList, tagTemplateList []string) ([]build.ExportTagF
return tagFuncList, nil
}

func getExportTagFunc(tmpl *template.Template, templateName string, imageNameList []string, tagTemplate string) (build.ExportTagFunc, error) {
func getExportTagFunc(tmpl *template.Template, templateName string, imageNameList []string, tagTemplate string) (image.ExportTagFunc, error) {
tmpl, err := tmpl.Parse(tagTemplate)
if err != nil {
return nil, err
Expand All @@ -269,7 +269,7 @@ func getExportTagFunc(tmpl *template.Template, templateName string, imageNameLis
}

tagOrFormat := buf.String()
var tagFunc build.ExportTagFunc
var tagFunc image.ExportTagFunc
tagFunc = func(imageName string, contentBasedTag string) string {
if strings.ContainsRune(tagOrFormat, '%') {
return fmt.Sprintf(tagOrFormat, imageName, slug.Slug(imageName), slug.DockerTag(imageName), contentBasedTag)
Expand Down
20 changes: 9 additions & 11 deletions cmd/werf/helm/get_autogenerated_values.go
Expand Up @@ -195,6 +195,10 @@ func runGetServiceValues(ctx context.Context) error {
if err != nil {
return err
}
useCustomTagFunc, err := common.GetUseCustomTagFunc(&getAutogeneratedValuedCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

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

Expand All @@ -212,19 +216,14 @@ func runGetServiceValues(ctx context.Context) error {
}

imagesRepository = storageManager.StagesStorage.String()
imagesInfoGetters = c.GetImageInfoGetters()
imagesInfoGetters = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})

return nil
}); err != nil {
return err
}
}

useCustomTagFunc, err := common.GetUseCustomTagFunc(&getAutogeneratedValuedCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return fmt.Errorf("getting HEAD commit hash failed: %w", err)
Expand All @@ -236,11 +235,10 @@ func runGetServiceValues(ctx context.Context) error {
}

serviceValues, err := helpers.GetServiceValues(ctx, projectName, imagesRepository, imagesInfoGetters, helpers.ServiceValuesOptions{
Namespace: namespace,
Env: environment,
CustomTagFunc: useCustomTagFunc,
CommitHash: headHash,
CommitDate: headTime,
Namespace: namespace,
Env: environment,
CommitHash: headHash,
CommitDate: headTime,
})
if err != nil {
return fmt.Errorf("error creating service values: %w", err)
Expand Down
12 changes: 5 additions & 7 deletions cmd/werf/render/render.go
Expand Up @@ -286,6 +286,10 @@ func runRender(ctx context.Context) error {
if err != nil {
return err
}
useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -315,7 +319,7 @@ func runRender(ctx context.Context) error {
}
}

imagesInfoGetters = c.GetImageInfoGetters()
imagesInfoGetters = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})

return nil
}); err != nil {
Expand Down Expand Up @@ -357,11 +361,6 @@ func runRender(ctx context.Context) error {
return err
}

useCustomTagFunc, err := common.GetUseCustomTagFunc(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return fmt.Errorf("getting HEAD commit hash failed: %w", err)
Expand All @@ -379,7 +378,6 @@ func runRender(ctx context.Context) error {
StubImagesNames: stubImagesNames,
SetDockerConfigJsonValue: *commonCmdData.SetDockerConfigJsonValue,
DockerConfigPath: *commonCmdData.DockerConfig,
CustomTagFunc: useCustomTagFunc,
CommitHash: headHash,
CommitDate: headTime,
}); err != nil {
Expand Down

0 comments on commit e785c87

Please sign in to comment.