mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-23 22:46:53 +00:00
[Backport release-1.5] Fix: the workflow records do not delete if the driver is MongoDB (#4722)
* Fix: the workflow records do not delete if the driver is MongoDB Signed-off-by: barnettZQG <barnett.zqg@gmail.com> (cherry picked from commit284197ef09) * Fix: change the unit test case Signed-off-by: barnettZQG <barnett.zqg@gmail.com> (cherry picked from commit934c04b511) Co-authored-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
co-authored by
barnettZQG
parent
e5b0149ce5
commit
3c9f359e60
@@ -150,7 +150,7 @@ func (w *WorkflowRecord) Index() map[string]string {
|
||||
index["namespace"] = w.Namespace
|
||||
}
|
||||
if w.WorkflowName != "" {
|
||||
index["workflowPrimaryKey"] = w.WorkflowName
|
||||
index["workflowName"] = w.WorkflowName
|
||||
}
|
||||
if w.AppPrimaryKey != "" {
|
||||
index["appPrimaryKey"] = w.AppPrimaryKey
|
||||
|
||||
@@ -121,23 +121,22 @@ func (w *workflowServiceImpl) DeleteWorkflowByApp(ctx context.Context, app *mode
|
||||
}
|
||||
for i := range workflows {
|
||||
workflow := workflows[i].(*model.Workflow)
|
||||
var record = model.WorkflowRecord{
|
||||
AppPrimaryKey: workflow.AppPrimaryKey,
|
||||
WorkflowName: workflow.Name,
|
||||
}
|
||||
records, err := w.Store.List(ctx, &record, &datastore.ListOptions{})
|
||||
if err != nil {
|
||||
log.Logger.Errorf("list workflow %s record failure %s", workflow.PrimaryKey(), err.Error())
|
||||
}
|
||||
for _, record := range records {
|
||||
if err := w.Store.Delete(ctx, record); err != nil {
|
||||
log.Logger.Errorf("delete workflow record %s failure %s", record.PrimaryKey(), err.Error())
|
||||
}
|
||||
}
|
||||
if err := w.Store.Delete(ctx, workflow); err != nil {
|
||||
log.Logger.Errorf("delete workflow %s failure %s", workflow.PrimaryKey(), err.Error())
|
||||
}
|
||||
}
|
||||
var record = model.WorkflowRecord{
|
||||
AppPrimaryKey: workflow.AppPrimaryKey,
|
||||
}
|
||||
records, err := w.Store.List(ctx, &record, &datastore.ListOptions{})
|
||||
if err != nil {
|
||||
log.Logger.Errorf("list workflow %s record failure %s", workflow.PrimaryKey(), err.Error())
|
||||
}
|
||||
for _, record := range records {
|
||||
if err := w.Store.Delete(ctx, record); err != nil {
|
||||
log.Logger.Errorf("delete workflow record %s failure %s", record.PrimaryKey(), err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/pkg/apiserver/domain/model"
|
||||
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore"
|
||||
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore/mongodb"
|
||||
apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/apply"
|
||||
@@ -563,6 +564,55 @@ var _ = Describe("Test workflow service functions", func() {
|
||||
Expect(record.Finished).Should(Equal("true"))
|
||||
Expect(record.Steps[1].Phase).Should(Equal(common.WorkflowStepPhaseStopped))
|
||||
})
|
||||
|
||||
It("Test deleting workflow", func() {
|
||||
By("Test deleting the workflow from the mongo")
|
||||
mongodbDriver, err := mongodb.New(context.TODO(), datastore.Config{
|
||||
URL: "mongodb://localhost:27017",
|
||||
Database: "kubevela",
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(mongodbDriver).ToNot(BeNil())
|
||||
|
||||
Expect(mongodbDriver.BatchAdd(context.Background(), []datastore.Entity{
|
||||
&model.Workflow{
|
||||
Name: "workflow-default",
|
||||
AppPrimaryKey: "war-app",
|
||||
},
|
||||
&model.WorkflowRecord{
|
||||
Name: "workflow-default-20220809081934217",
|
||||
WorkflowName: "workflow-default",
|
||||
AppPrimaryKey: "war-app",
|
||||
RevisionPrimaryKey: "20220809081934216",
|
||||
},
|
||||
&model.WorkflowRecord{
|
||||
WorkflowName: "workflow-default",
|
||||
AppPrimaryKey: "war-app",
|
||||
Name: "workflow-default-20220809082525833",
|
||||
RevisionPrimaryKey: "20220809082525832",
|
||||
},
|
||||
})).ToNot(HaveOccurred())
|
||||
|
||||
var record = model.WorkflowRecord{
|
||||
AppPrimaryKey: "war-app",
|
||||
WorkflowName: "workflow-default",
|
||||
}
|
||||
records, err := mongodbDriver.List(context.TODO(), &record, &datastore.ListOptions{})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(records)).Should(Equal(2))
|
||||
|
||||
srv := workflowServiceImpl{
|
||||
Store: mongodbDriver,
|
||||
}
|
||||
Expect(srv.DeleteWorkflowByApp(context.TODO(), &model.Application{Name: "war-app"})).ToNot(HaveOccurred())
|
||||
wc, err := mongodbDriver.Count(context.TODO(), &model.Workflow{AppPrimaryKey: "war-app"}, nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(int(wc)).Should(Equal(0))
|
||||
|
||||
list, err := mongodbDriver.List(context.TODO(), &model.WorkflowRecord{AppPrimaryKey: "war-app"}, nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(list)).Should(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
var yamlStr = `apiVersion: core.oam.dev/v1beta1
|
||||
|
||||
Reference in New Issue
Block a user