in hack/code/prices_gen/main.go [85:141]
func main() {
opts := NewOptions()
f, err := os.Create("pricing.heapprofile")
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
const region = "us-east-1"
os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
os.Setenv("AWS_REGION", region)
ctx := context.Background()
ctx = options.ToContext(ctx, test.Options())
cfg := lo.Must(config.LoadDefaultConfig(ctx, config.WithRegion(region)))
ec2api := ec2.NewFromConfig(cfg)
src := &bytes.Buffer{}
fmt.Fprintln(src, "//go:build !ignore_autogenerated")
license := lo.Must(os.ReadFile("hack/boilerplate.go.txt"))
fmt.Fprintln(src, string(license))
fmt.Fprintln(src, "package pricing")
now := time.Now().UTC().Format(time.RFC3339)
fmt.Fprintf(src, "// generated at %s for %s\n\n\n", now, region)
fmt.Fprintln(src, "import ec2types \"github.com/aws/aws-sdk-go-v2/service/ec2/types\"")
fmt.Fprintf(src, "var InitialOnDemandPrices%s = map[string]map[ec2types.InstanceType]float64{\n", getPartitionSuffix(opts.partition))
// record prices for each region we are interested in
for _, region := range getAWSRegions(opts.partition) {
log.Println("fetching for", region)
pricingProvider := pricing.NewDefaultProvider(ctx, pricing.NewAPI(cfg), ec2api, region)
controller := controllerspricing.NewController(pricingProvider)
_, err := controller.Reconcile(ctx)
if err != nil {
log.Fatalf("failed to initialize pricing provider %s", err)
}
instanceTypes := pricingProvider.InstanceTypes()
sort.SliceStable(instanceTypes, func(i, j int) bool {
return instanceTypes[i] < instanceTypes[j]
})
writePricing(src, instanceTypes, region, pricingProvider.OnDemandPrice)
}
fmt.Fprintln(src, "}")
formatted, err := format.Source(src.Bytes())
if err != nil {
if err := os.WriteFile(opts.output, src.Bytes(), 0644); err != nil {
log.Fatalf("writing output, %s", err)
}
log.Fatalf("formatting generated source, %s", err)
}
if err := os.WriteFile(opts.output, formatted, 0644); err != nil {
log.Fatalf("writing output, %s", err)
}
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}