Skip to content

Commit

Permalink
feat(alibaba): add --zone flag
Browse files Browse the repository at this point in the history
Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot committed Sep 25, 2020
1 parent 7b0cacf commit 038de63
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
28 changes: 20 additions & 8 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ func (p *Alibaba) runInstances(num int, master bool) error {
request.InternetMaxBandwidthOut = requests.NewInteger(outBandWidth)
request.Amount = requests.NewInteger(num)
request.UniqueSuffix = requests.NewBoolean(true)
if p.Zone != "" {
request.ZoneId = p.Zone
}

tag := []ecs.RunInstancesTag{{Key: "autok3s", Value: "true"}, {Key: "cluster", Value: p.Name}}
if master {
Expand All @@ -308,8 +311,8 @@ func (p *Alibaba) runInstances(num int, master bool) error {

response, err := p.c.RunInstances(request)
if err != nil || len(response.InstanceIdSets.InstanceIdSet) != num {
return fmt.Errorf("[%s] calling runInstances error. region=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, request.InstanceName, err)
return fmt.Errorf("[%s] calling runInstances error. region=%s, zone=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, p.Zone, request.InstanceName, err)
}
for _, id := range response.InstanceIdSets.InstanceIdSet {
if master {
Expand All @@ -333,9 +336,12 @@ func (p *Alibaba) getInstanceStatus() error {
request := ecs.CreateDescribeInstanceStatusRequest()
request.Scheme = "https"
request.InstanceId = &ids
if p.Zone != "" {
request.ZoneId = p.Zone
}

wait.ErrWaitTimeout = fmt.Errorf("[%s] calling getInstanceStatus error. region=%s, "+"instanceName=%s, message=not running status\n",
p.GetProviderName(), p.Region, ids)
wait.ErrWaitTimeout = fmt.Errorf("[%s] calling getInstanceStatus error. region=%s, zone=%s, "+"instanceName=%s, message=not running status\n",
p.GetProviderName(), p.Region, p.Zone, ids)

if err := wait.ExponentialBackoff(common.Backoff, func() (bool, error) {
response, err := p.c.DescribeInstanceStatus(request)
Expand Down Expand Up @@ -482,11 +488,14 @@ func (p *Alibaba) describeInstances() (*ecs.DescribeInstancesResponse, error) {
request := ecs.CreateDescribeInstancesRequest()
request.Scheme = "https"
request.Tag = &[]ecs.DescribeInstancesTag{{Key: "autok3s", Value: "true"}, {Key: "cluster", Value: p.Name}}
if p.Zone != "" {
request.ZoneId = p.Zone
}

response, err := p.c.DescribeInstances(request)
if err == nil && len(response.Instances.Instance) == 0 {
return nil, fmt.Errorf("[%s] calling describeInstances error. region=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, request.InstanceName, err)
return nil, fmt.Errorf("[%s] calling describeInstances error. region=%s, zone=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, p.Zone, request.InstanceName, err)
}

return response, nil
Expand All @@ -496,11 +505,14 @@ func (p *Alibaba) getVSwitchCIDR() (string, string, error) {
request := ecs.CreateDescribeVSwitchesRequest()
request.Scheme = "https"
request.VSwitchId = p.VSwitch
if p.Zone != "" {
request.ZoneId = p.Zone
}

response, err := p.c.DescribeVSwitches(request)
if err != nil || !response.IsSuccess() || len(response.VSwitches.VSwitch) < 1 {
return "", "", fmt.Errorf("[%s] calling describeVSwitches error. region=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, p.VSwitch, err)
return "", "", fmt.Errorf("[%s] calling describeVSwitches error. region=%s, zone=%s, "+"instanceName=%s, message=[%s]\n",
p.GetProviderName(), p.Region, p.Zone, p.VSwitch, err)
}

return response.VSwitches.VSwitch[0].VpcId, response.VSwitches.VSwitch[0].CidrBlock, nil
Expand Down
10 changes: 8 additions & 2 deletions pkg/providers/alibaba/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,22 @@ func (p *Alibaba) sharedFlags() []types.Flag {
Name: "name",
P: &p.Name,
V: p.Name,
Usage: "Cluster name.",
Usage: "Cluster name",
Required: true,
},
{
Name: "region",
P: &p.Region,
V: p.Region,
Usage: "Physical locations (data centers) that spread all over the world to reduce the network latency",
Usage: "Region is physical locations (data centers) that spread all over the world to reduce the network latency",
Required: true,
},
{
Name: "zone",
P: &p.Zone,
V: p.Zone,
Usage: "Zone is physical areas with independent power grids and networks within one region. e.g.(cn-hangzhou-i)",
},
{
Name: "key-pair",
P: &p.KeyPair,
Expand Down
1 change: 1 addition & 0 deletions pkg/types/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
KeyPair string `json:"key-pair,omitempty" yaml:"key-pair,omitempty"`
Region string `json:"region,omitempty" yaml:"region,omitempty"`
Zone string `json:"zone,omitempty" yaml:"zone,omitempty"`
Vpc string `json:"vpc,omitempty" yaml:"vpc,omitempty"`
VSwitch string `json:"v-switch,omitempty" yaml:"v-switch,omitempty"`
SecurityGroup string `json:"security-group,omitempty" yaml:"security-group,omitempty"`
Expand Down

0 comments on commit 038de63

Please # to comment.