From 8ba618bfdd2dc155b04b9fa6ff17479c88a8f5e5 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 11 Jun 2024 09:03:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=A4=9A=E4=BD=99=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 94004d05b18036a47773a3d4906aceb86e61e142 --- .../logic/core/centerresourceslogic.go | 8 +- .../logic/core/syncclusterloadlogic.go | 25 ++---- api/internal/scheduler/database/aiStorage.go | 13 +++ pkg/tracker/promql.go | 5 +- pkg/tracker/queryoptions.go | 87 ++----------------- pkg/tracker/tracker.go | 35 ++++++++ 6 files changed, 70 insertions(+), 103 deletions(-) diff --git a/api/internal/logic/core/centerresourceslogic.go b/api/internal/logic/core/centerresourceslogic.go index d1bf0a24..1775f68d 100644 --- a/api/internal/logic/core/centerresourceslogic.go +++ b/api/internal/logic/core/centerresourceslogic.go @@ -46,20 +46,22 @@ func (l *CenterResourcesLogic) CenterResources() (resp *types.CenterResourcesRes for _, centerIndex := range centersIndex { // Query the types of resource centers l.svcCtx.DbEngin.Raw("select name,type as CenterType from t_adapter where id = ?", centerIndex.Id).Scan(¢erIndex) - cpuRawData, err := l.svcCtx.PromClient.GetRawData("center_cpu_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) + var clustersName string + l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(name SEPARATOR '|' ) as clustersName from t_cluster where adapter_id = ?", centerIndex.Id).Scan(&clustersName) + cpuRawData, err := l.svcCtx.PromClient.GetRawData("center_cpu_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName}) cpuData := cpuRawData.(model.Vector) if err != nil { return nil, err } centerIndex.Cpu = cpuData[0].Value.String() - memoryRawData, err := l.svcCtx.PromClient.GetRawData("center_memory_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) + memoryRawData, err := l.svcCtx.PromClient.GetRawData("center_memory_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName}) if err != nil { return nil, err } memoryData := memoryRawData.(model.Vector) centerIndex.Memory = memoryData[0].Value.String() - diskRawData, err := l.svcCtx.PromClient.GetRawData("center_disk_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) + diskRawData, err := l.svcCtx.PromClient.GetRawData("center_disk_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName}) if err != nil { return nil, err } diff --git a/api/internal/logic/core/syncclusterloadlogic.go b/api/internal/logic/core/syncclusterloadlogic.go index abad70e5..034fc176 100644 --- a/api/internal/logic/core/syncclusterloadlogic.go +++ b/api/internal/logic/core/syncclusterloadlogic.go @@ -2,12 +2,11 @@ package core import ( "context" + "github.com/zeromicro/go-zero/core/logx" "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" - "strconv" - - "github.com/zeromicro/go-zero/core/logx" + tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" ) type SyncClusterLoadLogic struct { @@ -25,23 +24,11 @@ func NewSyncClusterLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *S } func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error { - if len(req.ClusterLoadRecords) != 0 { + if nil != req.ClusterLoadRecords { for _, record := range req.ClusterLoadRecords { - tracker.ClusterCpuUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuUtilisation) - tracker.ClusterCpuAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuAvail) - tracker.ClusterCpuTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuTotal) - - tracker.ClusterMemoryUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryUtilisation) - tracker.ClusterMemoryAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryAvail) - tracker.ClusterMemoryTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryTotal) - - tracker.ClusterDiskUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskUtilisation) - tracker.ClusterDiskAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskAvail) - tracker.ClusterDiskTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskTotal) - - tracker.ClusterPodUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.PodsUtilisation) - tracker.ClusterPodCountGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsCount)) - tracker.ClusterPodTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsTotal)) + var param tracker.ClusterLoadRecord + tool.Convert(record, ¶m) + tracker.SyncClusterLoad(param) } } return nil diff --git a/api/internal/scheduler/database/aiStorage.go b/api/internal/scheduler/database/aiStorage.go index 8b0ad7ca..d99b8690 100644 --- a/api/internal/scheduler/database/aiStorage.go +++ b/api/internal/scheduler/database/aiStorage.go @@ -6,6 +6,7 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker" "gorm.io/gorm" "strconv" "time" @@ -211,6 +212,18 @@ func (s *AiStorage) SaveClusterResources(clusterId string, clusterName string, c if tx.Error != nil { return tx.Error } + + // prometheus + param := tracker.ClusterLoadRecord{ + ClusterName: clusterName, + CpuAvail: cpuAvail, + CpuTotal: cpuTotal, + MemoryAvail: memAvail, + MemoryTotal: memTotal, + DiskAvail: diskAvail, + DiskTotal: diskTotal, + } + tracker.SyncClusterLoad(param) return nil } diff --git a/pkg/tracker/promql.go b/pkg/tracker/promql.go index ce84be43..a80657db 100644 --- a/pkg/tracker/promql.go +++ b/pkg/tracker/promql.go @@ -85,8 +85,6 @@ func makeExpr(metric string, opts QueryOptions) string { return makeContainerMetricExpr(tmpl, opts) case LevelPVC: return makePVCMetricExpr(tmpl, opts) - case LevelIngress: - return makeIngressMetricExpr(tmpl, opts) case LevelComponent: return tmpl default: @@ -108,6 +106,9 @@ func makeAdapterMetricExpr(tmpl string, o QueryOptions) string { if o.AdapterId != 0 { adapterSelector = fmt.Sprintf(`adapter_id="%d"`, o.AdapterId) } + if len(o.ClustersName) != 0 { + adapterSelector = fmt.Sprintf(`adapter_id="%d"`, o.AdapterId) + } return strings.Replace(tmpl, "$1", adapterSelector, -1) } diff --git a/pkg/tracker/queryoptions.go b/pkg/tracker/queryoptions.go index 2ec9588f..d1e7004a 100644 --- a/pkg/tracker/queryoptions.go +++ b/pkg/tracker/queryoptions.go @@ -29,12 +29,10 @@ const ( LevelNamespace LevelApplication LevelController - LevelService LevelPod LevelContainer LevelPVC LevelComponent - LevelIngress LevelAdapter ) @@ -46,7 +44,6 @@ var MeteringLevelMap = map[string]int{ "LevelNamespace": LevelNamespace, "LevelApplication": LevelApplication, "LevelController": LevelController, - "LevelService": LevelService, "LevelPod": LevelPod, "LevelContainer": LevelContainer, "LevelPVC": LevelPVC, @@ -57,15 +54,15 @@ type QueryOption interface { Apply(*QueryOptions) } -type Meteroptions struct { +type MeterOptions struct { Start time.Time End time.Time Step time.Duration } type QueryOptions struct { - Level Level - + Level Level + ClustersName string NamespacedResourcesFilter string QueryType string ResourceFilter string @@ -81,10 +78,9 @@ type QueryOptions struct { ContainerName string AdapterId int64 ServiceName string - Ingress string Job string Duration *time.Duration - MeterOptions *Meteroptions + MeterOptions *MeterOptions } func NewQueryOptions() *QueryOptions { @@ -92,12 +88,14 @@ func NewQueryOptions() *QueryOptions { } type AdapterOption struct { - AdapterId int64 + AdapterId int64 + ClustersName string } func (a AdapterOption) Apply(o *QueryOptions) { o.Level = LevelAdapter o.AdapterId = a.AdapterId + o.ClustersName = a.ClustersName } type ClusterOption struct { @@ -154,31 +152,6 @@ func (no NamespaceOption) Apply(o *QueryOptions) { o.Namespace = no.NamespaceName } -type ApplicationsOption struct { - NamespaceName string - Applications []string - StorageClassName string -} - -func (aso ApplicationsOption) Apply(o *QueryOptions) { - // nothing should be done - //nolint:gosimple - return -} - -type OpenpitrixsOption struct { - Cluster string - NamespaceName string - Openpitrixs []string - StorageClassName string -} - -func (oso OpenpitrixsOption) Apply(o *QueryOptions) { - // nothing should be done - //nolint:gosimple - return -} - // ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct type ApplicationOption struct { NamespaceName string @@ -213,17 +186,6 @@ func (wo WorkloadOption) Apply(o *QueryOptions) { o.WorkloadKind = wo.WorkloadKind } -type ServicesOption struct { - NamespaceName string - Services []string -} - -func (sso ServicesOption) Apply(o *QueryOptions) { - // nothing should be done - //nolint:gosimple - return -} - type ServiceOption struct { ResourceFilter string NamespaceName string @@ -231,20 +193,6 @@ type ServiceOption struct { PodNames []string } -func (so ServiceOption) Apply(o *QueryOptions) { - o.Level = LevelService - o.Namespace = so.NamespaceName - o.ServiceName = so.ServiceName - - pod_names := strings.Join(so.PodNames, "|") - - if len(pod_names) > 0 { - o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, pod_names, o.Namespace) - } else { - o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, ".*", o.Namespace) - } -} - type PodOption struct { NamespacedResourcesFilter string ResourceFilter string @@ -310,25 +258,6 @@ func (po PVCOption) Apply(o *QueryOptions) { o.Namespace = po.NamespaceName } -type IngressOption struct { - ResourceFilter string - NamespaceName string - Ingress string - Job string - Pod string - Duration *time.Duration -} - -func (no IngressOption) Apply(o *QueryOptions) { - o.Level = LevelIngress - o.ResourceFilter = no.ResourceFilter - o.Namespace = no.NamespaceName - o.Ingress = no.Ingress - o.Job = no.Job - o.PodName = no.Pod - o.Duration = no.Duration -} - type ComponentOption struct{} func (_ ComponentOption) Apply(o *QueryOptions) { @@ -342,7 +271,7 @@ type MeterOption struct { } func (mo MeterOption) Apply(o *QueryOptions) { - o.MeterOptions = &Meteroptions{ + o.MeterOptions = &MeterOptions{ Start: mo.Start, End: mo.End, Step: mo.Step, diff --git a/pkg/tracker/tracker.go b/pkg/tracker/tracker.go index 7941f74b..2e8384a6 100644 --- a/pkg/tracker/tracker.go +++ b/pkg/tracker/tracker.go @@ -107,6 +107,23 @@ var ( } ) +type ClusterLoadRecord struct { + AdapterId int64 `json:"adapterId,optional"` + ClusterName string `json:"clusterName,optional"` + CpuAvail float64 `json:"cpuAvail,optional"` + CpuTotal float64 `json:"cpuTotal,optional"` + CpuUtilisation float64 `json:"cpuUtilisation,optional"` + MemoryAvail float64 `json:"memoryAvail,optional"` + MemoryUtilisation float64 `json:"memoryUtilisation,optional"` + MemoryTotal float64 `json:"memoryTotal,optional"` + DiskAvail float64 `json:"diskAvail,optional"` + DiskTotal float64 `json:"diskTotal,optional"` + DiskUtilisation float64 `json:"diskUtilisation,optional"` + PodsUtilisation float64 `json:"podsUtilisation,optional"` + PodsCount int64 `json:"podsCount,optional"` + PodsTotal int64 `json:"podsTotal,optional"` +} + func init() { prometheus.MustRegister(metrics...) } @@ -302,3 +319,21 @@ func (p Prometheus) GetRawData(expr string, o QueryOption) (model.Value, error) } return value, nil } + +func SyncClusterLoad(record ClusterLoadRecord) { + ClusterCpuUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuUtilisation) + ClusterCpuAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuAvail) + ClusterCpuTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuTotal) + + ClusterMemoryUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryUtilisation) + ClusterMemoryAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryAvail) + ClusterMemoryTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryTotal) + + ClusterDiskUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskUtilisation) + ClusterDiskAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskAvail) + ClusterDiskTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskTotal) + + ClusterPodUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.PodsUtilisation) + ClusterPodCountGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsCount)) + ClusterPodTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsTotal)) +}