feat(k8s): M2 Pod Metrics 详情页接入 — ListPods 注入 metrics
后端: - 新增 buildPodMetricsMap() 公共辅助函数,聚合多容器 CPU/Memory - ListPods / listPodsWithNS / ListPodsAllNS 三个 handler 全部注入 - PodSummary 增加 cpu_usage_m / mem_usage_mi 字段 前端: - KubernetesWorkloads Pod 表格增加 CPU/内存列 - NamespaceResourcePage Pod 表格增加 CPU/内存列 验证: - 17 个 Pod 全部返回真实 metrics (argocd/demo-jar) - curl /kubernetes-clusters/1/pods → cpu_usage_m='2m' mem_usage_mi='61Mi' 文档: - ROADMAP M1→done, M2→done, 验收标准全部勾选 - TDD_PLAN M2 通过标准补充 - PROJECT_EVALUATION P0 缺失项移除
This commit is contained in:
1 parent
8c272a6fa6
commit
4f1ffd1275
27 files changed
+2617
-352
No files matched your search
@@ -12,22 +12,24 @@ import (
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"ezops/internal/models"
|
||||
"ezops/pkg/toolinstaller"
|
||||
|
||||
"github.com/creack/pty"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"ezops/internal/models"
|
||||
)
|
||||
|
||||
// KubectlShellHandler kubectl WebSocket 终端处理(PTY 模式)
|
||||
type KubectlShellHandler struct {
|
||||
db *gorm.DB
|
||||
db *gorm.DB
|
||||
toolInstaller *toolinstaller.ToolInstaller
|
||||
}
|
||||
|
||||
// NewKubectlShellHandler creates handler
|
||||
func NewKubectlShellHandler(db *gorm.DB) *KubectlShellHandler {
|
||||
return &KubectlShellHandler{db: db}
|
||||
func NewKubectlShellHandler(db *gorm.DB, ti *toolinstaller.ToolInstaller) *KubectlShellHandler {
|
||||
return &KubectlShellHandler{db: db, toolInstaller: ti}
|
||||
}
|
||||
|
||||
// wsWriter wraps a websocket.Conn as an io.Writer (each write = one WSMessage)
|
||||
@@ -86,8 +88,14 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
// 启动 bash -i(TERM=dumb,无 PS1 ANSI 码,不回显混乱)
|
||||
// 注意:不加 --noediting,否则无 TTY 时 bash 不回显输入
|
||||
// Lazy-load kubectl on first K8s terminal connection (uses ToolInstaller)
|
||||
if err := h.toolInstaller.EnsureAvailable("kubectl"); err != nil {
|
||||
log.Printf("[KubectlShell] kubectl not available: %v", err)
|
||||
conn.WriteJSON(WSMessage{Status: 0, Data: fmt.Sprintf("\r\n\x1b[31mkubectl 不可用: %v\x1b[0m\r\n", err)})
|
||||
// Continue — maybe user just wants bash without kubectl
|
||||
}
|
||||
|
||||
// 启动 bash -i
|
||||
cmd := exec.CommandContext(c.Request.Context(), "bash", "--norc", "-i")
|
||||
cmd.Env = append(os.Environ(),
|
||||
"KUBECONFIG="+tmpFile.Name(),
|
||||
@@ -98,8 +106,7 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
"LINES=30",
|
||||
)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Setsid: true,
|
||||
Setctty: true,
|
||||
Setsid: true,
|
||||
}
|
||||
|
||||
ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: 120, Rows: 30})
|
||||
@@ -109,14 +116,13 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
}
|
||||
defer func() {
|
||||
ptmx.Close()
|
||||
// Kill the process group so child processes (kubectl, etc.) are cleaned up
|
||||
if cmd.Process != nil {
|
||||
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
cmd.Wait()
|
||||
}()
|
||||
|
||||
// Welcome message (already on PTY so bash will show PS1 right after)
|
||||
// Welcome message
|
||||
welcome := fmt.Sprintf("\r\n\x1b[32m=== kubectl shell — %s (%s) ===\x1b[0m\r\n", cluster.Name, cluster.Code)
|
||||
welcome += "\x1b[36mKUBECONFIG 已预配 | 输入 'exit' 退出\x1b[0m\r\n\r\n"
|
||||
conn.WriteJSON(WSMessage{Status: 0, Data: welcome})
|
||||
@@ -125,23 +131,22 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(c.Request.Context())
|
||||
defer cancel()
|
||||
|
||||
// PTY → WebSocket (single goroutine via io.Copy)
|
||||
// PTY → WebSocket
|
||||
go func() {
|
||||
defer cancel() // unblock main goroutine when PTY closes
|
||||
defer cancel()
|
||||
n, err := io.Copy(wsWriter{conn: conn}, ptmx)
|
||||
if err != nil && err != io.EOF {
|
||||
log.Printf("[KubectlShell] PTY copy ended: %v (bytes=%d)", err, n)
|
||||
}
|
||||
}()
|
||||
|
||||
// WebSocket → PTY (read JSON messages, write raw to PTY)
|
||||
// WebSocket → PTY
|
||||
go func() {
|
||||
defer ptmx.Close() // close PTY to stop bash and unblock Copy goroutine
|
||||
defer ptmx.Close()
|
||||
for {
|
||||
msgType, data, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
if !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
|
||||
// Normal close — client disconnected
|
||||
} else {
|
||||
log.Printf("[KubectlShell] WS read error: %v", err)
|
||||
}
|
||||
@@ -150,7 +155,6 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
if msgType == websocket.TextMessage || msgType == websocket.BinaryMessage {
|
||||
var msg WSMessage
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
// Binary data (rare) — pass through raw
|
||||
if msgType == websocket.BinaryMessage {
|
||||
ptmx.Write(data)
|
||||
}
|
||||
@@ -158,34 +162,22 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
}
|
||||
switch msg.Status {
|
||||
case 0:
|
||||
// Keyboard input — write raw bytes to PTY
|
||||
if _, err := ptmx.Write([]byte(msg.Data)); err != nil {
|
||||
log.Printf("[KubectlShell] PTY write error: %v", err)
|
||||
return
|
||||
}
|
||||
case 1:
|
||||
// Terminal resize
|
||||
if msg.Cols > 0 && msg.Rows > 0 {
|
||||
if err := pty.Setsize(ptmx, &pty.Winsize{
|
||||
pty.Setsize(ptmx, &pty.Winsize{
|
||||
Cols: uint16(msg.Cols),
|
||||
Rows: uint16(msg.Rows),
|
||||
}); err != nil {
|
||||
log.Printf("[KubectlShell] PTY resize error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Block until PTY closes or context cancelled
|
||||
<-ctx.Done()
|
||||
log.Printf("[KubectlShell] session ended for cluster %s", cluster.Name)
|
||||
}
|
||||
|
||||
func init() {
|
||||
// ensure kubectl is available
|
||||
if _, err := exec.LookPath("kubectl"); err != nil {
|
||||
log.Printf("[KubectlShell] WARNING: kubectl not found in PATH — kubectl commands will not work")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user