From 038de6391e6c39c93c978ef0012c4961aa958e82 Mon Sep 17 00:00:00 2001 From: Jason-ZW Date: Fri, 11 Sep 2020 17:14:23 +0800 Subject: [PATCH] feat(alibaba): add --zone flag Signed-off-by: Jason-ZW --- pkg/providers/alibaba/alibaba.go | 28 ++++++++++++++++++++-------- pkg/providers/alibaba/flag.go | 10 ++++++++-- pkg/types/alibaba/alibaba.go | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/pkg/providers/alibaba/alibaba.go b/pkg/providers/alibaba/alibaba.go index 409ba027..46f2de41 100644 --- a/pkg/providers/alibaba/alibaba.go +++ b/pkg/providers/alibaba/alibaba.go @@ -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 { @@ -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 { @@ -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) @@ -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 @@ -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 diff --git a/pkg/providers/alibaba/flag.go b/pkg/providers/alibaba/flag.go index c0b4346d..6d84f7c6 100644 --- a/pkg/providers/alibaba/flag.go +++ b/pkg/providers/alibaba/flag.go @@ -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, diff --git a/pkg/types/alibaba/alibaba.go b/pkg/types/alibaba/alibaba.go index f149ee73..84d7695a 100644 --- a/pkg/types/alibaba/alibaba.go +++ b/pkg/types/alibaba/alibaba.go @@ -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"`