From 4236ba1a968cf4063201debc9b4da5579d665c67 Mon Sep 17 00:00:00 2001 From: jwcesign Date: Wed, 9 Oct 2024 15:50:20 +0800 Subject: [PATCH] chore: make aksk selection random and fix some things Signed-off-by: jwcesign --- cmd/app/options/options.go | 6 +- config/deployment.yaml | 10 ++-- hack/tools/pull-data/pull-latest-price.go | 3 +- pkg/client/alibabacloud.go | 70 ++++++++++++++++------- pkg/tools/utils.go | 30 ---------- 5 files changed, 59 insertions(+), 60 deletions(-) delete mode 100644 pkg/tools/utils.go diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index b1b542e0..f7ca1bc3 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -5,7 +5,7 @@ import ( "os" "github.com/cloudpilot-ai/priceserver/pkg/apis" - "github.com/cloudpilot-ai/priceserver/pkg/tools" + "github.com/cloudpilot-ai/priceserver/pkg/client" ) type Options struct { @@ -14,7 +14,7 @@ type Options struct { AWSCNAK string AWSCNSK string - AlibabaCloudAKSKPool map[string]string + AlibabaCloudAKSKPool []client.AKSKPair } func NewOptions() *Options { @@ -38,7 +38,7 @@ func (o *Options) ApplyAndValidate() error { if o.AWSCNSK == "" { return fmt.Errorf("aws china secret key is not set") } - o.AlibabaCloudAKSKPool = tools.ExtractAlibabaCloudAKSKPool() + o.AlibabaCloudAKSKPool = client.ExtractAlibabaCloudAKSKPool() if len(o.AlibabaCloudAKSKPool) == 0 { return fmt.Errorf("alibaba cloud access key and secret key pool is not set") } diff --git a/config/deployment.yaml b/config/deployment.yaml index 5b42897c..07e046bc 100644 --- a/config/deployment.yaml +++ b/config/deployment.yaml @@ -44,15 +44,15 @@ spec: httpGet: path: /healthz port: 8080 - initialDelaySeconds: 30 + initialDelaySeconds: 100 periodSeconds: 3 resources: requests: - cpu: 200m - memory: 250Mi + cpu: 250m + memory: 300Mi limits: - cpu: 200m - memory: 250Mi + cpu: 250m + memory: 300Mi terminationGracePeriodSeconds: 30 --- diff --git a/hack/tools/pull-data/pull-latest-price.go b/hack/tools/pull-data/pull-latest-price.go index eb2a6c36..6499202c 100644 --- a/hack/tools/pull-data/pull-latest-price.go +++ b/hack/tools/pull-data/pull-latest-price.go @@ -6,7 +6,6 @@ import ( "github.com/cloudpilot-ai/priceserver/pkg/apis" "github.com/cloudpilot-ai/priceserver/pkg/client" - "github.com/cloudpilot-ai/priceserver/pkg/tools" ) func handleAWSData() error { @@ -36,7 +35,7 @@ func handleAWSData() error { } func handleAlibabaCloudData() error { - alibabaCloudAKSKPool := tools.ExtractAlibabaCloudAKSKPool() + alibabaCloudAKSKPool := client.ExtractAlibabaCloudAKSKPool() alibabaCloudClient, err := client.NewAlibabaCloudPriceClient(alibabaCloudAKSKPool, false) if err != nil { diff --git a/pkg/client/alibabacloud.go b/pkg/client/alibabacloud.go index 6fedefd9..85d17698 100644 --- a/pkg/client/alibabacloud.go +++ b/pkg/client/alibabacloud.go @@ -6,8 +6,10 @@ import ( "encoding/json" "fmt" "io" + "math/rand" "net/http" "net/url" + "os" "regexp" "strconv" "strings" @@ -28,9 +30,36 @@ import ( //go:embed builtin-data/*.json var file embed.FS +type AKSKPair struct { + AK string + SK string +} + +func ExtractAlibabaCloudAKSKPool() []AKSKPair { + akskPool := os.Getenv(apis.AlibabaCloudAKSKPoolEnv) + if akskPool == "" { + return nil + } + + akskPair := []AKSKPair{} + for _, aksk := range strings.Split(akskPool, ",") { + aksk = strings.TrimSpace(aksk) + if aksk == "" { + continue + } + akskArray := strings.Split(aksk, ":") + if len(akskArray) != 2 { + continue + } + + akskPair = append(akskPair, AKSKPair{AK: akskArray[0], SK: akskArray[1]}) + } + + return akskPair +} + type AlibabaCloudPriceClient struct { - // akskPool maps from ak to sk - akskPool map[string]string + akskPool []AKSKPair regionList []string @@ -38,7 +67,7 @@ type AlibabaCloudPriceClient struct { priceData map[string]*apis.RegionalInstancePrice } -func NewAlibabaCloudPriceClient(akskPool map[string]string, initialSpotUpdate bool) (*AlibabaCloudPriceClient, error) { +func NewAlibabaCloudPriceClient(akskPool []AKSKPair, initialSpotUpdate bool) (*AlibabaCloudPriceClient, error) { data, err := file.ReadFile("builtin-data/alibabacloud_price.json") if err != nil { return nil, err @@ -146,10 +175,12 @@ func (a *AlibabaCloudPriceClient) refreshSpotPrice() { info.SpotPricePerHour = spotPrice a.dataMutex.Lock() if _, ok := a.priceData[region]; !ok { - a.priceData[region] = &apis.RegionalInstancePrice{} + a.priceData[region] = &apis.RegionalInstancePrice{ + InstanceTypePrices: map[string]*apis.InstanceTypePrice{}, + } } - if _, ok := a.priceData[region].InstanceTypePrices[instanceType]; !ok { - a.priceData[region].InstanceTypePrices = map[string]*apis.InstanceTypePrice{} + if _, ok := a.priceData[region].InstanceTypePrices[instanceType]; ok { + info.OnDemandPricePerHour = a.priceData[region].InstanceTypePrices[instanceType].OnDemandPricePerHour } a.priceData[region].InstanceTypePrices[instanceType] = info a.dataMutex.Unlock() @@ -397,21 +428,20 @@ func (a *AlibabaCloudPriceClient) initialRegions() error { } func (a *AlibabaCloudPriceClient) createECSClient(region string) (*ecsclient.Client, error) { - for ak, sk := range a.akskPool { - config := &openapi.Config{ - AccessKeyId: tea.String(ak), - AccessKeySecret: tea.String(sk), - RegionId: tea.String(region), - } - client, err := ecsclient.NewClient(config) - if err != nil { - klog.Errorf("Failed to create ecs client:%v", err) - return nil, err - } - return client, nil + // Take one ak/sk from pool + pick := rand.Intn(len(a.akskPool)) + ak, sk := a.akskPool[pick].AK, a.akskPool[pick].SK + config := &openapi.Config{ + AccessKeyId: tea.String(ak), + AccessKeySecret: tea.String(sk), + RegionId: tea.String(region), + } + client, err := ecsclient.NewClient(config) + if err != nil { + klog.Errorf("Failed to create ecs client:%v", err) + return nil, err } - - return nil, fmt.Errorf("failed to create ecs client") + return client, nil } func (a *AlibabaCloudPriceClient) ListRegionsInstancesPrice() map[string]*apis.RegionalInstancePrice { diff --git a/pkg/tools/utils.go b/pkg/tools/utils.go deleted file mode 100644 index e3184770..00000000 --- a/pkg/tools/utils.go +++ /dev/null @@ -1,30 +0,0 @@ -package tools - -import ( - "os" - "strings" - - "github.com/cloudpilot-ai/priceserver/pkg/apis" -) - -func ExtractAlibabaCloudAKSKPool() map[string]string { - akskPool := os.Getenv(apis.AlibabaCloudAKSKPoolEnv) - if akskPool == "" { - return nil - } - - akskMap := make(map[string]string) - for _, aksk := range strings.Split(akskPool, ",") { - aksk = strings.TrimSpace(aksk) - if aksk == "" { - continue - } - akskArray := strings.Split(aksk, ":") - if len(akskArray) != 2 { - continue - } - akskMap[akskArray[0]] = akskArray[1] - } - - return akskMap -}