Skip to content

Commit 0aa2f10

Browse files
committed
controller: apply/delete/get components
1 parent 0004c24 commit 0aa2f10

8 files changed

Lines changed: 57 additions & 55 deletions

File tree

apis/v1/types.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ type ComponentSpec struct {
8181

8282
// ComponentOverrides allows limited, safe overrides to a reused VM spec
8383
type ComponentOverrides struct {
84-
UserDataRefs []string `json:"userDataRefs,omitempty" yaml:"userDataRefs,omitempty" gorm:"serializer:json"`
85-
SSHKeys []string `json:"sshKeys,omitempty" yaml:"sshKeys,omitempty" gorm:"serializer:json"`
86-
SecurityGroups []string `json:"securityGroups,omitempty" yaml:"securityGroups,omitempty" gorm:"serializer:json"`
87-
AffinityGroups []string `json:"affinityGroups,omitempty" yaml:"affinityGroups,omitempty" gorm:"serializer:json"`
84+
Template string `json:"template,omitempty" yaml:"template,omitempty"`
85+
ServiceOffering string `json:"serviceOffering,omitempty" yaml:"serviceOffering,omitempty"`
86+
Volumes []VolumeSpec `json:"volumes,omitempty" yaml:"volumes,omitempty" gorm:"serializer:json"`
87+
UserDataRefs []string `json:"userDataRefs,omitempty" yaml:"userDataRefs,omitempty" gorm:"serializer:json"`
88+
SSHKeys []string `json:"sshKeys,omitempty" yaml:"sshKeys,omitempty" gorm:"serializer:json"`
89+
SecurityGroups []string `json:"securityGroups,omitempty" yaml:"securityGroups,omitempty" gorm:"serializer:json"`
90+
AffinityGroups []string `json:"affinityGroups,omitempty" yaml:"affinityGroups,omitempty" gorm:"serializer:json"`
8891
}
8992

9093
// VirtualMachine represents an individual VM instance in CloudStack

controller/controller.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,22 @@ func (c *Controller) applyComponent(comp *v1.Component) error {
714714
comp.Status.Ready = false
715715
comp.Status.LastChecked = time.Now()
716716
}
717-
// Persist desired component with effective spec (if present)
717+
// Resolve base VM spec (by reference or inline) and compute effective spec
718+
var base v1.VirtualMachineSpec
719+
if comp.Spec.VirtualMachineSpec != "" {
720+
var vsr v1.VirtualMachineSpecResource
721+
if err := db.DB.Where("name = ?", comp.Spec.VirtualMachineSpec).First(&vsr).Error; err == nil {
722+
base = vsr.Spec
723+
}
724+
} else {
725+
// if no referenced spec, allow inline EffectiveSpec to be used as base
726+
base = comp.EffectiveSpec
727+
}
728+
729+
effective := mergeVMSpec(base, comp.Spec.Overrides)
730+
comp.EffectiveSpec = effective
731+
732+
// Persist desired component with effective spec
718733
return db.DB.Save(comp).Error
719734
}
720735

controller/reconcile.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,5 +470,18 @@ func mergeVMSpec(base v1.VirtualMachineSpec, ov v1.ComponentOverrides) v1.Virtua
470470
}
471471
}
472472

473+
// Override template and service offering if provided
474+
if ov.Template != "" {
475+
out.Template = ov.Template
476+
}
477+
if ov.ServiceOffering != "" {
478+
out.ServiceOffering = ov.ServiceOffering
479+
}
480+
481+
// Override volumes if provided (replace)
482+
if len(ov.Volumes) > 0 {
483+
out.Volumes = ov.Volumes
484+
}
485+
473486
return out
474487
}

examples/cluster/component-advanced.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/cluster/component-backend.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ kind: Component
33
metadata:
44
name: backend
55
spec:
6-
vmSpec:
7-
name: backend-vmspec
8-
template: my-template
9-
serviceOffering: small
10-
count: 2
6+
virtualMachineSpec: backend-vmspec
7+
replicas: 2
8+
overrides:
9+
template: my-template
10+
serviceOffering: small
11+
healthChecks:
12+
- type: ping
13+
interval: 10s
14+
timeout: 5s

examples/cluster/component-frontend.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ kind: Component
33
metadata:
44
name: frontend
55
spec:
6-
vmSpec:
7-
name: frontend-vmspec
8-
template: my-template
9-
serviceOffering: small
10-
count: 2
6+
virtualMachineSpec: frontend-vmspec
7+
replicas: 2
8+
overrides:
9+
template: my-template
10+
serviceOffering: small
11+
healthChecks:
12+
- type: ping
13+
interval: 10s
14+
timeout: 5s

examples/virtualmachinespec.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

pkg/handlers/print.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,13 @@ func PrintVMsFromDB(vms []v1.VirtualMachine) {
175175
// PrintComponents prints components returned by the controller DB query.
176176
func PrintComponents(comps []v1.Component) {
177177
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
178-
fmt.Fprintln(w, "NAME\tREPLICAS\tVM SPEC\tOBSERVED REPLICAS")
178+
fmt.Fprintln(w, "NAME\tREPLICAS\tVM SPEC\tSTATE\tOBSERVED REPLICAS")
179179
for _, c := range comps {
180180
replicas := c.Spec.Replicas
181181
vmSpec := c.Spec.VirtualMachineSpec
182182
observed := c.ObservedReplicas
183-
fmt.Fprintf(w, "%s\t%d\t%s\t%d\n", c.Metadata.Name, replicas, vmSpec, observed)
183+
state := c.Status.ObservedState
184+
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%d\n", c.Metadata.Name, replicas, vmSpec, state, observed)
184185
}
185186
w.Flush()
186187
}

0 commit comments

Comments
 (0)