This commit is contained in:
zhangwei 2024-04-20 16:25:25 +08:00
parent 2b1496e501
commit bf8248a1b4
2 changed files with 10 additions and 6 deletions

View File

@ -28,6 +28,8 @@ type ClusterLoadRecord struct {
DiskTotal float64 `json:"diskTotal"` DiskTotal float64 `json:"diskTotal"`
DiskUtilisation float64 `json:"diskUtilisation"` DiskUtilisation float64 `json:"diskUtilisation"`
PodsUtilisation float64 `json:"podsUtilisation"` PodsUtilisation float64 `json:"podsUtilisation"`
PodsCount int64 `json:"podsCount"`
PodsTotal int64 `json:"podsTotal"`
} }
type SyncClusterAlertReq struct { type SyncClusterAlertReq struct {
@ -90,10 +92,12 @@ func syncClusterLoadRecords() {
} }
// pod utilisation // pod utilisation
if _, ok := apiserver.ApiServer.ClientSetMap[clusterName]; ok { if _, ok := apiserver.ApiServer.ClientSetMap[clusterName]; ok {
podUtilisation, err := v1.PodUtilisation(apiserver.ApiServer.ClientSetMap[clusterName]) podUtilisation, count, total, err := v1.PodUtilisation(apiserver.ApiServer.ClientSetMap[clusterName])
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
} }
clusterResourceLoad.PodsCount = count
clusterResourceLoad.PodsTotal = total
clusterResourceLoad.PodsUtilisation = podUtilisation clusterResourceLoad.PodsUtilisation = podUtilisation
} }

View File

@ -9,13 +9,13 @@ import (
"time" "time"
) )
func PodUtilisation(client *kubernetes.Clientset) (Utilisation float64, err error) { func PodUtilisation(client *kubernetes.Clientset) (Utilisation float64, count int64, total int64, err error) {
start := time.Now() start := time.Now()
var maxPods int64 var maxPods int64
nodeList, err := client.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{}) nodeList, err := client.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil { if err != nil {
logx.Error(err, "Failed to get node list") logx.Error(err, "Failed to get node list")
return float64(0), err return float64(0), 0, 0, err
} }
if len(nodeList.Items) != 0 { if len(nodeList.Items) != 0 {
for _, node := range nodeList.Items { for _, node := range nodeList.Items {
@ -25,13 +25,13 @@ func PodUtilisation(client *kubernetes.Clientset) (Utilisation float64, err erro
podList, err := client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{}) podList, err := client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
if err != nil { if err != nil {
logx.Error(err, "Failed to get pod list") logx.Error(err, "Failed to get pod list")
return float64(0), err return float64(0), 0, 0, err
} }
if len(podList.Items) == 0 { if len(podList.Items) == 0 {
return float64(0), nil return float64(0), 0, 0, nil
} }
end := time.Now() end := time.Now()
duration := end.Sub(start) duration := end.Sub(start)
fmt.Printf("请求耗时: %s\n", duration) fmt.Printf("请求耗时: %s\n", duration)
return float64(len(podList.Items)) / float64(maxPods), nil return float64(len(podList.Items)) / float64(maxPods), int64(len(podList.Items)), maxPods, nil
} }