Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle skipped stage by piped #3525

Merged
merged 3 commits into from
Apr 15, 2022
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
2 changes: 1 addition & 1 deletion pkg/app/piped/apistore/commandstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *store) sync(ctx context.Context) error {
applicationCommands = append(applicationCommands, s.makeReportableCommand(cmd))
case model.Command_CANCEL_DEPLOYMENT:
deploymentCommands = append(deploymentCommands, s.makeReportableCommand(cmd))
case model.Command_APPROVE_STAGE:
case model.Command_APPROVE_STAGE, model.Command_SKIP_STAGE:
stageCommands = append(stageCommands, s.makeReportableCommand(cmd))
case model.Command_BUILD_PLAN_PREVIEW:
planPreviewCommands = append(planPreviewCommands, s.makeReportableCommand(cmd))
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ func (s *scheduler) Run(ctx context.Context) error {
break
}

// If all operations of the stage were completed successfully
// If all operations of the stage were completed successfully or skipped by a web user
// handle the next stage.
if result == model.StageStatus_STAGE_SUCCESS {
if result == model.StageStatus_STAGE_SUCCESS || result == model.StageStatus_STAGE_SKIPPED {
continue
}

Expand Down
57 changes: 56 additions & 1 deletion pkg/app/piped/executor/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
"github.com/pipe-cd/pipecd/pkg/model"
)

const (
skippedByKey = "SkippedBy"
)

type Executor struct {
executor.Input

Expand Down Expand Up @@ -96,6 +100,31 @@ func (e *Executor) Execute(sig executor.StopSignal) model.StageStatus {

eg, ctxWithTimeout := errgroup.WithContext(ctxWithTimeout)

// Sync the skip command.
var (
status = model.StageStatus_STAGE_SUCCESS
doneCh = make(chan struct{})
)
defer close(doneCh)
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
Copy link
Member Author

Choose a reason for hiding this comment

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

If all stages of the analysis have already been done, this should be stopped gracefully too.

for {
select {
case <-ticker.C:
if !e.checkSkipped(ctx) {
continue
}
status = model.StageStatus_STAGE_SKIPPED
// Stop the context to cancel all running analysises.
ctxWithTimeout.Done()
Copy link
Member Author

Choose a reason for hiding this comment

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

If this stage has already been skipped, all of the analysis should be stopped gracefully.
Thus, call ctxWithTimeout.Done() in order to stop all stages of the analysis.

knanao marked this conversation as resolved.
Show resolved Hide resolved
return
case <-doneCh:
return
}
}
}()

// Run analyses with metrics providers.
for i := range options.Metrics {
cfg, err := e.getMetricsConfig(options.Metrics[i], templateCfg)
Expand Down Expand Up @@ -146,7 +175,7 @@ func (e *Executor) Execute(sig executor.StopSignal) model.StageStatus {
return model.StageStatus_STAGE_FAILURE
}

status := executor.DetermineStageStatus(sig.Signal(), e.Stage.Status, model.StageStatus_STAGE_SUCCESS)
status = executor.DetermineStageStatus(sig.Signal(), e.Stage.Status, status)
if status != model.StageStatus_STAGE_SUCCESS {
return status
}
Expand Down Expand Up @@ -307,3 +336,29 @@ func (e *Executor) buildAppArgs(customArgs map[string]string) argsTemplate {
args.K8s.Namespace = namespace
return args
}

func (e *Executor) checkSkipped(ctx context.Context) bool {
var skipCmd *model.ReportableCommand
commands := e.CommandLister.ListCommands()

for i, cmd := range commands {
if cmd.GetSkipStage() != nil {
skipCmd = &commands[i]
break
}
}
if skipCmd == nil {
return false
}

if err := e.MetadataStore.Stage(e.Stage.Id).Put(ctx, skippedByKey, skipCmd.Commander); err != nil {
e.LogPersister.Errorf("Unable to save the commander who skipped the stage information to deployment, %v", err)
}
e.LogPersister.Infof("Got the skip command from %q", skipCmd.Commander)
e.LogPersister.Infof("This stage has been skipped by user (%s)", skipCmd.Commander)

if err := skipCmd.Report(ctx, model.CommandStatus_COMMAND_SUCCEEDED, nil, nil); err != nil {
e.Logger.Error("failed to report handled command", zap.Error(err))
}
return true
}