Skip to content

Commit

Permalink
fix: use the correct pricing currency in cn-northwest-1 region (#5694)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 authored Mar 14, 2024
1 parent 0aa8845 commit 5c02af4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
31 changes: 31 additions & 0 deletions pkg/controllers/pricing/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,35 @@ var _ = Describe("Pricing", func() {
Expect(ok).To(BeTrue())
Expect(price).To(BeNumerically("==", 1.10))
})
It("should update on-demand pricing with response from the pricing API when in the CN partition", func() {
tmpPricingProvider := pricing.NewProvider(ctx, awsEnv.PricingAPI, awsEnv.EC2API, "cn-anywhere-1")
tmpController := controllerspricing.NewController(tmpPricingProvider)

now := time.Now()
awsEnv.EC2API.DescribeSpotPriceHistoryOutput.Set(&ec2.DescribeSpotPriceHistoryOutput{
SpotPriceHistory: []*ec2.SpotPrice{
{
AvailabilityZone: aws.String("test-zone-1a"),
InstanceType: aws.String("c99.large"),
SpotPrice: aws.String("1.23"),
Timestamp: &now,
},
},
})
awsEnv.PricingAPI.GetProductsOutput.Set(&awspricing.GetProductsOutput{
PriceList: []aws.JSONValue{
fake.NewOnDemandPriceWithCurrency("c98.large", 1.20, "CNY"),
fake.NewOnDemandPriceWithCurrency("c99.large", 1.23, "CNY"),
},
})
ExpectReconcileSucceeded(ctx, tmpController, types.NamespacedName{})

price, ok := tmpPricingProvider.OnDemandPrice("c98.large")
Expect(ok).To(BeTrue())
Expect(price).To(BeNumerically("==", 1.20))

price, ok = tmpPricingProvider.OnDemandPrice("c99.large")
Expect(ok).To(BeTrue())
Expect(price).To(BeNumerically("==", 1.23))
})
})
6 changes: 5 additions & 1 deletion pkg/fake/pricingapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (p *PricingAPI) GetProductsPagesWithContext(_ aws.Context, _ *pricing.GetPr
}

func NewOnDemandPrice(instanceType string, price float64) aws.JSONValue {
return NewOnDemandPriceWithCurrency(instanceType, price, "USD")
}

func NewOnDemandPriceWithCurrency(instanceType string, price float64, currency string) aws.JSONValue {
return aws.JSONValue{
"product": map[string]interface{}{
"attributes": map[string]interface{}{
Expand All @@ -63,7 +67,7 @@ func NewOnDemandPrice(instanceType string, price float64) aws.JSONValue {
"offerTermCode": "JRTCKXETXF",
"priceDimensions": map[string]interface{}{
"JRTCKXETXF.foo.bar": map[string]interface{}{
"pricePerUnit": map[string]interface{}{"USD": fmt.Sprintf("%f", price)},
"pricePerUnit": map[string]interface{}{currency: fmt.Sprintf("%f", price)},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/pricing/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (p *Provider) onDemandPage(ctx context.Context, prices map[string]float64)

return func(output *pricing.GetProductsOutput, b bool) bool {
currency := "USD"
if p.region == "cn-north-1" {
if strings.HasPrefix(p.region, "cn-") {
currency = "CNY"
}
for _, outer := range output.PriceList {
Expand Down

0 comments on commit 5c02af4

Please sign in to comment.