Skip to content

Commit 7689fae

Browse files
committed
Query EKS price from AWS Pricing API (#783)
(cherry picked from commit 06a8952)
1 parent 7aef67d commit 7689fae

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

cli/cmd/lib_cluster_config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,14 @@ func getClusterUpdateConfig(cachedClusterConfig clusterconfig.Config, awsCreds A
283283
}
284284

285285
func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds AWSCredentials, awsClient *aws.Client) {
286+
eksPrice := aws.EKSPrices[*clusterConfig.Region]
286287
operatorInstancePrice := aws.InstanceMetadatas[*clusterConfig.Region]["t3.medium"].Price
287288
operatorEBSPrice := aws.EBSMetadatas[*clusterConfig.Region].Price * 20 / 30 / 24
288289
elbPrice := aws.ELBMetadatas[*clusterConfig.Region].Price
289290
natPrice := aws.NATMetadatas[*clusterConfig.Region].Price
290291
apiInstancePrice := aws.InstanceMetadatas[*clusterConfig.Region][*clusterConfig.InstanceType].Price
291292
apiEBSPrice := aws.EBSMetadatas[*clusterConfig.Region].Price * float64(clusterConfig.InstanceVolumeSize) / 30 / 24
292-
fixedPrice := 0.20 + operatorInstancePrice + operatorEBSPrice + 2*elbPrice + natPrice
293+
fixedPrice := eksPrice + operatorInstancePrice + operatorEBSPrice + 2*elbPrice + natPrice
293294
totalMinPrice := fixedPrice + float64(*clusterConfig.MinInstances)*(apiInstancePrice+apiEBSPrice)
294295
totalMaxPrice := fixedPrice + float64(*clusterConfig.MaxInstances)*(apiInstancePrice+apiEBSPrice)
295296

@@ -301,7 +302,7 @@ func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds A
301302
}
302303

303304
rows := [][]interface{}{}
304-
rows = append(rows, []interface{}{"1 eks cluster", "$0.20"})
305+
rows = append(rows, []interface{}{"1 eks cluster", s.DollarsMaxPrecision(eksPrice)})
305306
rows = append(rows, []interface{}{"1 20gb ebs volume for the operator", s.DollarsAndTenthsOfCents(operatorEBSPrice)})
306307
rows = append(rows, []interface{}{"1 t3.medium for the operator", s.DollarsMaxPrecision(operatorInstancePrice)})
307308
rows = append(rows, []interface{}{"1 nat gateway", s.DollarsMaxPrecision(natPrice)})

pkg/lib/aws/gen_resource_metadata.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@
3939

4040
OUTPUT_FILE_NAME = "resource_metadata.go"
4141

42-
PRICING_ENDPOINT_TEMPLATE = (
42+
EC2_PRICING_ENDPOINT_TEMPLATE = (
4343
"https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/{}/index.json"
4444
)
4545

46+
EKS_PRICING_ENDPOINT_TEMPLATE = (
47+
"https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEKS/current/{}/index.json"
48+
)
49+
4650

4751
def get_instance_metadatas(pricing):
4852
instance_mapping = {}
@@ -136,6 +140,29 @@ def get_ebs_metadata(pricing):
136140
return {"price": float(price)}
137141

138142

143+
def get_eks_price(region):
144+
response = requests.get(EKS_PRICING_ENDPOINT_TEMPLATE.format(region))
145+
pricing = response.json()
146+
147+
for product_id, product in pricing["products"].items():
148+
if product.get("attributes") is None:
149+
continue
150+
if product.get("productFamily") != "Compute":
151+
continue
152+
if product["attributes"].get("servicecode") != "AmazonEKS":
153+
continue
154+
if product["attributes"].get("operation") != "CreateOperation":
155+
continue
156+
if not product["attributes"].get("usagetype", "").endswith("-AmazonEKS-Hours:perCluster"):
157+
continue
158+
159+
price_dimensions = list(pricing["terms"]["OnDemand"][product["sku"]].values())[0][
160+
"priceDimensions"
161+
]
162+
price = list(price_dimensions.values())[0]["pricePerUnit"]["USD"]
163+
return float(price)
164+
165+
139166
file_template = Template(
140167
"""/*
141168
Copyright 2020 Cortex Labs, Inc.
@@ -204,6 +231,11 @@ def get_ebs_metadata(pricing):
204231
var EBSMetadatas = map[string]EBSMetadata{
205232
${ebs_region_map}
206233
}
234+
235+
// region -> EKS price
236+
var EKSPrices = map[string]float64{
237+
${eks_region_map}
238+
}
207239
"""
208240
)
209241

@@ -234,23 +266,30 @@ def get_ebs_metadata(pricing):
234266
"""
235267
)
236268

269+
eks_region_map_template = Template(
270+
""""${region}": ${price},
271+
"""
272+
)
273+
237274

238275
def main():
239276
instance_region_map_str = ""
240277
elb_region_map_str = ""
241278
nat_region_map_str = ""
242279
ebs_region_map_str = ""
280+
eks_region_map_str = ""
243281

244282
for i, region in enumerate(sorted(REGIONS), start=1):
245283
print("generating region {}/{} ({})...".format(i, len(REGIONS), region))
246284

247-
response = requests.get(PRICING_ENDPOINT_TEMPLATE.format(region))
285+
response = requests.get(EC2_PRICING_ENDPOINT_TEMPLATE.format(region))
248286
pricing = response.json()
249287

250288
instance_metadatas = get_instance_metadatas(pricing)
251289
elb_metadata = get_elb_metadata(pricing)
252290
nat_metadata = get_nat_metadata(pricing)
253291
ebs_metadata = get_ebs_metadata(pricing)
292+
eks_price = get_eks_price(region)
254293

255294
instance_metadatas_str = ""
256295

@@ -279,13 +318,17 @@ def main():
279318
ebs_region_map_str += ebs_region_map_template.substitute(
280319
{"region": region, "price": ebs_metadata["price"]}
281320
)
321+
eks_region_map_str += eks_region_map_template.substitute(
322+
{"region": region, "price": eks_price}
323+
)
282324

283325
file_str = file_template.substitute(
284326
{
285327
"instance_region_map": instance_region_map_str,
286328
"elb_region_map": elb_region_map_str,
287329
"nat_region_map": nat_region_map_str,
288330
"ebs_region_map": ebs_region_map_str,
331+
"eks_region_map": eks_region_map_str,
289332
}
290333
)
291334

pkg/lib/aws/resource_metadata.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ var InstanceMetadatas = map[string]map[string]InstanceMetadata{
10701070
"g4dn.4xlarge": {Region: "ap-southeast-2", Type: "g4dn.4xlarge", Memory: kresource.MustParse("65536Mi"), CPU: kresource.MustParse("16"), GPU: 1, Price: 1.566},
10711071
"g4dn.8xlarge": {Region: "ap-southeast-2", Type: "g4dn.8xlarge", Memory: kresource.MustParse("131072Mi"), CPU: kresource.MustParse("32"), GPU: 1, Price: 2.83},
10721072
"g4dn.xlarge": {Region: "ap-southeast-2", Type: "g4dn.xlarge", Memory: kresource.MustParse("16384Mi"), CPU: kresource.MustParse("4"), GPU: 1, Price: 0.684},
1073-
"hs1.8xlarge": {Region: "ap-southeast-2", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("17"), GPU: 0, Price: 5.57},
1073+
"hs1.8xlarge": {Region: "ap-southeast-2", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 5.57},
10741074
"i2.2xlarge": {Region: "ap-southeast-2", Type: "i2.2xlarge", Memory: kresource.MustParse("62464Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 2.035},
10751075
"i2.4xlarge": {Region: "ap-southeast-2", Type: "i2.4xlarge", Memory: kresource.MustParse("124928Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 4.07},
10761076
"i2.8xlarge": {Region: "ap-southeast-2", Type: "i2.8xlarge", Memory: kresource.MustParse("249856Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 8.14},
@@ -1789,7 +1789,7 @@ var InstanceMetadatas = map[string]map[string]InstanceMetadata{
17891789
"h1.2xlarge": {Region: "eu-west-1", Type: "h1.2xlarge", Memory: kresource.MustParse("32768Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 0.519},
17901790
"h1.4xlarge": {Region: "eu-west-1", Type: "h1.4xlarge", Memory: kresource.MustParse("65536Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 1.038},
17911791
"h1.8xlarge": {Region: "eu-west-1", Type: "h1.8xlarge", Memory: kresource.MustParse("131072Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 2.076},
1792-
"hs1.8xlarge": {Region: "eu-west-1", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("17"), GPU: 0, Price: 4.9},
1792+
"hs1.8xlarge": {Region: "eu-west-1", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 4.9},
17931793
"i2.2xlarge": {Region: "eu-west-1", Type: "i2.2xlarge", Memory: kresource.MustParse("62464Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 1.876},
17941794
"i2.4xlarge": {Region: "eu-west-1", Type: "i2.4xlarge", Memory: kresource.MustParse("124928Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 3.751},
17951795
"i2.8xlarge": {Region: "eu-west-1", Type: "i2.8xlarge", Memory: kresource.MustParse("249856Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 7.502},
@@ -3367,3 +3367,24 @@ var EBSMetadatas = map[string]EBSMetadata{
33673367
"us-east-2": {Region: "us-east-2", Price: 0.1},
33683368
"us-west-2": {Region: "us-west-2", Price: 0.1},
33693369
}
3370+
3371+
// region -> EKS price
3372+
var EKSPrices = map[string]float64{
3373+
"ap-east-1": 0.1,
3374+
"ap-northeast-1": 0.1,
3375+
"ap-northeast-2": 0.1,
3376+
"ap-south-1": 0.1,
3377+
"ap-southeast-1": 0.1,
3378+
"ap-southeast-2": 0.1,
3379+
"ca-central-1": 0.1,
3380+
"eu-central-1": 0.1,
3381+
"eu-north-1": 0.1,
3382+
"eu-west-1": 0.1,
3383+
"eu-west-2": 0.1,
3384+
"eu-west-3": 0.1,
3385+
"me-south-1": 0.1,
3386+
"sa-east-1": 0.1,
3387+
"us-east-1": 0.1,
3388+
"us-east-2": 0.1,
3389+
"us-west-2": 0.1,
3390+
}

0 commit comments

Comments
 (0)