Skip to content

Commit

Permalink
add divideInstanceResourcesInRatio template function
Browse files Browse the repository at this point in the history
Signed-off-by: Noor Malik <[email protected]>
  • Loading branch information
Noor Malik committed Feb 1, 2024
1 parent 8caf60e commit b0a3449
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,38 @@ func sumQuantities(quantities ...string) (string, error) {
func awsValidID(id string) string {
return strings.Replace(id, ":", "__", -1)
}

// divideInstanceResourcesInRatio takes in an instance type and a ratio and returns the result of applying
// the ratio on the instance CPU and Memory sizes.
// The ratio must be a positive number and less than or equal to 1.
// The result is rounded down to the nearest integer, to avoid overcommitting resources.
// The units for CPU and memory are millicores and mebibytes (MiB) respectively.
func divideInstanceResourcesInRatio(adapter *awsAdapter, instanceType string, ratio float32) (cpu, memory int, err error) {
if adapter == nil || adapter.ec2Client == nil {
return 0, 0, fmt.Errorf("the ec2 client is not available")
}

// validate the ratio
if ratio <= 0 {
return 0, 0, fmt.Errorf("ratio must be a positive number")
} else if ratio > 1 {
return 0, 0, fmt.Errorf("ratio must be less than or equal to 1")
}

// get the instance type info
input := ec2.DescribeInstanceTypesInput{InstanceTypes: awsUtil.StringSlice([]string{instanceType})}
output, err := adapter.ec2Client.DescribeInstanceTypes(&input)
if err != nil {
return 0, 0, fmt.Errorf("failed to describe instance type %s: %v", instanceType, err)
}
if len(output.InstanceTypes) != 1 {
return 0, 0, fmt.Errorf("no instance type found with name: %s", instanceType)
}
instanceTypeInfo := output.InstanceTypes[0]

// calculate the CPU and memory sizes
cpu = int(float32(*instanceTypeInfo.VCpuInfo.DefaultVCpus) * ratio)
memory = int(float32(*instanceTypeInfo.MemoryInfo.SizeInMiB) * ratio)

return cpu, memory, nil
}

0 comments on commit b0a3449

Please sign in to comment.