diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 42ff9a8..5219bb7 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -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 diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 5c2e87b..59f7373 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -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, diff --git a/pkg/providers/ack/ack.go b/pkg/providers/ack/ack.go index 723cdca..fc47670 100644 --- a/pkg/providers/ack/ack.go +++ b/pkg/providers/ack/ack.go @@ -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" @@ -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, } } @@ -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") @@ -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 }