@@ -42,6 +42,7 @@ import (
4242 "k8s.io/client-go/kubernetes"
4343 resourcehelper "k8s.io/kubectl/pkg/util/resource"
4444 metrics "k8s.io/metrics/pkg/client/clientset/versioned"
45+ "math"
4546 "net/http"
4647 "strings"
4748 "time"
@@ -931,30 +932,51 @@ func getResourceString(quantity resource.Quantity, resourceName corev1.ResourceN
931932 //not a standard resource, we do not know if conversion would be valid or not
932933 //for example - pods: "250", this is not in bytes but an integer so conversion is invalid
933934 return quantity .String ()
934- } else {
935+ // exempting CPU resource as CPU's resource unit is in cores
936+ } else if resourceName != corev1 .ResourceCPU {
935937 var quantityStr string
936938 value := quantity .Value ()
937- valueGi := value / bean .Gibibyte
938- //allowing remainder 0 only, because for Gi rounding off will be highly erroneous
939- if valueGi > 1 && value % bean .Gibibyte == 0 {
940- quantityStr = fmt .Sprintf ("%dGi" , valueGi )
941- } else {
942- valueMi := value / bean .Mebibyte
943- if valueMi > 10 {
944- if value % bean .Mebibyte != 0 {
945- valueMi ++
946- }
947- quantityStr = fmt .Sprintf ("%dMi" , valueMi )
948- } else if value > 1000 {
949- valueKi := value / bean .Kibibyte
950- if value % bean .Kibibyte != 0 {
951- valueKi ++
952- }
953- quantityStr = fmt .Sprintf ("%dKi" , valueKi )
939+ //allowing remainder 0 only, because for Gi rounding off will be highly erroneous -
940+ //despite rounding allowing decimal value upto 2 decimal places
941+
942+ // first check for Gi
943+ valueGi := float64 (value ) / (bean .Gibibyte * 1.0 )
944+ if valueGi >= 1 {
945+ if valueGi == math .Floor (valueGi ) { // if the converted value is a whole number
946+ quantityStr = fmt .Sprintf ("%dGi" , int64 (valueGi ))
954947 } else {
955- quantityStr = fmt .Sprintf ("%dm" , quantity .MilliValue ())
948+ quantityStr = fmt .Sprintf ("%.2fGi" , valueGi )
949+ }
950+ } else if value >= bean .Mebibyte { // fall back to check for Mi
951+ valueMi := value / bean .Mebibyte
952+ if value % bean .Mebibyte != 0 {
953+ valueMi ++
954+ }
955+ quantityStr = fmt .Sprintf ("%dMi" , valueMi )
956+ } else if value >= bean .Kibibyte { // fall back to check for Ki
957+ valueKi := value / bean .Kibibyte
958+ if value % bean .Kibibyte != 0 {
959+ valueKi ++
956960 }
961+ quantityStr = fmt .Sprintf ("%dKi" , valueKi )
962+ } else { // else better to show in Bytes
963+ quantityStr = fmt .Sprintf ("%dB" , value )
964+ }
965+ return quantityStr
966+ } else {
967+ var quantityStr string
968+ cpuValueMilli := quantity .MilliValue () // it is safe to use MilliValue here as in real world the value would not exceed int64 range
969+ cpuValueCore := float64 (cpuValueMilli ) / 1000.0
970+ quantityStr = fmt .Sprintf ("%.2f" , cpuValueCore )
971+ // if cpuValueCore is less than 1 then show in milli core only
972+ if cpuValueCore < 1 {
973+ return fmt .Sprintf ("%dm" , cpuValueMilli )
974+ }
975+ // if the core value is a whole number then returning int else float
976+ if cpuValueCore == math .Floor (cpuValueCore ) {
977+ return fmt .Sprintf ("%d" , int64 (cpuValueCore ))
957978 }
979+ // showing values in cores upto 2 decimal value
958980 return quantityStr
959981 }
960982}
0 commit comments