Skip to content

Commit 9d5fcf5

Browse files
committed
cluster: get all vms with -A/--all
1 parent 1d12b69 commit 9d5fcf5

6 files changed

Lines changed: 46 additions & 17 deletions

File tree

Development.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ Kubernetes-style declarative orchestration tool for Apache CloudStack
44
## Build & Install
55

66
```bash
7-
# Build binary
7+
# Build binary for CLI
88
go build -o cloudstackctl main.go
99

10+
# Build binary for Controller (if run locally)
11+
go build -o cloudstackctl-controller cmd/controller
12+
1013
# Install to PATH
1114
sudo mv cloudstackctl /usr/local/bin/
1215
```

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ For local development configuration see [Development.md](Development.md).
88

99
# Architecture Overview
1010

11-
* Single `cloudstackctl` binary provides the CLI and controller runtime.
11+
* Single `cloudstackctl` binary provides the CLI.
12+
* Single `cloudstackctl-controller` binary provides the API server and controller runtime.
1213
* Controller sends async requests to CloudStack API, writes desired & observed state, manages health checks and dependency graph. It exposes a small health endpoint on port `65426` (`/health`).
1314
* CLI operates locally and uses the CloudStack client to perform direct actions when appropriate; credentials are loaded from a config file (see `-c`) or environment variables (`.env.cloudstack` is used by default if present).
1415

cmd/cli/get.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ var getCmd = &cobra.Command{
5050
// Cluster mode: forward to controller HTTP API (/list)
5151
var endpoint = "/list"
5252
q := url.Values{}
53+
if getAll && resourceType == "VirtualMachine" {
54+
q.Set("all", "true")
55+
}
5356
q.Set("kind", resourceType)
5457
if name != "" {
5558
q.Set("name", name)
@@ -61,7 +64,7 @@ var getCmd = &cobra.Command{
6164
}
6265

6366
// If controller returned VM objects from DB, pretty-print them
64-
if resourceType == "VirtualMachine" {
67+
if resourceType == "VirtualMachine" && !getAll {
6568
var vms []v1.VirtualMachine
6669
if err := json.Unmarshal(body, &vms); err == nil {
6770
handlers.PrintVMsFromDB(vms)
@@ -77,22 +80,25 @@ func init() {
7780
rootCmd.AddCommand(getCmd)
7881
}
7982

83+
var getAll bool
84+
85+
func init() {
86+
getCmd.Flags().BoolVarP(&getAll, "all", "A", false, "Show all VMs from CloudStack (include unmanaged) — cluster mode only")
87+
}
88+
8089
// tryDecodeAndPrint attempts to decode controller JSON into known typed
8190
// responses and prints them using the shared handlers. Returns true if
8291
// printing was performed.
8392
func tryDecodeAndPrint(resourceType string, body []byte) bool {
84-
// VMs are returned from the controller DB as []v1.VirtualMachine
85-
if resourceType == "VirtualMachine" {
86-
var vms []v1.VirtualMachine
87-
if err := json.Unmarshal(body, &vms); err == nil {
88-
handlers.PrintVMsFromDB(vms)
89-
return true
90-
}
91-
}
92-
9393
// Try CloudStack SDK response types for unmanaged resources so we can
9494
// preserve typed slices (e.g., []*cs.Network) when printing.
9595
switch resourceType {
96+
case "VirtualMachine":
97+
var resp cs.ListVirtualMachinesResponse
98+
if err := json.Unmarshal(body, &resp); err == nil {
99+
handlers.PrintCloudStackResource(resourceType, &resp)
100+
return true
101+
}
96102
case "Network":
97103
var resp cs.ListNetworksResponse
98104
if err := json.Unmarshal(body, &resp); err == nil {

controller/controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ func (c *Controller) Start() {
272272
w.Write(b)
273273
return
274274
case "VirtualMachine":
275+
// If client requested all VMs, delegate to handlers which will
276+
// query CloudStack directly. This preserves parity with
277+
// standalone mode when `--all` is used in the CLI.
278+
if r.URL.Query().Get("all") == "true" {
279+
payload := map[string]string{"kind": "VirtualMachine"}
280+
if name != "" {
281+
payload["name"] = name
282+
}
283+
raw, _ := json.Marshal(payload)
284+
obj, err := handlers.GetCloudStackResource(raw)
285+
if err != nil {
286+
http.Error(w, fmt.Sprintf("failed to list VirtualMachine: %v", err), http.StatusInternalServerError)
287+
return
288+
}
289+
b, _ := json.Marshal(obj)
290+
w.Header().Set("Content-Type", "application/json")
291+
w.WriteHeader(http.StatusOK)
292+
w.Write(b)
293+
return
294+
}
295+
275296
var vms []v1.VirtualMachine
276297
if db.DB == nil {
277298
http.Error(w, "database unavailable", http.StatusServiceUnavailable)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module cloudstackctl
33
go 1.26
44

55
require (
6-
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260314202825-5fe04df62a24
6+
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260315154432-66bc95151dde
77
github.com/spf13/cobra v1.10.2
88
gorm.io/driver/postgres v1.6.0
99
gorm.io/gorm v1.31.1

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
22
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
3-
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260314200611-0d1a4d5d553d h1:FszupfrdjfiPihpvlTYIhPqFEUIVwhX03x5C3q3KLJQ=
4-
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260314200611-0d1a4d5d553d/go.mod h1:p/YBUwIEkQN6CQxFhw8Ff0wzf1MY0qRRRuGYNbcb1F8=
5-
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260314202825-5fe04df62a24 h1:20hiJBwNKgbEXt2rBNJhAVurt8cn4xQMzcLU/TN+IWY=
6-
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260314202825-5fe04df62a24/go.mod h1:p/YBUwIEkQN6CQxFhw8Ff0wzf1MY0qRRRuGYNbcb1F8=
3+
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260315154432-66bc95151dde h1:pk4w1j9l674UkU/acmHWrRNilUC2tgzDDt9ScfLIOxE=
4+
github.com/apache/cloudstack-go/v2 v2.19.1-0.20260315154432-66bc95151dde/go.mod h1:p/YBUwIEkQN6CQxFhw8Ff0wzf1MY0qRRRuGYNbcb1F8=
75
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
86
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
97
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)