Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(nelm): --exit-code flag for werf plan
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Jan 18, 2024
1 parent 89b0a36 commit a9dcf1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/werf/main.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand All @@ -12,6 +13,7 @@ import (
helm_v3 "helm.sh/helm/v3/cmd/helm"

"github.com/werf/logboek"
"github.com/werf/nelm/pkg/resrcchangcalc"
"github.com/werf/werf/cmd/werf/build"
bundle_apply "github.com/werf/werf/cmd/werf/bundle/apply"
bundle_copy "github.com/werf/werf/cmd/werf/bundle/copy"
Expand Down Expand Up @@ -87,6 +89,9 @@ func main() {
if helm_v3.IsPluginError(err) {
common.ShutdownTelemetry(ctx, helm_v3.PluginErrorCode(err))
common.TerminateWithError(err.Error(), helm_v3.PluginErrorCode(err))
} else if errors.Is(err, resrcchangcalc.ErrChangesPlanned) {
common.ShutdownTelemetry(ctx, 2)
os.Exit(2)
} else {
common.ShutdownTelemetry(ctx, 1)
common.TerminateWithError(err.Error(), 1)
Expand Down
10 changes: 8 additions & 2 deletions cmd/werf/plan/plan.go
Expand Up @@ -56,7 +56,8 @@ import (
)

var cmdData struct {
Timeout int
Timeout int
DetailedExitCode bool
}

var commonCmdData common.CmdData
Expand Down Expand Up @@ -201,6 +202,7 @@ werf plan --repo registry.mydomain.com/web --env production`,
defaultTimeout = new(int64)
}
cmd.Flags().IntVarP(&cmdData.Timeout, "timeout", "t", int(*defaultTimeout), "Resources tracking timeout in seconds ($WERF_TIMEOUT by default)")
cmd.Flags().BoolVarP(&cmdData.DetailedExitCode, "exit-code", "", util.GetBoolEnvironmentDefaultFalse("WERF_EXIT_CODE"), "If true, returns exit code 0 if no changes, exit code 2 if any changes planned or exit code 1 in case of an error (default $WERF_EXIT_CODE or false)")

return cmd
}
Expand Down Expand Up @@ -627,7 +629,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}

log.Default.Info(ctx, "Calculating planned changes")
createdChanges, recreatedChanges, updatedChanges, appliedChanges, deletedChanges := resrcchangcalc.CalculatePlannedChanges(
createdChanges, recreatedChanges, updatedChanges, appliedChanges, deletedChanges, anyChangesPlanned := resrcchangcalc.CalculatePlannedChanges(
resProcessor.DeployableReleaseNamespaceInfo(),
resProcessor.DeployableStandaloneCRDsInfos(),
resProcessor.DeployableHookResourcesInfos(),
Expand All @@ -647,6 +649,10 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
deletedChanges,
)

if cmdData.DetailedExitCode && anyChangesPlanned {
return resrcchangcalc.ErrChangesPlanned
}

return nil
})
}
Expand Down

0 comments on commit a9dcf1b

Please sign in to comment.