Skip to content

Commit 8648806

Browse files
committed
controller: do not delete VMs when delete a component
1 parent c6dc36e commit 8648806

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

apis/v1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type VirtualMachine struct {
9898
Status Status `json:"status,omitempty" gorm:"embedded"`
9999
// ObservedSpec stores the configuration fetched from CloudStack (observed state)
100100
ObservedSpec VirtualMachineSpec `json:"observedSpec,omitempty" yaml:"observedSpec,omitempty" gorm:"serializer:json"`
101+
// ApplicationID links this VM to a parent Application (store application name or id)
102+
ApplicationID string `json:"applicationId,omitempty" yaml:"applicationId,omitempty" gorm:"column:application_id"`
101103
}
102104

103105
// VirtualMachineSpec defines reusable VM configuration (template/offering/network)

controller/cmd_delete.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"log"
88
)
99

10-
// DeleteApplication deletes an Application and its dependent resources
10+
// DeleteApplication deletes an Application and disassociates its Components
11+
// (it does NOT delete VMs; VMs are owned by the Application lifecycle)
1112
func DeleteApplication(name string) {
1213
var app v1.Application
1314
if db.DB == nil {
@@ -20,7 +21,9 @@ func DeleteApplication(name string) {
2021
}
2122

2223
for _, compRef := range app.Spec.Components {
23-
DeleteComponent(compRef.Name)
24+
// Remove the component record but do NOT delete VMs here; VMs belong to the application lifecycle.
25+
db.DB.Where("name = ?", compRef.Name).Delete(&v1.Component{})
26+
log.Printf("Component %s disassociated from application %s", compRef.Name, name)
2427
}
2528

2629
if err := db.DB.Delete(&app).Error; err != nil {
@@ -30,7 +33,8 @@ func DeleteApplication(name string) {
3033
log.Printf("Application %s deleted successfully", name)
3134
}
3235

33-
// DeleteComponent deletes a Component and its VMs
36+
// DeleteComponent deletes a Component record if it's not referenced by any
37+
// Application. It does NOT delete VMs (VMs belong to applications).
3438
func DeleteComponent(name string) {
3539
var comp v1.Component
3640
if db.DB == nil {
@@ -42,13 +46,16 @@ func DeleteComponent(name string) {
4246
log.Fatalf("Component %s not found: %v", name, err)
4347
}
4448

45-
var vms []v1.VirtualMachine
46-
if err := db.DB.Where("metadata_labels @> ?", map[string]string{"component": name}).Find(&vms).Error; err != nil {
47-
log.Fatalf("Failed to find VMs for component %s: %v", name, err)
48-
}
49-
50-
for _, vm := range vms {
51-
DeleteVM(vm.Metadata.Name)
49+
// Do not delete VMs here. Prevent deletion if the component is still referenced by any Application.
50+
var apps []v1.Application
51+
if err := db.DB.Find(&apps).Error; err == nil {
52+
for _, a := range apps {
53+
for _, cref := range a.Spec.Components {
54+
if cref.Name == name {
55+
log.Fatalf("Component %s is still referenced by Application %s; cannot delete", name, a.Metadata.Name)
56+
}
57+
}
58+
}
5259
}
5360

5461
if err := db.DB.Delete(&comp).Error; err != nil {

0 commit comments

Comments
 (0)