Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pkg/app/pipectl/cmd/planpreview/planpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ func convert(results []*model.PlanPreviewCommandResult) ReadableResult {
out.FailureApplications = append(out.FailureApplications, FailureApplication{
ApplicationInfo: appInfo,
Reason: a.Error,
PlanDetails: string(a.PlanDetails),
})
continue
}
out.Applications = append(out.Applications, ApplicationResult{
ApplicationInfo: appInfo,
SyncStrategy: a.SyncStrategy.String(),
Changes: string(a.Changes),
PlanSummary: string(a.PlanSummary),
PlanDetails: string(a.PlanDetails),
})
}
}
Expand All @@ -222,7 +224,8 @@ type ReadableResult struct {
type ApplicationResult struct {
ApplicationInfo
SyncStrategy string // QUICK_SYNC, PIPELINE
Changes string
PlanSummary string
PlanDetails string
}

type FailurePiped struct {
Expand All @@ -232,7 +235,8 @@ type FailurePiped struct {

type FailureApplication struct {
ApplicationInfo
Reason string
Reason string
PlanDetails string
}

type PipedInfo struct {
Expand Down Expand Up @@ -267,7 +271,8 @@ func (r ReadableResult) String() string {
for i, app := range r.Applications {
fmt.Fprintf(&b, "\n%d. app: %s, env: %s, kind: %s\n", i+1, app.ApplicationName, app.EnvName, app.ApplicationKind)
fmt.Fprintf(&b, " sync strategy: %s\n", app.SyncStrategy)
fmt.Fprintf(&b, " changes: %s\n", app.Changes)
fmt.Fprintf(&b, " summary: %s\n", app.PlanSummary)
fmt.Fprintf(&b, " details:\n\n ---DETAILS_BEGIN---\n%s\n ---DETAILS_END---\n", app.PlanDetails)
}
}

Expand All @@ -280,6 +285,9 @@ func (r ReadableResult) String() string {
for i, app := range r.FailureApplications {
fmt.Fprintf(&b, "\n%d. app: %s, env: %s, kind: %s\n", i+1, app.ApplicationName, app.EnvName, app.ApplicationKind)
fmt.Fprintf(&b, " reason: %s\n", app.Reason)
if len(app.PlanDetails) > 0 {
fmt.Fprintf(&b, " details:\n\n ---DETAILS_BEGIN---\n%s\n ---DETAILS_END---\n", app.PlanDetails)
}
}
}

Expand Down
30 changes: 24 additions & 6 deletions pkg/app/pipectl/cmd/planpreview/planpreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ There are no applications to build plan-preview
ApplicationKind: model.ApplicationKind_KUBERNETES,
EnvName: "env-1",
SyncStrategy: model.SyncStrategy_QUICK_SYNC,
Changes: []byte("changes-1"),
PlanSummary: []byte("2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed"),
PlanDetails: []byte("changes-1"),
},
},
},
Expand All @@ -61,7 +62,12 @@ Here are plan-preview for 1 application:

1. app: app-1, env: env-1, kind: KUBERNETES
sync strategy: QUICK_SYNC
changes: changes-1
summary: 2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed
details:

---DETAILS_BEGIN---
changes-1
---DETAILS_END---
`,
},
{
Expand Down Expand Up @@ -128,7 +134,8 @@ NOTE: An error occurred while building plan-preview for applications of the foll
ApplicationKind: model.ApplicationKind_KUBERNETES,
EnvName: "env-1",
SyncStrategy: model.SyncStrategy_QUICK_SYNC,
Changes: []byte("changes-1"),
PlanSummary: []byte("2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed"),
PlanDetails: []byte("changes-1"),
},
{
ApplicationId: "app-2",
Expand All @@ -137,7 +144,8 @@ NOTE: An error occurred while building plan-preview for applications of the foll
ApplicationKind: model.ApplicationKind_TERRAFORM,
EnvName: "env-2",
SyncStrategy: model.SyncStrategy_PIPELINE,
Changes: []byte("changes-2"),
PlanSummary: []byte("1 to add, 2 to change, 0 to destroy"),
PlanDetails: []byte("changes-2"),
},
{
ApplicationId: "app-3",
Expand Down Expand Up @@ -169,11 +177,21 @@ Here are plan-preview for 2 applications:

1. app: app-1, env: env-1, kind: KUBERNETES
sync strategy: QUICK_SYNC
changes: changes-1
summary: 2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed
details:

---DETAILS_BEGIN---
changes-1
---DETAILS_END---

2. app: app-2, env: env-2, kind: TERRAFORM
sync strategy: PIPELINE
changes: changes-2
summary: 1 to add, 2 to change, 0 to destroy
details:

---DETAILS_BEGIN---
changes-2
---DETAILS_END---

NOTE: An error occurred while building plan-preview for the following 2 applications:

Expand Down
3 changes: 3 additions & 0 deletions pkg/app/piped/cmd/piped/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,13 @@ func (p *piped) run(ctx context.Context, t cli.Telemetry) (runErr error) {
if p.enablePlanPreview {
h := planpreview.NewHandler(
gitClient,
apiClient,
commandLister,
applicationLister,
environmentStore,
lastTriggeredCommitGetter,
decrypter,
appManifestsCache,
cfg,
planpreview.WithLogger(t.Logger),
)
Expand Down
5 changes: 4 additions & 1 deletion pkg/app/piped/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ func (p *planner) Run(ctx context.Context) error {
}

in := pln.Input{
Deployment: p.deployment,
ApplicationID: p.deployment.ApplicationId,
ApplicationName: p.deployment.ApplicationName,
GitPath: *p.deployment.GitPath,
Trigger: *p.deployment.Trigger,
MostRecentSuccessfulCommitHash: p.lastSuccessfulCommitHash,
PipedConfig: p.pipedConfig,
AppManifestsCache: p.appManifestsCache,
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/planner/cloudrun/cloudrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/planner/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
Expand Down
20 changes: 10 additions & 10 deletions pkg/app/piped/planner/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu
}

manifestCache := provider.AppManifestsCache{
AppID: in.Deployment.ApplicationId,
AppID: in.ApplicationID,
Cache: in.AppManifestsCache,
Logger: in.Logger,
}

// Load previous deployed manifests and new manifests to compare.
newManifests, ok := manifestCache.Get(in.Deployment.Trigger.Commit.Hash)
newManifests, ok := manifestCache.Get(in.Trigger.Commit.Hash)
if !ok {
// When the manifests were not in the cache we have to load them.
loader := provider.NewManifestLoader(in.Deployment.ApplicationName, ds.AppDir, ds.RepoDir, in.Deployment.GitPath.ConfigFilename, cfg.Input, in.Logger)
loader := provider.NewManifestLoader(in.ApplicationName, ds.AppDir, ds.RepoDir, in.GitPath.ConfigFilename, cfg.Input, in.Logger)
newManifests, err = loader.LoadManifests(ctx)
if err != nil {
return
}
manifestCache.Put(in.Deployment.Trigger.Commit.Hash, newManifests)
manifestCache.Put(in.Trigger.Commit.Hash, newManifests)
}

// Determine application version from the manifests.
Expand All @@ -99,7 +99,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
Expand Down Expand Up @@ -127,13 +127,13 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// This deployment is triggered by a commit with the intent to perform pipeline.
// Commit Matcher will be ignored when triggered by a command.
if p := cfg.CommitMatcher.Pipeline; p != "" && in.Deployment.Trigger.Commander == "" {
if p := cfg.CommitMatcher.Pipeline; p != "" && in.Trigger.Commander == "" {
pipelineRegex, err := in.RegexPool.Get(p)
if err != nil {
err = fmt.Errorf("failed to compile commitMatcher.pipeline(%s): %w", p, err)
return out, err
}
if pipelineRegex.MatchString(in.Deployment.Trigger.Commit.Message) {
if pipelineRegex.MatchString(in.Trigger.Commit.Message) {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Sync progressively because the commit message was matching %q", p)
Expand All @@ -143,13 +143,13 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// This deployment is triggered by a commit with the intent to synchronize.
// Commit Matcher will be ignored when triggered by a command.
if s := cfg.CommitMatcher.QuickSync; s != "" && in.Deployment.Trigger.Commander == "" {
if s := cfg.CommitMatcher.QuickSync; s != "" && in.Trigger.Commander == "" {
syncRegex, err := in.RegexPool.Get(s)
if err != nil {
err = fmt.Errorf("failed to compile commitMatcher.sync(%s): %w", s, err)
return out, err
}
if syncRegex.MatchString(in.Deployment.Trigger.Commit.Message) {
if syncRegex.MatchString(in.Trigger.Commit.Message) {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync by applying all manifests because the commit message was matching %q", s)
Expand Down Expand Up @@ -178,7 +178,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu
return
}

loader := provider.NewManifestLoader(in.Deployment.ApplicationName, runningDs.AppDir, runningDs.RepoDir, in.Deployment.GitPath.ConfigFilename, cfg.Input, in.Logger)
loader := provider.NewManifestLoader(in.ApplicationName, runningDs.AppDir, runningDs.RepoDir, in.GitPath.ConfigFilename, cfg.Input, in.Logger)
oldManifests, err = loader.LoadManifests(ctx)
if err != nil {
err = fmt.Errorf("failed to load previously deployed manifests: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/planner/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
Expand Down
6 changes: 4 additions & 2 deletions pkg/app/piped/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ type Planner interface {
}

type Input struct {
// Readonly deployment model.
Deployment *model.Deployment
ApplicationID string
ApplicationName string
GitPath model.ApplicationGitPath
Trigger model.DeploymentTrigger
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing only needed data to make it can be reused by planpreview package where there is no deployment data.

MostRecentSuccessfulCommitHash string
PipedConfig *config.PipedSpec
TargetDSP deploysource.Provider
Expand Down
6 changes: 5 additions & 1 deletion pkg/app/piped/planner/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu

// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = "Quick sync by automatically applying any detected changes because no pipeline was configured (forced via web)"
return
Expand All @@ -63,6 +64,7 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu
err = fmt.Errorf("unable to force sync with pipeline because no pipeline was specified")
return
}
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = "Sync with the specified progressive pipeline (forced via web)"
return
Expand All @@ -72,11 +74,13 @@ func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Outpu
out.Version = "N/A"

if cfg.Pipeline == nil || len(cfg.Pipeline.Stages) == 0 {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, now)
out.Summary = "Quick sync by automatically applying any detected changes because no pipeline was configured"
return
}

out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, now)
out.Summary = "Sync with the specified progressive pipeline"
return
Expand Down
15 changes: 15 additions & 0 deletions pkg/app/piped/planpreview/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ go_library(
srcs = [
"builder.go",
"handler.go",
"kubernetesdiff.go",
"terraformdiff.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/planpreview",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/api/service/pipedservice:go_default_library",
"//pkg/app/piped/cloudprovider/kubernetes:go_default_library",
"//pkg/app/piped/cloudprovider/terraform:go_default_library",
"//pkg/app/piped/deploysource:go_default_library",
"//pkg/app/piped/planner:go_default_library",
"//pkg/app/piped/planner/registry:go_default_library",
"//pkg/app/piped/toolregistry:go_default_library",
"//pkg/app/piped/trigger:go_default_library",
"//pkg/cache:go_default_library",
"//pkg/config:go_default_library",
"//pkg/diff:go_default_library",
"//pkg/git:go_default_library",
"//pkg/model:go_default_library",
"//pkg/regexpool:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
"@org_uber_go_zap//:go_default_library",
],
)
Expand Down
Loading