Skip to content

Make AWS Pricing API Region Configurable via Env Var or Parameter #8172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Prankisster
Copy link

Adds support for configuring the AWS pricing API region using the PRICING_REGION_OVERRIDE environment variable or a function parameter. Defaults remain backward compatible with region-based fallbacks and us-east-1.

Fixes #8166

Description

feat(pricing): Make AWS Pricing API region configurable

This change allows consumers to override the default pricing region fallback behavior by either:

  • Setting the PRICING_REGION_OVERRIDE environment variable
  • Passing a regionOverride string to the NewAPI constructor

The default region logic (us-east-1, ap-south-1, cn-northwest-1, eu-central-1) remains as fallback if no override is provided, maintaining backward compatibility. This change supports customers with restricted region access and avoids hardcoding assumptions.

How was this change tested?

  • Unit tested locally by simulating region override via environment variable and function parameter
  • Verified fallback behavior by omitting the override
  • Manually tested with locked-down region constraints

Does this change impact docs?

  • Yes, PR includes docs updates <!-- docs must be added to /preview to be included in future version rel>
  • Yes, issue opened: #
  • No

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…on override

Adds support for configuring the AWS pricing API region using the PRICING_REGION_OVERRIDE environment variable or a function parameter. Defaults remain backward compatible with region-based fallbacks and us-east-1.
@Prankisster Prankisster requested a review from a team as a code owner June 9, 2025 09:21
@Prankisster Prankisster requested a review from jigisha620 June 9, 2025 09:21
Copy link

netlify bot commented Jun 9, 2025

Deploy Preview for karpenter-docs-prod canceled.

Name Link
🔨 Latest commit d7413bd
🔍 Latest deploy log https://app.netlify.com/projects/karpenter-docs-prod/deploys/6846a784b2a3d700089cdab3

func NewAPI(cfg aws.Config, pricingRegionOverride ...string) *pricing.Client {
pricingRegion := "us-east-1"

if val := os.Getenv("PRICING_REGION_OVERRIDE"); val != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than use the os.Getenv here, can we match the pattern established with other operator options?

func (o *Options) AddFlags(fs *coreoptions.FlagSet) {
fs.StringVar(&o.ClusterCABundle, "cluster-ca-bundle", env.WithDefaultString("CLUSTER_CA_BUNDLE", ""), "Cluster CA bundle for nodes to use for TLS connections with the API server. If not set, this is taken from the controller's TLS configuration.")
fs.StringVar(&o.ClusterName, "cluster-name", env.WithDefaultString("CLUSTER_NAME", ""), "[REQUIRED] The kubernetes cluster name for resource discovery.")
fs.StringVar(&o.ClusterEndpoint, "cluster-endpoint", env.WithDefaultString("CLUSTER_ENDPOINT", ""), "The external kubernetes cluster endpoint for new nodes to connect with. If not specified, will discover the cluster endpoint using DescribeCluster API.")
fs.BoolVarWithEnv(&o.IsolatedVPC, "isolated-vpc", "ISOLATED_VPC", false, "If true, then assume we can't reach AWS services which don't have a VPC endpoint. This also has the effect of disabling look-ups to the AWS on-demand pricing endpoint.")
fs.BoolVarWithEnv(&o.EKSControlPlane, "eks-control-plane", "EKS_CONTROL_PLANE", false, "Marking this true means that your cluster is running with an EKS control plane and Karpenter should attempt to discover cluster details from the DescribeCluster API ")
fs.Float64Var(&o.VMMemoryOverheadPercent, "vm-memory-overhead-percent", utils.WithDefaultFloat64("VM_MEMORY_OVERHEAD_PERCENT", 0.075), "The VM memory overhead as a percent that will be subtracted from the total memory for all instance types when cached information is unavailable.")
fs.StringVar(&o.InterruptionQueue, "interruption-queue", env.WithDefaultString("INTERRUPTION_QUEUE", ""), "Interruption queue is the name of the SQS queue used for processing interruption events from EC2. Interruption handling is disabled if not specified. Enabling interruption handling may require additional permissions on the controller service account. Additional permissions are outlined in the docs.")
fs.IntVar(&o.ReservedENIs, "reserved-enis", env.WithDefaultInt("RESERVED_ENIS", 0), "Reserved ENIs are not included in the calculations for max-pods or kube-reserved. This is most often used in the VPC CNI custom networking setup https://docs.aws.amazon.com/eks/latest/userguide/cni-custom-network.html.")
}

return pricing.NewFromConfig(pricingCfg)
// NewAPI returns a pricing API configured based on a specific region,
// with an optional override for the pricing endpoint region.
func NewAPI(cfg aws.Config, pricingRegionOverride ...string) *pricing.Client {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func NewAPI(cfg aws.Config, pricingRegionOverride ...string) *pricing.Client {
func NewAPI(cfg aws.Config, regionOverride string) *pricing.Client {

Comment on lines +110 to +122
} else if len(pricingRegionOverride) > 0 && pricingRegionOverride[0] != "" {
// If caller explicitly provided a region, honor it
pricingRegion = pricingRegionOverride[0]
} else {
// Otherwise use existing fallback logic
if strings.HasPrefix(cfg.Region, "ap-") {
pricingRegion = "ap-south-1"
} else if strings.HasPrefix(cfg.Region, "cn-") {
pricingRegion = "cn-northwest-1"
} else if strings.HasPrefix(cfg.Region, "eu-") {
pricingRegion = "eu-central-1"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding these conditionals, can we just override it if the override is not == ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hardcoded pricing regions -- please make this configurable!
2 participants