diff --git a/pkg/controllers/pricing/suite_test.go b/pkg/controllers/pricing/suite_test.go index 0427d6b2d642..97a11c18eb64 100644 --- a/pkg/controllers/pricing/suite_test.go +++ b/pkg/controllers/pricing/suite_test.go @@ -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)) + }) }) diff --git a/pkg/fake/pricingapi.go b/pkg/fake/pricingapi.go index ad0dc3ceac52..5cf0dfcf5fd1 100644 --- a/pkg/fake/pricingapi.go +++ b/pkg/fake/pricingapi.go @@ -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{}{ @@ -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)}, }, }, }, diff --git a/pkg/providers/pricing/pricing.go b/pkg/providers/pricing/pricing.go index 53c9f99991e2..3db8b0625aaf 100644 --- a/pkg/providers/pricing/pricing.go +++ b/pkg/providers/pricing/pricing.go @@ -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 {