Skip to content

Commit 8698c77

Browse files
committed
refactor: enhance health check logic to enforce deployment requirement when system probe is not specified
1 parent eec52ae commit 8698c77

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

pkg/debugcmd/health.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,24 @@ func ParseHealthRequest(payload []byte) (HealthRequest, error) {
4141
}
4242

4343
func Health(ctx context.Context, deps Deps, req HealthRequest) (HealthResponse, error) {
44-
twinID, contractID, err := ParseDeploymentID(req.Deployment)
45-
if err != nil {
46-
return HealthResponse{}, err
44+
var twinID uint32
45+
var contractID uint64
46+
var err error
47+
48+
hasSystemProbe := req.Options != nil && req.Options["system_probe"] != nil
49+
50+
if req.Deployment != "" {
51+
twinID, contractID, err = ParseDeploymentID(req.Deployment)
52+
if err != nil {
53+
return HealthResponse{}, err
54+
}
55+
} else if !hasSystemProbe {
56+
return HealthResponse{}, fmt.Errorf("deployment is required when system_probe is not specified")
4757
}
4858

4959
out := HealthResponse{TwinID: twinID, ContractID: contractID}
5060

51-
if req.Options != nil {
61+
if hasSystemProbe {
5262
if probeCmd, ok := req.Options["system_probe"].(string); ok && probeCmd != "" {
5363
checkData := &checks.CheckData{Twin: twinID, Contract: contractID}
5464
allChecks := checks.NewSystemChecker(probeCmd).Run(ctx, checkData)
@@ -58,33 +68,35 @@ func Health(ctx context.Context, deps Deps, req HealthRequest) (HealthResponse,
5868
}
5969
}
6070

61-
deployment, err := deps.Provision.Get(ctx, twinID, contractID)
62-
if err != nil {
63-
return HealthResponse{}, fmt.Errorf("failed to get deployment: %w", err)
64-
}
65-
66-
for _, wl := range deployment.Workloads {
67-
workloadID, err := gridtypes.NewWorkloadID(twinID, contractID, wl.Name)
71+
if req.Deployment != "" {
72+
deployment, err := deps.Provision.Get(ctx, twinID, contractID)
6873
if err != nil {
69-
continue
74+
return HealthResponse{}, fmt.Errorf("failed to get deployment: %w", err)
7075
}
7176

72-
checkData := &checks.CheckData{
73-
Network: deps.Network.Namespace,
74-
VM: deps.VM.Exists,
75-
Twin: twinID,
76-
Contract: contractID,
77-
Workload: wl,
78-
}
77+
for _, wl := range deployment.Workloads {
78+
workloadID, err := gridtypes.NewWorkloadID(twinID, contractID, wl.Name)
79+
if err != nil {
80+
continue
81+
}
82+
83+
checkData := &checks.CheckData{
84+
Network: deps.Network.Namespace,
85+
VM: deps.VM.Exists,
86+
Twin: twinID,
87+
Contract: contractID,
88+
Workload: wl,
89+
}
7990

80-
allChecks := checks.Run(ctx, wl.Type, checkData)
81-
if len(allChecks) > 0 {
82-
out.Workloads = append(out.Workloads, newWorkloadHealth(
83-
workloadID.String(),
84-
string(wl.Type),
85-
string(wl.Name),
86-
allChecks,
87-
))
91+
allChecks := checks.Run(ctx, wl.Type, checkData)
92+
if len(allChecks) > 0 {
93+
out.Workloads = append(out.Workloads, newWorkloadHealth(
94+
workloadID.String(),
95+
string(wl.Type),
96+
string(wl.Name),
97+
allChecks,
98+
))
99+
}
88100
}
89101
}
90102

0 commit comments

Comments
 (0)