Skip to content

Commit

Permalink
feat(provider): add spot instance support for alibaba/tencent provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao authored and Jason-ZW committed Jun 15, 2022
1 parent 743a2e2 commit e2eaf8a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
7 changes: 7 additions & 0 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const (
defaultSecurityGroupName = "autok3s"
vpcStatusAvailable = "Available"
defaultUser = "root"
defaultSpotStrategy = "NoSpot"
defaultSpotDuration = 1
)

// providerName is the name of this provider.
Expand Down Expand Up @@ -97,6 +99,8 @@ func newProvider() *Alibaba {
Zone: defaultZoneID,
EIP: false,
CloudControllerManager: false,
SpotStrategy: defaultSpotStrategy,
SpotDuration: defaultSpotDuration,
},
}
}
Expand Down Expand Up @@ -357,6 +361,9 @@ func (p *Alibaba) runInstances(num int, master bool, password string) error {
request.Amount = requests.NewInteger(num)
request.UniqueSuffix = requests.NewBoolean(false)
request.UserData = p.UserDataContent
request.SpotStrategy = p.SpotStrategy
request.SpotDuration = requests.NewInteger(p.SpotDuration)
request.SpotPriceLimit = requests.NewFloat(p.SpotPriceLimit)
// check `--eip` value
if !p.EIP {
bandwidth, err := strconv.Atoi(p.InternetMaxBandwidthOut)
Expand Down
18 changes: 18 additions & 0 deletions pkg/providers/alibaba/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,24 @@ func (p *Alibaba) sharedFlags() []types.Flag {
V: p.UserDataContent,
Usage: "user data content, must be base64-encoded text. For more information, see: https://help.aliyun.com/document_detail/108461.html",
},
{
Name: "spot-strategy",
P: &p.SpotStrategy,
V: p.SpotStrategy,
Usage: "the bidding policy for the pay-as-you-go instance, valid values NoSpot, SpotWithPriceLimit, SpotAsPriceGo, default is NoSpot, see: https://www.alibabacloud.com/help/zh/elastic-compute-service/latest/preemptible-instances-overview#section-pgb-zcy-wgb",
},
{
Name: "spot-duration",
P: &p.SpotDuration,
V: p.SpotDuration,
Usage: "the protection period(hours) of the preemptible instance, Valid values: 0, 1, 2, 3, 4, 5, and 6, default value 1. see: https://www.alibabacloud.com/help/zh/elastic-compute-service/latest/preemptible-instances-overview#section-xfb-ucm-r5s",
},
{
Name: "spot-price-limit",
P: &p.SpotPriceLimit,
V: p.SpotPriceLimit,
Usage: "the maximum hourly price of the instance, this parameter is valid only when the --spot-strategy parameter is set to SpotWithPriceLimit. see: https://www.alibabacloud.com/help/zh/elastic-compute-service/latest/preemptible-instances-overview#section-mdc-jt5-ydb",
},
}

return fs
Expand Down
13 changes: 6 additions & 7 deletions pkg/providers/tencent/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,6 @@ func (p *Tencent) sharedFlags() []types.Flag {
Usage: "Specify the type of VM instance, see: https://cloud.tencent.com/document/product/213/11518",
EnvVar: "CVM_INSTANCE_TYPE",
},
{
Name: "instance-charge-type",
P: &p.InstanceChargeType,
V: p.InstanceChargeType,
Usage: "Specify the charge type of VM instance, see: https://cloud.tencent.com/document/product/213/15730",
EnvVar: "CVM_INSTANCE_CHARGE_TYPE",
},
{
Name: "disk-category",
P: &p.SystemDiskType,
Expand Down Expand Up @@ -305,6 +298,12 @@ func (p *Tencent) sharedFlags() []types.Flag {
V: p.UserDataContent,
Usage: "Set user data content, must be base64-encoded text. see: https://cloud.tencent.com/document/product/213/17525",
},
{
Name: "spot",
P: &p.Spot,
V: p.Spot,
Usage: "Use spot instance, see: https://cloud.tencent.com/document/product/213/17816",
},
}

return fs
Expand Down
45 changes: 25 additions & 20 deletions pkg/providers/tencent/tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@ import (
)

const (
k3sInstallScript = "https://rancher-mirror.rancher.cn/k3s/k3s-install.sh"
secretID = "secret-id"
secretKey = "secret-key"
imageID = "img-pi0ii46r" /* Ubuntu Server 18.04.1 LTS x64 */
instanceType = "SA1.MEDIUM4" /* CPU:2 Memory:4 */
instanceChargeType = "POSTPAID_BY_HOUR"
internetMaxBandwidthOut = "5"
internetChargeType = "TRAFFIC_POSTPAID_BY_HOUR"
diskCategory = "CLOUD_SSD"
diskSize = "50"
defaultRegion = "ap-guangzhou"
defaultZone = "ap-guangzhou-3"
defaultSecurityGroupName = "autok3s"
vpcName = "autok3s-tencent-vpc"
subnetName = "autok3s-tencent-subnet"
vpcCidrBlock = "192.168.0.0/16"
subnetCidrBlock = "192.168.3.0/24"
ipRange = "0.0.0.0/0"
defaultUser = "ubuntu"
k3sInstallScript = "https://rancher-mirror.rancher.cn/k3s/k3s-install.sh"
secretID = "secret-id"
secretKey = "secret-key"
imageID = "img-pi0ii46r" /* Ubuntu Server 18.04.1 LTS x64 */
instanceType = "SA1.MEDIUM4" /* CPU:2 Memory:4 */
defaultInstanceChargeType = "POSTPAID_BY_HOUR"
spotInstanceChargeType = "SPOTPAID"
internetMaxBandwidthOut = "5"
internetChargeType = "TRAFFIC_POSTPAID_BY_HOUR"
diskCategory = "CLOUD_SSD"
diskSize = "50"
defaultRegion = "ap-guangzhou"
defaultZone = "ap-guangzhou-3"
defaultSecurityGroupName = "autok3s"
vpcName = "autok3s-tencent-vpc"
subnetName = "autok3s-tencent-subnet"
vpcCidrBlock = "192.168.0.0/16"
subnetCidrBlock = "192.168.3.0/24"
ipRange = "0.0.0.0/0"
defaultUser = "ubuntu"
)

// providerName is the name of this provider.
Expand Down Expand Up @@ -91,7 +92,7 @@ func newProvider() *Tencent {
Options: tencent.Options{
ImageID: imageID,
InstanceType: instanceType,
InstanceChargeType: instanceChargeType,
InstanceChargeType: defaultInstanceChargeType,
SystemDiskSize: diskSize,
SystemDiskType: diskCategory,
InternetMaxBandwidthOut: internetMaxBandwidthOut,
Expand Down Expand Up @@ -453,6 +454,10 @@ func (p *Tencent) generateInstance(ssh *types.SSH) (*types.Cluster, error) {
}
}

if p.Spot {
p.InstanceChargeType = spotInstanceChargeType
}

// run ecs master instances.
if masterNum > 0 {
p.Logger.Infof("[%s] %d number of master instances will be created", p.GetProviderName(), masterNum)
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Options struct {
CloudControllerManager bool `json:"cloud-controller-manager" yaml:"cloud-controller-manager"`
UserDataPath string `json:"user-data-path,omitempty" yaml:"user-data-path,omitempty"`
UserDataContent string `json:"user-data-content,omitempty" yaml:"user-data-content,omitempty"`
SpotStrategy string `json:"spot-strategy,omitempty" yaml:"spot-strategy,omitempty"`
SpotDuration int `json:"spot-duration,omitempty" yaml:"spot-duration,omitempty"`
SpotPriceLimit float64 `json:"spot-price-limit,omitempty" yaml:"spot-price-limit,omitempty"`
}

// Terway struct for alibaba terway.
Expand Down
1 change: 1 addition & 0 deletions pkg/types/tencent/tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Options struct {
CloudControllerManager bool `json:"cloud-controller-manager" yaml:"cloud-controller-manager"`
UserDataPath string `json:"user-data-path,omitempty" yaml:"user-data-path,omitempty"`
UserDataContent string `json:"user-data-content,omitempty" yaml:"user-data-content,omitempty"`
Spot bool `json:"spot,omitempty" yaml:"spot,omitempty"`
}

// CloudControllerManager struct for tencent cloud-controller-manager.
Expand Down

0 comments on commit e2eaf8a

Please sign in to comment.