Skip to content

Commit 4bbab43

Browse files
committed
correctly compute non-N1 shapes
1 parent e5707fa commit 4bbab43

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

internal/cloudinfo/providers/google/cloudinfo.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var regionNames = map[string]string{
4747
"asia-south2": "Asia Pacific (Delhi)",
4848
"asia-southeast1": "Asia Pacific (Singapore)",
4949
"asia-southeast2": "Asia Pacific (Jakarta)",
50+
5051
"australia-southeast1": "Asia Pacific (Sydney)",
5152
"australia-southeast2": "Asia Pacific (Melbourne)",
5253
"europe-north1": "EU (Finland)",
@@ -203,17 +204,28 @@ func (g *GceInfoer) Initialize() (map[string]map[string]types.Price, error) {
203204
region := r
204205
price := pricePerRegion[region]
205206
for _, mt := range allMts.Items {
206-
if !cloudinfo.Contains(unsupportedInstanceTypes, mt.Name) && !strings.HasSuffix(mt.Name, "-metal") {
207+
if !cloudinfo.Contains(unsupportedInstanceTypes, mt.Name) && !strings.HasSuffix(mt.Name, "-metal") &&
208+
!strings.HasPrefix(mt.Name, "m2-") && !strings.HasPrefix(mt.Name, "ct") {
207209
if allPrices[zone] == nil {
208210
allPrices[zone] = make(map[string]types.Price)
209211
}
210212
prices := allPrices[zone][mt.Name]
211213

212-
if mt.Name == "f1-micro" || mt.Name == "g1-small" {
214+
nameSplit := strings.Split(mt.Name, "-")
215+
family := nameSplit[0]
216+
switch {
217+
case mt.Name == "f1-micro" || mt.Name == "g1-small":
213218
prices.OnDemandPrice = price[mt.Name]["OnDemand"]
214-
} else {
219+
case family == "n1" || family == "c2":
215220
prices.OnDemandPrice = price[types.CPU]["OnDemand"]*float64(mt.GuestCpus) + price[types.Memory]["OnDemand"]*float64(mt.MemoryMb)/1024
221+
case family == "m1":
222+
prices.OnDemandPrice = price["m3-cpu"]["OnDemand"]*float64(mt.GuestCpus) + price["m3-memory"]["OnDemand"]*float64(mt.MemoryMb)/1024
223+
case isSupportedFamily(family):
224+
prices.OnDemandPrice = price[family+"-cpu"]["OnDemand"]*float64(mt.GuestCpus) + price[family+"-memory"]["OnDemand"]*float64(mt.MemoryMb)/1024
225+
default:
226+
g.log.Warn("could not get price", map[string]interface{}{"machineTypeName": mt.Name})
216227
}
228+
// TODO: update this code to make it zone-friendly and ordered
217229
spotPrice := make(types.SpotPriceInfo)
218230
for _, z := range zonesInRegions[region] {
219231
if mt.Name == "f1-micro" || mt.Name == "g1-small" {
@@ -242,6 +254,12 @@ func (g *GceInfoer) Initialize() (map[string]map[string]types.Price, error) {
242254
return allPrices, nil
243255
}
244256

257+
func isSupportedFamily(family string) bool {
258+
return family == "a2" || family == "a3" || family == "c3" || family == "c3d" || family == "c4" ||
259+
family == "e2" || family == "g2" || family == "h3" || family == "n2" || family == "n4" || family == "m3" ||
260+
family == "c4a" || family == "t2a" || family == "n2d" || family == "c2d" || family == "t2d" || family == "z3"
261+
}
262+
245263
func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error) {
246264
logger := log.WithFields(g.log, map[string]interface{}{"service": "compute"})
247265
logger.Debug("getting price")
@@ -301,8 +319,39 @@ func (g *GceInfoer) getPrice() (map[string]map[string]map[string]float64, error)
301319
logger.Debug("ignoring N1Standard", map[string]interface{}{"sku": sku})
302320
}
303321
}
304-
} else if sku.Category.UsageType == "OnDemand" {
305-
logger.Debug("unrecognized sku.Category.ResourceGroup", map[string]interface{}{"sku": sku})
322+
} else if (sku.Category.UsageType == "OnDemand" || sku.Category.UsageType == "Preemptible") &&
323+
(sku.Category.ResourceGroup == "RAM" || sku.Category.ResourceGroup == "CPU") {
324+
descSplit := strings.Split(sku.Description, " ")
325+
if len(descSplit) < 4 {
326+
continue
327+
}
328+
family := strings.ToLower(descSplit[0])
329+
if !isSupportedFamily(family) {
330+
continue
331+
}
332+
resMatch := (descSplit[1] == "Instance" && (descSplit[2] == "Ram" || descSplit[2] == "Core")) ||
333+
(descSplit[2] == "Instance" && (descSplit[3] == "Ram" || descSplit[3] == "Core") &&
334+
(descSplit[1] == "Memory-optimized" || descSplit[1] == "Arm" || descSplit[1] == "AMD"))
335+
if !resMatch {
336+
continue
337+
}
338+
priceInUsd, err := g.priceInUsd(sku.PricingInfo)
339+
if err != nil {
340+
return err
341+
}
342+
343+
for _, region := range sku.ServiceRegions {
344+
if price[region] == nil {
345+
price[region] = make(map[string]map[string]float64)
346+
}
347+
if sku.Category.ResourceGroup == "RAM" {
348+
memoryType := family + "-memory"
349+
price[region][memoryType] = g.priceFromSku(price, region, memoryType, sku.Category.UsageType, priceInUsd)
350+
} else { // sku.Category.ResourceGroup == "CPU"
351+
cpuType := family + "-cpu"
352+
price[region][cpuType] = g.priceFromSku(price, region, cpuType, sku.Category.UsageType, priceInUsd)
353+
}
354+
}
306355
}
307356
}
308357
return nil

0 commit comments

Comments
 (0)