diff --git a/pkg/api/resource_helpers.go b/pkg/api/resource_helpers.go index f960241a83b..74da82ef622 100644 --- a/pkg/api/resource_helpers.go +++ b/pkg/api/resource_helpers.go @@ -243,28 +243,28 @@ func ExtractContainerResourceValue(fs *ResourceFieldSelector, container *Contain switch fs.Resource { case "limits.cpu": - return ConvertResourceCPUToString(container.Resources.Limits.Cpu(), divisor) + return convertResourceCPUToString(container.Resources.Limits.Cpu(), divisor) case "limits.memory": - return ConvertResourceMemoryToString(container.Resources.Limits.Memory(), divisor) + return convertResourceMemoryToString(container.Resources.Limits.Memory(), divisor) case "requests.cpu": - return ConvertResourceCPUToString(container.Resources.Requests.Cpu(), divisor) + return convertResourceCPUToString(container.Resources.Requests.Cpu(), divisor) case "requests.memory": - return ConvertResourceMemoryToString(container.Resources.Requests.Memory(), divisor) + return convertResourceMemoryToString(container.Resources.Requests.Memory(), divisor) } return "", fmt.Errorf("unsupported container resource : %v", fs.Resource) } -// ConvertResourceCPUToString converts cpu value to the format of divisor and returns +// convertResourceCPUToString converts cpu value to the format of divisor and returns // ceiling of the value. -func ConvertResourceCPUToString(cpu *resource.Quantity, divisor resource.Quantity) (string, error) { +func convertResourceCPUToString(cpu *resource.Quantity, divisor resource.Quantity) (string, error) { c := int64(math.Ceil(float64(cpu.MilliValue()) / float64(divisor.MilliValue()))) return strconv.FormatInt(c, 10), nil } -// ConvertResourceMemoryToString converts memory value to the format of divisor and returns +// convertResourceMemoryToString converts memory value to the format of divisor and returns // ceiling of the value. -func ConvertResourceMemoryToString(memory *resource.Quantity, divisor resource.Quantity) (string, error) { +func convertResourceMemoryToString(memory *resource.Quantity, divisor resource.Quantity) (string, error) { m := int64(math.Ceil(float64(memory.Value()) / float64(divisor.Value()))) return strconv.FormatInt(m, 10), nil } diff --git a/pkg/api/v1/resource_helpers.go b/pkg/api/v1/resource_helpers.go index 78b12303e5a..cc769d12eb7 100644 --- a/pkg/api/v1/resource_helpers.go +++ b/pkg/api/v1/resource_helpers.go @@ -18,6 +18,8 @@ package v1 import ( "fmt" + "math" + "strconv" "time" "k8s.io/apimachinery/pkg/api/resource" @@ -303,18 +305,32 @@ func ExtractContainerResourceValue(fs *ResourceFieldSelector, container *Contain switch fs.Resource { case "limits.cpu": - return api.ConvertResourceCPUToString(container.Resources.Limits.Cpu(), divisor) + return convertResourceCPUToString(container.Resources.Limits.Cpu(), divisor) case "limits.memory": - return api.ConvertResourceMemoryToString(container.Resources.Limits.Memory(), divisor) + return convertResourceMemoryToString(container.Resources.Limits.Memory(), divisor) case "requests.cpu": - return api.ConvertResourceCPUToString(container.Resources.Requests.Cpu(), divisor) + return convertResourceCPUToString(container.Resources.Requests.Cpu(), divisor) case "requests.memory": - return api.ConvertResourceMemoryToString(container.Resources.Requests.Memory(), divisor) + return convertResourceMemoryToString(container.Resources.Requests.Memory(), divisor) } return "", fmt.Errorf("Unsupported container resource : %v", fs.Resource) } +// convertResourceCPUToString converts cpu value to the format of divisor and returns +// ceiling of the value. +func convertResourceCPUToString(cpu *resource.Quantity, divisor resource.Quantity) (string, error) { + c := int64(math.Ceil(float64(cpu.MilliValue()) / float64(divisor.MilliValue()))) + return strconv.FormatInt(c, 10), nil +} + +// convertResourceMemoryToString converts memory value to the format of divisor and returns +// ceiling of the value. +func convertResourceMemoryToString(memory *resource.Quantity, divisor resource.Quantity) (string, error) { + m := int64(math.Ceil(float64(memory.Value()) / float64(divisor.Value()))) + return strconv.FormatInt(m, 10), nil +} + // findContainerInPod finds a container by its name in the provided pod func findContainerInPod(pod *Pod, containerName string) (*Container, error) { for _, container := range pod.Spec.Containers {