Skip to content

Commit

Permalink
Merge pull request #131 from jxs1211/feat/cache-cluster-attached-script
Browse files Browse the repository at this point in the history
feat: cache cluster attach script
  • Loading branch information
jwcesign authored Nov 18, 2024
2 parents c19cbaf + d12c019 commit 407d77b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
AvailableIPAddressTTL = 5 * time.Minute
// InstanceTypeAvailableDiskTTL is the time refresh InstanceType compatible disk
InstanceTypeAvailableDiskTTL = 30 * time.Minute
// LaunchTemplateTTL is time to drop LaunchTemplate data
LaunchTemplateTTL = 5 * time.Minute
// ClusterAttachScriptTTL is the time refresh for the cluster attach script
ClusterAttachScriptTTL = 6 * time.Hour

// DefaultCleanupInterval triggers cache cleanup (lazy eviction) at this interval.
DefaultCleanupInterval = 1 * time.Minute
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont
securityGroupProvider := securitygroup.NewDefaultProvider(region, ecsClient, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval))
imageProvider := imagefamily.NewDefaultProvider(region, ecsClient, ackClient, versionProvider, cache.New(alicache.DefaultTTL, alicache.DefaultCleanupInterval))
imageResolver := imagefamily.NewDefaultResolver(region, ecsClient, cache.New(alicache.InstanceTypeAvailableDiskTTL, alicache.DefaultCleanupInterval))
ackProvider := ack.NewDefaultProvider(clusterID, ackClient)
ackProvider := ack.NewDefaultProvider(clusterID, ackClient, cache.New(alicache.ClusterAttachScriptTTL, alicache.DefaultCleanupInterval))

instanceProvider := instance.NewDefaultProvider(
region,
Expand Down
12 changes: 9 additions & 3 deletions pkg/providers/ack/ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

ackclient "github.com/alibabacloud-go/cs-20151215/v5/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/patrickmn/go-cache"
"sigs.k8s.io/controller-runtime/pkg/log"
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"

Expand All @@ -47,12 +48,14 @@ type DefaultProvider struct {

muClusterCNI sync.RWMutex
clusterCNI string
cache *cache.Cache
}

func NewDefaultProvider(clusterID string, ackClient *ackclient.Client) *DefaultProvider {
func NewDefaultProvider(clusterID string, ackClient *ackclient.Client, cache *cache.Cache) *DefaultProvider {
return &DefaultProvider{
clusterID: clusterID,
ackClient: ackClient,
cache: cache,
}
}

Expand Down Expand Up @@ -101,11 +104,13 @@ func (p *DefaultProvider) GetClusterCNI(_ context.Context) (string, error) {
func (p *DefaultProvider) GetNodeRegisterScript(ctx context.Context,
labels map[string]string,
kubeletCfg *v1alpha1.KubeletConfiguration) (string, error) {
if cachedScript, ok := p.cache.Get(p.clusterID); ok {
return p.resolveUserData(cachedScript.(string), labels, kubeletCfg), nil
}

reqPara := &ackclient.DescribeClusterAttachScriptsRequest{
KeepInstanceName: tea.Bool(true),
}

// TODO: Build a cache to store this
resp, err := p.ackClient.DescribeClusterAttachScripts(tea.String(p.clusterID), reqPara)
if err != nil {
log.FromContext(ctx).Error(err, "Failed to get node registration script")
Expand All @@ -118,6 +123,7 @@ func (p *DefaultProvider) GetNodeRegisterScript(ctx context.Context,
return "", err
}

p.cache.SetDefault(p.clusterID, s)
return p.resolveUserData(s, labels, kubeletCfg), nil
}

Expand Down

0 comments on commit 407d77b

Please sign in to comment.