Implement deregistration functionality for resources and VMs#2286
Implement deregistration functionality for resources and VMs#2286cb-github-robot merged 9 commits intocloud-barista:mainfrom
Conversation
seokho-son
left a comment
There was a problem hiding this comment.
Hi @leehyeoklee
I have some opinions for this PR. PTAL. :)
src/core/infra/manageInfo.go
Outdated
| err = common.CheckString(vmId) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("") | ||
| return err | ||
| } | ||
| check, _ := CheckVm(nsId, mciId, vmId) | ||
|
|
||
| if !check { | ||
| err := fmt.Errorf("The vm " + vmId + " does not exist.") | ||
| return err | ||
| } | ||
|
|
||
| log.Debug().Msg("[Deregister VM] " + vmId) | ||
|
|
||
| // get vm info | ||
| vmInfo, _ := GetVmObject(nsId, mciId, vmId) |
There was a problem hiding this comment.
GetVmObject will validate existence of the VM object too.
So, CheckVm related code can be moved.
and, vmInfo, _ := GetVmObject(nsId, mciId, vmId) needs err handling if we remove CheckVm here.
src/core/infra/manageInfo.go
Outdated
| } | ||
| log.Debug().Msg("Deregister request finished from " + url) | ||
|
|
||
| // delete vms info from TB |
There was a problem hiding this comment.
| // delete vms info from TB | |
| // delete the VM info from TB |
src/core/infra/manageInfo.go
Outdated
| return err | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I think the logic of this section is complex.
We can refine it more direct way. I guess.
- identify the (one) subnet object to be removed before removing vm object.
- remove identified subnet just after vm object has been removed.
src/core/infra/manageInfo.go
Outdated
| // Update associated object lists | ||
| _, err = resource.UpdateAssociatedObjectList(nsId, model.StrImage, vmInfo.ImageId, model.StrDelete, key) | ||
| if err != nil { | ||
| resource.UpdateAssociatedObjectList(nsId, model.StrCustomImage, vmInfo.ImageId, model.StrDelete, key) |
There was a problem hiding this comment.
why do we have additional trial only for image type ? intentional?
There was a problem hiding this comment.
I followed the delete method pattern but excluded associatedObjectList as it's not applicable to Images😓
src/core/resource/common.go
Outdated
| err := common.CheckString(nsId) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("") | ||
| return err | ||
| } | ||
|
|
||
| err = common.CheckString(resourceId) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("") | ||
| return err | ||
| } | ||
| check, err := CheckResource(nsId, resourceType, resourceId) | ||
|
|
||
| if err != nil { | ||
| log.Error().Err(err).Msg("") | ||
| return err | ||
| } | ||
|
|
||
| if !check { | ||
| errString := "The " + resourceType + " " + resourceId + " does not exist." | ||
| err := fmt.Errorf(errString) | ||
| return err | ||
| } |
There was a problem hiding this comment.
Let's reduce duplicated validations. :)
| log.Debug().Msg("Image deregistered successfully from database") | ||
| return nil | ||
|
|
||
| case model.StrCustomImage: |
There was a problem hiding this comment.
let's find an existing function for this operation.
There was a problem hiding this comment.
I replaced the direct ORM query with the GetResource(nsId, model.StrCustomImage, resourceId) funtion!
src/core/resource/common.go
Outdated
| url = model.SpiderRestUrl + "/regmyimage/" + temp.CspImageName | ||
| uid = temp.Uid | ||
|
|
||
| case model.StrSpec: |
There was a problem hiding this comment.
no need to remove Specs in general. too
src/core/resource/common.go
Outdated
| mciId, vmId, err := parseVMKeyPath(vmKey) | ||
| if err != nil { | ||
| log.Warn().Err(err).Str("vmKey", vmKey).Msg("Failed to parse VM key") | ||
| continue | ||
| } | ||
|
|
||
| // Get VM object from kvstore | ||
| vmStoreKey := common.GenMciKey(nsId, mciId, vmId) | ||
| vmKeyValue, exists, err := kvstore.GetKv(vmStoreKey) | ||
| if err != nil || !exists { | ||
| log.Warn().Msgf("Failed to get VM %s to update fields", vmId) | ||
| continue | ||
| } |
There was a problem hiding this comment.
We need to avoid writing codes directly controlling kvstore and the other DBs. I think we have existing functions for this.
src/core/resource/common.go
Outdated
| log.Debug().Msg("Deregister request finished from " + url) | ||
|
|
||
| // Get associated VMs and update their fields to remove references to this resource | ||
| err = ClearDependencyFromVMs(nsId, resourceType, resourceId) |
There was a problem hiding this comment.
should we clear Clear Dependency From VMs?
or should we block it?
There was a problem hiding this comment.
I updated the logic to prevent VM deregistration if any associated resources (SSH Key, Security Group, Data Disk) still exist. The system now checks for these dependencies and blocks the operation if any are found.
src/core/resource/common.go
Outdated
|
|
||
| // parseVMKeyPath parses a VM key path and extracts mciId and vmId | ||
| // Expected format: /ns/{nsId}/mci/{mciId}/vm/{vmId} | ||
| func parseVMKeyPath(vmKey string) (mciId string, vmId string, err error) { |
There was a problem hiding this comment.
we should avoid this pattern.
There was a problem hiding this comment.
I added the sys.securityGroup label to the VM creation/registration logic. This allowed me to remove the previous check by using a label selector to verify VM existence in the subnet instead
seokho-son
left a comment
There was a problem hiding this comment.
LGTM! @leehyeoklee thanks!
|
/approve |
Summary
This PR introduces resource deregistration functionality across major resources and significantly improves the validation logic for subnet deletion to prevent accidental data inconsistency.
Key Changes
1. Feature: Resource Deregistration
Implemented
Deregisteroperations for the following resources. This action removes the resource metadata from Spider and Tumblebug while preserving the actual resource on the CSP.Supported Resources:
Implementation Details:
/regkeypair/{Name},/regvpc/{Name}) instead of delete APIs.ClearDependencyFromVMsto remove references to the deregistered resource from associated VM fields.API Endpoints:
DELETE /ns/{nsId}/deregisterCspResource/sshKey/{sshKeyId}(New)DELETE /ns/{nsId}/deregisterCspResource/securityGroup/{securityGroupId}(New)DELETE /ns/{nsId}/deregisterCspResource/dataDisk/{dataDiskId}(New)DELETE /ns/{nsId}/deregisterCspResource/customImage/{customImageId}(New)2. Fix: Subnet Deletion/Deregistration Validation
Problem:
Previously, the code checked
subnetInfo.Status == NetworkInUseto prevent deleting in-use subnets. However, this status field was never properly set, allowing subnets attached to active VMs to be deleted or deregistered.Solution:
CheckSubnetInUseByVMs: A new function that scans all VMs in the namespace to verify if the targetsubnetIdis in use.DelSubnetandDeregisterSubnet.Todo: