-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (95 loc) · 2.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"time"
"github.com/SemanticSugar/rightsizer/clients"
"github.com/SemanticSugar/rightsizer/models"
"github.com/SemanticSugar/rightsizer/services"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/urfave/cli/v3"
"gopkg.in/yaml.v3"
)
func main() {
defaultDuration, _ := time.ParseDuration("336h")
app := &cli.Command{
Name: "rigthsizer",
Usage: "Right size your AWS ECS services.",
Version: "3.0.0",
HideHelpCommand: true,
ArgsUsage: "<cluster> <service>",
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.DurationFlag{
Name: "time-frame",
Aliases: []string{"t"},
Value: defaultDuration,
Usage: "Time `DURATION` to draw stats from",
},
&cli.StringFlag{
Name: "region",
Aliases: []string{"r"},
Usage: "AWS `REGION` to use",
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
if cmd.NArg() < 2 {
return errors.New("invocation requires <cluster> and <service> parameters")
}
clusterName := cmd.Args().Get(0)
serviceName := cmd.Args().Get(1)
// Parse that time frame
timeFrame := cmd.Duration("time-frame")
if timeFrame < time.Hour {
return errors.New("cannot see into the future just yet")
}
region := cmd.String("region")
if region == "" {
region = os.Getenv("AWS_REGION")
}
if region == "" {
return errors.New("cannot determine AWS region, checked the --region flag and the AWS_REGION environment variable")
}
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
if err != nil {
return fmt.Errorf("unable to load SDK config, %v", err)
}
awsCloudwatchClient := cloudwatch.NewFromConfig(cfg)
cloutwatchClient := clients.NewCloudWatchClient(awsCloudwatchClient)
usageService := services.NewUsageService(cloutwatchClient)
awsEcsClient := ecs.NewFromConfig(cfg)
ecsClient := clients.NewECSClient(awsEcsClient)
allocationService := services.NewAllocationService(ecsClient)
usage, err := usageService.GetUsage(ctx, &services.GetUsageInput{
ClusterName: clusterName,
ServiceName: serviceName,
TimeFrame: timeFrame,
})
if err != nil {
return fmt.Errorf("failed to get usage: %w", err)
}
allocation, err := allocationService.GetAllocation(ctx, &services.GetAllocationInput{
ClusterName: clusterName,
ServiceName: serviceName,
})
if err != nil {
return fmt.Errorf("failed to get allocation: %w", err)
}
newAllocation := allocation.Fix(usage, &models.Usage{CPU: 90, Memory: 90})
bytes, err := yaml.Marshal(newAllocation)
if err != nil {
return fmt.Errorf("failed to marshal allocation: %w", err)
}
fmt.Printf("%s", string(bytes))
return nil
},
}
if err := app.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}