Skip to content

Commit 93e3fbe

Browse files
committed
Fixing up some caching in EC2
1 parent 39f52de commit 93e3fbe

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

pkg/aws/albec2/ec2.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewEC2Metadata(awsSession *session.Session) {
5858

5959
// DescribeSGByPermissionGroup Finds an SG that the passed SG has permission to.
6060
func (e *EC2) DescribeSGByPermissionGroup(sg *string) (*string, error) {
61-
cache := "sg-permission-group"
61+
cache := "EC2-DescribeSGByPermissionGroup"
6262
key := cache + "." + *sg
6363
item := e.cache.Get(key)
6464

@@ -68,6 +68,8 @@ func (e *EC2) DescribeSGByPermissionGroup(sg *string) (*string, error) {
6868
return groupid, nil
6969
}
7070

71+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
72+
7173
in := &ec2.DescribeSecurityGroupsInput{
7274
Filters: []*ec2.Filter{
7375
{
@@ -86,13 +88,12 @@ func (e *EC2) DescribeSGByPermissionGroup(sg *string) (*string, error) {
8688
}
8789

8890
e.cache.Set(key, o.SecurityGroups[0].GroupId, time.Minute*5)
89-
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
9091
return o.SecurityGroups[0].GroupId, nil
9192
}
9293

9394
// DescribeSGPorts returns the ports associated with a SG.
9495
func (e *EC2) DescribeSGPorts(sgID *string) ([]int64, error) {
95-
cache := "sg-ports"
96+
cache := "EC2-DescribeSGPorts"
9697
key := cache + "." + *sgID
9798
item := e.cache.Get(key)
9899

@@ -102,6 +103,8 @@ func (e *EC2) DescribeSGPorts(sgID *string) ([]int64, error) {
102103
return ports, nil
103104
}
104105

106+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
107+
105108
in := &ec2.DescribeSecurityGroupsInput{
106109
GroupIds: []*string{sgID},
107110
}
@@ -117,12 +120,23 @@ func (e *EC2) DescribeSGPorts(sgID *string) ([]int64, error) {
117120
}
118121

119122
e.cache.Set(key, ports, time.Minute*5)
120-
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
121123
return ports, nil
122124
}
123125

124126
// DescribeSGInboundCidrs returns the inbound cidrs associated with a SG.
125127
func (e *EC2) DescribeSGInboundCidrs(sgID *string) ([]*string, error) {
128+
cache := "EC2-DescribeSGInboundCidrs"
129+
key := cache + "." + *sgID
130+
item := e.cache.Get(key)
131+
132+
if item != nil {
133+
tags := item.Value().([]*string)
134+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "hit"}).Add(float64(1))
135+
return tags, nil
136+
}
137+
138+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
139+
126140
in := &ec2.DescribeSecurityGroupsInput{
127141
GroupIds: []*string{sgID},
128142
}
@@ -139,12 +153,13 @@ func (e *EC2) DescribeSGInboundCidrs(sgID *string) ([]*string, error) {
139153
}
140154
}
141155

156+
e.cache.Set(key, inboundCidrs, time.Minute*5)
142157
return inboundCidrs, nil
143158
}
144159

145160
// DescribeSGTags returns tags for an sg when the sg-id is provided.
146161
func (e *EC2) DescribeSGTags(sgID *string) ([]*ec2.TagDescription, error) {
147-
cache := "sg-tags"
162+
cache := "EC2-DescribeSGTags"
148163
key := cache + "." + *sgID
149164
item := e.cache.Get(key)
150165

@@ -154,6 +169,8 @@ func (e *EC2) DescribeSGTags(sgID *string) ([]*ec2.TagDescription, error) {
154169
return tags, nil
155170
}
156171

172+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
173+
157174
in := &ec2.DescribeTagsInput{
158175
Filters: []*ec2.Filter{
159176
{
@@ -169,7 +186,6 @@ func (e *EC2) DescribeSGTags(sgID *string) ([]*ec2.TagDescription, error) {
169186
}
170187

171188
e.cache.Set(key, o.Tags, time.Minute*5)
172-
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "miss"}).Add(float64(1))
173189
return o.Tags, nil
174190
}
175191

@@ -189,7 +205,7 @@ func (e *EC2) DeleteSecurityGroupByID(sgID *string) error {
189205
// exists. If it does, it attempts to remove the managedSG from the list.
190206
func (e *EC2) DisassociateSGFromInstanceIfNeeded(instances []*string, managedSG *string) error {
191207
if managedSG == nil {
192-
return fmt.Errorf("Managed SG passed was empty unable to disassociate from instances.")
208+
return fmt.Errorf("Managed SG passed was empty unable to disassociate from instances")
193209
}
194210
in := &ec2.DescribeInstancesInput{
195211
InstanceIds: instances,
@@ -595,13 +611,14 @@ func (e *EC2) GetVPCID() (*string, error) {
595611

596612
// If previously looked up (and not expired) the VpcId will be stored in the cache under the
597613
// key 'vpc'.
598-
key := "vpc"
614+
cache := "EC2-GetVPCID"
615+
key := cache
599616
item := e.cache.Get(key)
600617

601618
// cache hit: return (pointer of) VpcId value
602619
if item != nil {
603620
vpc = item.Value().(*string)
604-
albprom.AWSCache.With(prometheus.Labels{"cache": "vpc", "action": "hit"}).Add(float64(1))
621+
albprom.AWSCache.With(prometheus.Labels{"cache": cache, "action": "hit"}).Add(float64(1))
605622
return vpc, nil
606623
}
607624

0 commit comments

Comments
 (0)