feat: add pod logs and delete operations
- Add pod logs button with SSE streaming (WIP: SSE connection issues with HTTP/2) - Add pod delete button with confirmation dialog - Use existing resp.SSE package for log streaming - Force HTTP/1.1 for k8s client to avoid stream closing issues - Update frontend to handle pod actions and dialogs 🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
@@ -31,6 +31,8 @@ import SettingsIcon from '@mui/icons-material/Settings'
|
|||||||
import CloseIcon from '@mui/icons-material/Close'
|
import CloseIcon from '@mui/icons-material/Close'
|
||||||
import AddIcon from '@mui/icons-material/Add'
|
import AddIcon from '@mui/icons-material/Add'
|
||||||
import UploadFileIcon from '@mui/icons-material/UploadFile'
|
import UploadFileIcon from '@mui/icons-material/UploadFile'
|
||||||
|
import VisibilityIcon from '@mui/icons-material/Visibility'
|
||||||
|
import DeleteIcon from '@mui/icons-material/Delete'
|
||||||
|
|
||||||
const KINDS = [
|
const KINDS = [
|
||||||
{ key: 'namespace', label: 'Namespace', endpoint: '/api/v1/k8s/namespace/list' },
|
{ key: 'namespace', label: 'Namespace', endpoint: '/api/v1/k8s/namespace/list' },
|
||||||
@@ -63,6 +65,13 @@ export default function K8sResourceList() {
|
|||||||
severity: 'success',
|
severity: 'success',
|
||||||
})
|
})
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||||
|
const [logsDialogOpen, setLogsDialogOpen] = useState(false)
|
||||||
|
const [logs, setLogs] = useState<string[]>([])
|
||||||
|
const [selectedPod, setSelectedPod] = useState<{ name: string; namespace: string } | null>(null)
|
||||||
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
|
||||||
|
const [deleteTarget, setDeleteTarget] = useState<{ name: string; namespace: string } | null>(null)
|
||||||
|
const [deleting, setDeleting] = useState(false)
|
||||||
|
const logsEndRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchKubeconfig()
|
fetchKubeconfig()
|
||||||
@@ -146,6 +155,60 @@ export default function K8sResourceList() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleViewLogs = (podName: string, podNamespace: string) => {
|
||||||
|
setSelectedPod({ name: podName, namespace: podNamespace })
|
||||||
|
setLogs([])
|
||||||
|
setLogsDialogOpen(true)
|
||||||
|
|
||||||
|
const eventSource = new EventSource(
|
||||||
|
`/api/v1/k8s/pod/logs?name=${encodeURIComponent(podName)}&namespace=${encodeURIComponent(podNamespace)}&tail=1000&follow=true`
|
||||||
|
)
|
||||||
|
|
||||||
|
eventSource.onmessage = (event) => {
|
||||||
|
setLogs((prev) => [...prev, event.data])
|
||||||
|
setTimeout(() => logsEndRef.current?.scrollIntoView({ behavior: 'smooth' }), 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
eventSource.onerror = () => {
|
||||||
|
eventSource.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => eventSource.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDeletePod = async () => {
|
||||||
|
if (!deleteTarget) return
|
||||||
|
|
||||||
|
setDeleting(true)
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/k8s/pod/delete', {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ name: deleteTarget.name, namespace: deleteTarget.namespace }),
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await res.json()
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(result.err || 'Failed to delete pod')
|
||||||
|
}
|
||||||
|
|
||||||
|
setSnackbar({ open: true, message: 'Pod 删除成功', severity: 'success' })
|
||||||
|
setDeleteDialogOpen(false)
|
||||||
|
setDeleteTarget(null)
|
||||||
|
fetchResources()
|
||||||
|
} catch (e: any) {
|
||||||
|
setSnackbar({ open: true, message: `删除失败: ${e.message}`, severity: 'error' })
|
||||||
|
} finally {
|
||||||
|
setDeleting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const openDeleteDialog = (podName: string, podNamespace: string) => {
|
||||||
|
setDeleteTarget({ name: podName, namespace: podNamespace })
|
||||||
|
setDeleteDialogOpen(true)
|
||||||
|
}
|
||||||
|
|
||||||
const fetchResources = async () => {
|
const fetchResources = async () => {
|
||||||
if (!kubeconfig) {
|
if (!kubeconfig) {
|
||||||
setKubeconfigError(true)
|
setKubeconfigError(true)
|
||||||
@@ -183,7 +246,7 @@ export default function K8sResourceList() {
|
|||||||
case 'configmap':
|
case 'configmap':
|
||||||
return ['Name', 'Namespace', 'Data Keys', 'Age']
|
return ['Name', 'Namespace', 'Data Keys', 'Age']
|
||||||
case 'pod':
|
case 'pod':
|
||||||
return ['Name', 'Namespace', 'Status', 'IP', 'Node', 'Age']
|
return ['Name', 'Namespace', 'Status', 'IP', 'Node', 'Age', 'Actions']
|
||||||
case 'pv':
|
case 'pv':
|
||||||
return ['Name', 'Capacity', 'Access Modes', 'Status', 'Claim', 'Age']
|
return ['Name', 'Capacity', 'Access Modes', 'Status', 'Claim', 'Age']
|
||||||
case 'pvc':
|
case 'pvc':
|
||||||
@@ -256,6 +319,23 @@ export default function K8sResourceList() {
|
|||||||
<TableCell>{status.podIP || '-'}</TableCell>
|
<TableCell>{status.podIP || '-'}</TableCell>
|
||||||
<TableCell>{spec.nodeName || '-'}</TableCell>
|
<TableCell>{spec.nodeName || '-'}</TableCell>
|
||||||
<TableCell>{getAge(metadata.creationTimestamp)}</TableCell>
|
<TableCell>{getAge(metadata.creationTimestamp)}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() => handleViewLogs(metadata.name, metadata.namespace)}
|
||||||
|
title="查看日志"
|
||||||
|
>
|
||||||
|
<VisibilityIcon fontSize="small" />
|
||||||
|
</IconButton>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="error"
|
||||||
|
onClick={() => openDeleteDialog(metadata.name, metadata.namespace)}
|
||||||
|
title="删除"
|
||||||
|
>
|
||||||
|
<DeleteIcon fontSize="small" />
|
||||||
|
</IconButton>
|
||||||
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)
|
)
|
||||||
case 'pv':
|
case 'pv':
|
||||||
@@ -469,6 +549,65 @@ export default function K8sResourceList() {
|
|||||||
</DialogActions>
|
</DialogActions>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|
||||||
|
<Dialog
|
||||||
|
open={logsDialogOpen}
|
||||||
|
onClose={() => setLogsDialogOpen(false)}
|
||||||
|
maxWidth="lg"
|
||||||
|
fullWidth
|
||||||
|
>
|
||||||
|
<DialogTitle>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||||
|
<Typography variant="h6">
|
||||||
|
Pod 日志: {selectedPod?.name} ({selectedPod?.namespace})
|
||||||
|
</Typography>
|
||||||
|
<IconButton onClick={() => setLogsDialogOpen(false)}>
|
||||||
|
<CloseIcon />
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
|
</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<Paper
|
||||||
|
sx={{
|
||||||
|
p: 2,
|
||||||
|
bgcolor: '#1e1e1e',
|
||||||
|
color: '#d4d4d4',
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
fontSize: '0.875rem',
|
||||||
|
maxHeight: '60vh',
|
||||||
|
overflow: 'auto',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{logs.length === 0 && <Typography>等待日志...</Typography>}
|
||||||
|
{logs.map((log, index) => (
|
||||||
|
<Box key={index} sx={{ whiteSpace: 'pre-wrap', wordBreak: 'break-all' }}>
|
||||||
|
{log}
|
||||||
|
</Box>
|
||||||
|
))}
|
||||||
|
<div ref={logsEndRef} />
|
||||||
|
</Paper>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
<Dialog open={deleteDialogOpen} onClose={() => setDeleteDialogOpen(false)}>
|
||||||
|
<DialogTitle>确认删除</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<Typography>
|
||||||
|
确定要删除 Pod <strong>{deleteTarget?.name}</strong> (namespace: {deleteTarget?.namespace}) 吗?
|
||||||
|
</Typography>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={() => setDeleteDialogOpen(false)}>取消</Button>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="error"
|
||||||
|
onClick={handleDeletePod}
|
||||||
|
disabled={deleting}
|
||||||
|
>
|
||||||
|
{deleting ? <CircularProgress size={24} /> : '删除'}
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
<Snackbar
|
<Snackbar
|
||||||
open={snackbar.open}
|
open={snackbar.open}
|
||||||
autoHideDuration={6000}
|
autoHideDuration={6000}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ export default defineConfig({
|
|||||||
'/api': {
|
'/api': {
|
||||||
target: 'http://127.0.0.1:9119',
|
target: 'http://127.0.0.1:9119',
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
|
ws: true,
|
||||||
|
timeout: 0,
|
||||||
// Removed rewrite so /api prefix is preserved for backend route /api/v1/...
|
// Removed rewrite so /api prefix is preserved for backend route /api/v1/...
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ func Init(ctx context.Context, address string, db *gorm.DB, store store.Store) (
|
|||||||
k8sAPI.Get("/statefulset/list", k8s.K8sStatefulSetList(ctx, db, store))
|
k8sAPI.Get("/statefulset/list", k8s.K8sStatefulSetList(ctx, db, store))
|
||||||
k8sAPI.Get("/configmap/list", k8s.K8sConfigMapList(ctx, db, store))
|
k8sAPI.Get("/configmap/list", k8s.K8sConfigMapList(ctx, db, store))
|
||||||
k8sAPI.Get("/pod/list", k8s.K8sPodList(ctx, db, store))
|
k8sAPI.Get("/pod/list", k8s.K8sPodList(ctx, db, store))
|
||||||
|
k8sAPI.Get("/pod/logs", k8s.K8sPodLogs(ctx, db, store))
|
||||||
|
k8sAPI.Delete("/pod/delete", k8s.K8sPodDelete(ctx, db, store))
|
||||||
k8sAPI.Get("/pv/list", k8s.K8sPVList(ctx, db, store))
|
k8sAPI.Get("/pv/list", k8s.K8sPVList(ctx, db, store))
|
||||||
k8sAPI.Get("/pvc/list", k8s.K8sPVCList(ctx, db, store))
|
k8sAPI.Get("/pvc/list", k8s.K8sPVCList(ctx, db, store))
|
||||||
k8sAPI.Get("/service/list", k8s.K8sServiceList(ctx, db, store))
|
k8sAPI.Get("/service/list", k8s.K8sServiceList(ctx, db, store))
|
||||||
|
|||||||
@@ -1,19 +1,23 @@
|
|||||||
package k8s
|
package k8s
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"gitea.loveuer.com/loveuer/cluster/internal/model"
|
"gitea.loveuer.com/loveuer/cluster/internal/model"
|
||||||
"gitea.loveuer.com/loveuer/cluster/pkg/resp"
|
"gitea.loveuer.com/loveuer/cluster/pkg/resp"
|
||||||
"gitea.loveuer.com/loveuer/cluster/pkg/store"
|
"gitea.loveuer.com/loveuer/cluster/pkg/store"
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -35,6 +39,8 @@ func getK8sClient(db *gorm.DB) (*kubernetes.Clientset, error) {
|
|||||||
return nil, fmt.Errorf("failed to parse kubeconfig: %w", err)
|
return nil, fmt.Errorf("failed to parse kubeconfig: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientConfig.Timeout = 0
|
||||||
|
|
||||||
clientset, err := kubernetes.NewForConfig(clientConfig)
|
clientset, err := kubernetes.NewForConfig(clientConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create k8s client: %w", err)
|
return nil, fmt.Errorf("failed to create k8s client: %w", err)
|
||||||
@@ -43,6 +49,29 @@ func getK8sClient(db *gorm.DB) (*kubernetes.Clientset, error) {
|
|||||||
return clientset, nil
|
return clientset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getK8sConfig(db *gorm.DB) (*rest.Config, error) {
|
||||||
|
var config model.ClusterConfig
|
||||||
|
|
||||||
|
if err := db.Where("key = ?", "kubeconfig").First(&config).Error; err != nil {
|
||||||
|
return nil, fmt.Errorf("kubeconfig not found: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Value == "" {
|
||||||
|
return nil, fmt.Errorf("kubeconfig is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
clientConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(config.Value))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse kubeconfig: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable HTTP/2 to avoid stream closing issues
|
||||||
|
clientConfig.TLSClientConfig.NextProtos = []string{"http/1.1"}
|
||||||
|
clientConfig.Timeout = 0
|
||||||
|
|
||||||
|
return clientConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
func K8sNamespaceList(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler {
|
func K8sNamespaceList(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler {
|
||||||
return func(c fiber.Ctx) error {
|
return func(c fiber.Ctx) error {
|
||||||
clientset, err := getK8sClient(db)
|
clientset, err := getK8sClient(db)
|
||||||
@@ -299,3 +328,105 @@ func getResourceName(kind string) string {
|
|||||||
|
|
||||||
return kind + "s"
|
return kind + "s"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func K8sPodLogs(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler {
|
||||||
|
return func(c fiber.Ctx) error {
|
||||||
|
podName := c.Query("name", "")
|
||||||
|
namespace := c.Query("namespace", "")
|
||||||
|
tailLines := int64(1000)
|
||||||
|
follow := c.Query("follow", "") == "true"
|
||||||
|
|
||||||
|
if podName == "" || namespace == "" {
|
||||||
|
return resp.R400(c, "", nil, fmt.Errorf("name and namespace are required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
restConfig, err := getK8sConfig(db)
|
||||||
|
if err != nil {
|
||||||
|
return resp.R500(c, "", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
clientset, err := kubernetes.NewForConfig(restConfig)
|
||||||
|
if err != nil {
|
||||||
|
return resp.R500(c, "", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
podLogOpts := &corev1.PodLogOptions{
|
||||||
|
TailLines: &tailLines,
|
||||||
|
Follow: follow,
|
||||||
|
}
|
||||||
|
|
||||||
|
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, podLogOpts)
|
||||||
|
|
||||||
|
logCtx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
stream, err := req.Stream(logCtx)
|
||||||
|
if err != nil {
|
||||||
|
return resp.R500(c, "", nil, fmt.Errorf("failed to get pod logs: %w", err))
|
||||||
|
}
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
|
// Use the existing SSE manager from resp package
|
||||||
|
manager := resp.SSE(c, "pod-logs")
|
||||||
|
|
||||||
|
// Start streaming logs in a goroutine
|
||||||
|
go func() {
|
||||||
|
defer manager.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(stream)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-logCtx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
manager.Send("[EOF]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
manager.Send(fmt.Sprintf("error: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
manager.Send(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Return nil since we're handling the response directly
|
||||||
|
c.Context().SetBodyStreamWriter(manager.Writer())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func K8sPodDelete(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler {
|
||||||
|
return func(c fiber.Ctx) error {
|
||||||
|
var req struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Namespace string `json:"namespace"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(c.Body(), &req); err != nil {
|
||||||
|
return resp.R400(c, "", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Name == "" || req.Namespace == "" {
|
||||||
|
return resp.R400(c, "", nil, fmt.Errorf("name and namespace are required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
clientset, err := getK8sClient(db)
|
||||||
|
if err != nil {
|
||||||
|
return resp.R500(c, "", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = clientset.CoreV1().Pods(req.Namespace).Delete(c.Context(), req.Name, metav1.DeleteOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return resp.R500(c, "", nil, fmt.Errorf("failed to delete pod: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.R200(c, map[string]any{
|
||||||
|
"name": req.Name,
|
||||||
|
"namespace": req.Namespace,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user