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

[Alibaba] resolve issue of Incorrect Zone-based control for VM #1313

Merged
merged 3 commits into from
Aug 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (cloudConn *AlibabaCloudConnection) CreatePublicIPHandler() (irs.PublicIPHa

func (cloudConn *AlibabaCloudConnection) CreateVMHandler() (irs.VMHandler, error) {
cblogger.Info("Alibaba Cloud Driver: called CreateVMHandler()!")
vmHandler := alirs.AlibabaVMHandler{cloudConn.Region, cloudConn.VMClient}
vmHandler := alirs.AlibabaVMHandler{cloudConn.Region, cloudConn.VMClient, cloudConn.VpcClient}
return &vmHandler, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1480,3 +1480,47 @@ func aliClusterTagList(CsClient *cs.Client, regionInfo idrv.RegionInfo, resType
}
return tagInfoList, err
}

func GetSubnet(VpcClient *vpc.Client, reqSubnetId string, zone string) (irs.SubnetInfo, error) {
cblogger.Infof("SubnetId : [%s]", reqSubnetId)

request := vpc.CreateDescribeVSwitchesRequest()
request.Scheme = "https"
request.VSwitchId = reqSubnetId

// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.ALIBABA,
RegionZone: zone,
ResourceType: call.VPCSUBNET,
ResourceName: reqSubnetId,
CloudOSAPI: "DescribeVSwitches()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()

result, err := VpcClient.DescribeVSwitches(request)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
//cblogger.Debug(result)
//cblogger.Info(result)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return irs.SubnetInfo{}, err
}
callogger.Info(call.String(callLogInfo))

if result.TotalCount < 1 {
return irs.SubnetInfo{}, errors.New("Notfound: '" + reqSubnetId + "' Subnet Not found")
}

if !reflect.ValueOf(result.VSwitches.VSwitch).IsNil() {
retSubnetInfo := ExtractSubnetDescribeInfo(result.VSwitches.VSwitch[0])
return retSubnetInfo, nil
} else {
return irs.SubnetInfo{}, errors.New("InvalidVSwitch.NotFound: The '" + reqSubnetId + "' does not exist")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
cdcom "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/common"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
Expand All @@ -26,8 +27,9 @@ import (
*/)

type AlibabaVMHandler struct {
Region idrv.RegionInfo
Client *ecs.Client
Region idrv.RegionInfo
Client *ecs.Client
VpcClient *vpc.Client
}

// 주어진 이미지 id에 대한 이미지 사이즈 조회
Expand Down Expand Up @@ -67,14 +69,34 @@ func (vmHandler *AlibabaVMHandler) GetImageSize(ImageSystemId string) (int64, er
// @TODO : PublicIp 요금제 방식과 대역폭 설정 방법 논의 필요
func (vmHandler *AlibabaVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo, error) {
cblogger.Debug(vmReqInfo)
zoneId := vmHandler.Region.Zone

vpcHandler := AlibabaVPCHandler{
Region: vmHandler.Region,
Client: vmHandler.VpcClient,
}

subnetInfo, err := GetSubnet(vpcHandler.Client, vmReqInfo.SubnetIID.SystemId, zoneId)
if err != nil {
return irs.VMInfo{}, errors.New("there is no available subnet")
}
cblogger.Info("subnetInfo response : ", subnetInfo)
if subnetInfo.Zone != "" {
zoneId = subnetInfo.Zone
}
cblogger.Debugf("Zone : %s", zoneId)
if zoneId == "" {
cblogger.Error("Connection information does not contain Zone information.")
return irs.VMInfo{}, errors.New("Connection Connection information does not contain Zone information.")
}
//cblogger.Debug(vmReqInfo)

/* 2021-10-26 이슈 #480에 의해 제거
// 2021-04-28 cbuser 추가에 따른 Local KeyPair만 VM 생성 가능하도록 강제
//=============================
// KeyPair의 PublicKey 정보 처리
//=============================
cblogger.Infof("[%s] KeyPair 조회 시작", vmReqInfo.KeyPairIID.SystemId)
cblogger.Infof("[%s] KeyPair 조회 시작", vmReqInfo.KeyPair
keypairHandler := AlibabaKeyPairHandler{
//CredentialInfo:
Region: vmHandler.Region,
Expand Down Expand Up @@ -183,7 +205,8 @@ func (vmHandler *AlibabaVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo,
//request.HostName = vmReqInfo.IId.NameId // OS 호스트 명
request.InstanceType = vmReqInfo.VMSpecName

request.ZoneId = vmHandler.Region.Zone // Disk의 경우 zone dependency가 있어 Zone 명시해야 함.(disk가 없으면 무시해도 됨.)
// request.ZoneId = vmHandler.Region.Zone // Disk의 경우 zone dependency가 있어 Zone 명시해야 함.(disk가 없으면 무시해도 됨.)
request.ZoneId = zoneId

// windows 일 떄는 password 만 set, keypairName은 비움.
// 다른 os일 때 password는 cb-user의 password 로 사용
Expand Down Expand Up @@ -228,7 +251,8 @@ func (vmHandler *AlibabaVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo,
// instance 사용 가능 검사
//=============================

availableResourceResp, err := DescribeAvailableResource(vmHandler.Client, vmHandler.Region.Region, vmHandler.Region.Zone, "instance", "InstanceType", vmReqInfo.VMSpecName)
// availableResourceResp, err := DescribeAvailableResource(vmHandler.Client, vmHandler.Region.Region, vmHandler.Region.Zone, "instance", "InstanceType", vmReqInfo.VMSpecName)
availableResourceResp, err := DescribeAvailableResource(vmHandler.Client, vmHandler.Region.Region, zoneId, "instance", "InstanceType", vmReqInfo.VMSpecName)
if err != nil {
cblogger.Error(err)
}
Expand All @@ -241,7 +265,8 @@ func (vmHandler *AlibabaVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo,
//=============================

// 인스턴스 타입 별로 가능한 목록 불러오기
availableSystemDisksResp, err := DescribeAvailableSystemDisksByInstanceType(vmHandler.Client, vmHandler.Region.Region, vmHandler.Region.Zone, "PostPaid", "SystemDisk", vmReqInfo.VMSpecName)
// availableSystemDisksResp, err := DescribeAvailableSystemDisksByInstanceType(vmHandler.Client, vmHandler.Region.Region, vmHandler.Region.Zone, "PostPaid", "SystemDisk", vmReqInfo.VMSpecName)
availableSystemDisksResp, err := DescribeAvailableSystemDisksByInstanceType(vmHandler.Client, vmHandler.Region.Region, zoneId, "PostPaid", "SystemDisk", vmReqInfo.VMSpecName)
if err != nil {
cblogger.Error(err)
}
Expand Down Expand Up @@ -431,7 +456,7 @@ func (vmHandler *AlibabaVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo,
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.ALIBABA,
RegionZone: vmHandler.Region.Zone,
RegionZone: zoneId,
ResourceType: call.VM,
ResourceName: vmReqInfo.IId.NameId,
CloudOSAPI: "RunInstances()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package resources

import (
"errors"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -198,7 +197,8 @@ func (VPCHandler *AlibabaVPCHandler) CreateSubnet(vpcId string, reqSubnetInfo ir
callogger.Info(call.String(callLogInfo))
//cblogger.Debug(response)

subnetInfo, errSunetInfo := VPCHandler.GetSubnet(response.VSwitchId)
//subnetInfo, errSunetInfo := VPCHandler.GetSubnet(response.VSwitchId)
subnetInfo, errSunetInfo := GetSubnet(VPCHandler.Client, response.VSwitchId, zoneId)
if errSunetInfo != nil {
cblogger.Error(subnetInfo)
return irs.SubnetInfo{}, errSunetInfo
Expand Down Expand Up @@ -370,7 +370,9 @@ func (VPCHandler *AlibabaVPCHandler) GetVPC(vpcIID irs.IID) (irs.VPCInfo, error)
//cblogger.Infof("\n\n\n\n")
//cblogger.Infof("---------------------------------------------------------------------")
cblogger.Infof("[%s] VSwitch Information retrieval", curSubnet)
subnetInfo, errSubnet := VPCHandler.GetSubnet(curSubnet)
// subnetInfo, errSubnet := VPCHandler.GetSubnet(curSubnet)
subnetInfo, errSubnet := GetSubnet(VPCHandler.Client, curSubnet, VPCHandler.Region.Zone)

if errSubnet != nil {
cblogger.Errorf("[%s] VSwitch Information retrieval failed", curSubnet)
cblogger.Error(errSubnet)
Expand Down Expand Up @@ -490,50 +492,6 @@ func (VPCHandler *AlibabaVPCHandler) DeleteSubnet(subnetIID irs.IID) (bool, erro
return true, nil
}

func (VPCHandler *AlibabaVPCHandler) GetSubnet(reqSubnetId string) (irs.SubnetInfo, error) {
cblogger.Infof("SubnetId : [%s]", reqSubnetId)

request := vpc.CreateDescribeVSwitchesRequest()
request.Scheme = "https"
request.VSwitchId = reqSubnetId

// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.ALIBABA,
RegionZone: VPCHandler.Region.Zone,
ResourceType: call.VPCSUBNET,
ResourceName: reqSubnetId,
CloudOSAPI: "DescribeVSwitches()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()

result, err := VPCHandler.Client.DescribeVSwitches(request)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
//cblogger.Debug(result)
//cblogger.Info(result)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return irs.SubnetInfo{}, err
}
callogger.Info(call.String(callLogInfo))

if result.TotalCount < 1 {
return irs.SubnetInfo{}, errors.New("Notfound: '" + reqSubnetId + "' Subnet Not found")
}

if !reflect.ValueOf(result.VSwitches.VSwitch).IsNil() {
retSubnetInfo := ExtractSubnetDescribeInfo(result.VSwitches.VSwitch[0])
return retSubnetInfo, nil
} else {
return irs.SubnetInfo{}, errors.New("InvalidVSwitch.NotFound: The '" + reqSubnetId + "' does not exist")
}
}

// Subnet(VSwitch) 정보를 추출함
func ExtractSubnetDescribeInfo(subnetInfo vpc.VSwitch) irs.SubnetInfo {
vNetworkInfo := irs.SubnetInfo{
Expand Down