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

Enhance precondition checking in mcis dynamic #1509

Merged
merged 1 commit into from
Apr 17, 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
15 changes: 6 additions & 9 deletions src/core/mcir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,16 @@ func DelAllResources(nsId string, resourceType string, subString string, forceFl
for _, v := range resourceIdList {
// if subString is provided, check the resourceId contains the subString.
if subString == "" || strings.Contains(v, subString) {
deleteStatus = ""

err := DelResource(nsId, resourceType, v, forceFlag)
deleteStatus = "[Done] "
errString := ""

err := DelResource(nsId, resourceType, v, forceFlag)
if err != nil {
deleteStatus = err.Error()
} else {
deleteStatus = " [Done]"
deleteStatus = "[Failed] "
errString = " (" + err.Error() + ")"
}

deletedResources.IdList = append(deletedResources.IdList, resourceType+": "+v+deleteStatus)
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+resourceType+": "+v+errString)
}
}
return deletedResources, nil
Expand Down Expand Up @@ -144,8 +143,6 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag
}

key := common.GenResourceKey(nsId, resourceType, resourceId)
log.Debug().Msg("key: " + key)

keyValue, _ := common.CBStore.Get(key)
// In CheckResource() above, calling 'CBStore.Get()' and checking err parts exist.
// So, in here, we don't need to check whether keyValue == nil or err != nil.
Expand Down
20 changes: 12 additions & 8 deletions src/core/mcis/manageInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) {

option = common.ToLower(option)
deletedResources := common.IdList{}
deleteStatus := " [Done]"
deleteStatus := "[Done] "

err := common.CheckString(nsId)
if err != nil {
Expand Down Expand Up @@ -1727,10 +1727,14 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) {
key := common.GenMcisKey(nsId, mcisId, "")

// delete associated MCIS Policy
err = DelMcisPolicy(nsId, mcisId)
if err == nil {
log.Error().Err(err).Msg("")
deletedResources.IdList = append(deletedResources.IdList, "Policy: "+mcisId+deleteStatus)
check, _ = CheckMcisPolicy(nsId, mcisId)
if check {
err = DelMcisPolicy(nsId, mcisId)
if err != nil {
log.Error().Err(err).Msg("")
return deletedResources, err
}
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"Policy: "+mcisId)
}

vmList, err := ListVmId(nsId, mcisId)
Expand Down Expand Up @@ -1773,7 +1777,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) {
for _, v2 := range vmInfo.DataDiskIds {
mcir.UpdateAssociatedObjectList(nsId, common.StrDataDisk, v2, common.StrDelete, vmKey)
}
deletedResources.IdList = append(deletedResources.IdList, "VM: "+v+deleteStatus)
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"VM: "+v)
}

// delete subGroup info
Expand All @@ -1789,7 +1793,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) {
log.Error().Err(err).Msg("")
return deletedResources, err
}
deletedResources.IdList = append(deletedResources.IdList, "SubGroup: "+v+deleteStatus)
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"SubGroup: "+v)
}

// delete associated CSP NLBs
Expand Down Expand Up @@ -1822,7 +1826,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) {
log.Error().Err(err).Msg("")
return deletedResources, err
}
deletedResources.IdList = append(deletedResources.IdList, "MCIS: "+mcisId+deleteStatus)
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"MCIS: "+mcisId)

return deletedResources, nil
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/mcis/nlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,17 +1066,17 @@ func DelAllNLB(nsId string, mcisId string, subString string, forceFlag string) (
for _, v := range resourceIdList {
// if subString is provided, check the resourceId contains the subString.
if subString == "" || strings.Contains(v, subString) {
deleteStatus = ""

err := DelNLB(nsId, mcisId, v, forceFlag)
deleteStatus = "[Done] "
errString := ""

err := DelNLB(nsId, mcisId, v, forceFlag)
if err != nil {
deleteStatus = err.Error()
} else {
deleteStatus = " [Done]"
deleteStatus = "[Failed] "
errString = " (" + err.Error() + ")"
}

deletedResources.IdList = append(deletedResources.IdList, "NLB: "+v+deleteStatus)
deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"NLB: "+v+errString)
}
}
return deletedResources, nil
Expand Down
15 changes: 1 addition & 14 deletions src/core/mcis/orchestration.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,6 @@ func CreateMcisPolicy(nsId string, mcisId string, u *McisPolicyReq) (McisPolicyI
func GetMcisPolicyObject(nsId string, mcisId string) (McisPolicyInfo, error) {
log.Debug().Msg("[GetMcisPolicyObject]" + mcisId)

err := common.CheckString(nsId)
if err != nil {
temp := McisPolicyInfo{}
log.Error().Err(err).Msg("")
return temp, err
}

err = common.CheckString(mcisId)
if err != nil {
temp := McisPolicyInfo{}
log.Error().Err(err).Msg("")
return temp, err
}
key := common.GenMcisPolicyKey(nsId, mcisId, "")
log.Debug().Msgf("Key: %v", key)
keyValue, err := common.CBStore.Get(key)
Expand Down Expand Up @@ -604,7 +591,7 @@ func DelMcisPolicy(nsId string, mcisId string) error {
check, _ := CheckMcisPolicy(nsId, mcisId)

if !check {
err := fmt.Errorf("The mcis Policy" + mcisId + " does not exist.")
err := fmt.Errorf("The mcis Policy " + mcisId + " does not exist.")
return err
}

Expand Down
80 changes: 72 additions & 8 deletions src/core/mcis/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,6 @@ func CreateMcisGroupVm(nsId string, mcisId string, vmRequest *TbVmReq, newSubGro

// Install CB-Dragonfly monitoring agent

fmt.Printf("\n[Init monitoring agent] for %+v\n - req.InstallMonAgent: %+v\n\n", mcisId, mcisTmp.InstallMonAgent)
if !strings.Contains(mcisTmp.InstallMonAgent, "no") {

// Sleep for 60 seconds for a safe DF agent installation.
Expand Down Expand Up @@ -1076,8 +1075,6 @@ func CreateMcis(nsId string, req *TbMcisReq, option string) (*TbMcisInfo, error)

// Install CB-Dragonfly monitoring agent

fmt.Printf("[Init monitoring agent] for %+v\n - req.InstallMonAgent: %+v\n\n", mcisTmp.Id, req.InstallMonAgent)

mcisTmp.InstallMonAgent = req.InstallMonAgent
UpdateMcisInfo(nsId, mcisTmp)

Expand Down Expand Up @@ -1255,7 +1252,21 @@ func CreateMcisDynamic(nsId string, req *TbMcisDynamicReq) (*TbMcisInfo, error)
}

vmRequest := req.Vm
// Check whether VM names meet requirement. If not, generate default resources dynamically.
// Check whether VM names meet requirement.
errStr := ""
for i, k := range vmRequest {
err = checkCommonResAvailable(&k)
if err != nil {
log.Error().Err(err).Msgf("[%d] Failed to find common resource for MCIS provision", i)
errStr += "{[" + strconv.Itoa(i+1) + "] " + err.Error() + "} "
}
}
if errStr != "" {
err = fmt.Errorf(errStr)
return emptyMcis, err
}

//If not, generate default resources dynamically.
for _, k := range vmRequest {
vmReq, err := getVmReqFromDynamicReq(nsId, &k)
if err != nil {
Expand Down Expand Up @@ -1306,6 +1317,62 @@ func CreateMcisVmDynamic(nsId string, mcisId string, req *TbVmDynamicReq) (*TbMc
return CreateMcisGroupVm(nsId, mcisId, vmReq, true)
}

// checkCommonResAvailable is func to check common resources availability
func checkCommonResAvailable(req *TbVmDynamicReq) error {

vmRequest := req
// Check whether VM names meet requirement.
k := vmRequest

vmReq := &TbVmReq{}
tempInterface, err := mcir.GetResource(common.SystemCommonNs, common.StrSpec, k.CommonSpec)
if err != nil {
err := fmt.Errorf("Failed to get the spec " + k.CommonSpec)
log.Error().Err(err).Msg("")
return err
}
specInfo := mcir.TbSpecInfo{}
err = common.CopySrcToDest(&tempInterface, &specInfo)
if err != nil {
err := fmt.Errorf("Failed to CopySrcToDest() " + k.CommonSpec)
log.Error().Err(err).Msg("")
return err
}

// remake vmReqest from given input and check resource availability
vmReq.ConnectionName = specInfo.ConnectionName

// If ConnectionName is specified by the request, Use ConnectionName from the request
if k.ConnectionName != "" {
vmReq.ConnectionName = k.ConnectionName
}

// validate the region for spec
_, err = common.GetConnConfig(specInfo.RegionName)
if err != nil {
err := fmt.Errorf("Failed to get RegionName (" + specInfo.RegionName + ") for Spec (" + k.CommonSpec + ") is not found.")
log.Error().Err(err).Msg("")
return err
}
// validate the GetConnConfig for spec
_, err = common.GetConnConfig(vmReq.ConnectionName)
if err != nil {
err := fmt.Errorf("Failed to get ConnectionName (" + vmReq.ConnectionName + ") for Spec (" + k.CommonSpec + ") is not found.")
log.Error().Err(err).Msg("")
return err
}

vmReq.ImageId = mcir.ToNamingRuleCompatible(vmReq.ConnectionName + "-" + k.CommonImage)
tempInterface, err = mcir.GetResource(common.SystemCommonNs, common.StrImage, vmReq.ImageId)
if err != nil {
err := fmt.Errorf("Failed to get Image " + k.CommonImage + " from " + vmReq.ConnectionName)
log.Error().Err(err).Msg("")
return err
}

return nil
}

// getVmReqForDynamicMcis is func to getVmReqFromDynamicReq
func getVmReqFromDynamicReq(nsId string, req *TbVmDynamicReq) (*TbVmReq, error) {

Expand Down Expand Up @@ -1448,9 +1515,8 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV
keyValue, err := common.CBStore.Get(key)
if err != nil {
log.Fatal().Err(err).Msg("AddVmToMcis(); CBStore.Get() returned an error.")
// return nil, err
return err
}

if keyValue == nil {
return fmt.Errorf("AddVmToMcis: Cannot find mcisId. Key: %s", key)
}
Expand Down Expand Up @@ -1530,8 +1596,6 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV
// CreateVm is func to create VM (option = "register" for register existing VM)
func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) error {

fmt.Printf("\n[CreateVm]\n")

var err error = nil
switch {
case vmInfoData.Name == "":
Expand Down
2 changes: 1 addition & 1 deletion src/core/mcis/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func RecommendVmLatency(nsId string, specList *[]mcir.TbSpecInfo, param *[]Param
sort.Slice(distances, func(i, j int) bool {
return distances[i].distance < distances[j].distance
})
fmt.Printf("\n[Latency]\n %v \n", distances)
//fmt.Printf("\n[Latency]\n %v \n", distances)

priorityCnt := 1
for i := range distances {
Expand Down