Skip to content

Commit fade7e8

Browse files
committed
apis: move networkIDs to networks
1 parent 8103c8a commit fade7e8

14 files changed

Lines changed: 102 additions & 25 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Quick reference for the key fields users will commonly set for higher-level reso
104104
| `metadata.name` | string | Reusable VM spec identifier |
105105
| `spec.template` | string | Template name or ID |
106106
| `spec.serviceOffering` | string | Service offering (size) |
107-
| `spec.networkIds` | list | Network IDs to attach |
107+
| `spec.networks` | list | Network IDs to attach |
108108
| `spec.sshKeys` | list | SSH key names to inject |
109109
| `spec.userDataRefs` | list | Optional references to `UserData` resources |
110110
---
@@ -122,7 +122,7 @@ spec:
122122
zone: zone-1
123123
template: ubuntu-22.04
124124
serviceOffering: medium
125-
networkIds:
125+
networks:
126126
- existing-network-1-id
127127
sshKeys:
128128
- platform-admin
@@ -183,7 +183,7 @@ spec:
183183
project: project-2
184184
template: ubuntu-22.04
185185
serviceOffering: medium
186-
networkIds:
186+
networks:
187187
- existing-network-1-id
188188
sshKeys:
189189
- platform-admin

apis/v1/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type VirtualMachineSpec struct {
106106
Project string `json:"project,omitempty" yaml:"project"` // CloudStack project ID or name
107107
Template string `json:"template,omitempty" yaml:"template"` // VM template name/ID
108108
ServiceOffering string `json:"serviceOffering,omitempty" yaml:"serviceOffering"` // VM service offering (size)
109-
NetworkIDs []string `json:"networkIds,omitempty" yaml:"networkIds" gorm:"serializer:json"` // Attached networks
109+
Networks []string `json:"networks,omitempty" yaml:"networks" gorm:"serializer:json"` // Attached networks
110110
SSHKeys []string `json:"sshKeys,omitempty" yaml:"sshKeys" gorm:"serializer:json"` // SSH keys for access
111111
SecurityGroups []string `json:"securityGroups,omitempty" yaml:"securityGroups" gorm:"serializer:json"` // Firewall groups
112112
AffinityGroups []string `json:"affinityGroups,omitempty" yaml:"affinityGroups" gorm:"serializer:json"` // Host/VM affinity rules

controller/cmd_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func ListVirtualMachineSpec(name string) {
117117
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
118118
fmt.Fprintln(w, "NAME\tTEMPLATE\tSERVICE_OFFERING\tNETWORKS\tVOLUMES")
119119
for _, s := range specs {
120-
nets := strings.Join(s.Spec.NetworkIDs, ",")
120+
nets := strings.Join(s.Spec.Networks, ",")
121121
volCount := 0
122122
if len(s.Spec.Volumes) > 0 {
123123
volCount = len(s.Spec.Volumes)

controller/controller.go

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"bytes"
45
v1 "cloudstackctl/apis/v1"
56
"cloudstackctl/db"
67
"cloudstackctl/pkg/handlers"
@@ -97,11 +98,60 @@ func (c *Controller) handleApply(w http.ResponseWriter, r *http.Request) {
9798
}
9899
applyErr = c.applyApplication(&app)
99100
case "Component":
100-
var comp v1.Component
101-
if err := json.Unmarshal(body, &comp); err != nil {
101+
// Accept two forms for Component.spec.virtualMachineSpec:
102+
// - a string referencing a named VirtualMachineSpec
103+
// - an inline object describing a VirtualMachineSpec
104+
type compSpecIn struct {
105+
VirtualMachineSpec json.RawMessage `json:"virtualMachineSpec"`
106+
Replicas int `json:"replicas"`
107+
Overrides v1.ComponentOverrides `json:"overrides"`
108+
HealthChecks []v1.HealthCheck `json:"healthChecks"`
109+
}
110+
type compIn struct {
111+
APIVersion string `json:"apiVersion"`
112+
Kind string `json:"kind"`
113+
Metadata v1.Metadata `json:"metadata"`
114+
Spec compSpecIn `json:"spec"`
115+
Status v1.Status `json:"status"`
116+
}
117+
118+
var ci compIn
119+
if err := json.Unmarshal(body, &ci); err != nil {
102120
http.Error(w, "failed to parse Component", http.StatusBadRequest)
103121
return
104122
}
123+
124+
comp := v1.Component{
125+
APIVersion: ci.APIVersion,
126+
Kind: ci.Kind,
127+
Metadata: ci.Metadata,
128+
Spec: v1.ComponentSpec{
129+
Replicas: ci.Spec.Replicas,
130+
Overrides: ci.Spec.Overrides,
131+
HealthChecks: ci.Spec.HealthChecks,
132+
},
133+
}
134+
135+
// Interpret VirtualMachineSpec field which may be a string or object
136+
if len(ci.Spec.VirtualMachineSpec) > 0 {
137+
// If it starts with a quote it's a JSON string
138+
b := ci.Spec.VirtualMachineSpec
139+
// trim whitespace
140+
trimmed := bytes.TrimSpace(b)
141+
if len(trimmed) > 0 && trimmed[0] == '"' {
142+
var ref string
143+
if err := json.Unmarshal(b, &ref); err == nil {
144+
comp.Spec.VirtualMachineSpec = ref
145+
}
146+
} else {
147+
// Attempt to decode inline VirtualMachineSpec
148+
var vms v1.VirtualMachineSpec
149+
if err := json.Unmarshal(b, &vms); err == nil {
150+
comp.EffectiveSpec = vms
151+
}
152+
}
153+
}
154+
105155
applyErr = c.applyComponent(&comp)
106156
case "VirtualMachine":
107157
var vm v1.VirtualMachine
@@ -119,7 +169,7 @@ func (c *Controller) handleApply(w http.ResponseWriter, r *http.Request) {
119169

120170
if applyErr != nil {
121171
log.Printf("apply error for kind=%s: %v", kind, applyErr)
122-
http.Error(w, "failed to apply resource", http.StatusInternalServerError)
172+
http.Error(w, fmt.Sprintf("failed to apply resource: %v", applyErr), http.StatusInternalServerError)
123173
return
124174
}
125175

@@ -618,11 +668,11 @@ func compareVMSpec(a, b v1.VirtualMachineSpec) bool {
618668
if a.Project != b.Project {
619669
return false
620670
}
621-
if len(a.NetworkIDs) != len(b.NetworkIDs) {
671+
if len(a.Networks) != len(b.Networks) {
622672
return false
623673
}
624-
for i := range a.NetworkIDs {
625-
if a.NetworkIDs[i] != b.NetworkIDs[i] {
674+
for i := range a.Networks {
675+
if a.Networks[i] != b.Networks[i] {
626676
return false
627677
}
628678
}

controller/reconcile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ func (c *Controller) populateObservedSpec(vm *v1.VirtualMachine) error {
125125
obs.ServiceOffering = v.Serviceofferingid
126126
}
127127

128-
// NICs -> NetworkIDs
128+
// NICs -> Networks
129129
if len(v.Nic) > 0 {
130-
obs.NetworkIDs = []string{}
130+
obs.Networks = []string{}
131131
for _, n := range v.Nic {
132132
if n.Networkid != "" {
133-
obs.NetworkIDs = append(obs.NetworkIDs, n.Networkid)
133+
obs.Networks = append(obs.Networks, n.Networkid)
134134
}
135135
}
136136
}

examples/cluster/component-advanced.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spec:
77
virtualMachineSpec:
88
template: ubuntu-22.04
99
serviceOffering: medium
10-
networkIds:
10+
networks:
1111
- existing-network-1-id
1212
sshKeys:
1313
- platform-admin
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: cloudstackctl/v1
2+
kind: Component
3+
metadata:
4+
name: backend
5+
spec:
6+
vmSpec:
7+
name: backend-vmspec
8+
template: my-template
9+
serviceOffering: small
10+
count: 2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: cloudstackctl/v1
2+
kind: VirtualMachineSpec
3+
metadata:
4+
name: backend-vmspec
5+
spec:
6+
template: my-template
7+
serviceOffering: small
8+
networks:
9+
- example-network
10+
sshKeys:
11+
- my-key
12+
volumes:
13+
- name: data-disk
14+
type: data
15+
diskOffering: standard-hdd
16+
size: 50

examples/cluster/virtualmachinespec.yaml renamed to examples/cluster/virtualmachinespec-frontend.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
spec:
66
template: my-template
77
serviceOffering: small
8-
networkIDs:
8+
networks:
99
- example-network
1010
sshKeys:
1111
- my-key

0 commit comments

Comments
 (0)