Skip to content

Commit b4a69a2

Browse files
authored
Merge pull request #626 from kubernetes-sigs/session-caching
Implement caching at the AWS SDK session
2 parents ed7905a + a66bcc9 commit b4a69a2

File tree

7,021 files changed

+3460936
-3617
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

7,021 files changed

+3460936
-3617
lines changed

Gopkg.lock

Lines changed: 46 additions & 526 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
"time"
2929

3030
"github.com/golang/glog"
31+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric/collectors"
3132
"github.com/prometheus/client_golang/prometheus"
3233
"github.com/prometheus/client_golang/prometheus/promhttp"
34+
"github.com/ticketmaster/aws-sdk-go-cache/cache"
3335

3436
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3537
"k8s.io/apimachinery/pkg/util/wait"
@@ -86,18 +88,21 @@ func main() {
8688

8789
conf.Client = kubeClient
8890

91+
cc := cache.NewConfig(5 * time.Minute)
92+
8993
reg := prometheus.NewRegistry()
9094

95+
reg.MustRegister(cc.NewCacheCollector(collectors.PrometheusNamespace))
9196
reg.MustRegister(prometheus.NewGoCollector())
92-
reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
97+
reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
9398

9499
mc, err := metric.NewCollector(reg)
95100
if err != nil {
96101
glog.Fatalf("Error creating prometheus collectos: %v", err)
97102
}
98103
mc.Start()
99104

100-
c := controller.NewALBController(conf, mc)
105+
c := controller.NewALBController(conf, mc, cc)
101106
go handleSigterm(c, func(code int) {
102107
os.Exit(code)
103108
})

internal/alb/ls/listener_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import (
1313
"github.com/aws/aws-sdk-go/aws"
1414
"github.com/aws/aws-sdk-go/service/elbv2"
1515
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/alb/tg"
16-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albcache"
1716
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albelbv2"
1817
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/loadbalancer"
1918
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
20-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric"
2119
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
2220
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
2321
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
@@ -43,8 +41,6 @@ func init() {
4341
albelbv2.ELBV2svc = albelbv2.NewDummy()
4442
albec2.EC2svc = &mocks.EC2API{}
4543

46-
albcache.NewCache(metric.DummyCollector{})
47-
4844
rOpts1 = &ReconcileOptions{
4945
TargetGroups: tg.TargetGroups{tg.DummyTG("tg1", "service")},
5046
LoadBalancerArn: nil,

internal/aws/albcache/cache.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

internal/aws/albec2/ec2.go

Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"sort"
77
"strings"
8-
"time"
98

109
"github.com/aws/aws-sdk-go/aws/awserr"
1110
"github.com/aws/aws-sdk-go/aws/request"
@@ -21,7 +20,6 @@ import (
2120
"github.com/aws/aws-sdk-go/service/ec2"
2221
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
2322

24-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albcache"
2523
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
2624
)
2725

@@ -34,11 +32,6 @@ const (
3432

3533
tagNameSubnetInternalELB = "kubernetes.io/role/internal-elb"
3634
tagNameSubnetPublicELB = "kubernetes.io/role/elb"
37-
38-
GetSecurityGroupsCacheTTL = time.Minute * 60
39-
GetSubnetsCacheTTL = time.Minute * 60
40-
41-
IsNodeHealthyCacheTTL = time.Minute * 5
4235
)
4336

4437
// EC2svc is the singleton points to our aws EC2 api
@@ -87,8 +80,6 @@ type EC2 struct {
8780
}
8881

8982
// EC2MData is our extension to AWS's ec2metadata.EC2Metadata
90-
// cache is not required for this struct as we only use it to lookup
91-
// instance metadata when the cache for the EC2 struct is expired.
9283
type EC2MData struct {
9384
*ec2metadata.EC2Metadata
9485
}
@@ -113,23 +104,6 @@ func (e *EC2) GetSubnets(names []*string) (subnets []*string, err error) {
113104
return
114105
}
115106

116-
cacheName := "EC2.GetSubnets"
117-
var queryNames []*string
118-
119-
for _, n := range names {
120-
item := albcache.Get(cacheName, *n)
121-
122-
if item != nil {
123-
subnets = append(subnets, item.Value().(*string))
124-
} else {
125-
queryNames = append(queryNames, n)
126-
}
127-
}
128-
129-
if len(queryNames) == 0 {
130-
return
131-
}
132-
133107
in := &ec2.DescribeSubnetsInput{Filters: []*ec2.Filter{
134108
{
135109
Name: aws.String("tag:Name"),
@@ -147,9 +121,8 @@ func (e *EC2) GetSubnets(names []*string) (subnets []*string, err error) {
147121
}
148122

149123
for _, subnet := range describeSubnetsOutput.Subnets {
150-
value, ok := util.EC2Tags(subnet.Tags).Get("Name")
124+
_, ok := util.EC2Tags(subnet.Tags).Get("Name")
151125
if ok {
152-
albcache.Set(cacheName, value, subnet.SubnetId, GetSubnetsCacheTTL)
153126
subnets = append(subnets, subnet.SubnetId)
154127
}
155128
}
@@ -162,27 +135,10 @@ func (e *EC2) GetSecurityGroups(names []*string) (sgs []*string, err error) {
162135
return
163136
}
164137

165-
cacheName := "EC2.GetSecurityGroups"
166-
var queryNames []*string
167-
168-
for _, n := range names {
169-
item := albcache.Get(cacheName, *n)
170-
171-
if item != nil {
172-
sgs = append(sgs, item.Value().(*string))
173-
} else {
174-
queryNames = append(queryNames, n)
175-
}
176-
}
177-
178-
if len(queryNames) == 0 {
179-
return
180-
}
181-
182138
in := &ec2.DescribeSecurityGroupsInput{Filters: []*ec2.Filter{
183139
{
184140
Name: aws.String("tag:Name"),
185-
Values: queryNames,
141+
Values: names,
186142
},
187143
{
188144
Name: aws.String("vpc-id"),
@@ -196,8 +152,6 @@ func (e *EC2) GetSecurityGroups(names []*string) (sgs []*string, err error) {
196152
}
197153

198154
for _, sg := range describeSecurityGroupsOutput.SecurityGroups {
199-
name, _ := util.EC2Tags(sg.Tags).Get("Name")
200-
albcache.Set(cacheName, name, sg.GroupId, GetSecurityGroupsCacheTTL)
201155
sgs = append(sgs, sg.GroupId)
202156
}
203157

@@ -304,27 +258,12 @@ func (e *EC2) GetVPCID() (*string, error) {
304258
return &v, nil
305259
}
306260

307-
// If previously looked up (and not expired) the VpcId will be stored in the cache under the
308-
// key 'vpc'.
309-
cacheName := "EC2.GetVPCID"
310-
item := albcache.Get(cacheName, "")
311-
312-
// cache hit: return (pointer of) VpcId value
313-
if item != nil {
314-
vpc = item.Value().(*string)
315-
return vpc, nil
316-
}
317-
318-
// cache miss: begin lookup of VpcId based on current EC2 instance
319-
// retrieve identity of current running instance
320261
identityDoc, err := EC2Metadatasvc.GetInstanceIdentityDocument()
321262
if err != nil {
322263
return nil, err
323264
}
324265

325266
// capture instance ID for lookup in DescribeInstances
326-
// don't bother caching this value as it should never be re-retrieved unless
327-
// the cache for the VpcId (looked up below) expires.
328267
descInstancesInput := &ec2.DescribeInstancesInput{
329268
InstanceIds: []*string{aws.String(identityDoc.InstanceID)},
330269
}
@@ -342,21 +281,10 @@ func (e *EC2) GetVPCID() (*string, error) {
342281
}
343282

344283
vpc = descInstancesOutput.Reservations[0].Instances[0].VpcId
345-
// cache the retrieved VpcId for next call
346-
albcache.Set(cacheName, "", vpc, time.Minute*60)
347284
return vpc, nil
348285
}
349286

350287
func (e *EC2) GetVPC(id *string) (*ec2.Vpc, error) {
351-
cacheName := "EC2.GetVPCID"
352-
item := albcache.Get(cacheName, *id)
353-
354-
// cache hit: return (pointer of) VpcId value
355-
if item != nil {
356-
vpc := item.Value().(*ec2.Vpc)
357-
return vpc, nil
358-
}
359-
360288
o, err := e.DescribeVpcs(&ec2.DescribeVpcsInput{
361289
VpcIds: []*string{id},
362290
})
@@ -367,7 +295,6 @@ func (e *EC2) GetVPC(id *string) (*ec2.Vpc, error) {
367295
return nil, fmt.Errorf("Invalid amount of VPCs %d returned for %s", len(o.Vpcs), *id)
368296
}
369297

370-
albcache.Set(cacheName, *id, o.Vpcs[0], time.Minute*60)
371298
return o.Vpcs[0], nil
372299
}
373300

@@ -410,8 +337,6 @@ func ClusterSubnets(scheme *string) (util.Subnets, error) {
410337
var out util.AWSStringSlice
411338
var key string
412339

413-
cacheName := "ClusterSubnets"
414-
415340
if *scheme == elbv2.LoadBalancerSchemeEnumInternal {
416341
key = tagNameSubnetInternalELB
417342
} else if *scheme == elbv2.LoadBalancerSchemeEnumInternetFacing {
@@ -431,15 +356,7 @@ func ClusterSubnets(scheme *string) (util.Subnets, error) {
431356
if *tag.Key == key {
432357
p := strings.Split(arn, "/")
433358
subnetID := &p[len(p)-1]
434-
item := albcache.Get(cacheName, *subnetID)
435-
if item != nil {
436-
if subnetIsUsable(item.Value().(*ec2.Subnet), useableSubnets) {
437-
useableSubnets = append(useableSubnets, item.Value().(*ec2.Subnet))
438-
out = append(out, item.Value().(*ec2.Subnet).SubnetId)
439-
}
440-
} else {
441-
filterValues = append(filterValues, subnetID)
442-
}
359+
filterValues = append(filterValues, subnetID)
443360
}
444361
}
445362
}
@@ -466,7 +383,6 @@ func ClusterSubnets(scheme *string) (util.Subnets, error) {
466383
if subnetIsUsable(subnet, useableSubnets) {
467384
useableSubnets = append(useableSubnets, subnet)
468385
out = append(out, subnet.SubnetId)
469-
albcache.Set(cacheName, *subnet.SubnetId, subnet, time.Minute*60)
470386
}
471387
}
472388

@@ -498,13 +414,6 @@ func subnetIsUsable(new *ec2.Subnet, existing []*ec2.Subnet) bool {
498414

499415
// IsNodeHealthy returns true if the node is ready
500416
func (e *EC2) IsNodeHealthy(instanceid string) (bool, error) {
501-
cacheName := "ec2.IsNodeHealthy"
502-
item := albcache.Get(cacheName, instanceid)
503-
504-
if item != nil {
505-
return item.Value().(bool), nil
506-
}
507-
508417
in := &ec2.DescribeInstanceStatusInput{
509418
InstanceIds: []*string{aws.String(instanceid)},
510419
}
@@ -518,10 +427,8 @@ func (e *EC2) IsNodeHealthy(instanceid string) (bool, error) {
518427
continue
519428
}
520429
if *instanceStatus.InstanceState.Code == 16 { // running
521-
albcache.Set(cacheName, instanceid, true, IsNodeHealthyCacheTTL)
522430
return true, nil
523431
}
524-
albcache.Set(cacheName, instanceid, false, IsNodeHealthyCacheTTL)
525432
return false, nil
526433
}
527434

internal/aws/albelbv2/dummy.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ func NewDummy() *Dummy {
3030
return d
3131
}
3232

33-
// CacheDelete ...
34-
func (d *Dummy) CacheDelete(string, string) {
35-
return
36-
}
37-
3833
// ClusterLoadBalancers ...
3934
func (d *Dummy) ClusterLoadBalancers() ([]*elbv2.LoadBalancer, error) {
4035
return nil, nil

0 commit comments

Comments
 (0)