diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index df879ab2..7e24e18d 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -158,7 +158,7 @@ type ( centerResourcesResp { CentersIndex []CenterIndex `json:"centersIndex"` } - CenterIndex{ + CenterIndex { name string `json:"name"` cpu float32 `json:"cpu"` memory float32 `json:"memory"` @@ -166,6 +166,20 @@ type ( centerType string `json:"centerType"` } ) +type ( + syncClusterLoadReq { + clusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"` + } + ClusterLoadRecord { + ClusterName string `json:"clusterName"` + CpuUsage float64 `json:"cpuUsage"` + MemoryUsage float64 `json:"memoryUsage"` + DiskUsage float64 `json:"diskUsage"` + } +) + + + type ( getClusterListReq { Id int64 `form:"id"` @@ -670,7 +684,7 @@ type ( type ( AdapterQueryReq { - Id string `form:"id,optional" db:"id"` + Id string `form:"id,optional" db:"id"` Name string `form:"name,optional"` Type string `form:"type,optional"` Nickname string `form:"nickname,optional"` @@ -678,7 +692,7 @@ type ( Server string `form:"server,optional"` } AdapterReq { - Id string `json:"id,optional" db:"id"` + Id string `json:"id,optional" db:"id"` Name string `json:"name,optional"` Type string `json:"type,optional"` Nickname string `json:"nickname,optional"` @@ -686,7 +700,7 @@ type ( Server string `json:"server,optional"` } AdapterCreateReq { - Id string `json:"id,optional" db:"id"` + Id string `json:"id,optional" db:"id"` Name string `json:"name"` Type string `json:"type"` Nickname string `json:"nickname"` @@ -694,7 +708,7 @@ type ( Server string `json:"server"` } AdapterDelReq { - Id string `form:"id,optional" db:"id"` + Id string `form:"id,optional" db:"id"` } AdapterInfo { Id string `json:"id,omitempty" db:"id"` @@ -727,51 +741,51 @@ type ( ) type ClusterReq { - Id string `form:"id,optional"` - AdapterId string `form:"adapterId,optional"` - Name string `json:"name,optional"` - Nickname string `json:"nickname,optional"` - Description string `json:"description,optional"` - Server string `json:"server,optional"` - MonitorServer string `json:"monitorServer,optional"` - Username string `json:"username,optional"` - Password string `json:"password,optional"` - Token string `json:"token,optional"` - Ak string `json:"ak,optional"` - Sk string `json:"sk,optional"` - Region string `json:"region,optional"` - ProjectId string `json:"projectId,optional"` - Version string `json:"version,optional"` - Label string `json:"label,optional"` - OwnerId string `json:"ownerId,omitempty,optional"` - AuthType string `json:"authType,optional"` - Type string `json:"type,optional"` + Id string `form:"id,optional"` + AdapterId string `form:"adapterId,optional"` + Name string `json:"name,optional"` + Nickname string `json:"nickname,optional"` + Description string `json:"description,optional"` + Server string `json:"server,optional"` + MonitorServer string `json:"monitorServer,optional"` + Username string `json:"username,optional"` + Password string `json:"password,optional"` + Token string `json:"token,optional"` + Ak string `json:"ak,optional"` + Sk string `json:"sk,optional"` + Region string `json:"region,optional"` + ProjectId string `json:"projectId,optional"` + Version string `json:"version,optional"` + Label string `json:"label,optional"` + OwnerId string `json:"ownerId,omitempty,optional"` + AuthType string `json:"authType,optional"` + Type string `json:"type,optional"` } type ClusterDelReq { - Id string `form:"id,optional"` + Id string `form:"id,optional"` } type ClusterInfo { - Id string `json:"id,omitempty" db:"id"` - AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` - Name string `json:"name,omitempty" db:"name"` - Nickname string `json:"nickname,omitempty" db:"nickname"` - Description string `json:"description,omitempty" db:"description"` - Server string `json:"server,omitempty" db:"server"` - MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` - Username string `json:"username,omitempty" db:"username"` - Password string `json:"password,omitempty" db:"password"` - Token string `json:"token,omitempty" db:"token"` - Ak string `json:"ak,omitempty" db:"ak"` - Sk string `json:"sk,omitempty" db:"sk"` - Region string `json:"region,omitempty" db:"region"` - ProjectId string `json:"projectId,omitempty" db:"project_id"` - Version string `json:"version,omitempty" db:"version"` - Label string `json:"label,omitempty" db:"label"` - OwnerId string `json:"ownerId,omitempty" db:"owner_id"` - AuthType string `json:"authType,omitempty" db:"auth_type"` - CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` + Id string `json:"id,omitempty" db:"id"` + AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` + Name string `json:"name,omitempty" db:"name"` + Nickname string `json:"nickname,omitempty" db:"nickname"` + Description string `json:"description,omitempty" db:"description"` + Server string `json:"server,omitempty" db:"server"` + MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` + Username string `json:"username,omitempty" db:"username"` + Password string `json:"password,omitempty" db:"password"` + Token string `json:"token,omitempty" db:"token"` + Ak string `json:"ak,omitempty" db:"ak"` + Sk string `json:"sk,omitempty" db:"sk"` + Region string `json:"region,omitempty" db:"region"` + ProjectId string `json:"projectId,omitempty" db:"project_id"` + Version string `json:"version,omitempty" db:"version"` + Label string `json:"label,omitempty" db:"label"` + OwnerId string `json:"ownerId,omitempty" db:"owner_id"` + AuthType string `json:"authType,omitempty" db:"auth_type"` + CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` } type ClusterResp { @@ -786,7 +800,7 @@ type clusterSumReq { } -type clusterSumReqResp{ +type clusterSumReqResp { ClusterSum int `json:"ClusterSum,omitempty"` AdapterSum int `json:"AdapterSum,omitempty"` TaskSum int `json:"TaskSum,omitempty"` @@ -800,23 +814,23 @@ type ClusterRelationInfo { Version string `json:"version,omitempty" db:"version"` Server string `json:"server,omitempty" db:"server"` CreateTime string `json:"createTime,omitempty" db:"create_time" gorm:"autoCreateTime"` - CId string `json:"cId,omitempty" db:"id"` - CAdapterId string `json:"cAdapterId,omitempty" db:"adapter_id"` - CName string `json:"cName,omitempty" db:"name"` - CNickname string `json:"cNickname,omitempty" db:"nickname"` - CDescription string `json:"cDescription,omitempty" db:"description"` - CServer string `json:"cServer,omitempty" db:"server"` - CMonitorServer string `json:"cMonitorServer,omitempty" db:"monitor_server"` - CUsername string `json:"cUsername,omitempty" db:"username"` - CPassword string `json:"cPassword,omitempty" db:"password"` - CToken string `json:"cToken,omitempty" db:"token"` - CAk string `json:"cAk,omitempty" db:"ak"` - CSk string `json:"cSk,omitempty" db:"sk"` - CRegion string `json:"cRegion,omitempty" db:"region"` - CProjectId string `json:"cProjectId,omitempty" db:"project_id"` - CVersion string `json:"cVersion,omitempty" db:"version"` - CLabel string `json:"cLabel,omitempty" db:"label"` - COwnerId string `json:"cOwnerId,omitempty" db:"owner_id"` - CAuthType string `json:"cAuthType,omitempty" db:"auth_type"` - CCreateTime string `json:"cCreateTime,omitempty" db:"created_time" gorm:"autoCreateTime"` + CId string `json:"cId,omitempty" db:"id"` + CAdapterId string `json:"cAdapterId,omitempty" db:"adapter_id"` + CName string `json:"cName,omitempty" db:"name"` + CNickname string `json:"cNickname,omitempty" db:"nickname"` + CDescription string `json:"cDescription,omitempty" db:"description"` + CServer string `json:"cServer,omitempty" db:"server"` + CMonitorServer string `json:"cMonitorServer,omitempty" db:"monitor_server"` + CUsername string `json:"cUsername,omitempty" db:"username"` + CPassword string `json:"cPassword,omitempty" db:"password"` + CToken string `json:"cToken,omitempty" db:"token"` + CAk string `json:"cAk,omitempty" db:"ak"` + CSk string `json:"cSk,omitempty" db:"sk"` + CRegion string `json:"cRegion,omitempty" db:"region"` + CProjectId string `json:"cProjectId,omitempty" db:"project_id"` + CVersion string `json:"cVersion,omitempty" db:"version"` + CLabel string `json:"cLabel,omitempty" db:"label"` + COwnerId string `json:"cOwnerId,omitempty" db:"owner_id"` + CAuthType string `json:"cAuthType,omitempty" db:"auth_type"` + CCreateTime string `json:"cCreateTime,omitempty" db:"created_time" gorm:"autoCreateTime"` } diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 2651f6ee..1b15ac59 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -112,6 +112,14 @@ service pcm { @doc "Center Resources top3" @handler centerResourcesHandler get /core/centerResources returns (centerResourcesResp) + + @doc "Synchronize Cluster Load Information" + @handler syncClusterLoadHandler + post /core/syncClusterLoad (syncClusterLoadReq) + + @doc "metrics" + @handler metricsHandler + get /core/metrics } //hpc二级接口 diff --git a/api/internal/handler/core/metricshandler.go b/api/internal/handler/core/metricshandler.go new file mode 100644 index 00000000..fb157525 --- /dev/null +++ b/api/internal/handler/core/metricshandler.go @@ -0,0 +1,12 @@ +package core + +import ( + "github.com/prometheus/client_golang/prometheus/promhttp" + "net/http" + + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" +) + +func MetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return promhttp.Handler().ServeHTTP +} diff --git a/api/internal/handler/core/syncclusterloadhandler.go b/api/internal/handler/core/syncclusterloadhandler.go new file mode 100644 index 00000000..cd8de3a3 --- /dev/null +++ b/api/internal/handler/core/syncclusterloadhandler.go @@ -0,0 +1,25 @@ +package core + +import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/core" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" +) + +func SyncClusterLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.SyncClusterLoadReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := core.NewSyncClusterLoadLogic(r.Context(), svcCtx) + err := l.SyncClusterLoad(&req) + result.HttpResult(r, w, nil, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 0280f82d..f944795a 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -133,6 +133,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/core/centerResources", Handler: core.CenterResourcesHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/core/syncClusterLoad", + Handler: core.SyncClusterLoadHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/core/metrics", + Handler: core.MetricsHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/logic/core/metricslogic.go b/api/internal/logic/core/metricslogic.go new file mode 100644 index 00000000..9f947fe3 --- /dev/null +++ b/api/internal/logic/core/metricslogic.go @@ -0,0 +1,28 @@ +package core + +import ( + "context" + + "github.com/zeromicro/go-zero/core/logx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" +) + +type MetricsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewMetricsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MetricsLogic { + return &MetricsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *MetricsLogic) Metrics() error { + // todo: add your logic here and delete this line + + return nil +} diff --git a/api/internal/logic/core/syncclusterloadlogic.go b/api/internal/logic/core/syncclusterloadlogic.go new file mode 100644 index 00000000..2c0f87e9 --- /dev/null +++ b/api/internal/logic/core/syncclusterloadlogic.go @@ -0,0 +1,35 @@ +package core + +import ( + "context" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker" + + "github.com/zeromicro/go-zero/core/logx" +) + +type SyncClusterLoadLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewSyncClusterLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncClusterLoadLogic { + return &SyncClusterLoadLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error { + if len(req.ClusterLoadRecords) != 0 { + for _, record := range req.ClusterLoadRecords { + tracker.ClusterCpuGauge.WithLabelValues(record.ClusterName).Set(record.CpuUsage) + tracker.ClusterMemoryGauge.WithLabelValues(record.ClusterName).Set(record.MemoryUsage) + tracker.ClusterDiskGauge.WithLabelValues(record.ClusterName).Set(record.DiskUsage) + } + } + return nil +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index bcc6c69c..eb4c203f 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -150,6 +150,17 @@ type CenterIndex struct { CenterType string `json:"centerType"` } +type SyncClusterLoadReq struct { + ClusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"` +} + +type ClusterLoadRecord struct { + ClusterName string `json:"clusterName"` + CpuUsage float64 `json:"cpuUsage"` + MemoryUsage float64 `json:"memoryUsage"` + DiskUsage float64 `json:"diskUsage"` +} + type GetClusterListReq struct { Id int64 `form:"id"` } diff --git a/pkg/tracker/queryoptions.go b/pkg/tracker/queryoptions.go index 06968907..bc0bc9ac 100644 --- a/pkg/tracker/queryoptions.go +++ b/pkg/tracker/queryoptions.go @@ -254,6 +254,7 @@ type ControllerOption struct { Namespace string Kind string WorkloadName string + PodsName string Level string } diff --git a/pkg/tracker/tracker.go b/pkg/tracker/tracker.go index c24b07f9..298d7635 100644 --- a/pkg/tracker/tracker.go +++ b/pkg/tracker/tracker.go @@ -18,6 +18,7 @@ import ( "context" "github.com/prometheus/client_golang/api" v1 "github.com/prometheus/client_golang/api/prometheus/v1" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "strconv" "strings" @@ -25,6 +26,30 @@ import ( "time" ) +var ( + ClusterCpuGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "cluster_cpu_usage", + Help: "Cluster CPU Utilization Rate.", + }, []string{"cluster_name"}) + ClusterMemoryGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "cluster_memory_usage", + Help: "Cluster Memory Utilization Rate.", + }, []string{"cluster_name"}) + ClusterDiskGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "cluster_disk_usage", + Help: "Cluster Disk Utilization Rate.", + }, []string{"cluster_name"}) + metrics = []prometheus.Collector{ + ClusterCpuGauge, + ClusterMemoryGauge, + ClusterDiskGauge, + } +) + +func init() { + prometheus.MustRegister(metrics...) +} + type Prometheus struct { prometheus Interface client v1.API