Skip to content
Permalink
Browse files
fix(custom-tags): support custom tags for --final-repo images
* 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 e785c87d79ff1fcf35eb766abb5462e019bc7403
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 114 deletions.
@@ -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)

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

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

return nil
}); err != nil {
@@ -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)
@@ -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 {
@@ -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)

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

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

return nil
}); err != nil {
@@ -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)
@@ -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 {
@@ -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"
)
@@ -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
@@ -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 {
@@ -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
@@ -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
@@ -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)

@@ -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 {
@@ -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)
@@ -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 {
@@ -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{}{
@@ -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 {
@@ -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
@@ -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)
@@ -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)

@@ -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)
@@ -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)
@@ -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)

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

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

return nil
}); err != nil {
@@ -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)
@@ -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 {

0 comments on commit e785c87

Please sign in to comment.