Skip to content

Commit 441e966

Browse files
committed
Revert "Merge pull request #626 from kubernetes-sigs/session-caching"
This reverts commit b4a69a2, reversing changes made to ed7905a.
1 parent b4a69a2 commit 441e966

File tree

7,021 files changed

+3618
-3460937
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

+3618
-3460937
lines changed

Gopkg.lock

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

cmd/main.go

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

3030
"github.com/golang/glog"
31-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric/collectors"
3231
"github.com/prometheus/client_golang/prometheus"
3332
"github.com/prometheus/client_golang/prometheus/promhttp"
34-
"github.com/ticketmaster/aws-sdk-go-cache/cache"
3533

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

8987
conf.Client = kubeClient
9088

91-
cc := cache.NewConfig(5 * time.Minute)
92-
9389
reg := prometheus.NewRegistry()
9490

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

9994
mc, err := metric.NewCollector(reg)
10095
if err != nil {
10196
glog.Fatalf("Error creating prometheus collectos: %v", err)
10297
}
10398
mc.Start()
10499

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

internal/alb/ls/listener_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ 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"
1617
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albelbv2"
1718
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/loadbalancer"
1819
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
20+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric"
1921
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
2022
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
2123
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
@@ -41,6 +43,8 @@ func init() {
4143
albelbv2.ELBV2svc = albelbv2.NewDummy()
4244
albec2.EC2svc = &mocks.EC2API{}
4345

46+
albcache.NewCache(metric.DummyCollector{})
47+
4448
rOpts1 = &ReconcileOptions{
4549
TargetGroups: tg.TargetGroups{tg.DummyTG("tg1", "service")},
4650
LoadBalancerArn: nil,

internal/aws/albcache/cache.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package albcache
2+
3+
import (
4+
"time"
5+
6+
"github.com/karlseguin/ccache"
7+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric"
8+
"github.com/prometheus/client_golang/prometheus"
9+
)
10+
11+
var cache *ccache.Cache
12+
var mc metric.Collector
13+
14+
func NewCache(m metric.Collector) {
15+
cache = ccache.New(ccache.Configure())
16+
mc = m
17+
}
18+
19+
func Get(c, n string) *ccache.Item {
20+
key := c + "." + n
21+
i := cache.Get(key)
22+
if i == nil || i.Expired() {
23+
mc.IncAPICacheCount(prometheus.Labels{"cache": c, "action": "miss"})
24+
return nil
25+
}
26+
mc.IncAPICacheCount(prometheus.Labels{"cache": c, "action": "hit"})
27+
return i
28+
}
29+
30+
func Set(c, n string, value interface{}, duration time.Duration) {
31+
key := c + "." + n
32+
cache.Set(key, value, duration)
33+
}
34+
35+
func Delete(cacheKey string) {
36+
cache.Delete(cacheKey)
37+
}

internal/aws/albec2/ec2.go

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

910
"github.com/aws/aws-sdk-go/aws/awserr"
1011
"github.com/aws/aws-sdk-go/aws/request"
@@ -20,6 +21,7 @@ import (
2021
"github.com/aws/aws-sdk-go/service/ec2"
2122
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
2223

24+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albcache"
2325
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
2426
)
2527

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

3335
tagNameSubnetInternalELB = "kubernetes.io/role/internal-elb"
3436
tagNameSubnetPublicELB = "kubernetes.io/role/elb"
37+
38+
GetSecurityGroupsCacheTTL = time.Minute * 60
39+
GetSubnetsCacheTTL = time.Minute * 60
40+
41+
IsNodeHealthyCacheTTL = time.Minute * 5
3542
)
3643

3744
// EC2svc is the singleton points to our aws EC2 api
@@ -80,6 +87,8 @@ type EC2 struct {
8087
}
8188

8289
// 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.
8392
type EC2MData struct {
8493
*ec2metadata.EC2Metadata
8594
}
@@ -104,6 +113,23 @@ func (e *EC2) GetSubnets(names []*string) (subnets []*string, err error) {
104113
return
105114
}
106115

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+
107133
in := &ec2.DescribeSubnetsInput{Filters: []*ec2.Filter{
108134
{
109135
Name: aws.String("tag:Name"),
@@ -121,8 +147,9 @@ func (e *EC2) GetSubnets(names []*string) (subnets []*string, err error) {
121147
}
122148

123149
for _, subnet := range describeSubnetsOutput.Subnets {
124-
_, ok := util.EC2Tags(subnet.Tags).Get("Name")
150+
value, ok := util.EC2Tags(subnet.Tags).Get("Name")
125151
if ok {
152+
albcache.Set(cacheName, value, subnet.SubnetId, GetSubnetsCacheTTL)
126153
subnets = append(subnets, subnet.SubnetId)
127154
}
128155
}
@@ -135,10 +162,27 @@ func (e *EC2) GetSecurityGroups(names []*string) (sgs []*string, err error) {
135162
return
136163
}
137164

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+
138182
in := &ec2.DescribeSecurityGroupsInput{Filters: []*ec2.Filter{
139183
{
140184
Name: aws.String("tag:Name"),
141-
Values: names,
185+
Values: queryNames,
142186
},
143187
{
144188
Name: aws.String("vpc-id"),
@@ -152,6 +196,8 @@ func (e *EC2) GetSecurityGroups(names []*string) (sgs []*string, err error) {
152196
}
153197

154198
for _, sg := range describeSecurityGroupsOutput.SecurityGroups {
199+
name, _ := util.EC2Tags(sg.Tags).Get("Name")
200+
albcache.Set(cacheName, name, sg.GroupId, GetSecurityGroupsCacheTTL)
155201
sgs = append(sgs, sg.GroupId)
156202
}
157203

@@ -258,12 +304,27 @@ func (e *EC2) GetVPCID() (*string, error) {
258304
return &v, nil
259305
}
260306

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
261320
identityDoc, err := EC2Metadatasvc.GetInstanceIdentityDocument()
262321
if err != nil {
263322
return nil, err
264323
}
265324

266325
// 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.
267328
descInstancesInput := &ec2.DescribeInstancesInput{
268329
InstanceIds: []*string{aws.String(identityDoc.InstanceID)},
269330
}
@@ -281,10 +342,21 @@ func (e *EC2) GetVPCID() (*string, error) {
281342
}
282343

283344
vpc = descInstancesOutput.Reservations[0].Instances[0].VpcId
345+
// cache the retrieved VpcId for next call
346+
albcache.Set(cacheName, "", vpc, time.Minute*60)
284347
return vpc, nil
285348
}
286349

287350
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+
288360
o, err := e.DescribeVpcs(&ec2.DescribeVpcsInput{
289361
VpcIds: []*string{id},
290362
})
@@ -295,6 +367,7 @@ func (e *EC2) GetVPC(id *string) (*ec2.Vpc, error) {
295367
return nil, fmt.Errorf("Invalid amount of VPCs %d returned for %s", len(o.Vpcs), *id)
296368
}
297369

370+
albcache.Set(cacheName, *id, o.Vpcs[0], time.Minute*60)
298371
return o.Vpcs[0], nil
299372
}
300373

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

413+
cacheName := "ClusterSubnets"
414+
340415
if *scheme == elbv2.LoadBalancerSchemeEnumInternal {
341416
key = tagNameSubnetInternalELB
342417
} else if *scheme == elbv2.LoadBalancerSchemeEnumInternetFacing {
@@ -356,7 +431,15 @@ func ClusterSubnets(scheme *string) (util.Subnets, error) {
356431
if *tag.Key == key {
357432
p := strings.Split(arn, "/")
358433
subnetID := &p[len(p)-1]
359-
filterValues = append(filterValues, subnetID)
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+
}
360443
}
361444
}
362445
}
@@ -383,6 +466,7 @@ func ClusterSubnets(scheme *string) (util.Subnets, error) {
383466
if subnetIsUsable(subnet, useableSubnets) {
384467
useableSubnets = append(useableSubnets, subnet)
385468
out = append(out, subnet.SubnetId)
469+
albcache.Set(cacheName, *subnet.SubnetId, subnet, time.Minute*60)
386470
}
387471
}
388472

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

415499
// IsNodeHealthy returns true if the node is ready
416500
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+
417508
in := &ec2.DescribeInstanceStatusInput{
418509
InstanceIds: []*string{aws.String(instanceid)},
419510
}
@@ -427,8 +518,10 @@ func (e *EC2) IsNodeHealthy(instanceid string) (bool, error) {
427518
continue
428519
}
429520
if *instanceStatus.InstanceState.Code == 16 { // running
521+
albcache.Set(cacheName, instanceid, true, IsNodeHealthyCacheTTL)
430522
return true, nil
431523
}
524+
albcache.Set(cacheName, instanceid, false, IsNodeHealthyCacheTTL)
432525
return false, nil
433526
}
434527

internal/aws/albelbv2/dummy.go

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

33+
// CacheDelete ...
34+
func (d *Dummy) CacheDelete(string, string) {
35+
return
36+
}
37+
3338
// ClusterLoadBalancers ...
3439
func (d *Dummy) ClusterLoadBalancers() ([]*elbv2.LoadBalancer, error) {
3540
return nil, nil

0 commit comments

Comments
 (0)