From 7c9414bc7ca061f264dffc58af31fce0927ffe77 Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Fri, 27 Sep 2024 10:51:38 +0800 Subject: [PATCH 1/9] fix: conditions for select registercenter instance from db --- internal/tools/orchestrator/dbclient/addon_instance.go | 1 + internal/tools/orchestrator/services/addon/addon.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/internal/tools/orchestrator/dbclient/addon_instance.go b/internal/tools/orchestrator/dbclient/addon_instance.go index 398da09237e..2d02872c2b6 100644 --- a/internal/tools/orchestrator/dbclient/addon_instance.go +++ b/internal/tools/orchestrator/dbclient/addon_instance.go @@ -118,6 +118,7 @@ func (db *DBClient) GetAddonInstanceByNameAndCluster(addonName, cluster string) var instance AddonInstance if err := db.Where("addon_name = ?", addonName). Where("az = ?", cluster). + Where("status = ?", apistructs.AddonAttached). Where("is_deleted = ?", apistructs.AddonNotDeleted). First(&instance).Error; err != nil { if gorm.IsRecordNotFoundError(err) { diff --git a/internal/tools/orchestrator/services/addon/addon.go b/internal/tools/orchestrator/services/addon/addon.go index db83e27f47f..d538fca77f6 100644 --- a/internal/tools/orchestrator/services/addon/addon.go +++ b/internal/tools/orchestrator/services/addon/addon.go @@ -3145,8 +3145,10 @@ func (a *Addon) deployAddons(req *apistructs.AddonCreateRequest, deploys []dbcli createItem := needDeployAddons[index] switch v.AddonName { case RegisterCenterAddon: + logrus.Infof("register-center version: [%s]->[%s]", createItem.Options["version"], regVersion) createItem.Options["version"] = regVersion case ConfigCenterAddon: + logrus.Infof("config-center version: [%s]->[%s]", createItem.Options["version"], confVersion) createItem.Options["version"] = confVersion } instanceRes, err := a.AttachAndCreate(&createItem) From 6a8d7c226d8adc4a304207fb038f8ef0e60f3dae Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Fri, 27 Sep 2024 15:10:02 +0800 Subject: [PATCH 2/9] feat: mysql remove the version condition in msp Signed-off-by: CeerDecy <1748788674@qq.com> --- .../resource/deploy/handlers/mysql/mysql.go | 15 ++++++++ .../deploy/handlers/mysql/mysql_test.go | 36 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go index 8041d07235d..f364f519a99 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go @@ -141,6 +141,21 @@ func (h *provider) DoDeploy(serviceGroupDeployRequest interface{}, resourceInfo return h.DefaultDeployHandler.DoDeploy(serviceGroupDeployRequest, resourceInfo, tmcInstance, clusterConfig) } +func (h *provider) CheckIfNeedTmcInstance(req *handlers.ResourceDeployRequest, resourceInfo *handlers.ResourceInfo) (*db.Instance, bool, error) { + // mysql remove the `version` condition. because in the old cluster nacos[1.1.0] depend on mysql[5.7] but now depend on mysql[8.0] + var where = map[string]any{ + "engine": resourceInfo.TmcVersion.Engine, + "az": req.Az, + "status": handlers.TmcInstanceStatusRunning, + "is_deleted": "N", + } + instance, ok, err := h.InstanceDb.First(where) + if err != nil { + return nil, false, err + } + return instance, !ok, nil +} + func (p *provider) DoPostDeployJob(tmcInstance *db.Instance, serviceGroupDeployResult interface{}, clusterConfig map[string]string) (map[string]string, error) { serviceGroup := serviceGroupDeployResult.(*apistructs.ServiceGroup) mysqlMap := ParseResp2MySQLDtoMap(tmcInstance, serviceGroup) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go index 8a9743b0d66..3117c0ab557 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go @@ -15,7 +15,11 @@ package mysql import ( + "bou.ke/monkey" "encoding/json" + "github.com/erda-project/erda/internal/apps/msp/resource/deploy/handlers" + "github.com/erda-project/erda/internal/apps/msp/resource/utils" + "reflect" "testing" "github.com/erda-project/erda/apistructs" @@ -59,3 +63,35 @@ func TestParseResp2MySQLDtoMap(t *testing.T) { t.Fatal("failed to parse") } } + +func TestCheckIfNeedTmcInstance(t *testing.T) { + p := &provider{ + DefaultDeployHandler: &handlers.DefaultDeployHandler{ + InstanceDb: &db.InstanceDB{}, + }, + } + req := &handlers.ResourceDeployRequest{ + Engine: "mysql", + Uuid: utils.GetRandomId(), + Az: "test-cluster", + } + info := &handlers.ResourceInfo{ + Tmc: &db.Tmc{}, + TmcVersion: &db.TmcVersion{ + Engine: "mysql", + }, + } + + monkey.PatchInstanceMethod(reflect.TypeOf(p.InstanceDb), "First", func(DB *db.InstanceDB, where map[string]any) (*db.Instance, bool, error) { + return &db.Instance{ + Engine: "mysql", + Version: "9.0", + ReleaseID: "i am release id!", + Status: "RUNNING", + Az: "test-cluster", + Config: "", + }, false, nil + }) + + _, _, _ = p.CheckIfNeedTmcInstance(req, info) +} From 0c052a959e0d4c36ae9c1c9be2bb19dd9b47a5de Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Fri, 27 Sep 2024 15:10:23 +0800 Subject: [PATCH 3/9] feat: more logs info Signed-off-by: CeerDecy <1748788674@qq.com> --- internal/tools/orchestrator/services/addon/addon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/tools/orchestrator/services/addon/addon.go b/internal/tools/orchestrator/services/addon/addon.go index d538fca77f6..5c77849bc10 100644 --- a/internal/tools/orchestrator/services/addon/addon.go +++ b/internal/tools/orchestrator/services/addon/addon.go @@ -3145,10 +3145,10 @@ func (a *Addon) deployAddons(req *apistructs.AddonCreateRequest, deploys []dbcli createItem := needDeployAddons[index] switch v.AddonName { case RegisterCenterAddon: - logrus.Infof("register-center version: [%s]->[%s]", createItem.Options["version"], regVersion) + logrus.Infof("register-center version: [%s]->[%s] in cluster: %s", createItem.Options["version"], regVersion, req.ClusterName) createItem.Options["version"] = regVersion case ConfigCenterAddon: - logrus.Infof("config-center version: [%s]->[%s]", createItem.Options["version"], confVersion) + logrus.Infof("config-center version: [%s]->[%s] in cluster: %s", createItem.Options["version"], confVersion, req.ClusterName) createItem.Options["version"] = confVersion } instanceRes, err := a.AttachAndCreate(&createItem) From edf8f24aa614bb9514f9d4709d1e417e8863710c Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Fri, 27 Sep 2024 15:42:47 +0800 Subject: [PATCH 4/9] sort imports Signed-off-by: CeerDecy <1748788674@qq.com> --- .../apps/msp/resource/deploy/handlers/mysql/mysql_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go index 3117c0ab557..3aaaafd2ab6 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go @@ -15,15 +15,16 @@ package mysql import ( - "bou.ke/monkey" "encoding/json" - "github.com/erda-project/erda/internal/apps/msp/resource/deploy/handlers" - "github.com/erda-project/erda/internal/apps/msp/resource/utils" "reflect" "testing" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/internal/apps/msp/instance/db" + "github.com/erda-project/erda/internal/apps/msp/resource/deploy/handlers" + "github.com/erda-project/erda/internal/apps/msp/resource/utils" + + "bou.ke/monkey" ) func TestTryReadFile(t *testing.T) { From 0d2eee70a245c6e31effc33e88853f68befe7716 Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Sun, 29 Sep 2024 11:23:22 +0800 Subject: [PATCH 5/9] sort imports Signed-off-by: CeerDecy <1748788674@qq.com> --- internal/apps/msp/resource/deploy/handlers/mysql/mysql.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go index f364f519a99..f4be9106212 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go @@ -22,8 +22,6 @@ import ( "github.com/pkg/errors" - "github.com/erda-project/erda/pkg/strutil" - "github.com/erda-project/erda/apistructs" conf "github.com/erda-project/erda/cmd/erda-server/conf/msp" "github.com/erda-project/erda/internal/apps/msp/instance/db" @@ -33,6 +31,7 @@ import ( "github.com/erda-project/erda/pkg/crypto/uuid" "github.com/erda-project/erda/pkg/mysqlhelper" "github.com/erda-project/erda/pkg/parser/diceyml" + "github.com/erda-project/erda/pkg/strutil" ) func (p *provider) IsMatch(tmc *db.Tmc) bool { From ea10ed96a5781cdf759bf21141743c3d7f1981c3 Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Mon, 28 Oct 2024 14:01:33 +0800 Subject: [PATCH 6/9] polish: use apistruct constant Signed-off-by: CeerDecy <1748788674@qq.com> --- internal/apps/msp/resource/deploy/handlers/mysql/mysql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go index f4be9106212..95795f035ff 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql.go @@ -146,7 +146,7 @@ func (h *provider) CheckIfNeedTmcInstance(req *handlers.ResourceDeployRequest, r "engine": resourceInfo.TmcVersion.Engine, "az": req.Az, "status": handlers.TmcInstanceStatusRunning, - "is_deleted": "N", + "is_deleted": apistructs.AddonNotDeleted, } instance, ok, err := h.InstanceDb.First(where) if err != nil { From 2598a9852f8be020f3c12dda9f5e2a6de4d74448 Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Mon, 28 Oct 2024 14:02:20 +0800 Subject: [PATCH 7/9] polish: sort imports Signed-off-by: CeerDecy <1748788674@qq.com> --- .../apps/msp/resource/deploy/handlers/mysql/mysql_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go index 3aaaafd2ab6..29a23b1d6cc 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go @@ -19,12 +19,12 @@ import ( "reflect" "testing" + "bou.ke/monkey" + "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/internal/apps/msp/instance/db" "github.com/erda-project/erda/internal/apps/msp/resource/deploy/handlers" "github.com/erda-project/erda/internal/apps/msp/resource/utils" - - "bou.ke/monkey" ) func TestTryReadFile(t *testing.T) { From 96bd7c43daa427ee9e83125b7c52c3205760c3eb Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Mon, 28 Oct 2024 14:12:44 +0800 Subject: [PATCH 8/9] remove coincident code Signed-off-by: CeerDecy <1748788674@qq.com> --- internal/tools/orchestrator/dbclient/addon_instance.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/tools/orchestrator/dbclient/addon_instance.go b/internal/tools/orchestrator/dbclient/addon_instance.go index eddfaf786f0..71afda4b437 100644 --- a/internal/tools/orchestrator/dbclient/addon_instance.go +++ b/internal/tools/orchestrator/dbclient/addon_instance.go @@ -118,7 +118,6 @@ func (db *DBClient) GetAddonInstanceByNameAndCluster(addonName, cluster string) var instance AddonInstance if err := db.Where("addon_name = ?", addonName). Where("az = ?", cluster). - Where("status = ?", apistructs.AddonAttached). Where("is_deleted = ?", apistructs.AddonNotDeleted). Where("status = ?", apistructs.AddonAttached). First(&instance).Error; err != nil { From ab421bf4d071aec0d18f2a2f58c4aa29d4e4db05 Mon Sep 17 00:00:00 2001 From: CeerDecy <1748788674@qq.com> Date: Wed, 18 Dec 2024 14:44:20 +0800 Subject: [PATCH 9/9] feat: gomonkey test --- .../msp/resource/deploy/handlers/mysql/mysql_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go index 29a23b1d6cc..bd1e7489816 100644 --- a/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go +++ b/internal/apps/msp/resource/deploy/handlers/mysql/mysql_test.go @@ -19,7 +19,8 @@ import ( "reflect" "testing" - "bou.ke/monkey" + "github.com/agiledragon/gomonkey/v2" + "github.com/stretchr/testify/assert" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/internal/apps/msp/instance/db" @@ -83,7 +84,7 @@ func TestCheckIfNeedTmcInstance(t *testing.T) { }, } - monkey.PatchInstanceMethod(reflect.TypeOf(p.InstanceDb), "First", func(DB *db.InstanceDB, where map[string]any) (*db.Instance, bool, error) { + applyFunc := gomonkey.ApplyMethod(reflect.TypeOf(p.InstanceDb), "First", func(DB *db.InstanceDB, where map[string]any) (*db.Instance, bool, error) { return &db.Instance{ Engine: "mysql", Version: "9.0", @@ -93,6 +94,8 @@ func TestCheckIfNeedTmcInstance(t *testing.T) { Config: "", }, false, nil }) + defer applyFunc.Reset() - _, _, _ = p.CheckIfNeedTmcInstance(req, info) + _, _, err := p.CheckIfNeedTmcInstance(req, info) + assert.NoError(t, err) }