From 5d80d187a5644cf0fc245301de37b1467387b99e Mon Sep 17 00:00:00 2001 From: zeaslity Date: Sun, 28 Apr 2024 16:55:18 +0800 Subject: [PATCH] [Agent][GO] - optimize agent initialization --- .run/agent-go main.run.xml | 3 +-- agent-common/utils/StringUtils.go | 34 ++++++++++++++++++++++++++ agent-go/a_init/AgentInitialization.go | 14 +++++------ agent-operator/CmiiK8sOperator_test.go | 7 ++++-- cmii_operator/log/cmii-update-log.txt | 5 ++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/.run/agent-go main.run.xml b/.run/agent-go main.run.xml index 6c14cc7..78a18cf 100644 --- a/.run/agent-go main.run.xml +++ b/.run/agent-go main.run.xml @@ -2,9 +2,8 @@ - - + diff --git a/agent-common/utils/StringUtils.go b/agent-common/utils/StringUtils.go index ad85839..54f9f98 100644 --- a/agent-common/utils/StringUtils.go +++ b/agent-common/utils/StringUtils.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "math/rand" "time" ) @@ -15,3 +16,36 @@ func GenerateRandomString(length int) string { } return string(b) } + +func ByteSizeToString(size uint64) string { + const ( + B = 1 + KB = 1024 * B + MB = 1024 * KB + GB = 1024 * MB + TB = 1024 * GB + ) + + var unit string + var value float64 + + switch { + case size >= TB: + value = float64(size) / TB + unit = "TB" + case size >= GB: + value = float64(size) / GB + unit = "GB" + case size >= MB: + value = float64(size) / MB + unit = "MB" + case size >= KB: + value = float64(size) / KB + unit = "KB" + default: + value = float64(size) + unit = "B" + } + + return fmt.Sprintf("%.2f %s", value, unit) +} diff --git a/agent-go/a_init/AgentInitialization.go b/agent-go/a_init/AgentInitialization.go index c28553f..29febd5 100644 --- a/agent-go/a_init/AgentInitialization.go +++ b/agent-go/a_init/AgentInitialization.go @@ -253,14 +253,14 @@ func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo // cpu part agentServerInfo.CPUCore = strconv.FormatInt(int64(agentInfo.CPUInfo.NumCores), 10) if len(agentInfo.CPUInfo.CPUInfo) > 0 { - marshal, _ := json.Marshal(agentInfo.CPUInfo.CPUInfo[0]) + marshal, _ := json.Marshal(agentInfo.CPUInfo.CPUInfo[0].ModelName) agentServerInfo.CPUBrand = string(marshal) } // os info - agentServerInfo.OSInfo = agentInfo.HostInfo.PlatformFamily + agentInfo.HostInfo.Platform + agentInfo.HostInfo.PlatformVersion + agentServerInfo.OSInfo = agentInfo.HostInfo.PlatformFamily + " " + agentInfo.HostInfo.Platform + " " + agentInfo.HostInfo.PlatformVersion agentServerInfo.OSKernelInfo = agentInfo.HostInfo.KernelVersion - agentServerInfo.Virtualization = agentInfo.HostInfo.VirtualizationSystem + agentInfo.HostInfo.VirtualizationRole + agentServerInfo.Virtualization = agentInfo.HostInfo.VirtualizationSystem + " " + agentInfo.HostInfo.VirtualizationRole agentServerInfo.Platform = agentInfo.HostInfo.Platform agentServerInfo.PlatformFamily = agentInfo.HostInfo.PlatformFamily agentServerInfo.PlatformVersion = agentInfo.HostInfo.PlatformVersion @@ -268,8 +268,8 @@ func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo agentServerInfo.KernelArch = agentInfo.HostInfo.KernelArch // memory part - agentServerInfo.MemoryTotal = strconv.FormatInt(int64(agentInfo.MemoryInfo.TotalMemory), 10) - agentServerInfo.SwapTotal = strconv.FormatInt(int64(agentInfo.MemoryInfo.SwapTotal), 10) + agentServerInfo.MemoryTotal = utils.ByteSizeToString(agentInfo.MemoryInfo.TotalMemory) + agentServerInfo.SwapTotal = utils.ByteSizeToString(agentInfo.MemoryInfo.SwapTotal) // disk part info := agentInfo.DiskInfo @@ -282,8 +282,8 @@ func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo diskUsage = diskInfo.Used } } - agentServerInfo.DiskTotal = strconv.FormatUint(diskTotal, 10) - agentServerInfo.DiskUsage = strconv.FormatUint(diskUsage, 10) + agentServerInfo.DiskTotal = utils.ByteSizeToString(diskTotal) + agentServerInfo.DiskUsage = utils.ByteSizeToString(diskUsage) // network part refreshAgentNetworkInfo(agentInfo, agentServerInfo) diff --git a/agent-operator/CmiiK8sOperator_test.go b/agent-operator/CmiiK8sOperator_test.go index 5f55246..1a1e26f 100644 --- a/agent-operator/CmiiK8sOperator_test.go +++ b/agent-operator/CmiiK8sOperator_test.go @@ -252,7 +252,7 @@ func TestUpdateCmiiDeploymentImageTag(t *testing.T) { // 计算20:00的时间 now := time.Now() - targetTime := time.Date(now.Year(), now.Month(), now.Day(), 17, 45, 00, 0, now.Location()) + targetTime := time.Date(now.Year(), now.Month(), now.Day(), 12, 03, 00, 0, now.Location()) duration := time.Duration(0) @@ -275,7 +275,10 @@ func TestUpdateCmiiDeploymentImageTag(t *testing.T) { appNameTagMap := map[string]string{ //"cmii-uav-multilink": "5.5.0", - "cmii-uav-data-post-process": "5.5.0-042501", + "cmii-uav-platform-cms-portal": "5.5.0-042801", + "cmii-uav-platform": "5.5.0-042801", + "cmii-uav-platform-oms": "5.5.0-042801", + "cmii-uav-user": "5.5.0-042801", } for appName, newTag := range appNameTagMap { diff --git a/cmii_operator/log/cmii-update-log.txt b/cmii_operator/log/cmii-update-log.txt index c2b0845..4aa8948 100644 --- a/cmii_operator/log/cmii-update-log.txt +++ b/cmii_operator/log/cmii-update-log.txt @@ -21,3 +21,8 @@ 2024-04-25-17-42-00 uavcloud-demo cmii-uav-platform 5.5.0-042501 5.5.0-042503 2024-04-25-17-42-06 uavcloud-demo cmii-uav-platform-splice 5.5.0 5.5.0-042501 2024-04-25-17-45-00 uavcloud-demo cmii-uav-data-post-process 5.5.0 5.5.0-042501 +2024-04-26-17-55-00 uavcloud-demo cmii-uav-platform-splice 5.5.0-042501 5.5.0-042601 +2024-04-28-12-03-00 uavcloud-demo cmii-uav-platform-cms-portal 5.5.0 5.5.0-042801 +2024-04-28-12-03-05 uavcloud-demo cmii-uav-platform 5.5.0-042503 5.5.0-042801 +2024-04-28-12-03-10 uavcloud-demo cmii-uav-platform-oms 5.5.0 5.5.0-042801 +2024-04-28-12-03-13 uavcloud-demo cmii-uav-user 5.5.0 5.5.0-042801