forked from kubernetes-sigs/aws-load-balancer-controller
/
ec2.go
105 lines (86 loc) · 2.94 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
package awsutil
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/golang/glog"
"github.com/karlseguin/ccache"
"github.com/prometheus/client_golang/prometheus"
)
// EC2 is our extension to AWS's ec2.EC2
type EC2 struct {
Svc ec2iface.EC2API
}
var ec2Cache = ccache.New(ccache.Configure())
// NewEC2 returns an awsutil EC2 service
func NewEC2(awsconfig *aws.Config) *EC2 {
awsSession, err := session.NewSession(awsconfig)
if err != nil {
AWSErrorCount.With(prometheus.Labels{"service": "EC2", "request": "NewSession"}).Add(float64(1))
glog.Errorf("Failed to create AWS session. Error: %s.", err.Error())
return nil
}
awsSession.Handlers.Send.PushFront(func(r *request.Request) {
AWSRequest.With(prometheus.Labels{"service": r.ClientInfo.ServiceName, "operation": r.Operation.Name}).Add(float64(1))
if AWSDebug {
glog.Infof("Request: %s/%s, Payload: %s", r.ClientInfo.ServiceName, r.Operation, r.Params)
}
})
elbClient := EC2{
ec2.New(awsSession),
}
return &elbClient
}
// DescribeSubnets looks up Subnets based on input and returns a list of Subnets.
func (e *EC2) DescribeSubnets(in ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) {
o, err := e.Svc.DescribeSubnets(&in)
if err != nil {
AWSErrorCount.With(
prometheus.Labels{"service": "EC2", "request": "DescribeSubnets"}).Add(float64(1))
return nil, err
}
return o.Subnets, nil
}
// DescribeSecurityGroups looks up Security Groups based on input and returns a list of Security
// Groups.
func (e *EC2) DescribeSecurityGroups(in ec2.DescribeSecurityGroupsInput) ([]*ec2.SecurityGroup, error) {
o, err := e.Svc.DescribeSecurityGroups(&in)
if err != nil {
AWSErrorCount.With(
prometheus.Labels{"service": "EC2", "request": "DescribeSecurityGroups"}).Add(float64(1))
return nil, err
}
return o.SecurityGroups, nil
}
// GetVPCID retrieves the VPC that the subents passed are contained in.
func (e *EC2) GetVPCID(subnets []*string) (*string, error) {
var vpc *string
if len(subnets) == 0 {
return nil, fmt.Errorf("Empty subnet list provided to getVPCID")
}
key := fmt.Sprintf("%s-vpc", *subnets[0])
item := ec2Cache.Get(key)
if item == nil {
subnetInfo, err := e.Svc.DescribeSubnets(&ec2.DescribeSubnetsInput{
SubnetIds: subnets,
})
if err != nil {
AWSErrorCount.With(prometheus.Labels{"service": "EC2", "request": "DescribeSubnets"}).Add(float64(1))
return nil, err
}
if len(subnetInfo.Subnets) == 0 {
return nil, fmt.Errorf("DescribeSubnets returned no subnets")
}
vpc = subnetInfo.Subnets[0].VpcId
ec2Cache.Set(key, vpc, time.Minute*60)
AWSCache.With(prometheus.Labels{"cache": "vpc", "action": "miss"}).Add(float64(1))
} else {
vpc = item.Value().(*string)
AWSCache.With(prometheus.Labels{"cache": "vpc", "action": "hit"}).Add(float64(1))
}
return vpc, nil
}