-
Notifications
You must be signed in to change notification settings - Fork 155
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
Implement rollback for script run as pre defined stage #4743
Changes from all commits
be09a10
48be28e
0d0ecee
53401db
e1c150e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -369,34 +369,37 @@ func (s *scheduler) Run(ctx context.Context) error { | |
// we start rollback stage if the auto-rollback option is true. | ||
if deploymentStatus == model.DeploymentStatus_DEPLOYMENT_CANCELLED || | ||
deploymentStatus == model.DeploymentStatus_DEPLOYMENT_FAILURE { | ||
if stage, ok := s.deployment.FindRollbackStage(); ok { | ||
|
||
if rollbackStages, ok := s.deployment.FindRollbackStages(); ok { | ||
// Update to change deployment status to ROLLING_BACK. | ||
if err := s.reportDeploymentStatusChanged(ctx, model.DeploymentStatus_DEPLOYMENT_ROLLING_BACK, statusReason); err != nil { | ||
return err | ||
} | ||
|
||
// Start running rollback stage. | ||
var ( | ||
sig, handler = executor.NewStopSignal() | ||
doneCh = make(chan struct{}) | ||
) | ||
go func() { | ||
rbs := *stage | ||
rbs.Requires = []string{lastStage.Id} | ||
s.executeStage(sig, rbs, func(in executor.Input) (executor.Executor, bool) { | ||
return s.executorRegistry.RollbackExecutor(s.deployment.Kind, in) | ||
}) | ||
close(doneCh) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
handler.Terminate() | ||
<-doneCh | ||
return nil | ||
|
||
case <-doneCh: | ||
break | ||
for _, stage := range rollbackStages { | ||
// Start running rollback stage. | ||
var ( | ||
sig, handler = executor.NewStopSignal() | ||
doneCh = make(chan struct{}) | ||
) | ||
go func() { | ||
rbs := *stage | ||
rbs.Requires = []string{lastStage.Id} | ||
s.executeStage(sig, rbs, func(in executor.Input) (executor.Executor, bool) { | ||
return s.executorRegistry.RollbackExecutor(s.deployment.Kind, in) | ||
}) | ||
close(doneCh) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
handler.Terminate() | ||
<-doneCh | ||
return nil | ||
|
||
case <-doneCh: | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -433,6 +436,24 @@ func (s *scheduler) executeStage(sig executor.StopSignal, ps model.PipelineStage | |
lp.Complete(time.Minute) | ||
}() | ||
|
||
// Check whether to execute the script rollback stage or not. | ||
// If the base stage is executed, the script rollback stage will be executed. | ||
if ps.Name == model.StageScriptRunRollback.String() { | ||
baseStageID := ps.Metadata["baseStageID"] | ||
if baseStageID == "" { | ||
return | ||
} | ||
|
||
baseStageStatus, ok := s.stageStatuses[baseStageID] | ||
if !ok { | ||
return | ||
} | ||
|
||
if baseStageStatus == model.StageStatus_STAGE_NOT_STARTED_YET || baseStageStatus == model.StageStatus_STAGE_SKIPPED { | ||
return | ||
} | ||
} | ||
Comment on lines
+441
to
+455
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Check the status of the script run stage to decide whether to rollback or not. |
||
|
||
// Update stage status to RUNNING if needed. | ||
if model.CanUpdateStageStatus(ps.Status, model.StageStatus_STAGE_RUNNING) { | ||
if err := s.reportStageStatus(ctx, ps.Id, model.StageStatus_STAGE_RUNNING, ps.Requires); err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"go.uber.org/zap" | ||
|
@@ -27,6 +30,8 @@ | |
|
||
type rollbackExecutor struct { | ||
executor.Input | ||
|
||
appDir string | ||
} | ||
|
||
func (e *rollbackExecutor) Execute(sig executor.StopSignal) model.StageStatus { | ||
|
@@ -39,7 +44,8 @@ | |
switch model.Stage(e.Stage.Name) { | ||
case model.StageRollback: | ||
status = e.ensureRollback(ctx) | ||
|
||
case model.StageScriptRunRollback: | ||
status = e.ensureScriptRunRollback(ctx) | ||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Added the logic to rollback executor for each application (firstly, I added on the k8s's one) because rollback stages are executed per the kind of application not per stage. I don't have any idea to separate the rollback for application and the rollback for the stage. |
||
default: | ||
e.LogPersister.Errorf("Unsupported stage %s for kubernetes application", e.Stage.Name) | ||
return model.StageStatus_STAGE_FAILURE | ||
|
@@ -74,6 +80,8 @@ | |
} | ||
} | ||
|
||
e.appDir = ds.AppDir | ||
|
||
loader := provider.NewLoader(e.Deployment.ApplicationName, ds.AppDir, ds.RepoDir, e.Deployment.GitPath.ConfigFilename, appCfg.Input, e.GitClient, e.Logger) | ||
e.Logger.Info("start executing kubernetes stage", | ||
zap.String("stage-name", e.Stage.Name), | ||
|
@@ -171,3 +179,45 @@ | |
} | ||
return model.StageStatus_STAGE_SUCCESS | ||
} | ||
|
||
func (e *rollbackExecutor) ensureScriptRunRollback(ctx context.Context) model.StageStatus { | ||
e.LogPersister.Info("Runnnig commands for rollback...") | ||
|
||
onRollback, ok := e.Stage.Metadata["onRollback"] | ||
if !ok { | ||
e.LogPersister.Error("onRollback metadata is missing") | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
|
||
if onRollback == "" { | ||
e.LogPersister.Info("No commands to run") | ||
return model.StageStatus_STAGE_SUCCESS | ||
} | ||
Comment on lines
+192
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to ensure I understand this right: This means that if there is no user-defined There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ffjlabo What I mean here is that we may make this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the second thought, I got your idea.
|
||
|
||
envStr, ok := e.Stage.Metadata["env"] | ||
env := make(map[string]string, 0) | ||
if ok { | ||
_ = json.Unmarshal([]byte(envStr), &env) | ||
} | ||
|
||
for _, v := range strings.Split(onRollback, "\n") { | ||
if v != "" { | ||
e.LogPersister.Infof(" %s", v) | ||
} | ||
} | ||
|
||
envs := make([]string, 0, len(env)) | ||
for key, value := range env { | ||
envs = append(envs, key+"="+value) | ||
} | ||
|
||
cmd := exec.Command("/bin/sh", "-l", "-c", onRollback) | ||
cmd.Dir = e.appDir | ||
cmd.Env = append(os.Environ(), envs...) | ||
cmd.Stdout = e.LogPersister | ||
cmd.Stderr = e.LogPersister | ||
if err := cmd.Run(); err != nil { | ||
return model.StageStatus_STAGE_FAILURE | ||
} | ||
return model.StageStatus_STAGE_SUCCESS | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package kubernetes | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
|
@@ -114,6 +115,31 @@ | |
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}) | ||
|
||
// Add a stage for rolling back script run stages. | ||
for i, s := range pp.Stages { | ||
if s.Name == model.StageScriptRun { | ||
// Use metadata as a way to pass parameters to the stage. | ||
envStr, _ := json.Marshal(s.ScriptRunStageOptions.Env) | ||
metadata := map[string]string{ | ||
"baseStageID": out[i].Id, | ||
"onRollback": s.ScriptRunStageOptions.OnRollback, | ||
"env": string(envStr), | ||
} | ||
Comment on lines
+121
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 I use metadata for the stage to recognize below on executing it.
|
||
ss, _ := planner.GetPredefinedStage(planner.PredefinedStageScriptRunRollback) | ||
out = append(out, &model.PipelineStage{ | ||
Id: ss.ID, | ||
Name: ss.Name.String(), | ||
Desc: ss.Desc, | ||
Predefined: true, | ||
Visible: false, | ||
Status: model.StageStatus_STAGE_NOT_STARTED_YET, | ||
Metadata: metadata, | ||
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return out | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Changed to execute multiple kinds of rollback stages.
The changes are to do logic just like the same as before with each rollback stage.