From 192b81fc4db1f52291790a212a69ad785f760c32 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 26 Mar 2024 11:37:29 +0800 Subject: [PATCH] fix(obcluster): prevent negtive remain resource --- .../business/oceanbase/obcluster_usage.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/dashboard/business/oceanbase/obcluster_usage.go b/internal/dashboard/business/oceanbase/obcluster_usage.go index 03448dd96..be3d9ebd6 100644 --- a/internal/dashboard/business/oceanbase/obcluster_usage.go +++ b/internal/dashboard/business/oceanbase/obcluster_usage.go @@ -13,6 +13,7 @@ See the Mulan PSL v2 for more details. package oceanbase import ( + "cmp" "context" "k8s.io/apimachinery/pkg/api/resource" @@ -94,10 +95,10 @@ func getServerUsages(gvservers []model.GVOBServer) ([]response.OBServerAvailable zoneResource := &response.OBZoneAvaiableResource{ ServerCount: 1, OBZone: gvserver.Zone, - AvailableCPU: gvserver.CPUCapacity - gvserver.CPUAssigned, - AvailableMemory: gvserver.MemCapacity - gvserver.MemAssigned, - AvailableLogDisk: gvserver.LogDiskCapacity - gvserver.LogDiskAssigned, - AvailableDataDisk: gvserver.DataDiskCapacity - gvserver.DataDiskAllocated, + AvailableCPU: max(gvserver.CPUCapacity-gvserver.CPUAssigned, 0), + AvailableMemory: max(gvserver.MemCapacity-gvserver.MemAssigned, 0), + AvailableLogDisk: max(gvserver.LogDiskCapacity-gvserver.LogDiskAssigned, 0), + AvailableDataDisk: max(gvserver.DataDiskCapacity-gvserver.DataDiskAllocated, 0), } serverUsage := response.OBServerAvailableResource{ OBServerIP: gvserver.ServerIP, @@ -124,3 +125,10 @@ func getServerUsages(gvservers []model.GVOBServer) ([]response.OBServerAvailable } return serverUsages, zoneMapping } + +func max[t cmp.Ordered](a, b t) t { + if a > b { + return a + } + return b +}