From eca8d1d6f1b51e1f1fa6a3ff02ba64fa6c508589 Mon Sep 17 00:00:00 2001 From: Tianxin Dong Date: Wed, 19 Apr 2023 16:55:28 +0800 Subject: [PATCH] Fix: fix rollback sync and terminate case (#781) Signed-off-by: FogDong --- go.mod | 2 +- go.sum | 4 ++-- pkg/server/domain/service/application.go | 13 ++++++++++++- pkg/server/domain/service/workflow.go | 18 ++++++++++++++++-- pkg/server/event/sync/convert/convert.go | 7 +++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index b71c87e25..47bbd6650 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 75b37417e..83a681c84 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/server/domain/service/application.go b/pkg/server/domain/service/application.go index 2bcec4011..b0fc7a8ec 100644 --- a/pkg/server/domain/service/application.go +++ b/pkg/server/domain/service/application.go @@ -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 { @@ -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 } } diff --git a/pkg/server/domain/service/workflow.go b/pkg/server/domain/service/workflow.go index 7aafe3b0b..219880279 100644 --- a/pkg/server/domain/service/workflow.go +++ b/pkg/server/domain/service/workflow.go @@ -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", @@ -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) { @@ -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()) @@ -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 { diff --git a/pkg/server/event/sync/convert/convert.go b/pkg/server/event/sync/convert/convert.go index c3fa27682..d18b2ca53 100644 --- a/pkg/server/event/sync/convert/convert.go +++ b/pkg/server/event/sync/convert/convert.go @@ -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,