# K8S 指令结构定义

## K8sExecCommand - 执行指令

```go
type K8sExecCommand struct {
    CommandID  string   `json:"command_id"`           // 唯一指令ID
    Namespace  string   `json:"namespace"`            // 目标namespace
    Resource   string   `json:"resource"`             // 资源类型: deployment, pod, statefulset...
    Name       string   `json:"name"`                 // 资源名称
    Action     string   `json:"action"`               // 操作: logs, exec, scale, restart, delete, get, apply
    Container  string   `json:"container,omitempty"`  // 容器名(logs/exec用)
    Command    []string `json:"command,omitempty"`    // 命令(exec用)
    Timeout    int      `json:"timeout"`              // 超时秒数
    TailLines  int      `json:"tail_lines,omitempty"` // 日志行数(logs用)
    FollowLogs bool     `json:"follow_logs,omitempty"`// 实时日志(logs用)
    Scale      int      `json:"scale,omitempty"`      // 副本数(scale用)
    YamlContent string  `json:"yaml_content,omitempty"`// YAML内容(apply用)
}
```

## ExecResult - 执行结果

```go
type ExecResult struct {
    CommandID string `json:"command_id"` // 对应指令ID
    Status    string `json:"status"`     // success, failure, timeout
    ExitCode  int    `json:"exit_code"`  // 退出码
    Output    string `json:"output"`     // 标准输出
    Error     string `json:"error"`      // 错误信息
    StartTime int64  `json:"start_time"` // 开始时间戳(ms)
    EndTime   int64  `json:"end_time"`   // 结束时间戳(ms)
    Duration  int64  `json:"duration"`   // 执行时长(ms)
}
```

## MQTT消息封装

### 下发指令 (Exchange-Hub → Watchdog)
```json
{
    "message_id": "uuid",
    "type": "command",
    "project_id": "project_12345678",
    "timestamp": 1700000000000,
    "data_type": "k8s_exec",
    "payload": {
        "command_id": "cmd_uuid",
        "namespace": "my-namespace",
        "resource": "pod",
        "name": "my-pod",
        "action": "logs",
        "tail_lines": 100
    }
}
```

### 上报结果 (Watchdog → Exchange-Hub)
```json
{
    "message_id": "uuid",
    "type": "message",
    "project_id": "project_12345678",
    "timestamp": 1700000001000,
    "data_type": "exec_result",
    "payload": {
        "command_id": "cmd_uuid",
        "status": "success",
        "exit_code": 0,
        "output": "log content...",
        "duration": 1500
    }
}
```
