in scripts/gen_vpc_limits.go [45:142]
func main() {
log := zap.New(zap.UseDevMode(true))
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
_, err := sess.Config.Credentials.Get()
svc := ec2.New(sess)
describeInstanceInput := &ec2.DescribeInstanceTypesInput{}
limitsMap := make(map[string]*VPCLimits)
for {
// Make EC2 call to describe the instance type till the
output, err := svc.DescribeInstanceTypes(describeInstanceInput)
if err != nil {
log.Error(err, "failed to describe the instance")
panic(err)
}
for _, instanceDetail := range output.InstanceTypes {
limit := &VPCLimits{
Interface: int(*instanceDetail.NetworkInfo.MaximumNetworkInterfaces),
IPv4PerInterface: int(*instanceDetail.NetworkInfo.Ipv4AddressesPerInterface),
IsTrunkingCompatible: false,
}
limitsMap[*instanceDetail.InstanceType] = limit
}
if output.NextToken == nil {
break
}
describeInstanceInput = &ec2.DescribeInstanceTypesInput{
NextToken: output.NextToken,
}
}
log.Info("instance retrieved", "count", len(limitsMap))
// Get the Branch ENIDetails
branchConfigPath, _ := filepath.Abs(BranchENILimitsFile)
branchENIFileContent, err := ioutil.ReadFile(branchConfigPath)
if err != nil {
panic(err)
}
instanceLimits := strings.Split(string(branchENIFileContent), "\n")
// Regex pattern to parse the temporary branch limits file
matchInstanceType, _ := regexp.Compile(`[^#].*".*"`)
matchCapacity, _ := regexp.Compile(`[0-9]*;`)
// For each instance from the custom file, edit the branch limits for the map generated using the EC2 API Call
for _, instanceLimit := range instanceLimits {
if matchInstanceType.MatchString(instanceLimit) && matchCapacity.MatchString(instanceLimit) {
instanceType := strings.ReplaceAll(matchInstanceType.FindString(instanceLimit), "\"", "")
instanceLimit := strings.ReplaceAll(matchCapacity.FindString(instanceLimit), ";", "")
// Trim whitespaces
instanceType = strings.TrimSpace(instanceType)
branchLimit, _ := strconv.Atoi(instanceLimit)
limit, found := limitsMap[instanceType]
if !found {
log.Info("skipping instance as failed to find details from ec2 describe instance type call",
"instance type", instanceType)
continue
}
limit.IsTrunkingCompatible = true
limit.BranchInterface = branchLimit - limit.Interface
}
}
// Generate the file
f, err := os.Create(VPCLimitsFileName)
if err != nil {
panic(err)
}
limitsTemplate.Execute(f, struct {
Timestamp string
Limits map[string]*VPCLimits
}{
Timestamp: time.Now().Format(time.RFC3339),
Limits: limitsMap,
})
vpcFileAbsPath, _ := filepath.Abs(VPCLimitsFileName)
log.Info("running go fmt on the generated file", "file", vpcFileAbsPath)
cmd := exec.Command("go", "fmt", vpcFileAbsPath)
err = cmd.Run()
if err != nil {
panic(err)
}
}