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

chore: make aksk selection random and fix some things #3

Merged
merged 1 commit into from
Oct 9, 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
6 changes: 3 additions & 3 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,7 +14,7 @@ type Options struct {
AWSCNAK string
AWSCNSK string

AlibabaCloudAKSKPool map[string]string
AlibabaCloudAKSKPool []client.AKSKPair
}

func NewOptions() *Options {
Expand All @@ -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")
}
Expand Down
10 changes: 5 additions & 5 deletions config/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

---
Expand Down
3 changes: 1 addition & 2 deletions hack/tools/pull-data/pull-latest-price.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
70 changes: 50 additions & 20 deletions pkg/client/alibabacloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -28,17 +30,44 @@ 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

dataMutex sync.RWMutex
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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 0 additions & 30 deletions pkg/tools/utils.go

This file was deleted.