Use private dup methods instead

This commit is contained in:
Harry Zhang 2017-02-24 15:33:36 +08:00
parent 953c9fa6bb
commit 658e7053f8
2 changed files with 28 additions and 12 deletions

View File

@ -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
}

View File

@ -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 {