Skip to content

Commit 34bb5a5

Browse files
committed
refactor(k8s-dbs): 获取集群操作记录 #11246
1 parent d910e42 commit 34bb5a5

16 files changed

+192
-55
lines changed

dbm-services/k8s-dbs/core/provider/cluster_provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,12 @@ func (c *ClusterProvider) createRequestEntity(
473473
}
474474

475475
requestRecord := &provderentity.ClusterRequestRecordEntity{
476-
RequestID: util.RequestID(),
477-
RequestType: requestType,
478-
RequestParams: string(requestBytes),
476+
K8sClusterName: request.K8sClusterName,
477+
ClusterName: request.ClusterName,
478+
NameSpace: request.Namespace,
479+
RequestID: util.RequestID(),
480+
RequestType: requestType,
481+
RequestParams: string(requestBytes),
479482
}
480483

481484
addedRequestRecord, err := c.reqRecordProvider.CreateRequestRecord(requestRecord)

dbm-services/k8s-dbs/core/provider/ops_provider.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,11 @@ func (o *OpsRequestProvider) createRequestEntity(
610610
request *coreentity.Request,
611611
requestType string,
612612
) (*providerentity.ClusterRequestRecordEntity, error) {
613-
// Serialize request
614613
requestBytes, err := json.Marshal(request)
615614
if err != nil {
616615
return nil, fmt.Errorf("serialization request failed: %v", err)
617616
}
618617

619-
// Construct a request instance object
620618
requestRecord := &providerentity.ClusterRequestRecordEntity{
621619
RequestID: util.RequestID(),
622620
RequestType: requestType,

dbm-services/k8s-dbs/metadata/api/controller/addon_helm_repo_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ func (c *AddonHelmRepoController) CreateAddonHelmRepo(ctx *gin.Context) {
9494

9595
// GetAddonHelmRepoByParam get addon helm repo by its Param.
9696
func (c *AddonHelmRepoController) GetAddonHelmRepoByParam(ctx *gin.Context) {
97-
chartName := ctx.Param("chart_name")
98-
chartVersion := ctx.Param("chart_version")
97+
chartName := ctx.Param("chartName")
98+
chartVersion := ctx.Param("chartVersion")
9999
if chartName == "" || chartVersion == "" {
100100
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr,
101-
fmt.Errorf("chart_namechart_version 参数不能为空")))
101+
fmt.Errorf("chartNamechartVersion 参数不能为空")))
102102
return
103103
}
104104
params := map[string]interface{}{

dbm-services/k8s-dbs/metadata/api/controller/addoncluster_helm_repo_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ func (c *ClusterHelmRepoController) CreateClusterHelmRepo(ctx *gin.Context) {
9494

9595
// GetClusterHelmRepoByParam get addon cluster helm repo by its Param.
9696
func (c *ClusterHelmRepoController) GetClusterHelmRepoByParam(ctx *gin.Context) {
97-
chartName := ctx.Param("chart_name")
98-
chartVersion := ctx.Param("chart_version")
97+
chartName := ctx.Param("chartName")
98+
chartVersion := ctx.Param("chartVersion")
9999
if chartName == "" || chartVersion == "" {
100100
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr,
101-
fmt.Errorf("chart_namechart_version 参数不能为空")))
101+
fmt.Errorf("chartNamechartVersion 参数不能为空")))
102102
return
103103
}
104104
params := map[string]interface{}{

dbm-services/k8s-dbs/metadata/api/controller/addoncluster_release_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *ClusterReleaseController) GetClusterRelease(ctx *gin.Context) {
6868

6969
// GetClusterReleaseByParam get addon cluster release by its Param.
7070
func (c *ClusterReleaseController) GetClusterReleaseByParam(ctx *gin.Context) {
71-
releaseNameParam := ctx.Param("release_name")
71+
releaseNameParam := ctx.Param("releaseName")
7272
namespaceParam := ctx.Param("namespace")
7373
if releaseNameParam == "" || namespaceParam == "" {
7474
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, fmt.Errorf("cluster_name 参数不能为空")))
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
TencentBlueKing is pleased to support the open source community by making
3+
蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
5+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
6+
7+
Licensed under the MIT License (the "License");
8+
you may not use this file except in compliance with the License.
9+
10+
You may obtain a copy of the License at
11+
https://opensource.org/licenses/MIT
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package controller
21+
22+
import (
23+
"k8s-dbs/core/entity"
24+
"k8s-dbs/core/errors"
25+
"k8s-dbs/metadata/api/vo/resp"
26+
"k8s-dbs/metadata/provider"
27+
28+
"github.com/gin-gonic/gin"
29+
"github.com/jinzhu/copier"
30+
31+
commconst "k8s-dbs/common/api/constant"
32+
)
33+
34+
// ClusterRequestRecordController manages metadata for addons.
35+
type ClusterRequestRecordController struct {
36+
clusterRequestProvider provider.ClusterRequestRecordProvider
37+
}
38+
39+
// NewClusterRequestRecordController creates a new instance of ClusterRequestRecordController.
40+
func NewClusterRequestRecordController(
41+
clusterRequestProvider provider.ClusterRequestRecordProvider,
42+
) *ClusterRequestRecordController {
43+
return &ClusterRequestRecordController{clusterRequestProvider}
44+
}
45+
46+
// GetRecordsByCluster 根据集群名称来获取对应的集群操作记录.
47+
func (k *ClusterRequestRecordController) GetRecordsByCluster(ctx *gin.Context) {
48+
k8sClusterName := ctx.Query("k8sClusterName")
49+
clusterName := ctx.Query("clusterName")
50+
namespace := ctx.Query("namespace")
51+
params := map[string]interface{}{
52+
"k8s_cluster_name": k8sClusterName,
53+
"cluster_name": clusterName,
54+
"namespace": namespace,
55+
}
56+
records, err := k.clusterRequestProvider.FindRecordsByParams(params)
57+
if err != nil {
58+
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, err))
59+
return
60+
}
61+
var data []resp.ClusterRequestRecordRespVo
62+
if err := copier.Copy(&data, records); err != nil {
63+
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, err))
64+
return
65+
}
66+
entity.SuccessResponse(ctx, data, commconst.Success)
67+
}

dbm-services/k8s-dbs/metadata/api/controller/k8s_cluster_addons_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ func (k *K8sClusterAddonsController) GetAddon(ctx *gin.Context) {
6464

6565
// GetAddonsByClusterName retrieves cluster addons by k8s_cluster_name.
6666
func (k *K8sClusterAddonsController) GetAddonsByClusterName(ctx *gin.Context) {
67-
k8sClusterName := ctx.Query("k8s_cluster_name")
68-
params := make(map[string]interface{})
69-
params["k8s_cluster_name"] = k8sClusterName
67+
k8sClusterName := ctx.Query("k8sClusterName")
68+
params := map[string]interface{}{
69+
"k8s_cluster_name": k8sClusterName,
70+
}
7071
clusterAddons, err := k.caProvider.FindClusterAddonByParams(params)
7172
if err != nil {
7273
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, err))

dbm-services/k8s-dbs/metadata/api/controller/k8s_cluster_config_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func (k *K8sClusterConfigController) GetK8sClusterConfigByID(ctx *gin.Context) {
6767

6868
// GetK8sClusterConfigByName get clusterConfig by its Name.
6969
func (k *K8sClusterConfigController) GetK8sClusterConfigByName(ctx *gin.Context) {
70-
nameParam := ctx.Param("cluster_name")
70+
nameParam := ctx.Param("clusterName")
7171
if nameParam == "" {
72-
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, fmt.Errorf("cluster_name 参数不能为空")))
72+
entity.ErrorResponse(ctx, errors.NewGlobalError(errors.GetMetaDataErr, fmt.Errorf("clusterName 参数不能为空")))
7373
return
7474
}
7575
config, err := k.configProvider.FindConfigByName(nameParam)

dbm-services/k8s-dbs/metadata/api/vo/req/cluster_request_record_entity.go renamed to dbm-services/k8s-dbs/metadata/api/vo/req/cluster_request_reqvo.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ import (
2525

2626
// ClusterRequestRecordReqVo represents the request data structure of record.
2727
type ClusterRequestRecordReqVo struct {
28-
RequestID string `json:"requestId" required:"true"`
29-
RequestType string `json:"requestType" required:"true"`
30-
RequestParams string `json:"requestParams" required:"true"`
31-
Description string `json:"description" required:"true"`
32-
CreatedBy string `json:"createdBy" required:"true"`
33-
CreatedAt time.Time `json:"createdAt"`
34-
UpdatedBy string `json:"updatedBy"`
35-
UpdatedAt time.Time `json:"updatedAt"`
28+
RequestID string `json:"requestId" required:"true"`
29+
K8sClusterName string `json:"k8sClusterName" required:"true"`
30+
ClusterName string `json:"clusterName" required:"true"`
31+
NameSpace string `json:"namespace" required:"true"`
32+
RequestType string `json:"requestType" required:"true"`
33+
RequestParams string `json:"requestParams" required:"true"`
34+
Description string `json:"description" required:"true"`
35+
CreatedBy string `json:"createdBy" required:"true"`
36+
CreatedAt time.Time `json:"createdAt"`
37+
UpdatedBy string `json:"updatedBy"`
38+
UpdatedAt time.Time `json:"updatedAt"`
3639
}

dbm-services/k8s-dbs/metadata/api/vo/resp/cluster_request_record_entity.go renamed to dbm-services/k8s-dbs/metadata/api/vo/resp/cluster_request_respvo.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import (
2525

2626
// ClusterRequestRecordRespVo defines the response data structure of request record.
2727
type ClusterRequestRecordRespVo struct {
28-
ID uint64 `json:"id"`
29-
RequestID string `json:"requestId"`
30-
RequestType string `json:"requestType"`
31-
RequestParams string `json:"requestParams"`
32-
Status string `json:"status"`
33-
Description string `json:"description"`
34-
CreatedBy string `json:"createdBy"`
35-
CreatedAt time.Time `json:"createdAt"`
36-
UpdatedBy string `json:"updatedBy"`
37-
UpdatedAt time.Time `json:"updatedAt"`
28+
ID uint64 `json:"id"`
29+
RequestID string `json:"requestId"`
30+
K8sClusterName string `json:"k8sClusterName"`
31+
ClusterName string `json:"clusterName"`
32+
NameSpace string `json:"namespace"`
33+
RequestType string `json:"requestType"`
34+
RequestParams string `json:"requestParams"`
35+
Status string `json:"status"`
36+
Description string `json:"description"`
37+
CreatedBy string `json:"createdBy"`
38+
CreatedAt time.Time `json:"createdAt"`
39+
UpdatedBy string `json:"updatedBy"`
40+
UpdatedAt time.Time `json:"updatedAt"`
3841
}

dbm-services/k8s-dbs/metadata/dbaccess/cluster_request_dbaccess.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ limitations under the License.
2020
package dbaccess
2121

2222
import (
23+
"errors"
2324
"fmt"
2425
"k8s-dbs/common/entity"
26+
mconst "k8s-dbs/metadata/constant"
2527
models "k8s-dbs/metadata/dbaccess/model"
2628
"log/slog"
2729

@@ -35,13 +37,33 @@ type ClusterRequestRecordDbAccess interface {
3537
FindByID(id uint64) (*models.ClusterRequestRecordModel, error)
3638
Update(model *models.ClusterRequestRecordModel) (uint64, error)
3739
ListByPage(pagination entity.Pagination) ([]models.ClusterRequestRecordModel, int64, error)
40+
FindByParams(params map[string]interface{}) ([]models.ClusterRequestRecordModel, error)
3841
}
3942

4043
// ClusterRequestRecordDbAccessImpl ClusterRequestRecordDbAccess 的具体实现
4144
type ClusterRequestRecordDbAccessImpl struct {
4245
db *gorm.DB
4346
}
4447

48+
// FindByParams 通过参数查询
49+
func (k *ClusterRequestRecordDbAccessImpl) FindByParams(params map[string]interface{}) (
50+
[]models.ClusterRequestRecordModel,
51+
error,
52+
) {
53+
var recordModels []models.ClusterRequestRecordModel
54+
if err := k.db.
55+
Where(params).
56+
Limit(mconst.MaxFetchSize).
57+
Find(&recordModels).Error; err != nil {
58+
if errors.Is(err, gorm.ErrRecordNotFound) {
59+
return []models.ClusterRequestRecordModel{}, nil
60+
}
61+
slog.Error("failed to find by params", "error", err)
62+
return nil, fmt.Errorf("database query failed: %w", err)
63+
}
64+
return recordModels, nil
65+
}
66+
4567
// Create 创建元数据接口实现
4668
func (k *ClusterRequestRecordDbAccessImpl) Create(model *models.ClusterRequestRecordModel) (
4769
*models.ClusterRequestRecordModel, error,

dbm-services/k8s-dbs/metadata/dbaccess/model/cluster_request_model.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ import (
2626

2727
// ClusterRequestRecordModel represents the database model of request record
2828
type ClusterRequestRecordModel struct {
29-
ID uint64 `gorm:"primaryKey;autoIncrement;column:id" json:"id"`
30-
RequestID string `gorm:"size:50;not null;column:request_id" json:"requestId"`
31-
RequestType string `gorm:"size:50;not null;column:request_type" json:"requestType"`
32-
RequestParams string `gorm:"type:text;column:request_params" json:"requestParams"`
33-
Status string `gorm:"size:32;column:status" json:"status"`
34-
Description string `gorm:"size:100;column:description" json:"description"`
35-
CreatedBy string `gorm:"size:50;not null;column:created_by" json:"createdBy"`
36-
CreatedAt time.Time `gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;column:created_at" json:"createdAt"` //nolint:lll
37-
UpdatedBy string `gorm:"size:50;not null;column:updated_by" json:"updatedBy"`
38-
UpdatedAt time.Time `gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP;column:updated_at" json:"updatedAt"` //nolint:lll
29+
ID uint64 `gorm:"primaryKey;autoIncrement;column:id" json:"id"`
30+
RequestID string `gorm:"size:50;not null;column:request_id" json:"requestId"`
31+
K8sClusterName string `gorm:"size:32;column:k8s_cluster_name" json:"k8sClusterName"`
32+
ClusterName string `gorm:"size:32;column:cluster_name" json:"clusterName"`
33+
NameSpace string `gorm:"size:32;column:namespace" json:"namespace"`
34+
RequestType string `gorm:"size:50;not null;column:request_type" json:"requestType"`
35+
RequestParams string `gorm:"type:text;column:request_params" json:"requestParams"`
36+
Status string `gorm:"size:32;column:status" json:"status"`
37+
Description string `gorm:"size:100;column:description" json:"description"`
38+
CreatedBy string `gorm:"size:50;not null;column:created_by" json:"createdBy"`
39+
CreatedAt time.Time `gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;column:created_at" json:"createdAt"` //nolint:lll
40+
UpdatedBy string `gorm:"size:50;not null;column:updated_by" json:"updatedBy"`
41+
UpdatedAt time.Time `gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP;column:updated_at" json:"updatedAt"` //nolint:lll
3942
}
4043

4144
// TableName 获取 model 对应的数据库表名

dbm-services/k8s-dbs/metadata/provider/cluster_request_record_provider.go renamed to dbm-services/k8s-dbs/metadata/provider/cluster_request_provider.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,30 @@ type ClusterRequestRecordProvider interface {
3434
DeleteRequestRecordByID(id uint64) (uint64, error)
3535
FindRequestRecordByID(id uint64) (*entitys.ClusterRequestRecordEntity, error)
3636
UpdateRequestRecord(entity *entitys.ClusterRequestRecordEntity) (uint64, error)
37+
FindRecordsByParams(params map[string]interface{}) ([]entitys.ClusterRequestRecordEntity, error)
3738
}
3839

3940
// ClusterRequestRecordProviderImpl ClusterRequestRecordProvider 具体实现
4041
type ClusterRequestRecordProviderImpl struct {
4142
dbAccess dbaccess.ClusterRequestRecordDbAccess
4243
}
4344

45+
// FindRecordsByParams 通过参数查询 request record
46+
func (k *ClusterRequestRecordProviderImpl) FindRecordsByParams(params map[string]interface{}) (
47+
[]entitys.ClusterRequestRecordEntity,
48+
error,
49+
) {
50+
recordModels, err := k.dbAccess.FindByParams(params)
51+
if err != nil {
52+
return nil, err
53+
}
54+
var recordEntities []entitys.ClusterRequestRecordEntity
55+
if err := copier.Copy(&recordEntities, recordModels); err != nil {
56+
return nil, err
57+
}
58+
return recordEntities, nil
59+
}
60+
4461
// CreateRequestRecord 创建 request record
4562
func (k *ClusterRequestRecordProviderImpl) CreateRequestRecord(entity *entitys.ClusterRequestRecordEntity) (
4663
*entitys.ClusterRequestRecordEntity, error,

dbm-services/k8s-dbs/metadata/provider/entity/cluster_request_entity.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import (
2525

2626
// ClusterRequestRecordEntity request record entity 定义
2727
type ClusterRequestRecordEntity struct {
28-
ID uint64 `json:"id"`
29-
RequestID string `json:"requestId"`
30-
RequestType string `json:"requestType"`
31-
RequestParams string `json:"requestParams"`
32-
Status string `json:"status"`
33-
Description string `json:"description"`
34-
CreatedBy string `json:"createdBy"`
35-
CreatedAt time.Time `json:"createdAt"`
36-
UpdatedBy string `json:"updatedBy"`
37-
UpdatedAt time.Time `json:"updatedAt"`
28+
ID uint64 `json:"id"`
29+
RequestID string `json:"requestId"`
30+
K8sClusterName string `json:"k8sClusterName"`
31+
ClusterName string `json:"clusterName"`
32+
NameSpace string `json:"namespace"`
33+
RequestType string `json:"requestType"`
34+
RequestParams string `json:"requestParams"`
35+
Status string `json:"status"`
36+
Description string `json:"description"`
37+
CreatedBy string `json:"createdBy"`
38+
CreatedAt time.Time `json:"createdAt"`
39+
UpdatedBy string `json:"updatedBy"`
40+
UpdatedAt time.Time `json:"updatedAt"`
3841
}

dbm-services/k8s-dbs/router/metadata_router.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func buildMetaRouter(db *gorm.DB, router *gin.Engine) {
6363
buildK8sClusterAddonsRouter(db, metaRouter)
6464

6565
buildAddonClusterVersionRouter(db, metaRouter)
66+
67+
buildRequestRecordRouter(db, metaRouter)
6668
}
6769
}
6870

@@ -276,3 +278,15 @@ func buildAddonClusterVersionRouter(db *gorm.DB, metaRouter *gin.RouterGroup) {
276278
metaGroup.PUT("/:id", metaController.UpdateAcVersion)
277279
}
278280
}
281+
282+
// buildRequestRecordRouter cluster request record 管理路由构建
283+
func buildRequestRecordRouter(db *gorm.DB, metaRouter *gin.RouterGroup) {
284+
metaDbAccess := metadbaccess.NewClusterRequestRecordDbAccess(db)
285+
metaProvider := metaprovider.NewClusterRequestRecordProvider(metaDbAccess)
286+
metaController := metacontroller.NewClusterRequestRecordController(metaProvider)
287+
288+
metaGroup := metaRouter.Group("/cluster_request_record")
289+
{
290+
metaGroup.GET("", metaController.GetRecordsByCluster)
291+
}
292+
}

dbm-services/k8s-dbs/scrips/sql/dbs.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ CREATE TABLE IF NOT EXISTS tb_k8s_cluster_service (
103103
CREATE TABLE IF NOT EXISTS tb_cluster_request_record (
104104
id bigint PRIMARY KEY AUTO_INCREMENT COMMENT '主键 id',
105105
request_id varchar(50) NOT NULL COMMENT '请求Id,使用全局的UUID',
106+
cluster_name varchar(32) COMMENT '集群名称',
107+
namespace varchar(32) COMMENT '集群所在的命名空间',
108+
k8s_cluster_name varchar(32) COMMENT 'k8s 集群名称',
106109
request_type varchar(50) NOT NULL COMMENT '操作记录类型 Create/Delete/Restart/Start/Stop/Switchover/Upgrade/HorizontalScaling/VerticalScaling/VolumeExpansion',
107110
request_params text COMMENT '操作记录请求信息',
108111
status varchar(100) COMMENT '操作记录请求状态 Cancelled/Cancelling/Creating/Failed/Pending/Running/Succeed',

0 commit comments

Comments
 (0)