diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go index bc07bd934b1a..b7eaa87eba90 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go @@ -24,6 +24,7 @@ import ( "github.com/pkg/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/autoscaler/cluster-autoscaler/utils/units" ) const ( @@ -185,16 +186,16 @@ func parseCPUCapacity(annotations map[string]string) (resource.Quantity, error) } func parseMemoryCapacity(annotations map[string]string) (resource.Quantity, error) { - // the value for the memoryKey is expected to have the unit type included, - // eg "1024Mi". if only a number is present, we add the suffix "Mi". + // The value for the memoryKey is expected to be an integer representing Mebibytes. e.g. "1024". + // https://www.iec.ch/si/binary.htm val, exists := annotations[memoryKey] if exists && val != "" { - // TODO remove this check once we ensured that the providers are using the correct values - if _, err := strconv.Atoi(val); err == nil { - // value is a number, we will append "Mi" as the unit type - val = fmt.Sprintf("%sMi", val) + valInt, err := strconv.ParseInt(val, 10, 0) + if err != nil { + return zeroQuantity.DeepCopy(), fmt.Errorf("value %q from annotation %q expected to be an integer: %v", val, memoryKey, err) } - return resource.ParseQuantity(val) + // Convert from Mebibytes to bytes + return *resource.NewQuantity(valInt*units.MiB, resource.DecimalSI), nil } return zeroQuantity.DeepCopy(), nil } diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go index 78bd802d44b7..ed1b6b42feee 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go @@ -22,6 +22,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/autoscaler/cluster-autoscaler/utils/units" ) const ( @@ -513,20 +514,20 @@ func TestParseMemoryCapacity(t *testing.T) { expectedQuantity: zeroQuantity.DeepCopy(), expectedError: true, }, { - description: "valid quantity without unit type", + description: "valid quantity", annotations: map[string]string{memoryKey: "456"}, expectedError: false, - expectedQuantity: resource.MustParse("456Mi"), + expectedQuantity: *resource.NewQuantity(456*units.MiB, resource.DecimalSI), }, { - description: "valid quantity with unit type (Mi)", + description: "quantity with unit type (Mi)", annotations: map[string]string{memoryKey: "456Mi"}, - expectedError: false, - expectedQuantity: resource.MustParse("456Mi"), + expectedError: true, + expectedQuantity: zeroQuantity.DeepCopy(), }, { - description: "valid quantity with unit type (Gi)", + description: "quantity with unit type (Gi)", annotations: map[string]string{memoryKey: "8Gi"}, - expectedError: false, - expectedQuantity: resource.MustParse("8Gi"), + expectedError: true, + expectedQuantity: zeroQuantity.DeepCopy(), }} { t.Run(tc.description, func(t *testing.T) { got, err := parseMemoryCapacity(tc.annotations)