Skip to content

Commit

Permalink
feat(provider): add user-data for alibaba/tencent providers
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao authored and Jason-ZW committed Jun 2, 2022
1 parent adfa12a commit 5bb13b4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
19 changes: 19 additions & 0 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -355,6 +356,7 @@ func (p *Alibaba) runInstances(num int, master bool, password string) error {
request.SecurityGroupId = p.SecurityGroup
request.Amount = requests.NewInteger(num)
request.UniqueSuffix = requests.NewBoolean(false)
request.UserData = p.UserDataContent
// check `--eip` value
if !p.EIP {
bandwidth, err := strconv.Atoi(p.InternetMaxBandwidthOut)
Expand Down Expand Up @@ -687,6 +689,13 @@ func (p *Alibaba) CreateCheck() error {
return fmt.Errorf("[%s] calling preflight error: must set `--zone` in specified region %s to create default vswitch or set exist `--vswitch` in specified region", p.GetProviderName(), p.Region)
}

if p.UserDataPath != "" {
_, err = os.Stat(p.UserDataPath)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -1028,6 +1037,16 @@ func (p *Alibaba) generateInstance(ssh *types.SSH) (*types.Cluster, error) {
p.VpcCIDR = vpcCIDR
}

if p.UserDataPath != "" {
userDataBytes, err := ioutil.ReadFile(p.UserDataPath)
if err != nil {
return nil, err
}
if len(userDataBytes) > 0 {
p.UserDataContent = base64.StdEncoding.EncodeToString(userDataBytes)
}
}

// run ecs master instances.
if masterNum > 0 {
p.Logger.Infof("[%s] prepare for %d of master instances", p.GetProviderName(), masterNum)
Expand Down
12 changes: 12 additions & 0 deletions pkg/providers/alibaba/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ func (p *Alibaba) sharedFlags() []types.Flag {
V: p.TerwayMaxPoolSize,
Usage: "Max pool size for terway ENI mode",
},
{
Name: "user-data-path",
P: &p.UserDataPath,
V: p.UserDataPath,
Usage: "file path of user data to make available to the ECS instance. For more information, see: https://help.aliyun.com/document_detail/108461.html",
},
{
Name: "user-data-content",
P: &p.UserDataContent,
V: p.UserDataContent,
Usage: "user data content, must be base64-encoded text. For more information, see: https://help.aliyun.com/document_detail/108461.html",
},
}

return fs
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/harvester/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (h *Harvester) runInstances(num int, master bool, ssh *types.SSH) error {
Labels(h.setVMLabels(master)).
PVCDisk(rootDiskName, builder.DiskBusVirtio, false, false, 1, h.DiskSize, "", pvcOption).
CloudInitDisk(builder.CloudInitDiskName, builder.DiskBusVirtio, false, 0, *cloudInitSource).
EvictionStrategy(true).DefaultPodAntiAffinity().RunStrategy(kubevirtv1.RunStrategyRerunOnFailure)
EvictionStrategy(true).DefaultPodAntiAffinity().Run(false)

if h.KeypairName != "" {
vmBuilder = vmBuilder.SSHKey(h.KeypairName)
Expand Down
6 changes: 6 additions & 0 deletions pkg/providers/tencent/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ func (p *Tencent) sharedFlags() []types.Flag {
V: p.UserDataPath,
Usage: "Set user data, i.e.( --user-data-path /file/path ), see: https://cloud.tencent.com/document/product/213/17525",
},
{
Name: "user-data-content",
P: &p.UserDataContent,
V: p.UserDataContent,
Usage: "Set user data content, must be base64-encoded text. see: https://cloud.tencent.com/document/product/213/17525",
},
}

return fs
Expand Down
17 changes: 13 additions & 4 deletions pkg/providers/tencent/tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ func (p *Tencent) generateInstance(ssh *types.SSH) (*types.Cluster, error) {
p.Logger.Infof("[%s] launching instance with auto-generated password...", p.GetProviderName())
}

if p.UserDataPath != "" {
userDataBytes, err := ioutil.ReadFile(p.UserDataPath)
if err != nil {
return nil, err
}
if len(userDataBytes) > 0 {
p.UserDataContent = base64.StdEncoding.EncodeToString(userDataBytes)
}
}

// run ecs master instances.
if masterNum > 0 {
p.Logger.Infof("[%s] %d number of master instances will be created", p.GetProviderName(), masterNum)
Expand Down Expand Up @@ -604,8 +614,8 @@ func (p *Tencent) deleteInstance(f bool) (string, error) {
func (p *Tencent) CreateCheck() error {
if p.UserDataPath != "" {
_, err := os.Stat(p.UserDataPath)
if os.IsNotExist(err) {
return fmt.Errorf("[%s] calling preflight error: file %s is not exist, msg: %v", p.GetProviderName(), p.UserDataPath, err)
if err != nil {
return err
}
}
if p.KeypairID != "" && p.SSHKeyPath == "" {
Expand Down Expand Up @@ -749,8 +759,7 @@ func (p *Tencent) runInstances(num int, master bool, password string) error {
diskSize, _ := strconv.ParseInt(p.SystemDiskSize, 10, 64)
bandwidth, _ := strconv.ParseInt(p.InternetMaxBandwidthOut, 10, 64)

userdata, _ := ioutil.ReadFile(p.UserDataPath)
request.UserData = tencentCommon.StringPtr(base64.StdEncoding.EncodeToString(userdata))
request.UserData = tencentCommon.StringPtr(p.UserDataContent)
request.InstanceCount = tencentCommon.Int64Ptr(int64(num))
request.ImageId = tencentCommon.StringPtr(p.ImageID)
request.InstanceType = tencentCommon.StringPtr(p.InstanceType)
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Options struct {
EIP bool `json:"eip,omitempty" yaml:"eip,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
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"`
}

// 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 @@ -41,6 +41,7 @@ type Options struct {
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
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"`
}

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

0 comments on commit 5bb13b4

Please sign in to comment.