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.35.x) #6730

Merged
merged 2 commits 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
4 changes: 2 additions & 2 deletions hack/toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ main() {

tools() {
go install github.com/google/go-licenses@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1
go install github.com/google/ko@latest
go install github.com/mikefarah/yq/v4@latest
go install github.com/norwoodj/helm-docs/cmd/helm-docs@latest
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.15.0
go install github.com/sigstore/cosign/v2/cmd/cosign@latest
go install -tags extended github.com/gohugoio/[email protected]
go install golang.org/x/vuln/cmd/govulncheck@latest
Expand Down
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 @@ -61,6 +61,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 @@ -87,6 +88,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 @@ -143,7 +145,8 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
*sess.Config.Region,
)
versionProvider := version.NewProvider(operator.KubernetesInterface, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
amiProvider := amifamily.NewProvider(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.NewProvider(versionProvider, ssmProvider, ec2api, cache.New(awscache.DefaultTTL, awscache.DefaultCleanupInterval))
amiResolver := amifamily.New(amiProvider)
launchTemplateProvider := launchtemplate.NewProvider(
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 @@ -44,10 +43,10 @@ import (
type Provider struct {
sync.Mutex
cache *cache.Cache
ssm ssmiface.SSMAPI
ec2api ec2iface.EC2API
cm *pretty.ChangeMonitor
versionProvider *version.Provider
ssmProvider ssm.Provider
}

type AMI struct {
Expand Down Expand Up @@ -98,12 +97,12 @@ func (a AMIs) MapToInstanceTypes(instanceTypes []*cloudprovider.InstanceType) ma
return amiIDs
}

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

func (p *Provider) 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 *Provider) 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.Provider
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.NewProvider(ec2api, securityGroupCache)
versionProvider := version.NewProvider(env.KubernetesInterface, kubernetesVersionCache)
instanceProfileProvider := instanceprofile.NewProvider(fake.DefaultRegion, iamapi, instanceProfileCache)
amiProvider := amifamily.NewProvider(versionProvider, ssmapi, ec2api, ec2Cache)
ssmProvider := ssm.NewDefaultProvider(ssmapi, ssmProviderCache)
amiProvider := amifamily.NewProvider(versionProvider, ssmProvider, ec2api, ec2Cache)
amiResolver := amifamily.New(amiProvider)
instanceTypesProvider := instancetype.NewProvider(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