Skip to content

Commit

Permalink
Add test for pipedv1's buildPlan method, and remove platform specific…
Browse files Browse the repository at this point in the history
… config from configv1's GenericApplicationConfig (#5238)

* Add test for buildPlan

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Fix test input

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Remove platform-specific fields from GenericApplicationSpec

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Remove broken k8s-specific test from configv1

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Fix the test for pipedv1 buildPlan

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Remove mistakenly commited changes

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Remove stage-specific tests that are broken

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Move parsing GenericApplicationSpec to configv1

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Add KindApplication and remove ParseApplication

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Oct 1, 2024
1 parent 552173b commit 44cfc93
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 1,108 deletions.
83 changes: 45 additions & 38 deletions pkg/app/pipedv1/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"path/filepath"
"sort"
"time"

Expand All @@ -30,10 +28,9 @@ import (
"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/controller/controllermetrics"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/deploysource"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/metadatastore"
"github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/model"
pluginapi "github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1"
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
Expand Down Expand Up @@ -192,27 +189,30 @@ func (p *planner) Run(ctx context.Context) error {
controllermetrics.UpdateDeploymentStatus(p.deployment, p.doneDeploymentStatus)
}()

repoCfg := config.PipedRepository{
RepoID: p.deployment.GitPath.Repo.Id,
Remote: p.deployment.GitPath.Repo.Remote,
Branch: p.deployment.GitPath.Repo.Branch,
}
// TODO: Prepare running deploy source and target deploy source.
var runningDS, targetDS *model.DeploymentSource

// Prepare target deploy source.
targetDSP := deploysource.NewProvider(
filepath.Join(p.workingDir, "deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "target", p.deployment.Trigger.Commit.Hash),
*p.deployment.GitPath,
nil, // TODO: Revise this secret decryter, is this need?
)
// repoCfg := config.PipedRepository{
// RepoID: p.deployment.GitPath.Repo.Id,
// Remote: p.deployment.GitPath.Repo.Remote,
// Branch: p.deployment.GitPath.Repo.Branch,
// }

targetDS, err := targetDSP.Get(ctx, io.Discard)
if err != nil {
return fmt.Errorf("error while preparing deploy source data (%v)", err)
}
// Prepare target deploy source.
// targetDSP := deploysource.NewProvider(
// filepath.Join(p.workingDir, "deploysource"),
// deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "target", p.deployment.Trigger.Commit.Hash),
// *p.deployment.GitPath,
// nil, // TODO: Revise this secret decryter, is this need?
// )

// targetDS, err := targetDSP.Get(ctx, io.Discard)
// if err != nil {
// return fmt.Errorf("error while preparing deploy source data (%v)", err)
// }

// TODO: Pass running DS as well if need?
out, err := p.buildPlan(ctx, targetDS)
out, err := p.buildPlan(ctx, runningDS, targetDS)

// If the deployment was already cancelled, we ignore the plan result.
select {
Expand Down Expand Up @@ -243,13 +243,15 @@ func (p *planner) Run(ctx context.Context) error {
// - CommitMatcher ensure pipeline/quick sync based on the commit message
// - Force quick sync if there is no previous deployment (aka. this is the first deploy)
// - Based on PlannerService.DetermineStrategy returned by plugins
func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySource) (*plannerOutput, error) {
func (p *planner) buildPlan(ctx context.Context, runningDS, targetDS *model.DeploymentSource) (*plannerOutput, error) {
out := &plannerOutput{}

input := &deployment.PlanPluginInput{
Deployment: p.deployment,
Deployment: p.deployment,
RunningDeploymentSource: runningDS,
TargetDeploymentSource: targetDS,
// TODO: Add more planner input fields.
// NOTE: As discussed we pass targetDS & runningDS here.
// we need passing PluginConfig
}

// Build deployment target versions.
Expand All @@ -270,20 +272,25 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo
}
}

cfg := targetDS.GenericApplicationConfig
cfg, err := config.DecodeYAML(targetDS.GetApplicationConfig())
if err != nil {
p.logger.Error("unable to parse application config", zap.Error(err))
return nil, err
}
spec := cfg.ApplicationSpec

// In case the strategy has been decided by trigger.
// For example: user triggered the deployment via web console.
switch p.deployment.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
if stages, err := p.buildQuickSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildQuickSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Summary = p.deployment.Trigger.StrategySummary
out.Stages = stages
return out, nil
}
case model.SyncStrategy_PIPELINE:
if stages, err := p.buildPipelineSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildPipelineSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Summary = p.deployment.Trigger.StrategySummary
out.Stages = stages
Expand All @@ -292,8 +299,8 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo
}

// When no pipeline was configured, do the quick sync.
if cfg.Pipeline == nil || len(cfg.Pipeline.Stages) == 0 {
if stages, err := p.buildQuickSyncStages(ctx, cfg); err == nil {
if spec.Pipeline == nil || len(spec.Pipeline.Stages) == 0 {
if stages, err := p.buildQuickSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Summary = "Quick sync due to the pipeline was not configured"
out.Stages = stages
Expand All @@ -302,8 +309,8 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo
}

// Force to use pipeline when the `spec.planner.alwaysUsePipeline` was configured.
if cfg.Planner.AlwaysUsePipeline {
if stages, err := p.buildPipelineSyncStages(ctx, cfg); err == nil {
if spec.Planner.AlwaysUsePipeline {
if stages, err := p.buildPipelineSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Summary = "Sync with the specified pipeline (alwaysUsePipeline was set)"
out.Stages = stages
Expand All @@ -315,10 +322,10 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo

// This deployment is triggered by a commit with the intent to perform pipeline.
// Commit Matcher will be ignored when triggered by a command.
if pattern := cfg.CommitMatcher.Pipeline; pattern != "" && p.deployment.Trigger.Commander == "" {
if pattern := spec.CommitMatcher.Pipeline; pattern != "" && p.deployment.Trigger.Commander == "" {
if pipelineRegex, err := regexPool.Get(pattern); err == nil &&
pipelineRegex.MatchString(p.deployment.Trigger.Commit.Message) {
if stages, err := p.buildPipelineSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildPipelineSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Summary = fmt.Sprintf("Sync progressively because the commit message was matching %q", pattern)
out.Stages = stages
Expand All @@ -329,10 +336,10 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo

// This deployment is triggered by a commit with the intent to synchronize.
// Commit Matcher will be ignored when triggered by a command.
if pattern := cfg.CommitMatcher.QuickSync; pattern != "" && p.deployment.Trigger.Commander == "" {
if pattern := spec.CommitMatcher.QuickSync; pattern != "" && p.deployment.Trigger.Commander == "" {
if syncRegex, err := regexPool.Get(pattern); err == nil &&
syncRegex.MatchString(p.deployment.Trigger.Commit.Message) {
if stages, err := p.buildQuickSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildQuickSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Summary = fmt.Sprintf("Quick sync because the commit message was matching %q", pattern)
out.Stages = stages
Expand All @@ -343,7 +350,7 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo

// Quick sync if this is the first time to deploy this application or it was unable to retrieve running commit hash.
if p.lastSuccessfulCommitHash == "" {
if stages, err := p.buildQuickSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildQuickSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Summary = "Quick sync, it seems this is the first deployment of the application"
out.Stages = stages
Expand Down Expand Up @@ -372,14 +379,14 @@ func (p *planner) buildPlan(ctx context.Context, targetDS *deploysource.DeploySo

switch strategy {
case model.SyncStrategy_QUICK_SYNC:
if stages, err := p.buildQuickSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildQuickSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Summary = summary
out.Stages = stages
return out, nil
}
case model.SyncStrategy_PIPELINE:
if stages, err := p.buildPipelineSyncStages(ctx, cfg); err == nil {
if stages, err := p.buildPipelineSyncStages(ctx, spec); err == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Summary = summary
out.Stages = stages
Expand Down
Loading

0 comments on commit 44cfc93

Please sign in to comment.