Go(Gin) + React(Vite + shadcn/ui + Tailwind) 前后端分离,全中文 UI - 集群管理:Kubeconfig / Token 双接入,kubectl + helm 部署 - 主机管理:SSH 密钥 / 密码双认证,CPU/内存/磁盘使用率监控,SSH 错误中文友好提示 - 存储配置、部署管理、系统设置 - Docker 镜像内置 kubectl + helm,开箱即用 - run.sh 一键启停(start/stop/restart/status) Co-Authored-By: Claude <noreply@anthropic.com>
138 lines
3.5 KiB
Go
138 lines
3.5 KiB
Go
package controllers
|
|
|
|
import (
|
|
"lokideploy/models"
|
|
"lokideploy/services"
|
|
"lokideploy/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ClusterController struct {
|
|
service *services.ClusterService
|
|
}
|
|
|
|
func NewClusterController() *ClusterController {
|
|
return &ClusterController{service: services.NewClusterService()}
|
|
}
|
|
|
|
type CreateClusterRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
ConfigType string `json:"configType"` // kubeconfig | token
|
|
APIServer string `json:"apiServer"` // token 模式必填
|
|
KubeConfig string `json:"kubeconfig"` // kubeconfig 模式必填
|
|
Token string `json:"token"` // token 模式必填
|
|
Context string `json:"context"`
|
|
}
|
|
|
|
func (ctrl *ClusterController) Create(c *gin.Context) {
|
|
var req CreateClusterRequest
|
|
if !utils.BindAndValidate(c, &req) {
|
|
return
|
|
}
|
|
if req.ConfigType == "" {
|
|
req.ConfigType = "kubeconfig"
|
|
}
|
|
if req.ConfigType == "kubeconfig" && req.KubeConfig == "" {
|
|
utils.BadRequest(c, "请填写 kubeconfig 内容")
|
|
return
|
|
}
|
|
if req.ConfigType == "token" && (req.APIServer == "" || req.Token == "") {
|
|
utils.BadRequest(c, "Token 接入方式需要填写 API Server 和 Token")
|
|
return
|
|
}
|
|
|
|
cluster, err := ctrl.service.Create(req.Name, req.Description, req.ConfigType, req.APIServer, req.KubeConfig, req.Token, req.Context)
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(cluster))
|
|
}
|
|
|
|
func (ctrl *ClusterController) Get(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
cluster, err := ctrl.service.Get(uint(id))
|
|
if err != nil {
|
|
utils.NotFound(c, "集群不存在")
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(cluster))
|
|
}
|
|
|
|
func (ctrl *ClusterController) List(c *gin.Context) {
|
|
page, pageSize := utils.Pagination(c)
|
|
clusters, total, err := ctrl.service.List(page, pageSize)
|
|
if err != nil {
|
|
utils.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, utils.PageData{
|
|
List: models.SanitizedJSON(clusters), Total: total, Page: page, PageSize: pageSize,
|
|
})
|
|
}
|
|
|
|
func (ctrl *ClusterController) Update(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
var updates map[string]interface{}
|
|
if !utils.BindAndValidate(c, &updates) {
|
|
return
|
|
}
|
|
if err := ctrl.service.Update(uint(id), updates); err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
func (ctrl *ClusterController) Delete(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
if err := ctrl.service.Delete(uint(id)); err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"message": "删除成功"})
|
|
}
|
|
|
|
func (ctrl *ClusterController) Validate(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
go ctrl.service.Validate(uint(id))
|
|
utils.Success(c, gin.H{"message": "已开始验证连接"})
|
|
}
|
|
|
|
func (ctrl *ClusterController) GetInfo(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
cluster, err := ctrl.service.Get(uint(id))
|
|
if err != nil {
|
|
utils.NotFound(c, "集群不存在")
|
|
return
|
|
}
|
|
info, err := services.GetClusterInfo(c.Request.Context(), cluster)
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, info)
|
|
}
|