diff --git a/bcs-services/bcs-project-manager/internal/store/config/config.go b/bcs-services/bcs-project-manager/internal/store/config/config.go index 4aba248222..9ee3ca5fd5 100644 --- a/bcs-services/bcs-project-manager/internal/store/config/config.go +++ b/bcs-services/bcs-project-manager/internal/store/config/config.go @@ -39,6 +39,14 @@ const ( ConfigKeyUpdateNamespaceItsmServiceID = "update_namespace_itsm_service_id" // ConfigKeyDeleteNamespaceItsmServiceID used to create an itsm ticket when deleting a namespace in a shared cluster ConfigKeyDeleteNamespaceItsmServiceID = "delete_namespace_itsm_service_id" + // ConfigCreateNamespaceItsmServiceName used to create an itsm service when creating a namespace in a shared cluster + ConfigCreateNamespaceItsmServiceName = "创建共享集群命名空间" + // ConfigUpdateNamespaceItsmServiceName used to create an itsm service when updating a namespace in a shared cluster + ConfigUpdateNamespaceItsmServiceName = "更新共享集群命名空间" + // ConfigDeleteNamespaceItsmServiceName used to create an itsm service when deleting a namespace in a shared cluster + ConfigDeleteNamespaceItsmServiceName = "删除共享集群命名空间" + // QuotaManagerCommonItsmServiceID used to create an itsm ticket when quota manager + QuotaManagerCommonItsmServiceID = "quota_manager_common_itsm_service_id" ) // NOCC:deadcode/unused(设计如此:) diff --git a/bcs-services/bcs-project-manager/script/migrations/itsm/migrate.go b/bcs-services/bcs-project-manager/script/migrations/itsm/migrate.go index e0ef205a5f..a463db4614 100644 --- a/bcs-services/bcs-project-manager/script/migrations/itsm/migrate.go +++ b/bcs-services/bcs-project-manager/script/migrations/itsm/migrate.go @@ -17,16 +17,14 @@ import ( "context" "embed" "encoding/json" - "errors" "fmt" "strconv" "strings" "text/template" - "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers" - "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/component/itsm" "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/config" + "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/logging" "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store" cm "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store/config" ) @@ -144,17 +142,10 @@ func createITSMCatalog() (uint32, error) { func importCreateNamespaceService(catalogID uint32) error { // check whether the service has been imported before // if not, import it, else update it. - - var imported bool - serviceIDStr, err := model.GetConfig(context.Background(), cm.ConfigKeyCreateNamespaceItsmServiceID) - if err != nil && !errors.Is(err, drivers.ErrTableRecordNotFound) { + serviceID, err := getServiceIDByName(catalogID, cm.ConfigCreateNamespaceItsmServiceName) + if err != nil { return err } - if err == nil { - imported = true - } else { - imported = false - } // 自定义模板分隔符为 [[ ]],例如 [[ .Name ]],避免和 ITSM 模板变量格式冲突 tmpl, err := template.New("create_shared_namespace.json.tpl").Delims("[[", "]]"). ParseFS(WorkflowTemplates, "templates/create_shared_namespace.json.tpl") @@ -175,8 +166,8 @@ func importCreateNamespaceService(catalogID uint32) error { } importReq := itsm.ImportServiceReq{ Key: "request", - Name: "创建共享集群命名空间", - Desc: "创建共享集群命名空间", + Name: cm.ConfigCreateNamespaceItsmServiceName, + Desc: cm.ConfigCreateNamespaceItsmServiceName, CatelogID: catalogID, Owners: "admin", CanTicketAgency: false, @@ -187,38 +178,34 @@ func importCreateNamespaceService(catalogID uint32) error { ProjectKey: "0", Workflow: mp, } - var serviceID int - if !imported { + if serviceID == 0 { + logging.Info("service(name: %s) not found in itsm, creating", cm.ConfigCreateNamespaceItsmServiceName) serviceID, err = itsm.ImportService(importReq) if err != nil { return err } - return model.SetConfig(context.Background(), cm.ConfigKeyCreateNamespaceItsmServiceID, - strconv.Itoa(serviceID)) - } - serviceID, err = strconv.Atoi(serviceIDStr) - if err != nil { - return err + } else { + logging.Info("service(name: %s, id: %d) found in itsm, updating", + cm.ConfigCreateNamespaceItsmServiceName, serviceID) + err = itsm.UpdateService(itsm.UpdateServiceReq{ + ID: serviceID, + ImportServiceReq: importReq, + }) + if err != nil { + return err + } } - return itsm.UpdateService(itsm.UpdateServiceReq{ - ID: serviceID, - ImportServiceReq: importReq, - }) + return model.SetConfig(context.Background(), cm.ConfigKeyCreateNamespaceItsmServiceID, + strconv.Itoa(serviceID)) } func importUpdateNamespaceService(catalogID uint32) error { // check whether the service has been imported before // if not, import it, else update it. - var imported bool - serviceIDStr, err := model.GetConfig(context.Background(), cm.ConfigKeyUpdateNamespaceItsmServiceID) - if err != nil && !errors.Is(err, drivers.ErrTableRecordNotFound) { + serviceID, err := getServiceIDByName(catalogID, cm.ConfigUpdateNamespaceItsmServiceName) + if err != nil { return err } - if err == nil { - imported = true - } else { - imported = false - } // 自定义模板分隔符为 [[ ]],例如 [[ .Name ]],避免和 ITSM 模板变量格式冲突 tmpl, err := template.New("update_shared_namespace.json.tpl").Delims("[[", "]]"). ParseFS(WorkflowTemplates, "templates/update_shared_namespace.json.tpl") @@ -239,8 +226,8 @@ func importUpdateNamespaceService(catalogID uint32) error { } importReq := itsm.ImportServiceReq{ Key: "request", - Name: "更新共享集群命名空间", - Desc: "更新共享集群命名空间", + Name: cm.ConfigUpdateNamespaceItsmServiceName, + Desc: cm.ConfigUpdateNamespaceItsmServiceName, CatelogID: catalogID, Owners: "admin", CanTicketAgency: false, @@ -251,38 +238,34 @@ func importUpdateNamespaceService(catalogID uint32) error { ProjectKey: "0", Workflow: mp, } - var serviceID int - if !imported { + if serviceID == 0 { + logging.Info("service(name: %s) not found in itsm, creating", cm.ConfigUpdateNamespaceItsmServiceName) serviceID, err = itsm.ImportService(importReq) if err != nil { return err } - return model.SetConfig(context.Background(), cm.ConfigKeyUpdateNamespaceItsmServiceID, - strconv.Itoa(serviceID)) - } - serviceID, err = strconv.Atoi(serviceIDStr) - if err != nil { - return err + } else { + logging.Info("service(name: %s, id: %d) found in itsm, updating", + cm.ConfigUpdateNamespaceItsmServiceName, serviceID) + err = itsm.UpdateService(itsm.UpdateServiceReq{ + ID: serviceID, + ImportServiceReq: importReq, + }) + if err != nil { + return err + } } - return itsm.UpdateService(itsm.UpdateServiceReq{ - ID: serviceID, - ImportServiceReq: importReq, - }) + return model.SetConfig(context.Background(), cm.ConfigKeyUpdateNamespaceItsmServiceID, + strconv.Itoa(serviceID)) } func importDeleteNamespaceService(catalogID uint32) error { // check whether the service has been imported before // if not, import it, else update it. - var imported bool - serviceIDStr, err := model.GetConfig(context.Background(), cm.ConfigKeyDeleteNamespaceItsmServiceID) - if err != nil && !errors.Is(err, drivers.ErrTableRecordNotFound) { + serviceID, err := getServiceIDByName(catalogID, cm.ConfigDeleteNamespaceItsmServiceName) + if err != nil { return err } - if err == nil { - imported = true - } else { - imported = false - } // 自定义模板分隔符为 [[ ]],例如 [[ .Name ]],避免和 ITSM 模板变量格式冲突 tmpl, err := template.New("delete_shared_namespace.json.tpl").Delims("[[", "]]"). ParseFS(WorkflowTemplates, "templates/delete_shared_namespace.json.tpl") @@ -303,8 +286,8 @@ func importDeleteNamespaceService(catalogID uint32) error { } importReq := itsm.ImportServiceReq{ Key: "request", - Name: "删除共享集群命名空间", - Desc: "删除共享集群命名空间", + Name: cm.ConfigDeleteNamespaceItsmServiceName, + Desc: cm.ConfigDeleteNamespaceItsmServiceName, CatelogID: catalogID, Owners: "admin", CanTicketAgency: false, @@ -315,21 +298,37 @@ func importDeleteNamespaceService(catalogID uint32) error { ProjectKey: "0", Workflow: mp, } - var serviceID int - if !imported { + + if serviceID == 0 { + logging.Info("service(name: %s) not found in itsm, creating", cm.ConfigDeleteNamespaceItsmServiceName) serviceID, err = itsm.ImportService(importReq) if err != nil { return err } - return model.SetConfig(context.Background(), cm.ConfigKeyDeleteNamespaceItsmServiceID, - strconv.Itoa(serviceID)) + } else { + logging.Info("service(name: %s, id: %d) found in itsm, updating", + cm.ConfigDeleteNamespaceItsmServiceName, serviceID) + err = itsm.UpdateService(itsm.UpdateServiceReq{ + ID: serviceID, + ImportServiceReq: importReq, + }) + if err != nil { + return err + } } - serviceID, err = strconv.Atoi(serviceIDStr) + return model.SetConfig(context.Background(), cm.ConfigKeyDeleteNamespaceItsmServiceID, + strconv.Itoa(serviceID)) +} + +func getServiceIDByName(catalogID uint32, name string) (int, error) { + services, err := itsm.ListServices(catalogID) if err != nil { - return err + return 0, err } - return itsm.UpdateService(itsm.UpdateServiceReq{ - ID: serviceID, - ImportServiceReq: importReq, - }) + for _, service := range services { + if service.Name == name { + return service.ID, nil + } + } + return 0, nil }