Move universal constants to separate module

This commit is contained in:
Aleksandra Malinowska 2018-05-17 17:57:18 +02:00
parent fcc3d004f5
commit 3ccfa5be23
7 changed files with 34 additions and 16 deletions

View File

@ -42,9 +42,9 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/config/dynamic"
"k8s.io/autoscaler/cluster-autoscaler/utils/gpu"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
provider_gce "k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
)
@ -1026,10 +1026,10 @@ func (m *gceManagerImpl) fetchResourceLimiter() error {
// GKE API provides memory in GB, but ResourceLimiter expects them in bytes
if _, found := minLimits[cloudprovider.ResourceNameMemory]; found {
minLimits[cloudprovider.ResourceNameMemory] = minLimits[cloudprovider.ResourceNameMemory] * config.Gigabyte
minLimits[cloudprovider.ResourceNameMemory] = minLimits[cloudprovider.ResourceNameMemory] * units.Gigabyte
}
if _, found := maxLimits[cloudprovider.ResourceNameMemory]; found {
maxLimits[cloudprovider.ResourceNameMemory] = maxLimits[cloudprovider.ResourceNameMemory] * config.Gigabyte
maxLimits[cloudprovider.ResourceNameMemory] = maxLimits[cloudprovider.ResourceNameMemory] * units.Gigabyte
}
resourceLimiter := cloudprovider.NewResourceLimiter(minLimits, maxLimits)

View File

@ -22,6 +22,7 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/autoscaler/cluster-autoscaler/utils/gpu"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
)
@ -36,7 +37,6 @@ const (
preemptibleDiscount = 0.00698 / 0.033174
gpuPricePerHour = 0.700
gigabyte = 1024.0 * 1024.0 * 1024.0
preemptibleLabel = "cloud.google.com/gke-preemptible"
)
@ -147,7 +147,7 @@ func getBasePrice(resources apiv1.ResourceList, startTime time.Time, endTime tim
cpu := resources[apiv1.ResourceCPU]
mem := resources[apiv1.ResourceMemory]
price += float64(cpu.MilliValue()) / 1000.0 * cpuPricePerHour * hours
price += float64(mem.Value()) / gigabyte * memoryPricePerHourPerGb * hours
price += float64(mem.Value()) / float64(units.Gigabyte) * memoryPricePerHourPerGb * hours
return price
}

View File

@ -17,15 +17,8 @@ limitations under the License.
package config
const (
// Defaults.
// DefaultMaxClusterCores is the default maximum number of cores in the cluster.
DefaultMaxClusterCores = 5000 * 64
// DefaultMaxClusterMemory is the default maximum number of gigabytes of memory in cluster.
DefaultMaxClusterMemory = 5000 * 64 * 20
// Useful universal constants.
// Gigabyte is 2^30 bytes.
Gigabyte = 1024 * 1024 * 1024
)

View File

@ -38,6 +38,7 @@ import (
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
scheduler_util "k8s.io/autoscaler/cluster-autoscaler/utils/scheduler"
. "k8s.io/autoscaler/cluster-autoscaler/utils/test"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
"k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"
@ -863,7 +864,7 @@ var defaultScaleDownOptions = context.AutoscalingOptions{
MinCoresTotal: 0,
MinMemoryTotal: 0,
MaxCoresTotal: config.DefaultMaxClusterCores,
MaxMemoryTotal: config.DefaultMaxClusterMemory * config.Gigabyte,
MaxMemoryTotal: config.DefaultMaxClusterMemory * units.Gigabyte,
}
func TestScaleDownEmptyMultipleNodeGroups(t *testing.T) {

View File

@ -34,6 +34,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/simulator"
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
. "k8s.io/autoscaler/cluster-autoscaler/utils/test"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
apiv1 "k8s.io/api/core/v1"
extensionsv1 "k8s.io/api/extensions/v1beta1"
@ -50,7 +51,7 @@ import (
var defaultOptions = context.AutoscalingOptions{
EstimatorName: estimator.BinpackingEstimatorName,
MaxCoresTotal: config.DefaultMaxClusterCores,
MaxMemoryTotal: config.DefaultMaxClusterMemory * config.Gigabyte,
MaxMemoryTotal: config.DefaultMaxClusterMemory * units.Gigabyte,
MinCoresTotal: 0,
MinMemoryTotal: 0,
}

View File

@ -40,6 +40,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/simulator"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
@ -144,8 +145,8 @@ func createAutoscalingOptions() context.AutoscalingOptions {
glog.Fatalf("Failed to parse flags: %v", err)
}
// Convert memory limits to bytes.
minMemoryTotal = minMemoryTotal * config.Gigabyte
maxMemoryTotal = maxMemoryTotal * config.Gigabyte
minMemoryTotal = minMemoryTotal * units.Gigabyte
maxMemoryTotal = maxMemoryTotal * units.Gigabyte
return context.AutoscalingOptions{
CloudConfig: *cloudConfig,

View File

@ -0,0 +1,22 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package units
const (
// Gigabyte is 2^30 bytes.
Gigabyte = 1024 * 1024 * 1024
)