Skip to content

Commit 63ff842

Browse files
committed
controller: move CloudStackID from Status to VirtualMachine
1 parent 8648806 commit 63ff842

8 files changed

Lines changed: 28 additions & 27 deletions

File tree

apis/v1/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type Metadata struct {
2020
type Status struct {
2121
ObservedState string `json:"observedState,omitempty"` // Running/Failed/Pending
2222
Ready bool `json:"ready,omitempty"` // Health check status
23-
CloudStackID string `json:"cloudStackId,omitempty"` // External ID in CloudStack
2423
LastChecked time.Time `json:"lastChecked,omitempty"` // Last health check timestamp
2524
Drift bool `json:"drift,omitempty"` // True if desired != observed state
2625
}
@@ -100,6 +99,8 @@ type VirtualMachine struct {
10099
ObservedSpec VirtualMachineSpec `json:"observedSpec,omitempty" yaml:"observedSpec,omitempty" gorm:"serializer:json"`
101100
// ApplicationID links this VM to a parent Application (store application name or id)
102101
ApplicationID string `json:"applicationId,omitempty" yaml:"applicationId,omitempty" gorm:"column:application_id"`
102+
// CloudStackID is the external provider ID for this VM in CloudStack
103+
CloudStackID string `json:"cloudStackId,omitempty" yaml:"cloudStackId,omitempty" gorm:"column:cloudstack_id"`
103104
}
104105

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

controller/cmd_delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func DeleteVM(name string) {
9696
log.Fatalf("VM %s not found: %v", name, err)
9797
}
9898

99-
if vm.Status.CloudStackID != "" {
99+
if vm.CloudStackID != "" {
100100
csClient, err := cloudstack.NewClient()
101101
if err != nil {
102102
log.Printf("Warning: CloudStack client unavailable, skipping CloudStack delete: %v", err)
103103
} else {
104-
params := csClient.VirtualMachine.NewDestroyVirtualMachineParams(vm.Status.CloudStackID)
104+
params := csClient.VirtualMachine.NewDestroyVirtualMachineParams(vm.CloudStackID)
105105
if _, err := csClient.VirtualMachine.DestroyVirtualMachine(params); err != nil {
106106
log.Printf("Warning: Failed to delete VM %s from CloudStack: %v", name, err)
107107
}

controller/cmd_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func ListVMs(name string) {
161161
fmt.Fprintln(w, "NAME\tID\tTEMPLATE\tSERVICE OFFERING\tSTATUS\tREADY\tDRIFT")
162162

163163
for _, vm := range vms {
164-
id := vm.Status.CloudStackID
164+
id := vm.CloudStackID
165165
tmpl := vm.Spec.Template
166166
if tmpl == "" && vm.ObservedSpec.Template != "" {
167167
tmpl = vm.ObservedSpec.Template

controller/controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ func (c *Controller) handleDelete(w http.ResponseWriter, r *http.Request) {
586586
http.Error(w, "virtualmachine not found", http.StatusNotFound)
587587
return
588588
}
589-
if vm.Status.CloudStackID != "" {
590-
dp := c.csClient.VirtualMachine.NewDestroyVirtualMachineParams(vm.Status.CloudStackID)
589+
if vm.CloudStackID != "" {
590+
dp := c.csClient.VirtualMachine.NewDestroyVirtualMachineParams(vm.CloudStackID)
591591
c.csClient.VirtualMachine.DestroyVirtualMachine(dp)
592592
}
593593
db.DB.Delete(&vm)
@@ -704,7 +704,7 @@ func (c *Controller) applyVMSpec(vs *v1.VirtualMachineSpecResource) error {
704704
func (c *Controller) applyVM(vm *v1.VirtualMachine) error {
705705
// Attempt to find the VM in CloudStack by CloudStackID or by name
706706
// If CloudStackID is empty, try to discover VM in CloudStack
707-
if vm.Status.CloudStackID == "" {
707+
if vm.CloudStackID == "" {
708708
// search CloudStack by name and project
709709
params := c.csClient.VirtualMachine.NewListVirtualMachinesParams()
710710
params.SetName(vm.Metadata.Name)
@@ -720,7 +720,7 @@ func (c *Controller) applyVM(vm *v1.VirtualMachine) error {
720720
resp, err := c.csClient.VirtualMachine.ListVirtualMachines(params)
721721
if err == nil && resp != nil && len(resp.VirtualMachines) > 0 {
722722
// associate existing CloudStack VM
723-
vm.Status.CloudStackID = resp.VirtualMachines[0].Id
723+
vm.CloudStackID = resp.VirtualMachines[0].Id
724724
vm.Status.ObservedState = resp.VirtualMachines[0].State
725725
}
726726
}
@@ -746,8 +746,8 @@ func (c *Controller) applyVM(vm *v1.VirtualMachine) error {
746746
}
747747

748748
// Update CloudStackID and observed state if discovered
749-
if vm.Status.CloudStackID != "" {
750-
existing.Status.CloudStackID = vm.Status.CloudStackID
749+
if vm.CloudStackID != "" {
750+
existing.CloudStackID = vm.CloudStackID
751751
existing.Status.ObservedState = vm.Status.ObservedState
752752
}
753753

controller/drift.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ func (c *Controller) DetectDrift(vm *v1.VirtualMachine) error {
1313
log.Printf("Checking drift for VM: %s", vm.Metadata.Name)
1414

1515
// Skip if VM not created in CloudStack
16-
if vm.Status.CloudStackID == "" {
16+
if vm.CloudStackID == "" {
1717
vm.Status.Drift = false
1818
return nil
1919
}
2020

2121
// Get actual VM state from CloudStack
22-
actualState, err := cloudstack.GetVMState(c.csClient, vm.Status.CloudStackID)
22+
actualState, err := cloudstack.GetVMState(c.csClient, vm.CloudStackID)
2323
if err != nil {
2424
return err
2525
}

controller/health.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ func (c *Controller) CheckComponentHealth(component *v1.Component) (bool, error)
6161
// CheckVMHealth performs ping/SSH health check for a VM
6262
func (c *Controller) CheckVMHealth(vm *v1.VirtualMachine) (bool, error) {
6363
// Skip if VM not created in CloudStack
64-
if vm.Status.CloudStackID == "" {
64+
if vm.CloudStackID == "" {
6565
return false, nil
6666
}
6767
// Query CloudStack for the VM to obtain its IP(s)
6868
params := c.csClient.VirtualMachine.NewListVirtualMachinesParams()
69-
params.SetId(vm.Status.CloudStackID)
69+
params.SetId(vm.CloudStackID)
7070
resp, err := c.csClient.VirtualMachine.ListVirtualMachines(params)
7171
if err != nil {
7272
log.Printf("failed to describe VM %s: %v", vm.Metadata.Name, err)
@@ -75,7 +75,7 @@ func (c *Controller) CheckVMHealth(vm *v1.VirtualMachine) (bool, error) {
7575
return false, db.DB.Save(vm).Error
7676
}
7777
if resp == nil || len(resp.VirtualMachines) == 0 {
78-
log.Printf("no CloudStack VM found for %s (id=%s)", vm.Metadata.Name, vm.Status.CloudStackID)
78+
log.Printf("no CloudStack VM found for %s (id=%s)", vm.Metadata.Name, vm.CloudStackID)
7979
vm.Status.Ready = false
8080
vm.Status.LastChecked = time.Now()
8181
return false, db.DB.Save(vm).Error
@@ -92,7 +92,7 @@ func (c *Controller) CheckVMHealth(vm *v1.VirtualMachine) (bool, error) {
9292
}
9393
}
9494
if vmIP == "" {
95-
log.Printf("no IP address found for VM %s (id=%s)", vm.Metadata.Name, vm.Status.CloudStackID)
95+
log.Printf("no IP address found for VM %s (id=%s)", vm.Metadata.Name, vm.CloudStackID)
9696
vm.Status.Ready = false
9797
vm.Status.LastChecked = time.Now()
9898
return false, db.DB.Save(vm).Error

controller/reconcile.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func (c *Controller) ReconcileVM(vm *v1.VirtualMachine) error {
8282
}
8383

8484
// Check if VM exists; if not, create it
85-
if vm.Status.CloudStackID == "" {
85+
if vm.CloudStackID == "" {
8686
if id, err := handlers.ApplyVirtualMachineManaged(vm, true); err != nil {
8787
return err
8888
} else {
8989
if id != "" {
90-
vm.Status.CloudStackID = id
90+
vm.CloudStackID = id
9191
db.DB.Save(vm)
9292
}
9393
}
@@ -112,8 +112,8 @@ func (c *Controller) ReconcileVM(vm *v1.VirtualMachine) error {
112112
func (c *Controller) populateObservedSpec(vm *v1.VirtualMachine) error {
113113
// Use SDK to list by id or name
114114
lp := c.csClient.VirtualMachine.NewListVirtualMachinesParams()
115-
if vm.Status.CloudStackID != "" {
116-
lp.SetId(vm.Status.CloudStackID)
115+
if vm.CloudStackID != "" {
116+
lp.SetId(vm.CloudStackID)
117117
} else {
118118
lp.SetName(vm.Metadata.Name)
119119
}
@@ -128,9 +128,9 @@ func (c *Controller) populateObservedSpec(vm *v1.VirtualMachine) error {
128128

129129
v := resp.VirtualMachines[0]
130130

131-
// if vm.Status.CloudStackID is not set, set it from the observed VM
132-
if vm.Status.CloudStackID == "" && v.Id != "" {
133-
vm.Status.CloudStackID = v.Id
131+
// if vm.CloudStackID is not set, set it from the observed VM
132+
if vm.CloudStackID == "" && v.Id != "" {
133+
vm.CloudStackID = v.Id
134134
}
135135

136136
// Map some observed fields into ObservedSpec using SDK types directly
@@ -194,9 +194,9 @@ func (c *Controller) populateObservedSpec(vm *v1.VirtualMachine) error {
194194
}
195195

196196
// Volumes: list volumes attached to the VM if we have CloudStack ID
197-
if vm.Status.CloudStackID != "" {
197+
if vm.CloudStackID != "" {
198198
vp := c.csClient.Volume.NewListVolumesParams()
199-
vp.SetVirtualmachineid(vm.Status.CloudStackID)
199+
vp.SetVirtualmachineid(vm.CloudStackID)
200200
volResp, verr := c.csClient.Volume.ListVolumes(vp)
201201
if verr == nil && volResp != nil && len(volResp.Volumes) > 0 {
202202
obs.Volumes = []v1.VolumeSpec{}
@@ -280,7 +280,7 @@ func (c *Controller) createComponentVMs(comp *v1.Component, compRef v1.Component
280280
return err
281281
} else {
282282
if id != "" {
283-
vm.Status.CloudStackID = id
283+
vm.CloudStackID = id
284284
db.DB.Save(vm)
285285
}
286286
}

pkg/handlers/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func PrintVMsFromDB(vms []v1.VirtualMachine) {
150150
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
151151
fmt.Fprintln(w, "NAME\tID\tTEMPLATE\tSERVICE OFFERING\tSTATUS\tREADY\tDRIFT")
152152
for _, vm := range vms {
153-
id := vm.Status.CloudStackID
153+
id := vm.CloudStackID
154154
tmpl := vm.Spec.Template
155155
if tmpl == "" && vm.ObservedSpec.Template != "" {
156156
tmpl = vm.ObservedSpec.Template

0 commit comments

Comments
 (0)