Skip to content

Commit 7af4e40

Browse files
iamanmolmHarness
authored andcommitted
feat: [CCM-23530]: Support calculating price for all GCP standard machine series (#12)
* feat: [CCM-23530]: Support all valid groups while fetching machine prices
1 parent 61b324e commit 7af4e40

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

internal/cloudinfo/providers/google/cloudinfo.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (g *GceInfoer) Initialize() (map[string]map[string]types.Price, error) {
216216
return nil, err
217217
}
218218

219-
pricePerRegion, err := g.getPrice()
219+
pricePerRegion, regionStandardMachineMap, err := g.getPrice()
220220
if err != nil {
221221
return nil, err
222222
}
@@ -235,16 +235,21 @@ func (g *GceInfoer) Initialize() (map[string]map[string]types.Price, error) {
235235
}
236236
prices := allPrices[region][mt.Name]
237237

238+
standardMachineType := strings.Split(mt.Name, "-")[0]
239+
238240
if mt.Name == "f1-micro" || mt.Name == "g1-small" {
239241
prices.OnDemandPrice = price[mt.Name]["OnDemand"]
242+
} else if regionStandardMachineMap[region][standardMachineType] {
243+
prices.OnDemandPrice = price[standardMachineType+"-"+types.CPU]["OnDemand"]*float64(mt.GuestCpus) + price[standardMachineType+"-"+types.Memory]["OnDemand"]*float64(mt.MemoryMb)/1024
240244
} else {
241245
prices.OnDemandPrice = price[types.CPU]["OnDemand"]*float64(mt.GuestCpus) + price[types.Memory]["OnDemand"]*float64(mt.MemoryMb)/1024
242246
}
243247
spotPrice := make(types.SpotPriceInfo)
244248
for _, z := range zonesInRegions[region] {
245249
if mt.Name == "f1-micro" || mt.Name == "g1-small" {
246250
spotPrice[z] = price[mt.Name]["Preemptible"]
247-
metrics.ReportGoogleSpotPrice(region, z, mt.Name, spotPrice[z])
251+
} else if regionStandardMachineMap[region][standardMachineType] {
252+
spotPrice[z] = price[standardMachineType+"-"+types.CPU]["Preemptible"]*float64(mt.GuestCpus) + price[standardMachineType+"-"+types.Memory]["Preemptible"]*float64(mt.MemoryMb)/1024
248253
} else {
249254
spotPrice[z] = price[types.CPU]["Preemptible"]*float64(mt.GuestCpus) + price[types.Memory]["Preemptible"]*float64(mt.MemoryMb)/1024
250255
}
@@ -268,7 +273,7 @@ func (g *GceInfoer) Initialize() (map[string]map[string]types.Price, error) {
268273
return allPrices, nil
269274
}
270275

271-
func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error) {
276+
func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, map[string]map[string]bool, error) {
272277
var compEngId string
273278
var nextPageToken string
274279

@@ -279,7 +284,7 @@ func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error)
279284
PageToken(nextPageToken).
280285
Do()
281286
if err != nil {
282-
return nil, fmt.Errorf("error fetching services: %w", err)
287+
return nil, nil, fmt.Errorf("error fetching services: %w", err)
283288
}
284289

285290
for _, svc := range svcList.Services {
@@ -303,12 +308,28 @@ func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error)
303308
// Handle the case where "Compute Engine" service is not found
304309
if compEngId == "" {
305310
g.log.Error("Compute Engine service not found")
306-
return nil, fmt.Errorf("Compute Engine service not found")
311+
return nil, nil, fmt.Errorf("compute Engine service not found")
312+
}
313+
314+
standardMachineTypes := []string{
315+
"A2", "A3", "A4", "C2", "C2D", "C3", "C3D", "C4", "C4A", "C4D",
316+
"CT3", "CT3P", "CT5LP", "CT5P", "CT6E", "E2", "F1", "G1", "G2",
317+
"M1", "M2", "M3", "M4", "N1", "N2", "N2D", "N4", "T2A", "T2D",
318+
"X4", "Z3",
307319
}
308320

321+
// This map stores standard machine with respect to region
322+
regionStandardMachineMap := make(map[string]map[string]bool)
323+
309324
price := make(map[string]map[string]map[string]float64)
310325
err := g.cbSvc.Services.Skus.List(compEngId).Pages(context.Background(), func(response *cloudbilling.ListSkusResponse) error {
311326
for _, sku := range response.Skus {
327+
if sku.Category.ResourceFamily != "Compute" {
328+
// It can be Compute, Network, Storage
329+
// We skip the SKU which is not Compute
330+
continue
331+
}
332+
312333
if sku.Category.ResourceGroup == "G1Small" || sku.Category.ResourceGroup == "F1Micro" {
313334
priceInUsd, err := g.priceInUsd(sku.PricingInfo)
314335
if err != nil {
@@ -339,19 +360,48 @@ func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error)
339360
}
340361
if strings.Contains(sku.Description, "Instance Ram") {
341362
price[region][types.Memory] = g.priceFromSku(price, region, types.Memory, sku.Category.UsageType, priceInUsd)
342-
} else {
363+
} else if strings.Contains(sku.Description, "Instance Core") {
343364
price[region][types.CPU] = g.priceFromSku(price, region, types.CPU, sku.Category.UsageType, priceInUsd)
344365
}
345366
}
346367
}
347368
}
369+
if sku.Category.ResourceGroup == "RAM" || sku.Category.ResourceGroup == "CPU" {
370+
for _, standardMachineType := range standardMachineTypes {
371+
if strings.Contains(sku.Description, standardMachineType) {
372+
priceInUsd, err := g.priceInUsd(sku.PricingInfo)
373+
if err != nil {
374+
return err
375+
}
376+
377+
standardMachineTypeName := strings.ToLower(standardMachineType) + "-" + types.CPU
378+
if sku.Category.ResourceGroup == "RAM" {
379+
standardMachineTypeName = strings.ToLower(standardMachineType) + "-" + types.Memory
380+
}
381+
382+
for _, region := range sku.ServiceRegions {
383+
if price[region] == nil {
384+
price[region] = make(map[string]map[string]float64)
385+
}
386+
if regionStandardMachineMap[region] == nil {
387+
regionStandardMachineMap[region] = make(map[string]bool)
388+
}
389+
390+
price[region][standardMachineTypeName] = g.priceFromSku(price, region, standardMachineTypeName, sku.Category.UsageType, priceInUsd)
391+
regionStandardMachineMap[region][strings.ToLower(standardMachineType)] = true
392+
}
393+
394+
break
395+
}
396+
}
397+
}
348398
}
349399
return nil
350400
})
351401
if err != nil {
352-
return nil, err
402+
return nil, nil, err
353403
}
354-
return price, nil
404+
return price, regionStandardMachineMap, nil
355405
}
356406

357407
func (g *GceInfoer) priceInUsd(pricingInfos []*cloudbilling.PricingInfo) (float64, error) {

0 commit comments

Comments
 (0)