Skip to content

Commit

Permalink
Merge pull request #161 from kaytu-io/feat-adds-terraform-automation
Browse files Browse the repository at this point in the history
fix: Fixes pull request
  • Loading branch information
salehkhazaei authored May 28, 2024
2 parents 6f70789 + b93c23a commit 75ca598
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kaytu-io/kaytu/preferences"
"github.com/spf13/cobra"
"github.com/zclconf/go-cty/cty"
"math"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -99,9 +100,12 @@ var terraformCmd = &cobra.Command{
}

recommendation := map[string]string{}
current := map[string]string{}
savings := map[string]float64{}
rightSizingDescription := map[string]string{}
for _, item := range jsonObj.Items {
var recommendedInstanceSize string
var currentInstanceSize string
maxRuntimeHours := int64(1) // since default for ignoreYoungerThan is 1
for _, device := range item.Devices {
for _, property := range device.Properties {
Expand All @@ -111,8 +115,10 @@ var terraformCmd = &cobra.Command{
}
if property.Key == "Instance Size" && property.Current != property.Recommended {
recommendedInstanceSize = property.Recommended
currentInstanceSize = property.Current
}
}
savings[item.Id] += device.CurrentCost - device.RightSizedCost
}

if maxRuntimeHours < ignoreYoungerThan {
Expand All @@ -122,6 +128,7 @@ var terraformCmd = &cobra.Command{
continue
}
recommendation[item.Id] = recommendedInstanceSize
current[item.Id] = currentInstanceSize
rightSizingDescription[item.Id] = item.Description
}

Expand All @@ -133,6 +140,7 @@ var terraformCmd = &cobra.Command{
body := file.Body()
localVars := map[string]string{}
countRightSized := 0
totalSavings := 0.0
var rightSizedIds []string
for _, block := range body.Blocks() {
if block.Type() == "locals" {
Expand Down Expand Up @@ -166,6 +174,7 @@ var terraformCmd = &cobra.Command{
block.Body().SetAttributeValue("instance_class", cty.StringVal(v))
countRightSized++
rightSizedIds = append(rightSizedIds, k)
totalSavings += savings[k]
}
}
}
Expand All @@ -176,6 +185,8 @@ var terraformCmd = &cobra.Command{
block.Body().SetAttributeValue("instance_class", cty.StringVal(recommendation[value]))
countRightSized++
rightSizedIds = append(rightSizedIds, value)
totalSavings += savings[value]

}
}
}
Expand All @@ -184,23 +195,30 @@ var terraformCmd = &cobra.Command{

description := ""
for _, id := range rightSizedIds {
description += fmt.Sprintf("Changing instance class of %s to %s\n", id, recommendation[id])
description += rightSizingDescription[id] + "\n\n"
description += fmt.Sprintf("**%s:**\n", id)
description += fmt.Sprintf("- Changing instance class from %s to %s\n\n", current[id], recommendation[id])
description += "Reasoning: " + rightSizingDescription[id] + "\n\n"
description += "-------------------------------------------------------------------------\n\n"
}

if countRightSized == 0 {
return nil
}

reduceOrIncreaseWord := "reduces"
if totalSavings < 0 {
reduceOrIncreaseWord = "increases"
}
return github.ApplyChanges(
utils.ReadStringFlag(cmd, "github-owner"),
utils.ReadStringFlag(cmd, "github-repo"),
utils.ReadStringFlag(cmd, "github-username"),
utils.ReadStringFlag(cmd, "github-token"),
utils.ReadStringFlag(cmd, "github-base-branch"),
fmt.Sprintf("SRE Bot right sizing %d resources", countRightSized),
fmt.Sprintf("srebot: resizing %d resources.", countRightSized),
utils.ReadStringFlag(cmd, "terraform-file-path"),
string(file.Bytes()),
fmt.Sprintf("SRE Bot right sizing %d resources", countRightSized),
fmt.Sprintf("srebot: resizing %d resources. %s by $%.0f", countRightSized, reduceOrIncreaseWord, math.Abs(totalSavings)),
description,
)
},
Expand Down

0 comments on commit 75ca598

Please sign in to comment.