in hack/code/bandwidth_gen/main.go [61:136]
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("Usage: `bandwidth_gen.go pkg/providers/instancetype/zz_generated.bandwidth.go`")
}
bandwidth := map[ec2types.InstanceType]int64{}
vagueBandwidth := map[ec2types.InstanceType]string{}
for uri, selector := range uriSelectors {
func() {
response := lo.Must(http.Get(uri))
defer response.Body.Close()
doc := lo.Must(goquery.NewDocumentFromReader(response.Body))
// grab the table that contains the network performance values. Some instance types will have vague
// description for bandwidth such as "Very Low", "Low", "Low to Moderate", etc. These instance types
// will be ignored since we don't know the exact bandwidth for these instance types
for _, row := range doc.Find(selector).NextAllFiltered(".table-container").Eq(0).Find("tbody").Find("tr").Nodes {
instanceTypeName := strings.TrimSpace(row.FirstChild.NextSibling.FirstChild.Data)
if !strings.ContainsAny(instanceTypeName, ".") {
continue
}
bandwidthData := row.FirstChild.NextSibling.NextSibling.NextSibling.FirstChild.Data
// exclude all rows that contain any of the following strings
if containsAny(bandwidthData, "Low", "Moderate", "High", "Up to") {
vagueBandwidth[ec2types.InstanceType(instanceTypeName)] = bandwidthData
continue
}
bandwidthSlice := strings.Split(bandwidthData, " ")
// if the first value contains a multiplier i.e. (4x 100 Gigabit)
if strings.HasSuffix(bandwidthSlice[0], "x") {
multiplier := lo.Must(strconv.ParseFloat(bandwidthSlice[0][:len(bandwidthSlice[0])-1], 64))
bandwidth[ec2types.InstanceType(instanceTypeName)] = int64(lo.Must(strconv.ParseFloat(bandwidthSlice[1], 64)) * 1000 * multiplier)
// Check row for instancetype for described network performance value i.e (2 Gigabit)
} else {
bandwidth[ec2types.InstanceType(instanceTypeName)] = int64(lo.Must(strconv.ParseFloat(bandwidthSlice[0], 64)) * 1000)
}
}
}()
}
allInstanceTypes := getAllInstanceTypes()
instanceTypes := lo.Keys(bandwidth)
// 2d sort for readability
sort.SliceStable(allInstanceTypes, func(i, j int) bool {
return allInstanceTypes[i] < allInstanceTypes[j]
})
sort.SliceStable(instanceTypes, func(i, j int) bool {
return instanceTypes[i] < instanceTypes[j]
})
sort.SliceStable(instanceTypes, func(i, j int) bool {
return bandwidth[instanceTypes[i]] < bandwidth[instanceTypes[j]]
})
// Generate body
var body string
for _, instanceType := range lo.Without(allInstanceTypes, instanceTypes...) {
if lo.Contains(lo.Keys(vagueBandwidth), instanceType) {
body += fmt.Sprintf("// %s has vague bandwidth information, bandwidth is %s\n", instanceType, vagueBandwidth[instanceType])
continue
}
body += fmt.Sprintf("// %s is not available in https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-network-bandwidth.html\n", instanceType)
}
for _, instanceType := range instanceTypes {
body += fmt.Sprintf("\t\"%s\": %d,\n", instanceType, bandwidth[instanceType])
}
license := lo.Must(os.ReadFile("hack/boilerplate.go.txt"))
// Format and print to the file
formatted := lo.Must(format.Source([]byte(fmt.Sprintf(fileFormat, license, body))))
file := lo.Must(os.Create(flag.Args()[0]))
lo.Must(file.Write(formatted))
file.Close()
}