Skip to content

Commit 8f9a502

Browse files
shoekstraM00nF1sh
authored andcommitted
Use apimachinery/pkg/util/cache for caching
Signed-off-by: Stephen Hoekstra <[email protected]>
1 parent a6df3bc commit 8f9a502

File tree

8 files changed

+30
-33
lines changed

8 files changed

+30
-33
lines changed

docs/deploy/configurations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Currently, you can set only 1 namespace to watch in this flag. See [this Kuberne
7171
|aws-api-throttle | AWS Throttle Config | [default value](#default-throttle-config ) | throttle settings for AWS APIs, format: serviceID1:operationRegex1=rate:burst,serviceID2:operationRegex2=rate:burst |
7272
|aws-max-retries | int | 10 | Maximum retries for AWS APIs |
7373
|aws-region | string | [instance metadata](#instance-metadata) | AWS Region for the kubernetes cluster |
74-
|aws-vpc-cache-duration | string | 10m | Length of time in minutes to cache VPC info |
74+
|aws-vpc-cache-ttl | string | 10m | Length of time in minutes to cache VPC info |
7575
|aws-vpc-id | string | [instance metadata](#instance-metadata) | ID of the AWS VPC where Load Balancer resources will be created |
7676
|cluster-name | string | | Kubernetes cluster name|
7777
|default-tags | stringMap | | AWS Tags that will be applied to all AWS resources managed by this controller. Specified Tags takes highest priority |

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/google/go-cmp v0.5.6
1111
github.com/onsi/ginkgo v1.16.4
1212
github.com/onsi/gomega v1.16.0
13-
github.com/patrickmn/go-cache v2.1.0+incompatible
1413
github.com/pkg/errors v0.9.1
1514
github.com/prometheus/client_golang v1.11.0
1615
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,6 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
643643
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
644644
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
645645
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
646-
github.com/patrickmn/go-cache v1.0.0 h1:3gD5McaYs9CxjyK5AXGcq8gdeCARtd/9gJDUvVeaZ0Y=
647-
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
648-
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
649646
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
650647
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
651648
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func main() {
103103
sgManager := networking.NewDefaultSecurityGroupManager(cloud.EC2(), ctrl.Log)
104104
sgReconciler := networking.NewDefaultSecurityGroupReconciler(sgManager, ctrl.Log)
105105
azInfoProvider := networking.NewDefaultAZInfoProvider(cloud.EC2(), ctrl.Log.WithName("az-info-provider"))
106-
vpcInfoProvider := networking.NewDefaultVPCInfoProvider(cloud.VpcCacheDuration(), cloud.EC2(), ctrl.Log.WithName("vpc-info-provider"))
106+
vpcInfoProvider := networking.NewDefaultVPCInfoProvider(cloud.EC2(), ctrl.Log.WithName("vpc-info-provider"), cloud.VpcCacheTTL())
107107
subnetResolver := networking.NewDefaultSubnetsResolver(azInfoProvider, cloud.EC2(), cloud.VpcID(), controllerCFG.ClusterName, ctrl.Log.WithName("subnets-resolver"))
108108
vpcResolver := networking.NewDefaultVPCResolver(cloud.EC2(), cloud.VpcID(), ctrl.Log.WithName("vpc-resolver"))
109109
tgbResManager := targetgroupbinding.NewDefaultResourceManager(mgr.GetClient(), cloud.ELBV2(), cloud.EC2(),

pkg/aws/cloud.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type Cloud interface {
4343
// ID of VPC to create load balancers in
4444
VpcID() string
4545

46-
// VPC cache duration in minutes
47-
VpcCacheDuration() time.Duration
46+
// VPC cache TTL in minutes
47+
VpcCacheTTL() time.Duration
4848
}
4949

5050
// NewCloud constructs new Cloud implementation.
@@ -155,6 +155,6 @@ func (c *defaultCloud) VpcID() string {
155155
return c.cfg.VpcID
156156
}
157157

158-
func (c *defaultCloud) VpcCacheDuration() time.Duration {
159-
return c.cfg.VpcCacheDuration
158+
func (c *defaultCloud) VpcCacheTTL() time.Duration {
159+
return c.cfg.VpcCacheTTL
160160
}

pkg/aws/cloud_config.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
)
99

1010
const (
11-
flagAWSRegion = "aws-region"
12-
flagAWSAPIEndpoints = "aws-api-endpoints"
13-
flagAWSAPIThrottle = "aws-api-throttle"
14-
flagAWSVpcID = "aws-vpc-id"
15-
flagAWSVpcCacheDuration = "aws-vpc-cache-duration"
16-
flagAWSMaxRetries = "aws-max-retries"
17-
defaultVpcID = ""
18-
defaultRegion = ""
19-
defaultAPIMaxRetries = 10
20-
defaultVpcCacheDuration = time.Minute * 10
11+
flagAWSRegion = "aws-region"
12+
flagAWSAPIEndpoints = "aws-api-endpoints"
13+
flagAWSAPIThrottle = "aws-api-throttle"
14+
flagAWSVpcID = "aws-vpc-id"
15+
flagAWSVpcCacheTTL = "aws-vpc-cache-ttl"
16+
flagAWSMaxRetries = "aws-max-retries"
17+
defaultVpcID = ""
18+
defaultRegion = ""
19+
defaultAPIMaxRetries = 10
20+
defaultVpcCacheTTL = time.Minute * 10
2121
)
2222

2323
type CloudConfig struct {
@@ -30,8 +30,8 @@ type CloudConfig struct {
3030
// ID of VPC to create load balancers in
3131
VpcID string
3232

33-
// VPC cache duration in minutes
34-
VpcCacheDuration time.Duration
33+
// VPC cache TTL in minutes
34+
VpcCacheTTL time.Duration
3535

3636
// Max retries configuration for AWS APIs
3737
MaxRetries int
@@ -44,7 +44,7 @@ func (cfg *CloudConfig) BindFlags(fs *pflag.FlagSet) {
4444
fs.StringVar(&cfg.Region, flagAWSRegion, defaultRegion, "AWS Region for the kubernetes cluster")
4545
fs.Var(cfg.ThrottleConfig, flagAWSAPIThrottle, "throttle settings for AWS APIs, format: serviceID1:operationRegex1=rate:burst,serviceID2:operationRegex2=rate:burst")
4646
fs.StringVar(&cfg.VpcID, flagAWSVpcID, defaultVpcID, "AWS ID of VPC to create load balancers in")
47-
fs.DurationVar(&cfg.VpcCacheDuration, flagAWSVpcCacheDuration, defaultVpcCacheDuration, "VPC cache duration in minutes")
47+
fs.DurationVar(&cfg.VpcCacheTTL, flagAWSVpcCacheTTL, defaultVpcCacheTTL, "VPC cache TTL in minutes")
4848
fs.IntVar(&cfg.MaxRetries, flagAWSMaxRetries, defaultAPIMaxRetries, "Maximum retries for AWS APIs")
4949
fs.StringToStringVar(&cfg.AWSEndpoints, flagAWSAPIEndpoints, nil, "Custom AWS endpoint configuration, format: serviceID1=URL1,serviceID2=URL2")
5050
}

pkg/networking/vpc_info_provider.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
awssdk "github.com/aws/aws-sdk-go/aws"
99
ec2sdk "github.com/aws/aws-sdk-go/service/ec2"
1010
"github.com/go-logr/logr"
11-
"github.com/patrickmn/go-cache"
11+
"k8s.io/apimachinery/pkg/util/cache"
1212
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
1313
)
1414

@@ -18,11 +18,12 @@ type VPCInfoProvider interface {
1818
}
1919

2020
// NewDefaultVPCInfoProvider constructs new defaultVPCInfoProvider.
21-
func NewDefaultVPCInfoProvider(cacheDuration time.Duration, ec2Client services.EC2, logger logr.Logger) *defaultVPCInfoProvider {
21+
func NewDefaultVPCInfoProvider(ec2Client services.EC2, logger logr.Logger, vpcInfoCacheTTL time.Duration) *defaultVPCInfoProvider {
2222
return &defaultVPCInfoProvider{
2323
ec2Client: ec2Client,
24-
vpcInfoCache: cache.New(time.Duration(cacheDuration)*time.Minute, 10*time.Minute),
24+
vpcInfoCache: cache.NewExpiring(),
2525
vpcInfoCacheMutex: sync.RWMutex{},
26+
vpcInfoCacheTTL: vpcInfoCacheTTL,
2627
logger: logger,
2728
}
2829
}
@@ -32,8 +33,9 @@ var _ VPCInfoProvider = &defaultVPCInfoProvider{}
3233
// default implementation for VPCInfoProvider.
3334
type defaultVPCInfoProvider struct {
3435
ec2Client services.EC2
35-
vpcInfoCache *cache.Cache
36+
vpcInfoCache *cache.Expiring
3637
vpcInfoCacheMutex sync.RWMutex
38+
vpcInfoCacheTTL time.Duration
3739

3840
logger logr.Logger
3941
}
@@ -57,19 +59,18 @@ func (p *defaultVPCInfoProvider) fetchVPCInfoFromCache() *ec2sdk.Vpc {
5759
p.vpcInfoCacheMutex.RLock()
5860
defer p.vpcInfoCacheMutex.RUnlock()
5961

60-
vpcInfo, found := p.vpcInfoCache.Get("vpcInfo")
61-
if !found {
62-
return nil
62+
if rawCacheItem, exists := p.vpcInfoCache.Get("vpcInfo"); exists {
63+
return rawCacheItem.(*ec2sdk.Vpc)
6364
}
6465

65-
return vpcInfo.(*ec2sdk.Vpc)
66+
return nil
6667
}
6768

6869
func (p *defaultVPCInfoProvider) saveVPCInfoToCache(vpcInfo *ec2sdk.Vpc) {
6970
p.vpcInfoCacheMutex.Lock()
7071
defer p.vpcInfoCacheMutex.Unlock()
7172

72-
p.vpcInfoCache.SetDefault("vpcInfo", vpcInfo)
73+
p.vpcInfoCache.Set("vpcInfo", vpcInfo, p.vpcInfoCacheTTL)
7374
}
7475

7576
// fetchVPCInfoFromAWS will fetch VPC info from the AWS API.

pkg/networking/vpc_info_provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Test_defaultVPCInfoProvider_FetchVPCInfo(t *testing.T) {
6363
defer ctrl.Finish()
6464

6565
ec2Client := services.NewMockEC2(ctrl)
66-
p := NewDefaultVPCInfoProvider(10*time.Minute, ec2Client, &log.NullLogger{})
66+
p := NewDefaultVPCInfoProvider(ec2Client, &log.NullLogger{}, 10*time.Minute)
6767

6868
for _, tt := range tests {
6969
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)