Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache SSM calls (v0.36.x) #6729

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
InstanceTypesAndZonesTTL = 5 * time.Minute
// InstanceProfileTTL is the time before we refresh checking instance profile existence at IAM
InstanceProfileTTL = 15 * time.Minute
// SSMProviderTTL is the time to drop SSM Provider data. This only queries EKS Optimized AMI
// releases, so we should expect this to be updated relatively infrequently.
SSMProviderTTL = 24 * time.Hour
)

const (
Expand Down
5 changes: 4 additions & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/providers/securitygroup"
ssmp "github.com/aws/karpenter-provider-aws/pkg/providers/ssm"
"github.com/aws/karpenter-provider-aws/pkg/providers/subnet"
"github.com/aws/karpenter-provider-aws/pkg/providers/version"
)
Expand All @@ -86,6 +87,7 @@ type Operator struct {
VersionProvider version.Provider
InstanceTypesProvider instancetype.Provider
InstanceProvider instance.Provider
SSMProvider ssmp.Provider
}

func NewOperator(ctx context.Context, operator *operator.Operator) (context.Context, *Operator) {
Expand Down Expand Up @@ -142,7 +144,8 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
*sess.Config.Region,
)
versionProvider := version.NewDefaultProvider(operator.KubernetesInterface, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssm.New(sess), ec2api, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
ssmProvider := ssmp.NewDefaultProvider( ssm.New(sess), cache.New(awscache.SSMProviderTTL, awscache.DefaultCleanupInterval))
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssmProvider, ec2api, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
amiResolver := amifamily.NewResolver(amiProvider)
launchTemplateProvider := launchtemplate.NewDefaultProvider(
ctx,
Expand Down
16 changes: 7 additions & 9 deletions pkg/providers/amifamily/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/mitchellh/hashstructure/v2"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/logging"

"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1"
"github.com/aws/karpenter-provider-aws/pkg/providers/ssm"
"github.com/aws/karpenter-provider-aws/pkg/providers/version"

"sigs.k8s.io/karpenter/pkg/cloudprovider"
Expand All @@ -48,9 +47,9 @@ type Provider interface {
type DefaultProvider struct {
sync.Mutex
cache *cache.Cache
ssm ssmiface.SSMAPI
ec2api ec2iface.EC2API
cm *pretty.ChangeMonitor
ssmProvider ssm.Provider
versionProvider version.Provider
}

Expand Down Expand Up @@ -102,12 +101,12 @@ func (a AMIs) MapToInstanceTypes(instanceTypes []*cloudprovider.InstanceType) ma
return amiIDs
}

func NewDefaultProvider(versionProvider version.Provider, ssm ssmiface.SSMAPI, ec2api ec2iface.EC2API, cache *cache.Cache) *DefaultProvider {
func NewDefaultProvider(versionProvider version.Provider, ssmProvider ssm.Provider, ec2api ec2iface.EC2API, cache *cache.Cache) *DefaultProvider {
return &DefaultProvider{
cache: cache,
ssm: ssm,
ec2api: ec2api,
cm: pretty.NewChangeMonitor(),
ssmProvider: ssmProvider,
versionProvider: versionProvider,
}
}
Expand Down Expand Up @@ -178,12 +177,11 @@ func (p *DefaultProvider) getDefaultAMIs(ctx context.Context, nodeClass *v1beta1
}

func (p *DefaultProvider) resolveSSMParameter(ctx context.Context, ssmQuery string) (string, error) {
output, err := p.ssm.GetParameterWithContext(ctx, &ssm.GetParameterInput{Name: aws.String(ssmQuery)})
imageID, err := p.ssmProvider.Get(ctx, ssmQuery)
if err != nil {
return "", fmt.Errorf("getting ssm parameter %q, %w", ssmQuery, err)
return "", err
}
ami := aws.StringValue(output.Parameter.Value)
return ami, nil
return imageID, nil
}

func (p *DefaultProvider) getAMIs(ctx context.Context, terms []v1beta1.AMISelectorTerm) (AMIs, error) {
Expand Down
61 changes: 61 additions & 0 deletions pkg/providers/ssm/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ssm

import (
"context"
"fmt"
"sync"

"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
"knative.dev/pkg/logging"
)

type Provider interface {
Get(context.Context, string) (string, error)
}

type DefaultProvider struct {
sync.Mutex
cache *cache.Cache
ssmapi ssmiface.SSMAPI
}

func NewDefaultProvider(ssmapi ssmiface.SSMAPI, cache *cache.Cache) *DefaultProvider {
return &DefaultProvider{
ssmapi: ssmapi,
cache: cache,
}
}

func (p *DefaultProvider) Get(ctx context.Context, parameter string) (string, error) {
p.Lock()
defer p.Unlock()
if result, ok := p.cache.Get(parameter); ok {
return result.(string), nil
}
result, err := p.ssmapi.GetParameterWithContext(ctx, &ssm.GetParameterInput{
Name: lo.ToPtr(parameter),
})
if err != nil {
return "", fmt.Errorf("getting ssm parameter %q, %w", parameter, err)
}
p.cache.SetDefault(parameter, lo.FromPtr(result.Parameter.Value))
logging.FromContext(ctx).With("parameter", parameter, "value", lo.FromPtr(result.Parameter.Value)).Info("discovered ssm parameter")
return lo.FromPtr(result.Parameter.Value), nil
}
8 changes: 7 additions & 1 deletion pkg/test/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/providers/securitygroup"
"github.com/aws/karpenter-provider-aws/pkg/providers/ssm"
"github.com/aws/karpenter-provider-aws/pkg/providers/subnet"
"github.com/aws/karpenter-provider-aws/pkg/providers/version"

Expand Down Expand Up @@ -66,6 +67,7 @@ type Environment struct {
SubnetCache *cache.Cache
SecurityGroupCache *cache.Cache
InstanceProfileCache *cache.Cache
SSMProviderCache *cache.Cache

// Providers
InstanceTypesProvider *instancetype.DefaultProvider
Expand Down Expand Up @@ -96,6 +98,7 @@ func NewEnvironment(ctx context.Context, env *coretest.Environment) *Environment
subnetCache := cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval)
securityGroupCache := cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval)
instanceProfileCache := cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval)
ssmProviderCache := cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval)
fakePricingAPI := &fake.PricingAPI{}

// Providers
Expand All @@ -104,7 +107,8 @@ func NewEnvironment(ctx context.Context, env *coretest.Environment) *Environment
securityGroupProvider := securitygroup.NewDefaultProvider(ec2api, securityGroupCache)
versionProvider := version.NewDefaultProvider(env.KubernetesInterface, kubernetesVersionCache)
instanceProfileProvider := instanceprofile.NewProvider(fake.DefaultRegion, iamapi, instanceProfileCache)
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssmapi, ec2api, ec2Cache)
ssmProvider := ssm.NewDefaultProvider(ssmapi, ssmProviderCache)
amiProvider := amifamily.NewDefaultProvider(versionProvider, ssmProvider, ec2api, ec2Cache)
amiResolver := amifamily.NewResolver(amiProvider)
instanceTypesProvider := instancetype.NewDefaultProvider(fake.DefaultRegion, instanceTypeCache, ec2api, subnetProvider, unavailableOfferingsCache, pricingProvider)
launchTemplateProvider :=
Expand Down Expand Up @@ -147,6 +151,7 @@ func NewEnvironment(ctx context.Context, env *coretest.Environment) *Environment
SecurityGroupCache: securityGroupCache,
InstanceProfileCache: instanceProfileCache,
UnavailableOfferingsCache: unavailableOfferingsCache,
SSMProviderCache: ssmProviderCache,

InstanceTypesProvider: instanceTypesProvider,
InstanceProvider: instanceProvider,
Expand Down Expand Up @@ -177,6 +182,7 @@ func (env *Environment) Reset() {
env.SubnetCache.Flush()
env.SecurityGroupCache.Flush()
env.InstanceProfileCache.Flush()
env.SSMProviderCache.Flush()

mfs, err := crmetrics.Registry.Gather()
if err != nil {
Expand Down
Loading