Skip to content

Commit

Permalink
Fix: fix rollback sync and terminate case (#781)
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <[email protected]>
  • Loading branch information
FogDong authored Apr 19, 2023
1 parent cff8c89 commit eca8d1d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ require (
sigs.k8s.io/yaml v1.3.0
)

require github.com/oam-dev/kubevela v1.8.0-rc.1.0.20230414094557-fcd721ffed60
require github.com/oam-dev/kubevela v1.8.1-0.20230418085840-007901f9f1d5

require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,8 @@ github.com/oam-dev/cluster-gateway v1.7.0-alpha.1 h1:51p4y2rzO2gLRZiCjY/Sh7O5ibv
github.com/oam-dev/cluster-gateway v1.7.0-alpha.1/go.mod h1:wsob8xoOCEX39QWhDAAB/tOcCBjLGVf9w8ZvVPYAKDE=
github.com/oam-dev/cluster-register v1.0.4-0.20220928064144-5f76a9d7ca8c h1:RE/33gzs9hoyegcTaUXDG/hayffJ197ljMUGNP6I/5A=
github.com/oam-dev/cluster-register v1.0.4-0.20220928064144-5f76a9d7ca8c/go.mod h1:nKEUMfuEB8pHKsaSah9IA+UQzezrPYebBdRozyNtlZc=
github.com/oam-dev/kubevela v1.8.0-rc.1.0.20230414094557-fcd721ffed60 h1:0HcvfgV5ae08n2RDdZbcMoerasdDjbuRU+2nKqspVV0=
github.com/oam-dev/kubevela v1.8.0-rc.1.0.20230414094557-fcd721ffed60/go.mod h1:vrEm8xO7b5yAfyB8GOuSKPWimFelQUcRsAdPTwnqY34=
github.com/oam-dev/kubevela v1.8.1-0.20230418085840-007901f9f1d5 h1:CbNywf1TUf8V6IkpPYMyEqDTIejwAaW9tAxOOANzuUc=
github.com/oam-dev/kubevela v1.8.1-0.20230418085840-007901f9f1d5/go.mod h1:vrEm8xO7b5yAfyB8GOuSKPWimFelQUcRsAdPTwnqY34=
github.com/oam-dev/stern v1.13.2 h1:jlGgtJbKmIVhzkH44ft5plkgs8XEfvxbFrQdX60CQR4=
github.com/oam-dev/stern v1.13.2/go.mod h1:0pLjZt0amXE/ErF16Rdrgd98H2owN8Hmn3/7CX5+AeA=
github.com/oam-dev/terraform-config-inspect v0.0.0-20210418082552-fc72d929aa28 h1:tD8HiFKnt0jnwdTWjeqUnfnUYLD/+Nsmj8ZGIxqDWiU=
Expand Down
13 changes: 12 additions & 1 deletion pkg/server/domain/service/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -1759,22 +1759,29 @@ func (c *applicationServiceImpl) resetApp(ctx context.Context, targetApp *v1beta
}

func (c *applicationServiceImpl) RollbackWithRevision(ctx context.Context, application *model.Application, revisionVersion string) (*apisv1.ApplicationRollbackResponse, error) {
revision, err := c.DetailRevision(ctx, application.Name, revisionVersion)
revisionDetail, err := c.DetailRevision(ctx, application.Name, revisionVersion)
if err != nil {
return nil, err
}
revision := revisionDetail.ApplicationRevision
appCR, err := c.GetApplicationCRInEnv(ctx, application, revision.EnvName)
if err != nil {
return nil, err
}
var publishVersion = utils.GenerateVersion(revision.WorkflowName)
noRevision := false
revisionCRName := revision.RevisionCRName
var rollbackApplication *v1beta1.Application
if appCR != nil {
// The RevisionCRName is incorrect in the old version, ignore it.
if revision.RevisionCRName == revision.Version || revision.RevisionCRName == "" {
noRevision = true
} else {
// clear the revisionCR name
revision.RevisionCRName = ""
if err := c.Store.Put(ctx, &revision); err != nil {
return nil, err
}
_, appCR, err := app.RollbackApplicationWithRevision(context.WithValue(ctx, &app.RevisionContextKey, utils.WithProject(ctx, "")), c.KubeClient, appCR.Name, appCR.Namespace, revision.RevisionCRName, publishVersion)
if err != nil {
switch {
Expand All @@ -1783,6 +1790,10 @@ func (c *applicationServiceImpl) RollbackWithRevision(ctx context.Context, appli
case errors.Is(err, app.ErrRevisionNotChange):
return nil, bcode.ErrApplicationRevisionConflict
default:
revision.RevisionCRName = revisionCRName
if err := c.Store.Put(ctx, &revision); err != nil {
return nil, err
}
return nil, err
}
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/server/domain/service/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ func (w *workflowServiceImpl) DetailWorkflowRecord(ctx context.Context, workflow

// nolint:gocyclo
func (w *workflowServiceImpl) SyncWorkflowRecord(ctx context.Context, appPrimaryKey, recordName string, app *v1beta1.Application, workflowContext map[string]string) error {
// get workflow record
r := &model.WorkflowRecord{
Name: recordName,
AppPrimaryKey: appPrimaryKey,
}
if err := w.Store.Get(ctx, r); err != nil {
return err
}
records := []*model.WorkflowRecord{r}
// if the workflow is restarted, sync the old unfinished workflow record
unfinished := &model.WorkflowRecord{
Finished: "false",
Expand All @@ -422,6 +431,11 @@ func (w *workflowServiceImpl) SyncWorkflowRecord(ctx context.Context, appPrimary
}
for _, item := range unfinishedRecords {
record := item.(*model.WorkflowRecord)
if record.Name != recordName {
records = append(records, record)
}
}
for _, record := range records {
revision := &model.ApplicationRevision{AppPrimaryKey: appPrimaryKey, Version: record.RevisionPrimaryKey}
if err := w.Store.Get(ctx, revision); err != nil {
if errors.Is(err, datastore.ErrRecordNotExist) {
Expand All @@ -442,7 +456,7 @@ func (w *workflowServiceImpl) SyncWorkflowRecord(ctx context.Context, appPrimary
}
continue
}
if revision.RevisionCRName != "" {
if revision.RevisionCRName != "" && record.CreateTime.Before(r.CreateTime) {
// sync from application revision
if err := w.syncRecordFromApplicationRevision(ctx, record, revision); err != nil {
klog.Errorf("failed to sync workflow record %s from application revision %s", record.Name, err.Error())
Expand Down Expand Up @@ -520,7 +534,7 @@ func (w *workflowServiceImpl) syncRecordFromApplicationStatus(ctx context.Contex
}

revision.Status = generateRevisionStatus(status.Phase)
if app.Status.LatestRevision != nil {
if app.Status.LatestRevision != nil && revision.RevisionCRName == "" {
revision.RevisionCRName = app.Status.LatestRevision.Name
}
if err := w.Store.Put(ctx, revision); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/event/sync/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ func FromCRWorkflowRecord(app *v1beta1.Application, workflow model.Workflow, rev
Type: step.Type,
},
}
for _, subStep := range step.SubSteps {
steps[i].SubStepsStatus = append(steps[i].SubStepsStatus, model.StepStatus{
Name: subStep.Name,
Alias: subStep.Alias,
Type: subStep.Type,
})
}
}
return &model.WorkflowRecord{
WorkflowName: workflow.Name,
Expand Down

0 comments on commit eca8d1d

Please sign in to comment.