Skip to content

Commit

Permalink
Merge pull request kubernetes#4235 from DataDog/fix-tests-and-gcp-pri…
Browse files Browse the repository at this point in the history
…cing

cluster-autoscaler: fix unit tests
  • Loading branch information
k8s-ci-robot authored and Jian Cheung committed Jul 29, 2022
1 parent 545e9d9 commit bba38b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestBuildGenericLabels(t *testing.T) {
}
nodeName := "virtual-node"
labels := buildGenericLabels(template, nodeName)
assert.Equal(t, labels[apiv1.LabelInstanceType], template.InstanceType.instanceTypeID)
assert.Equal(t, labels[apiv1.LabelInstanceTypeStable], template.InstanceType.instanceTypeID)
}

func TestExtractLabelsFromAsg(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cluster-autoscaler/cloudprovider/aws/aws_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func TestBuildGenericLabels(t *testing.T) {
},
Region: "us-east-1",
}, "sillyname")
assert.Equal(t, "us-east-1", labels[apiv1.LabelZoneRegion])
assert.Equal(t, "us-east-1", labels[apiv1.LabelZoneRegionStable])
assert.Equal(t, "sillyname", labels[apiv1.LabelHostname])
assert.Equal(t, "c4.large", labels[apiv1.LabelInstanceType])
assert.Equal(t, "c4.large", labels[apiv1.LabelInstanceTypeStable])
assert.Equal(t, cloudprovider.DefaultArch, labels[apiv1.LabelArchStable])
assert.Equal(t, cloudprovider.DefaultOS, labels[apiv1.LabelOSStable])
}
Expand Down
21 changes: 17 additions & 4 deletions cluster-autoscaler/cloudprovider/gce/gce_price_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (model *GcePriceModel) NodePrice(node *apiv1.Node, startTime time.Time, end
// Base instance price
if node.Labels != nil {
isPreemptible = node.Labels[preemptibleLabel] == "true"
if machineType, found := node.Labels[apiv1.LabelInstanceType]; found {
if machineType, found := getInstanceTypeFromLabels(node.Labels); found {
priceMapToUse := instancePrices
if isPreemptible {
priceMapToUse = preemptiblePrices
Expand All @@ -345,8 +345,10 @@ func (model *GcePriceModel) NodePrice(node *apiv1.Node, startTime time.Time, end
}
}
if !basePriceFound {
price = getBasePrice(node.Status.Capacity, node.Labels[apiv1.LabelInstanceType], startTime, endTime)
price = price * getPreemptibleDiscount(node)
if machineType, found := getInstanceTypeFromLabels(node.Labels); found {
price = getBasePrice(node.Status.Capacity, machineType, startTime, endTime)
price = price * getPreemptibleDiscount(node)
}
}

// GPUs
Expand Down Expand Up @@ -390,7 +392,10 @@ func getPreemptibleDiscount(node *apiv1.Node) float64 {
if node.Labels[preemptibleLabel] != "true" {
return 1.0
}
instanceType := node.Labels[apiv1.LabelInstanceType]
instanceType, found := getInstanceTypeFromLabels(node.Labels)
if !found {
return 1.0
}
instanceFamily := getInstanceFamily(instanceType)

discountMap := predefinedPreemptibleDiscount
Expand Down Expand Up @@ -459,3 +464,11 @@ func getAdditionalPrice(resources apiv1.ResourceList, startTime time.Time, endTi
price += float64(gpu.MilliValue()) / 1000.0 * gpuPricePerHour * hours
return price
}

func getInstanceTypeFromLabels(labels map[string]string) (string, bool) {
machineType, found := labels[apiv1.LabelInstanceTypeStable]
if !found {
machineType, found = labels[apiv1.LabelInstanceType]
}
return machineType, found
}

0 comments on commit bba38b3

Please sign in to comment.