Files
rdp-proxy/agents/rap-node-agent/internal/supervisor/supervisor.go
T
2026-05-12 21:02:29 +03:00

165 lines
5.9 KiB
Go

package supervisor
import (
"context"
"strings"
"time"
"github.com/example/remote-access-platform/agents/rap-node-agent/internal/client"
)
type Supervisor interface {
Apply(ctx context.Context, desired []client.DesiredWorkload) ([]client.WorkloadStatusRequest, error)
}
type StubSupervisor struct {
Version string
}
func (s StubSupervisor) Apply(_ context.Context, desired []client.DesiredWorkload) ([]client.WorkloadStatusRequest, error) {
statuses := make([]client.WorkloadStatusRequest, 0, len(desired))
for _, workload := range desired {
statuses = append(statuses, s.applyOne(workload))
}
return statuses, nil
}
func (s StubSupervisor) applyOne(workload client.DesiredWorkload) client.WorkloadStatusRequest {
serviceType := strings.TrimSpace(workload.ServiceType)
desiredState := strings.TrimSpace(strings.ToLower(workload.DesiredState))
if desiredState == "" {
desiredState = "disabled"
}
runtimeMode := strings.TrimSpace(strings.ToLower(workload.RuntimeMode))
if runtimeMode == "" {
runtimeMode = "native"
}
version := strings.TrimSpace(workload.Version)
if version == "" {
version = s.Version
}
payload := map[string]any{
"schema_version": "rap.node_agent.workload_supervision.v1",
"supervisor": "node-agent-local",
"desired_state": desiredState,
"service_type": serviceType,
"runtime_mode": runtimeMode,
"observed_at": time.Now().UTC().Format(time.RFC3339Nano),
}
if desiredState != "enabled" {
payload["reason"] = "desired_state_not_enabled"
return client.WorkloadStatusRequest{
ReportedState: "stopped",
RuntimeMode: runtimeMode,
Version: version,
StatusPayload: payload,
}
}
if serviceType == "core-mesh" || serviceType == "mesh-listener" {
payload["reason"] = "builtin_node_agent_service_ready"
payload["execution_mode"] = "builtin"
payload["traffic"] = serviceTrafficMode(serviceType)
return client.WorkloadStatusRequest{
ReportedState: "running",
RuntimeMode: runtimeMode,
Version: version,
StatusPayload: payload,
}
}
if serviceType == "synthetic.echo" && runtimeMode == "native" {
payload["reason"] = "internal_synthetic_echo_ready"
payload["execution_mode"] = "builtin"
payload["traffic"] = "test_service_only"
return client.WorkloadStatusRequest{
ReportedState: "running",
RuntimeMode: runtimeMode,
Version: version,
StatusPayload: payload,
}
}
if serviceType == "rdp-worker" && runtimeMode == "native" && boolConfig(workload.Config, "adapter_contract_probe") {
payload["reason"] = "remote_workspace_adapter_contract_probe_ready"
payload["execution_mode"] = "contract_probe"
payload["service_class"] = "remote_workspace"
payload["fabric_service_channel_required"] = true
payload["backend_relay_steady_state"] = false
payload["channels"] = remoteWorkspaceAdapterChannels()
payload["frame_batch_contract"] = remoteWorkspaceFrameBatchContract()
payload["traffic"] = "none"
return client.WorkloadStatusRequest{
ReportedState: "running",
RuntimeMode: runtimeMode,
Version: version,
StatusPayload: payload,
}
}
payload["reason"] = "service_runtime_not_implemented"
payload["traffic"] = "blocked"
return client.WorkloadStatusRequest{
ReportedState: "degraded",
RuntimeMode: runtimeMode,
Version: version,
StatusPayload: payload,
}
}
func boolConfig(values map[string]any, key string) bool {
if values == nil {
return false
}
value, ok := values[key]
if !ok {
return false
}
switch typed := value.(type) {
case bool:
return typed
case string:
return strings.EqualFold(strings.TrimSpace(typed), "true")
default:
return false
}
}
func remoteWorkspaceAdapterChannels() []map[string]any {
return []map[string]any{
{"name": "input", "direction": "client_to_adapter", "reliability": "reliable_ordered", "priority": "critical", "droppable": true, "may_block_input": false},
{"name": "control", "direction": "bidirectional", "reliability": "reliable_ordered", "priority": "high", "droppable": false, "may_block_input": false},
{"name": "display", "direction": "adapter_to_client", "reliability": "droppable_latest", "priority": "high", "droppable": true, "may_block_input": false},
{"name": "cursor", "direction": "adapter_to_client", "reliability": "droppable_latest", "priority": "high", "droppable": true, "may_block_input": false},
{"name": "clipboard", "direction": "bidirectional", "reliability": "reliable_ordered", "priority": "medium", "droppable": false, "may_block_input": false},
{"name": "file_transfer", "direction": "bidirectional", "reliability": "reliable_chunked", "priority": "medium", "droppable": false, "may_block_input": false},
{"name": "audio", "direction": "adapter_to_client", "reliability": "adaptive_droppable", "priority": "medium", "droppable": true, "may_block_input": false},
{"name": "device", "direction": "bidirectional", "reliability": "reliable_ordered", "priority": "medium", "droppable": false, "may_block_input": false},
{"name": "telemetry", "direction": "adapter_to_client", "reliability": "sampled_droppable", "priority": "low", "droppable": true, "may_block_input": false},
}
}
func remoteWorkspaceFrameBatchContract() map[string]any {
return map[string]any{
"schema_version": "rap.remote_workspace_frame_batch.v1",
"adapter_contract_id": "rap.rdp_worker.remote_workspace_adapter_contract_probe.v1",
"probe_only": true,
"payload_forwarding": "not_implemented",
"service_class": "remote_workspace",
"allowed_flow_classes": []string{"control", "interactive", "reliable", "bulk", "droppable"},
"allowed_payload_encodings": []string{
"none",
"base64",
},
"max_probe_frames": 32,
"channels": remoteWorkspaceAdapterChannels(),
}
}
func serviceTrafficMode(serviceType string) string {
switch serviceType {
case "core-mesh":
return "fabric_control"
case "mesh-listener":
return "entry_listener"
default:
return "unknown"
}
}