Skip to content

Commit

Permalink
fix: rework build/deploy report options
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Feb 10, 2023
1 parent 98ac7f9 commit 40a8e81
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 216 deletions.
4 changes: 2 additions & 2 deletions cmd/werf/build/main.go
Expand Up @@ -99,10 +99,10 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupKubeConfigBase64(&commonCmdData, cmd)
common.SetupKubeContext(&commonCmdData, cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportFormat(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupBuildReportFormat(&commonCmdData, cmd)

common.SetupAddCustomTag(&commonCmdData, cmd)
common.SetupVirtualMerge(&commonCmdData, cmd)
Expand Down
12 changes: 11 additions & 1 deletion cmd/werf/bundle/apply/apply.go
Expand Up @@ -87,6 +87,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupSecretValues(&commonCmdData, cmd)
common.SetupIgnoreSecretKey(&commonCmdData, cmd)

common.SetupSaveDeployReport(&commonCmdData, cmd)
common.SetupDeployReportPath(&commonCmdData, cmd)

common.SetupKubeConfig(&commonCmdData, cmd)
Expand Down Expand Up @@ -219,6 +220,15 @@ func runApply(ctx context.Context) error {
ChartExtender: bundle,
}

var deployReportPath *string
if common.GetSaveDeployReport(&commonCmdData) {
if path, err := common.GetDeployReportPath(&commonCmdData); err != nil {
return fmt.Errorf("unable to get deploy report path: %w", err)
} else {
deployReportPath = &path
}
}

helmUpgradeCmd, _ := helm_v3.NewUpgradeCmd(actionConfig, logboek.Context(ctx).OutStream(), helm_v3.UpgradeCmdOptions{
StagesSplitter: helm.NewStagesSplitter(),
StagesExternalDepsGenerator: helm.NewStagesExternalDepsGenerator(&actionConfig.RESTClientGetter, &namespace),
Expand All @@ -236,7 +246,7 @@ func runApply(ctx context.Context) error {
Timeout: common.NewDuration(time.Duration(cmdData.Timeout) * time.Second),
IgnorePending: common.NewBool(true),
CleanupOnFail: common.NewBool(true),
DeployReportPath: commonCmdData.DeployReportPath,
DeployReportPath: deployReportPath,
})

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -126,10 +126,10 @@ func NewCmd(ctx context.Context) *cobra.Command {
commonCmdData.SetupDisableDefaultValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportFormat(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupBuildReportFormat(&commonCmdData, cmd)

common.SetupUseCustomTag(&commonCmdData, cmd)
common.SetupVirtualMerge(&commonCmdData, cmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -115,10 +115,10 @@ func NewCmd(ctx context.Context) *cobra.Command {
commonCmdData.SetupDisableDefaultValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportFormat(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupBuildReportFormat(&commonCmdData, cmd)

common.SetupUseCustomTag(&commonCmdData, cmd)
common.SetupAddCustomTag(&commonCmdData, cmd)
Expand Down
9 changes: 6 additions & 3 deletions cmd/werf/common/cmd_data.go
Expand Up @@ -93,9 +93,12 @@ type CmdData struct {

DeprecatedReportPath *string
DeprecatedReportFormat *string
BuildReportPath *string
BuildReportFormat *string
DeployReportPath *string

SaveBuildReport *bool
BuildReportPath *string

SaveDeployReport *bool
DeployReportPath *string

VirtualMerge *bool

Expand Down
124 changes: 86 additions & 38 deletions cmd/werf/common/common.go
Expand Up @@ -36,6 +36,12 @@ const (
StubTag = "TAG"
DefaultBuildParallelTasksLimit = 5
DefaultCleanupParallelTasksLimit = 10

DefaultSaveBuildReport = false
DefaultBuildReportPathJSON = ".werf-build-report.json"

DefaultSaveDeployReport = false
DefaultDeployReportPathJSON = ".werf-deploy-report.json"
)

func GetLongCommandDescription(text string) string {
Expand Down Expand Up @@ -135,13 +141,13 @@ Defaults to $WERF_SSH_KEY_*, system ssh-agent or ~/.ssh/{id_rsa|id_dsa}, see htt

func SetupDeprecatedReportPath(cmdData *CmdData, cmd *cobra.Command) {
cmdData.DeprecatedReportPath = new(string)
cmd.Flags().StringVarP(cmdData.DeprecatedReportPath, "report-path", "", os.Getenv("WERF_REPORT_PATH"), "DEPRECATED: use --build-report-path.\nReport save path ($WERF_REPORT_PATH by default)")
cmd.Flags().StringVarP(cmdData.DeprecatedReportPath, "report-path", "", os.Getenv("WERF_REPORT_PATH"), "DEPRECATED: use --save-build-report with optional --build-report-path.\nReport save path ($WERF_REPORT_PATH by default)")
}

func SetupDeprecatedReportFormat(cmdData *CmdData, cmd *cobra.Command) {
cmdData.DeprecatedReportFormat = new(string)

cmd.Flags().StringVarP(cmdData.DeprecatedReportFormat, "report-format", "", os.Getenv("WERF_REPORT_FORMAT"), fmt.Sprintf(`DEPRECATED: use --build-report-format.
cmd.Flags().StringVarP(cmdData.DeprecatedReportFormat, "report-format", "", os.Getenv("WERF_REPORT_FORMAT"), fmt.Sprintf(`DEPRECATED: use --save-build-report with optional --build-report-path.
Report format: %[1]s or %[2]s (%[1]s or $WERF_REPORT_FORMAT by default) %[1]s:
{
"Images": {
Expand All @@ -164,62 +170,104 @@ Report format: %[1]s or %[2]s (%[1]s or $WERF_REPORT_FORMAT by default) %[1]s:
- charset /- is replaced with _ (DEV/APP-FRONTEND -> DEV_APP_FRONTEND)`, string(build.ReportJSON), string(build.ReportEnvFile)))
}

func GetDeprecatedReportFormat(cmdData *CmdData) (build.ReportFormat, error) {
switch format := build.ReportFormat(*cmdData.DeprecatedReportFormat); format {
func GetDeprecatedReportPath(ctx context.Context, cmdData *CmdData) string {
if cmdData.DeprecatedReportPath == nil {
return ""
}

logboek.Context(ctx).Warn().LogF("DEPRECATED: use --save-build-report ($WERF_SAVE_BUILD_REPORT) with optional --build-report-path ($WERF_BUILD_REPORT_PATH) instead of --report-path ($WERF_REPORT_PATH)\n")

return *cmdData.DeprecatedReportPath
}

func GetDeprecatedReportFormat(ctx context.Context, cmdData *CmdData) (build.ReportFormat, error) {
if cmdData.DeprecatedReportFormat == nil {
return build.ReportJSON, nil
}

var reportFormat build.ReportFormat
switch reportFormat = build.ReportFormat(*cmdData.DeprecatedReportFormat); reportFormat {
case build.ReportJSON, build.ReportEnvFile:
return format, nil
case "":
return build.ReportJSON, nil
defaultFormat := build.ReportJSON
reportFormat = defaultFormat
default:
return "", fmt.Errorf("bad --report-format given %q, expected: \"%s\"", format, strings.Join([]string{string(build.ReportJSON), string(build.ReportEnvFile)}, "\", \""))
return "", fmt.Errorf("bad --report-format given %q, expected: \"%s\"", reportFormat, strings.Join([]string{string(build.ReportJSON), string(build.ReportEnvFile)}, "\", \""))
}

logboek.Context(ctx).Warn().LogF("DEPRECATED: use --save-build-report ($WERF_SAVE_BUILD_REPORT) and optionally --build-report-path ($WERF_BUILD_REPORT_PATH) instead of --report-format ($WERF_REPORT_FORMAT)\n")

return reportFormat, nil
}

func SetupSaveBuildReport(cmdData *CmdData, cmd *cobra.Command) {
cmdData.SaveBuildReport = new(bool)
cmd.Flags().BoolVarP(cmdData.SaveBuildReport, "save-build-report", "", util.GetBoolEnvironmentDefaultFalse("WERF_SAVE_BUILD_REPORT"), fmt.Sprintf("Save build report (by default $WERF_SAVE_BUILD_REPORT or %t)", DefaultSaveBuildReport))
}

func SetupBuildReportPath(cmdData *CmdData, cmd *cobra.Command) {
cmdData.BuildReportPath = new(string)
cmd.Flags().StringVarP(cmdData.BuildReportPath, "build-report-path", "", os.Getenv("WERF_BUILD_REPORT_PATH"), "Report save path ($WERF_BUILD_REPORT_PATH by default)")
cmd.Flags().StringVarP(cmdData.BuildReportPath, "build-report-path", "", os.Getenv("WERF_BUILD_REPORT_PATH"), fmt.Sprintf("Change build report save path (by default $WERF_BUILD_REPORT_PATH or %q if not set). Extension must be either .json or .env, .json will be used if not specified", DefaultBuildReportPathJSON))
}

func SetupBuildReportFormat(cmdData *CmdData, cmd *cobra.Command) {
cmdData.BuildReportFormat = new(string)

cmd.Flags().StringVarP(cmdData.BuildReportFormat, "build-report-format", "", os.Getenv("WERF_BUILD_REPORT_FORMAT"), fmt.Sprintf(`Report format: %[1]s or %[2]s (%[1]s or $WERF_BUILD_REPORT_FORMAT by default)
%[1]s:
{
"Images": {
"<WERF_IMAGE_NAME>": {
"WerfImageName": "<WERF_IMAGE_NAME>",
"DockerRepo": "<REPO>",
"DockerTag": "<TAG>"
"DockerImageName": "<REPO>:<TAG>",
"DockerImageID": "<SHA256>",
"DockerImageDigest": "<SHA256>",
},
...
}
func GetSaveBuildReport(cmdData *CmdData) bool {
if cmdData.SaveBuildReport == nil {
return false
}
%[2]s:
WERF_<FORMATTED_WERF_IMAGE_NAME>_DOCKER_IMAGE_NAME=<REPO>:<TAG>
...
<FORMATTED_WERF_IMAGE_NAME> is werf image name from werf.yaml modified according to the following rules:
- all characters are uppercase (app -> APP);
- charset /- is replaced with _ (DEV/APP-FRONTEND -> DEV_APP_FRONTEND)`, string(build.ReportJSON), string(build.ReportEnvFile)))

return *cmdData.SaveBuildReport
}

func GetBuildReportFormat(cmdData *CmdData) (build.ReportFormat, error) {
switch format := build.ReportFormat(*cmdData.BuildReportFormat); format {
case build.ReportJSON, build.ReportEnvFile:
return format, nil
func GetBuildReportPathAndFormat(cmdData *CmdData) (string, build.ReportFormat, error) {
unspecifiedPath := cmdData.BuildReportPath == nil || *cmdData.BuildReportPath == ""
if unspecifiedPath {
return DefaultBuildReportPathJSON, build.ReportJSON, nil
}

switch ext := filepath.Ext(*cmdData.BuildReportPath); ext {
case ".json":
return *cmdData.BuildReportPath, build.ReportJSON, nil
case ".env":
return *cmdData.BuildReportPath, build.ReportEnvFile, nil
case "":
return build.ReportJSON, nil
return *cmdData.BuildReportPath + ".json", build.ReportJSON, nil
default:
return "", fmt.Errorf("bad --build-report-format given %q, expected: \"%s\"", format, strings.Join([]string{string(build.ReportJSON), string(build.ReportEnvFile)}, "\", \""))
return "", "", fmt.Errorf("invalid --build-report-path %q: extension must be either .json or .env or unspecified", *cmdData.BuildReportPath)
}
}

func SetupSaveDeployReport(cmdData *CmdData, cmd *cobra.Command) {
cmdData.SaveDeployReport = new(bool)
cmd.Flags().BoolVarP(cmdData.SaveDeployReport, "save-deploy-report", "", util.GetBoolEnvironmentDefaultFalse("WERF_SAVE_DEPLOY_REPORT"), fmt.Sprintf("Save deploy report (by default $WERF_SAVE_DEPLOY_REPORT or %t)", DefaultSaveDeployReport))
}

func SetupDeployReportPath(cmdData *CmdData, cmd *cobra.Command) {
cmdData.DeployReportPath = new(string)
cmd.Flags().StringVarP(cmdData.DeployReportPath, "deploy-report-path", "", os.Getenv("WERF_DEPLOY_REPORT_PATH"), "Deploy report save path ($WERF_DEPLOY_REPORT_PATH by default)")
cmd.Flags().StringVarP(cmdData.DeployReportPath, "deploy-report-path", "", os.Getenv("WERF_DEPLOY_REPORT_PATH"), fmt.Sprintf("Change deploy report save path (by default $WERF_DEPLOY_REPORT_PATH or %q if not set). Extension must be either .json or unspecified", DefaultDeployReportPathJSON))
}

func GetSaveDeployReport(cmdData *CmdData) bool {
if cmdData.SaveDeployReport == nil {
return false
}

return *cmdData.SaveDeployReport
}

func GetDeployReportPath(cmdData *CmdData) (string, error) {
unspecifiedPath := cmdData.DeployReportPath == nil || *cmdData.DeployReportPath == ""
if unspecifiedPath {
return DefaultDeployReportPathJSON, nil
}

switch ext := filepath.Ext(*cmdData.DeployReportPath); ext {
case ".json":
return *cmdData.DeployReportPath, nil
case "":
return *cmdData.DeployReportPath + ".json", nil
default:
return "", fmt.Errorf("invalid --deploy-report-path %q: extension must be either .json or unspecified", *cmdData.DeployReportPath)
}
}

func SetupWithoutKube(cmdData *CmdData, cmd *cobra.Command) {
Expand Down
53 changes: 22 additions & 31 deletions cmd/werf/common/conveyor_options.go
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"text/template"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
Expand Down Expand Up @@ -57,34 +56,6 @@ func GetBuildOptions(ctx context.Context, commonCmdData *CmdData, giterminismMan
return buildOptions, err
}

var buildReportPath string
if commonCmdData.BuildReportPath != nil && *commonCmdData.BuildReportPath != "" && commonCmdData.DeprecatedReportPath != nil && *commonCmdData.DeprecatedReportPath != "" {
return buildOptions, fmt.Errorf("you can't use both --report-path ($WERF_REPORT_PATH) and --build-report-path ($WERF_BUILD_REPORT_PATH), use only the latter instead")
} else if commonCmdData.BuildReportPath != nil && *commonCmdData.BuildReportPath != "" {
buildReportPath = *commonCmdData.BuildReportPath
} else if commonCmdData.DeprecatedReportPath != nil && *commonCmdData.DeprecatedReportPath != "" {
logboek.Context(ctx).Warn().LogF("DEPRECATED: use --build-report-path ($WERF_BUILD_REPORT_PATH) instead of --report-path ($WERF_REPORT_PATH)\n")
buildReportPath = *commonCmdData.DeprecatedReportPath
}

var buildReportFormat build.ReportFormat
if commonCmdData.BuildReportFormat != nil && *commonCmdData.BuildReportFormat != "" && commonCmdData.DeprecatedReportFormat != nil && *commonCmdData.DeprecatedReportFormat != "" {
return buildOptions, fmt.Errorf("you can't use both --report-format ($WERF_REPORT_FORMAT) and --build-report-format ($WERF_BUILD_REPORT_FORMAT), use only the latter instead")
} else if commonCmdData.BuildReportFormat != nil && *commonCmdData.BuildReportFormat != "" {
buildReportFormat, err = GetBuildReportFormat(commonCmdData)
if err != nil {
return buildOptions, fmt.Errorf("error getting build report format: %w", err)
}
} else if commonCmdData.DeprecatedReportFormat != nil && *commonCmdData.DeprecatedReportFormat != "" {
logboek.Context(ctx).Warn().LogF("DEPRECATED: use --build-report-format ($WERF_BUILD_REPORT_FORMAT) instead of --report-format ($WERF_REPORT_FORMAT)\n")
buildReportFormat, err = GetDeprecatedReportFormat(commonCmdData)
if err != nil {
return buildOptions, fmt.Errorf("error getting report format: %w", err)
}
} else {
buildReportFormat = build.ReportJSON
}

customTagFuncList, err := getCustomTagFuncList(getCustomTagOptionValues(commonCmdData), commonCmdData, giterminismManager, werfConfig)
if err != nil {
return buildOptions, err
Expand All @@ -98,8 +69,28 @@ func GetBuildOptions(ctx context.Context, commonCmdData *CmdData, giterminismMan
IntrospectBeforeError: *commonCmdData.IntrospectBeforeError,
},
IntrospectOptions: introspectOptions,
ReportPath: buildReportPath,
ReportFormat: buildReportFormat,
}

usedNewBuildReportOption := (commonCmdData.SaveBuildReport != nil && *commonCmdData.SaveBuildReport == true) || (commonCmdData.BuildReportPath != nil && *commonCmdData.BuildReportPath != "")

usedOldBuildReportOption := (commonCmdData.DeprecatedReportPath != nil && *commonCmdData.DeprecatedReportPath != "") || (commonCmdData.DeprecatedReportFormat != nil && *commonCmdData.DeprecatedReportFormat != "")

if usedNewBuildReportOption && usedOldBuildReportOption {
return buildOptions, fmt.Errorf("you can't use deprecated options --report-path and --report-format along with new options --save-build-report, --build-report-path, use only the latter instead")
}

if usedNewBuildReportOption && GetSaveBuildReport(commonCmdData) {
buildOptions.ReportPath, buildOptions.ReportFormat, err = GetBuildReportPathAndFormat(commonCmdData)
if err != nil {
return buildOptions, fmt.Errorf("getting build report path failed: %w", err)
}
} else if usedOldBuildReportOption {
buildOptions.ReportFormat, err = GetDeprecatedReportFormat(ctx, commonCmdData)
if err != nil {
return buildOptions, fmt.Errorf("getting deprecated build report format failed: %w", err)
}

buildOptions.ReportPath = GetDeprecatedReportPath(ctx, commonCmdData)
}

return buildOptions, nil
Expand Down
17 changes: 14 additions & 3 deletions cmd/werf/converge/converge.go
Expand Up @@ -159,10 +159,12 @@ werf converge --repo registry.mydomain.com/web --env production`,
commonCmdData.SetupDisableDefaultSecretValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportFormat(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupBuildReportFormat(&commonCmdData, cmd)

common.SetupSaveDeployReport(&commonCmdData, cmd)
common.SetupDeployReportPath(&commonCmdData, cmd)

common.SetupUseCustomTag(&commonCmdData, cmd)
Expand Down Expand Up @@ -474,6 +476,15 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return err
}

var deployReportPath *string
if common.GetSaveDeployReport(&commonCmdData) {
if path, err := common.GetDeployReportPath(&commonCmdData); err != nil {
return fmt.Errorf("unable to get deploy report path: %w", err)
} else {
deployReportPath = &path
}
}

helmUpgradeCmd, _ := helm_v3.NewUpgradeCmd(actionConfig, logboek.OutStream(), helm_v3.UpgradeCmdOptions{
StagesSplitter: helm.NewStagesSplitter(),
StagesExternalDepsGenerator: helm.NewStagesExternalDepsGenerator(&actionConfig.RESTClientGetter, &namespace),
Expand All @@ -486,7 +497,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
Timeout: common.NewDuration(time.Duration(cmdData.Timeout) * time.Second),
IgnorePending: common.NewBool(true),
CleanupOnFail: common.NewBool(true),
DeployReportPath: commonCmdData.DeployReportPath,
DeployReportPath: deployReportPath,
})

return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/render/render.go
Expand Up @@ -137,10 +137,10 @@ func NewCmd(ctx context.Context) *cobra.Command {
commonCmdData.SetupDisableDefaultSecretValues(cmd)
commonCmdData.SetupSkipDependenciesRepoRefresh(cmd)

common.SetupSaveBuildReport(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportPath(&commonCmdData, cmd)
common.SetupDeprecatedReportFormat(&commonCmdData, cmd)
common.SetupBuildReportPath(&commonCmdData, cmd)
common.SetupBuildReportFormat(&commonCmdData, cmd)

common.SetupUseCustomTag(&commonCmdData, cmd)
common.SetupVirtualMerge(&commonCmdData, cmd)
Expand Down

0 comments on commit 40a8e81

Please sign in to comment.