-
Notifications
You must be signed in to change notification settings - Fork 3
/
ec2.go
111 lines (95 loc) · 2.84 KB
/
ec2.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
// Contains a set of method for getting EC2 information
package amazon
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/patrickmn/go-cache"
"github.com/re-cinq/aether/pkg/providers/util"
v1 "github.com/re-cinq/aether/pkg/types/v1"
)
// Helper service to get EC2 data
type ec2Client struct {
client *ec2.Client
}
// New instance
func NewEC2Client(cfg *aws.Config) *ec2Client {
emptyOptions := func(o *ec2.Options) {}
// Init the EC2 client
client := ec2.NewFromConfig(*cfg, emptyOptions)
// Make sure the initialisation was successful
if client == nil {
slog.Error("failed to create AWS EC2 client")
os.Exit(1)
}
// Return the ec2 service
return &ec2Client{
client: client,
}
}
// refresh stores all the instances for a specific region in cache
func (e *ec2Client) Refresh(ctx context.Context, ca *cache.Cache, region string) error {
// Override the region
withRegion := func(o *ec2.Options) {
o.Region = region
}
// First request
output, err := e.client.DescribeInstances(ctx, buildListPaginationRequest(nil), withRegion)
if err != nil || output == nil {
return fmt.Errorf("failed to retrieve ec2 instances from region: %s: %s", region, err)
}
// Collect all the responses for all the pages
instances := []ec2.DescribeInstancesOutput{*output}
for output.NextToken != nil {
output, err = e.client.DescribeInstances(ctx, buildListPaginationRequest(output.NextToken), withRegion)
if err != nil || output == nil {
return fmt.Errorf("failed to retrieve ec2 instances %s", err)
}
instances = append(instances, *output)
}
for _, reservation := range output.Reservations {
for index := range reservation.Instances {
instance := reservation.Instances[index]
id := aws.ToString(instance.InstanceId)
ca.Set(util.CacheKey(region, ec2Service, id),
&v1.Resource{
ID: id,
Name: getInstanceTag(instance.Tags, "Name"),
Service: ec2Service,
Region: region,
Kind: string(instance.InstanceType),
Lifecycle: string(instance.InstanceLifecycle),
VCPUCount: int(aws.ToInt32(instance.CpuOptions.CoreCount) * aws.ToInt32(instance.CpuOptions.ThreadsPerCore)),
LastUpdated: time.Now().UTC(),
},
cache.DefaultExpiration,
)
}
}
return nil
}
func getInstanceTag(tags []types.Tag, key string) string {
for _, tag := range tags {
if aws.ToString(tag.Key) == key {
return aws.ToString(tag.Value)
}
}
return ""
}
func buildListPaginationRequest(nextToken *string) *ec2.DescribeInstancesInput {
return &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("instance-state-name"),
Values: []string{"running", "pending"},
},
},
MaxResults: aws.Int32(50),
NextToken: nextToken,
}
}