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

Auto-assign bastion node #1359

Merged
merged 1 commit into from
Oct 10, 2023
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
7 changes: 7 additions & 0 deletions src/core/mcis/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,5 +1835,12 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e

UpdateVmInfo(nsId, mcisId, *vmInfoData)

// Assign a Bastion if none (randomly)
_, err = SetBastionNodes(nsId, mcisId, vmInfoData.Id, "")
if err != nil {
// just log error and continue
common.CBLog.Error(err)
}

return nil
}
38 changes: 32 additions & 6 deletions src/core/mcis/remoteCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,18 @@ type BastionInfo struct {

// SetBastionNodes func sets bastion nodes
func SetBastionNodes(nsId string, mcisId string, targetVmId string, bastionVmId string) (string, error) {

// Check if bastion node already exists for the target VM (for random assignment)
currentBastion, err := GetBastionNodes(nsId, mcisId, targetVmId)
if err != nil {
common.CBLog.Error(err)
return "", err
}
if len(currentBastion.VmId) > 0 && bastionVmId == "" {
return "", fmt.Errorf("bastion node already exists for VM (ID: %s) in MCIS (ID: %s) under namespace (ID: %s)",
targetVmId, mcisId, nsId)
}

vmObj, err := GetVmObject(nsId, mcisId, targetVmId)
if err != nil {
common.CBLog.Error(err)
Expand All @@ -550,10 +562,25 @@ func SetBastionNodes(nsId string, mcisId string, targetVmId string, bastionVmId
// find subnet and append bastion node
for i, subnetInfo := range tempVNetInfo.SubnetInfoList {
if subnetInfo.Id == vmObj.SubnetId {
for _, existingId := range subnetInfo.BastionNodeIds {
if existingId == bastionVmId {
return fmt.Sprintf("Bastion (ID: %s) already exists in subnet (ID: %s) in VNet (ID: %s).",
bastionVmId, subnetInfo.Id, vmObj.VNetId), nil

if bastionVmId == "" {
vmIdsInSubnet, err := ListVmByFilter(nsId, mcisId, "SubnetId", subnetInfo.Id)
if err != nil {
common.CBLog.Error(err)
}
for _, v := range vmIdsInSubnet {
tmpPublicIp, _, _ := GetVmIp(nsId, mcisId, v)
if tmpPublicIp != "" {
bastionVmId = v
break
}
}
} else {
for _, existingId := range subnetInfo.BastionNodeIds {
if existingId == bastionVmId {
return fmt.Sprintf("Bastion (ID: %s) already exists in subnet (ID: %s) in VNet (ID: %s).",
bastionVmId, subnetInfo.Id, vmObj.VNetId), nil
}
}
}

Expand Down Expand Up @@ -598,8 +625,7 @@ func GetBastionNodes(nsId string, mcisId string, targetVmId string) (BastionInfo
for _, subnetInfo := range tempVNetInfo.SubnetInfoList {
if subnetInfo.Id == vmObj.SubnetId {
if subnetInfo.BastionNodeIds == nil {
return returnValue, fmt.Errorf("no assigned bastion in Subnet (ID: %s) of VNet (ID: %s) for VM (ID: %s)",
vmObj.SubnetId, vmObj.VNetId, targetVmId)
return returnValue, nil
}
returnValue.VmId = subnetInfo.BastionNodeIds
return returnValue, nil
Expand Down