From efd1072d1959aac824830a2128a49e47f4efb615 Mon Sep 17 00:00:00 2001 From: Alexey Igrychev Date: Wed, 13 Apr 2022 15:18:16 +0100 Subject: [PATCH] feat(custom-tags): add %image_content_based_tag% shortcut Signed-off-by: Alexey Igrychev --- cmd/werf/common/common.go | 18 ++++++++++------- cmd/werf/common/conveyor_options.go | 20 ++++++++++--------- docs/_includes/reference/cli/werf_build.md | 14 +++++++------ .../reference/cli/werf_bundle_export.md | 11 +++++----- .../reference/cli/werf_bundle_publish.md | 11 +++++----- docs/_includes/reference/cli/werf_converge.md | 11 +++++----- .../cli/werf_helm_get_autogenerated_values.md | 11 +++++----- docs/_includes/reference/cli/werf_render.md | 11 +++++----- pkg/build/build_phase.go | 10 ++++++---- pkg/build/conveyor.go | 2 +- .../chart_extender/helpers/service_values.go | 5 +++-- 11 files changed, 70 insertions(+), 54 deletions(-) diff --git a/cmd/werf/common/common.go b/cmd/werf/common/common.go index d00da4dc7a..68aea229f1 100644 --- a/cmd/werf/common/common.go +++ b/cmd/werf/common/common.go @@ -426,18 +426,22 @@ Also, can be specified with $WERF_SECONDARY_REPO_* (e.g. $WERF_SECONDARY_REPO_1= func SetupAddCustomTag(cmdData *CmdData, cmd *cobra.Command) { cmdData.AddCustomTag = new([]string) - cmd.Flags().StringArrayVarP(cmdData.AddCustomTag, "add-custom-tag", "", []string{}, `Set tag aliases for the content-based tag of each image. -It is necessary to use the image name shortcut %image% or %image_slug% in the tag format if there is more than one image in the werf config. -Also, can be defined with $WERF_ADD_CUSTOM_TAG_* (e.g. $WERF_ADD_CUSTOM_TAG_1="%image%-tag1", $WERF_ADD_CUSTOM_TAG_2="%image%-tag2"). -For cleaning custom tags and associated content-based tag are treated as one`) + cmd.Flags().StringArrayVarP(cmdData.AddCustomTag, "add-custom-tag", "", []string{}, `Set tag alias for the content-based tag. +The alias may contain the following shortcuts: +- %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there is more than one image in the werf config); +- %image_content_based_tag% to use a content-based tag. +For cleaning custom tags and associated content-based tag are treated as one. +Also can be defined with $WERF_ADD_CUSTOM_TAG_* (e.g. $WERF_ADD_CUSTOM_TAG_1="%image%-tag1", $WERF_ADD_CUSTOM_TAG_2="%image%-tag2")`) } func SetupUseCustomTag(cmdData *CmdData, cmd *cobra.Command) { cmdData.UseCustomTag = new(string) cmd.Flags().StringVarP(cmdData.UseCustomTag, "use-custom-tag", "", os.Getenv("WERF_USE_CUSTOM_TAG"), `Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). -It is necessary to use the image name shortcut %image% or %image_slug% in the tag format if there is more than one image in the werf config. -Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag"). -For cleaning custom tags and associated content-based tag are treated as one`) +The alias may contain the following shortcuts: +- %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there is more than one image in the werf config); +- %image_content_based_tag% to use a content-based tag. +For cleaning custom tags and associated content-based tag are treated as one. +Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag")`) } func SetupCacheStagesStorageOptions(cmdData *CmdData, cmd *cobra.Command) { diff --git a/cmd/werf/common/conveyor_options.go b/cmd/werf/common/conveyor_options.go index b5a2fae918..19c771e567 100644 --- a/cmd/werf/common/conveyor_options.go +++ b/cmd/werf/common/conveyor_options.go @@ -78,7 +78,7 @@ func GetBuildOptions(commonCmdData *CmdData, giterminismManager giterminism_mana return buildOptions, nil } -func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) ([]func(string) string, error) { +func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) ([]build.CustomTagFunc, error) { tagOptionValues := getCustomTagOptionValues(commonCmdData) if len(tagOptionValues) == 0 { return nil, nil @@ -95,12 +95,13 @@ func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism templateName := "--add/use-custom-tag" tmpl := template.New(templateName).Delims("%", "%") tmpl = tmpl.Funcs(map[string]interface{}{ - "image": func() string { return "%[1]s" }, - "image_slug": func() string { return "%[2]s" }, - "image_safe_slug": func() string { return "%[3]s" }, + "image": func() string { return "%[1]s" }, + "image_slug": func() string { return "%[2]s" }, + "image_safe_slug": func() string { return "%[3]s" }, + "image_content_based_tag": func() string { return "%[4]s" }, }) - var tagFuncList []func(string) string + var tagFuncList []build.CustomTagFunc for _, optionValue := range tagOptionValues { tmpl, err := tmpl.Parse(optionValue) if err != nil { @@ -113,17 +114,18 @@ func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism } tagOrFormat := buf.String() - tagFunc := func(imageName string) string { + tagFunc := func(imageName, contentBasedTag string) string { if strings.ContainsRune(tagOrFormat, '%') { - return fmt.Sprintf(tagOrFormat, imageName, slug.Slug(imageName), slug.DockerTag(imageName)) + return fmt.Sprintf(tagOrFormat, imageName, slug.Slug(imageName), slug.DockerTag(imageName), contentBasedTag) } else { return tagOrFormat } } + contentBasedTagStub := strings.Repeat("x", 70) // 1b77754d35b0a3e603731828ee6f2400c4f937382874db2566c616bb-1624991915332 var prevImageTag string for _, img := range werfConfig.GetAllImages() { - imageTag := tagFunc(img.GetName()) + imageTag := tagFunc(img.GetName(), contentBasedTagStub) if err := slug.ValidateDockerTag(imageTag); err != nil { return nil, fmt.Errorf("invalid custom tag %q: %w", optionValue, err) @@ -143,7 +145,7 @@ func getCustomTagFuncList(commonCmdData *CmdData, giterminismManager giterminism return tagFuncList, nil } -func GetUseCustomTagFunc(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) (func(string) string, error) { +func GetUseCustomTagFunc(commonCmdData *CmdData, giterminismManager giterminism_manager.Interface, werfConfig *config.WerfConfig) (build.CustomTagFunc, error) { customTagFuncList, err := getCustomTagFuncList(commonCmdData, giterminismManager, werfConfig) if err != nil { return nil, err diff --git a/docs/_includes/reference/cli/werf_build.md b/docs/_includes/reference/cli/werf_build.md index eb9422327b..721efeeceb 100644 --- a/docs/_includes/reference/cli/werf_build.md +++ b/docs/_includes/reference/cli/werf_build.md @@ -42,12 +42,14 @@ werf build [IMAGE_NAME...] [options] ```shell --add-custom-tag=[] - Set tag aliases for the content-based tag of each image. - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_ADD_CUSTOM_TAG_* (e.g. - $WERF_ADD_CUSTOM_TAG_1="%image%-tag1", $WERF_ADD_CUSTOM_TAG_2="%image%-tag2"). - For cleaning custom tags and associated content-based tag are treated as one + Set tag alias for the content-based tag. + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also can be defined with $WERF_ADD_CUSTOM_TAG_* (e.g. + $WERF_ADD_CUSTOM_TAG_1="%image%-tag1", $WERF_ADD_CUSTOM_TAG_2="%image%-tag2") --allowed-docker-storage-volume-usage=70 Set allowed percentage of docker storage volume usage which will cause cleanup of least recently used local docker images (default 70% or diff --git a/docs/_includes/reference/cli/werf_bundle_export.md b/docs/_includes/reference/cli/werf_bundle_export.md index e2461cbdbc..48b90cf466 100644 --- a/docs/_includes/reference/cli/werf_bundle_export.md +++ b/docs/_includes/reference/cli/werf_bundle_export.md @@ -269,11 +269,12 @@ werf bundle export [options] --use-custom-tag='' Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. - $WERF_USE_CUSTOM_TAG="%image%-tag"). - For cleaning custom tags and associated content-based tag are treated as one + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag") --values=[] Specify helm values in a YAML file or a URL (can specify multiple). Also, can be defined with $WERF_VALUES_* (e.g. $WERF_VALUES_ENV=.helm/values_test.yaml, diff --git a/docs/_includes/reference/cli/werf_bundle_publish.md b/docs/_includes/reference/cli/werf_bundle_publish.md index 5ad8aca812..68a755b104 100644 --- a/docs/_includes/reference/cli/werf_bundle_publish.md +++ b/docs/_includes/reference/cli/werf_bundle_publish.md @@ -280,11 +280,12 @@ werf bundle publish [options] --use-custom-tag='' Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. - $WERF_USE_CUSTOM_TAG="%image%-tag"). - For cleaning custom tags and associated content-based tag are treated as one + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag") --values=[] Specify helm values in a YAML file or a URL (can specify multiple). Also, can be defined with $WERF_VALUES_* (e.g. $WERF_VALUES_ENV=.helm/values_test.yaml, diff --git a/docs/_includes/reference/cli/werf_converge.md b/docs/_includes/reference/cli/werf_converge.md index 9700dcafc9..31dbed300d 100644 --- a/docs/_includes/reference/cli/werf_converge.md +++ b/docs/_includes/reference/cli/werf_converge.md @@ -337,11 +337,12 @@ werf converge --repo registry.mydomain.com/web --env production --use-custom-tag='' Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. - $WERF_USE_CUSTOM_TAG="%image%-tag"). - For cleaning custom tags and associated content-based tag are treated as one + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag") --values=[] Specify helm values in a YAML file or a URL (can specify multiple). Also, can be defined with $WERF_VALUES_* (e.g. $WERF_VALUES_ENV=.helm/values_test.yaml, diff --git a/docs/_includes/reference/cli/werf_helm_get_autogenerated_values.md b/docs/_includes/reference/cli/werf_helm_get_autogenerated_values.md index 78cc9b0990..feeda581e5 100644 --- a/docs/_includes/reference/cli/werf_helm_get_autogenerated_values.md +++ b/docs/_includes/reference/cli/werf_helm_get_autogenerated_values.md @@ -149,11 +149,12 @@ werf helm get-autogenerated-values [options] --use-custom-tag='' Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. - $WERF_USE_CUSTOM_TAG="%image%-tag"). - For cleaning custom tags and associated content-based tag are treated as one + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag") --virtual-merge=false Enable virtual/ephemeral merge commit mode when building current application state ($WERF_VIRTUAL_MERGE by default) diff --git a/docs/_includes/reference/cli/werf_render.md b/docs/_includes/reference/cli/werf_render.md index 4d55f64674..66780bebe6 100644 --- a/docs/_includes/reference/cli/werf_render.md +++ b/docs/_includes/reference/cli/werf_render.md @@ -272,11 +272,12 @@ werf render [options] --use-custom-tag='' Use a tag alias in helm templates instead of an image content-based tag (NOT RECOMMENDED). - It is necessary to use the image name shortcut %image% or %image_slug% in the tag - format if there is more than one image in the werf config. - Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. - $WERF_USE_CUSTOM_TAG="%image%-tag"). - For cleaning custom tags and associated content-based tag are treated as one + The alias may contain the following shortcuts: + - %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there + is more than one image in the werf config); + - %image_content_based_tag% to use a content-based tag. + For cleaning custom tags and associated content-based tag are treated as one. + Also, can be defined with $WERF_USE_CUSTOM_TAG (e.g. $WERF_USE_CUSTOM_TAG="%image%-tag") --validate=false Validate your manifests against the Kubernetes cluster you are currently pointing at (default $WERF_VALIDATE) diff --git a/pkg/build/build_phase.go b/pkg/build/build_phase.go index fa3110e6b8..62cd6b2313 100644 --- a/pkg/build/build_phase.go +++ b/pkg/build/build_phase.go @@ -43,9 +43,11 @@ type BuildOptions struct { ReportFormat ReportFormat SkipImageMetadataPublication bool - CustomTagFuncList []func(string) string + CustomTagFuncList []CustomTagFunc } +type CustomTagFunc func(string, string) string + type IntrospectOptions struct { Targets []IntrospectTarget } @@ -321,7 +323,7 @@ func (phase *BuildPhase) addCustomImageTagsToStagesStorage(ctx context.Context, return addCustomImageTags(ctx, phase.Conveyor.StorageManager.GetStagesStorage(), img, phase.CustomTagFuncList) } -func addCustomImageTags(ctx context.Context, stagesStorage storage.StagesStorage, img *Image, customTagFuncList []func(string) string) error { +func addCustomImageTags(ctx context.Context, stagesStorage storage.StagesStorage, img *Image, customTagFuncList []CustomTagFunc) error { if len(customTagFuncList) == 0 { return nil } @@ -332,7 +334,7 @@ func addCustomImageTags(ctx context.Context, stagesStorage storage.StagesStorage }). DoError(func() error { for _, tagFunc := range customTagFuncList { - tag := tagFunc(img.GetName()) + tag := tagFunc(img.GetName(), img.GetStageID()) if err := addCustomImageTag(ctx, stagesStorage, img, tag); err != nil { return err } @@ -363,7 +365,7 @@ func (phase *BuildPhase) checkCustomImageTagsExistence(ctx context.Context, img stageDesc := img.GetLastNonEmptyStage().GetStageImage().Image.GetStageDescription() for _, tagFunc := range phase.CustomTagFuncList { - tag := tagFunc(img.GetName()) + tag := tagFunc(img.GetName(), img.GetStageID()) if err := phase.Conveyor.StorageManager.GetStagesStorage().CheckStageCustomTag(ctx, stageDesc, tag); err != nil { return fmt.Errorf("check custom tag %q existence failed: %w", tag, err) } diff --git a/pkg/build/conveyor.go b/pkg/build/conveyor.go index 7a11dda557..4c2b5f4321 100644 --- a/pkg/build/conveyor.go +++ b/pkg/build/conveyor.go @@ -307,7 +307,7 @@ func (c *Conveyor) GetRemoteGitRepo(key string) *git_repo.Remote { } type ShouldBeBuiltOptions struct { - CustomTagFuncList []func(string) string + CustomTagFuncList []CustomTagFunc } func (c *Conveyor) ShouldBeBuilt(ctx context.Context, opts ShouldBeBuiltOptions) error { diff --git a/pkg/deploy/helm/chart_extender/helpers/service_values.go b/pkg/deploy/helm/chart_extender/helpers/service_values.go index ccc60dd07e..87789226a8 100644 --- a/pkg/deploy/helm/chart_extender/helpers/service_values.go +++ b/pkg/deploy/helm/chart_extender/helpers/service_values.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/yaml" "github.com/werf/logboek" + "github.com/werf/werf/pkg/build" "github.com/werf/werf/pkg/image" "github.com/werf/werf/pkg/werf" ) @@ -38,7 +39,7 @@ type ServiceValuesOptions struct { Env string IsStub bool StubImagesNames []string - CustomTagFunc func(string) string + CustomTagFunc build.CustomTagFunc CommitHash string CommitDate *time.Time @@ -98,7 +99,7 @@ func GetServiceValues(ctx context.Context, projectName string, repo string, imag var image string if opts.CustomTagFunc != nil { - tag = opts.CustomTagFunc(imageInfoGetter.GetWerfImageName()) + tag = opts.CustomTagFunc(imageInfoGetter.GetWerfImageName(), imageInfoGetter.GetTag()) image = strings.Join([]string{repo, tag}, ":") } else { tag = imageInfoGetter.GetTag()