Skip to content

Commit

Permalink
fix: Fixes right sizing pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
salehkhazaei committed May 28, 2024
1 parent 60b1b1b commit 2cafcb1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 120 deletions.
15 changes: 15 additions & 0 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/zclconf/go-cty/cty"
"regexp"
"strconv"
"strings"
"time"
)
Expand All @@ -23,6 +24,7 @@ var terraformCmd = &cobra.Command{
Short: "Create pull request for right sizing opportunities on your terraform git",
Long: "Create pull request for right sizing opportunities on your terraform git",
RunE: func(cmd *cobra.Command, args []string) error {
ignoreYoungerThan := utils.ReadIntFlag(cmd, "ignore-younger-than")
contentBytes, err := github.GetFile(
utils.ReadStringFlag(cmd, "github-owner"),
utils.ReadStringFlag(cmd, "github-repo"),
Expand Down Expand Up @@ -100,13 +102,22 @@ var terraformCmd = &cobra.Command{
rightSizingDescription := map[string]string{}
for _, item := range jsonObj.Items {
var recommendedInstanceSize string
maxRuntimeHours := int64(1) // since default for ignoreYoungerThan is 1
for _, device := range item.Devices {
for _, property := range device.Properties {
if property.Key == "RuntimeHours" {
i, _ := strconv.ParseInt(property.Current, 10, 64)
maxRuntimeHours = max(maxRuntimeHours, i)
}
if property.Key == "Instance Size" && property.Current != property.Recommended {
recommendedInstanceSize = property.Recommended
}
}
}

if maxRuntimeHours < ignoreYoungerThan {
continue
}
if recommendedInstanceSize == "" {
continue
}
Expand Down Expand Up @@ -176,6 +187,10 @@ var terraformCmd = &cobra.Command{
description += fmt.Sprintf("Changing instance class of %s to %s\n", id, recommendation[id])
description += rightSizingDescription[id] + "\n\n"
}

if countRightSized == 0 {
return nil
}
return github.ApplyChanges(
utils.ReadStringFlag(cmd, "github-owner"),
utils.ReadStringFlag(cmd, "github-repo"),
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/proto/plugin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ message Property {
string average = 3;
string max = 4;
string recommended = 5;
bool hidden = 6;
}

message Device {
Expand Down
Loading

0 comments on commit 2cafcb1

Please sign in to comment.