From 859305b517fa5a61ebde88cc40bbbb7200622dd9 Mon Sep 17 00:00:00 2001 From: jeff Date: Fri, 29 Jun 2018 21:10:14 +0800 Subject: [PATCH] fix resource limit --- pkg/models/metrics/containers.go | 8 ++++---- pkg/models/metrics/pods.go | 16 ++++++++++++---- pkg/models/routes.go | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/models/metrics/containers.go b/pkg/models/metrics/containers.go index 63fa1590a..d734e7dde 100644 --- a/pkg/models/metrics/containers.go +++ b/pkg/models/metrics/containers.go @@ -83,7 +83,7 @@ func FormatContainerMetrics(namespace, podName, containerName string) ContainerM cpuRequestMetrics, err := cpuRequest.GetObjectArray("metrics") if err == nil && len(cpuRequestMetrics) != 0 { requestCpu, _ := cpuRequestMetrics[0].GetFloat64("value") - resultContainer.CpuRequest = fmt.Sprintf("%.1f", requestCpu) + resultContainer.CpuRequest = FormatResourceLimit(requestCpu) } else { resultContainer.CpuRequest = Inf } @@ -92,7 +92,7 @@ func FormatContainerMetrics(namespace, podName, containerName string) ContainerM cpuLimitMetrics, err := cpuLimit.GetObjectArray("metrics") if err == nil && len(cpuLimitMetrics) != 0 { limitCpu, _ := cpuLimitMetrics[0].GetFloat64("value") - resultContainer.CpuLimit = fmt.Sprintf("%.1f", limitCpu) + resultContainer.CpuLimit = FormatResourceLimit(limitCpu) } else { resultContainer.CpuLimit = Inf } @@ -101,7 +101,7 @@ func FormatContainerMetrics(namespace, podName, containerName string) ContainerM memoryRequstMetrics, err := memoryRequst.GetObjectArray("metrics") if err == nil && len(memoryRequstMetrics) != 0 { requestMemory, _ := memoryRequstMetrics[0].GetFloat64("value") - resultContainer.MemoryRequest = fmt.Sprintf("%.1f", requestMemory) + resultContainer.MemoryRequest = FormatResourceLimit(requestMemory) } else { resultContainer.MemoryRequest = Inf } @@ -110,7 +110,7 @@ func FormatContainerMetrics(namespace, podName, containerName string) ContainerM memoryLimitMetrics, err := memoryLimit.GetObjectArray("metrics") if err == nil && len(memoryLimitMetrics) != 0 { limitMemory, _ := memoryLimitMetrics[0].GetFloat64("value") - resultContainer.MemoryLimit = fmt.Sprintf("%.1f", limitMemory) + resultContainer.MemoryLimit = FormatResourceLimit(limitMemory) } else { resultContainer.MemoryLimit = Inf } diff --git a/pkg/models/metrics/pods.go b/pkg/models/metrics/pods.go index 7b7a710dc..fa1140f50 100644 --- a/pkg/models/metrics/pods.go +++ b/pkg/models/metrics/pods.go @@ -228,6 +228,14 @@ func GetAllPodMetrics() constants.PageableResponse { return podMetrics } +func FormatResourceLimit(limit float64) string { + if limit <= 0 { + return Inf + } else { + return fmt.Sprintf("%.1f", limit) + } +} + func FormatPodMetrics(namespace string, pod string) PodMetrics { var resultPod PodMetrics @@ -260,8 +268,8 @@ func FormatPodMetrics(namespace string, pod string) PodMetrics { if len(cpuLimitMetrics) == 0 { resultPod.CPULimit = Inf } else { - data, _ := cpuLimitMetrics[0].GetNumber("value") - resultPod.CPULimit = data.String() + data, _ := cpuLimitMetrics[0].GetFloat64("value") + resultPod.CPULimit = FormatResourceLimit(data) } memoryRequest := client.GetHeapsterMetricsJson("/namespaces/" + namespace + "/pods/" + pod + "/metrics/memory/request") @@ -276,7 +284,7 @@ func FormatPodMetrics(namespace string, pod string) PodMetrics { data, _ := memoryRequestMetrics[0].GetNumber("value") memoryReq, _ := data.Float64() memoryReq = memoryReq / 1024 / 1024 - resultPod.MemoryRequest = fmt.Sprintf("%.1f", memoryReq) + resultPod.MemoryRequest = FormatResourceLimit(memoryReq) } memoryLimit := client.GetHeapsterMetricsJson("/namespaces/" + namespace + "/pods/" + pod + "/metrics/memory/limit") @@ -288,7 +296,7 @@ func FormatPodMetrics(namespace string, pod string) PodMetrics { data, _ := memoryLimitMetrics[0].GetNumber("value") memoryLim, _ := data.Float64() memoryLim = memoryLim / 1024 / 1024 - resultPod.MemoryLimit = fmt.Sprintf("%.1f", memoryLim) + resultPod.MemoryLimit = FormatResourceLimit(memoryLim) } cpuUsageRate := client.GetHeapsterMetricsJson("/namespaces/" + namespace + "/pods/" + pod + "/metrics/cpu/usage_rate") diff --git a/pkg/models/routes.go b/pkg/models/routes.go index 4d31ac9ce..21a95cf9f 100644 --- a/pkg/models/routes.go +++ b/pkg/models/routes.go @@ -76,7 +76,7 @@ func GetAllRoutersOfUser(username string) ([]coreV1.Service, error) { allNamespace, namespaces, err := iam.GetUserNamespaces(username, v1.PolicyRule{ Verbs: []string{"get", "list"}, APIGroups: []string{"extensions"}, - Resources: []string{"ingresses"}, + Resources: []string{"services"}, }) // return by cluster role