- 品牌: Go 模块/数据文件/二进制/构建部署资产全部 opsx→lokistack;前端 localStorage 命名空间迁移(读旧写新,含按用户 id 前缀迁移);修复 nginx upstream 与 Makefile 引用已改名服务的断裂 - i18n: react-i18next 落地,common + 11 个页面域命名空间全站中英(菜单/顶栏/登录/列表页/表单/确认弹窗/状态徽章);设置页语言即时切换;修复词典自注册导致的循环导入白屏 - 用户管理: 批量删除(POST /users/batch-delete,不删自己/最后管理员,部分失败不阻断)+ 行复选/全选/批量工具条 - 主题: 浅色侧栏(中性灰激活态)、四态模式(浅色/深色/跟随系统/定时切换,按时区判定)、顶栏太阳月亮二态、设置页紧凑分段选择器 - UI: 登录记住用户名、CardActionButton 统一五页卡片操作钮、shadcn Badge 替换手写徽章、语义色修正 - 清理: 移除主机 Web 终端(前后端)、k8s-helm/Helm 遗留代码与字段、README 重写精简 - 修复: 侧栏与日志告警 order 重复、路由标题 i18n key 未翻译、词典双逗号等
173 lines
4.2 KiB
Go
173 lines
4.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"lokistack/dto"
|
|
"lokistack/models"
|
|
"lokistack/services"
|
|
"lokistack/utils"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (ctrl *ClusterController) Namespaces(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
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 12*time.Second)
|
|
defer cancel()
|
|
items, err := services.ListClusterNamespaces(ctx, cluster)
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"namespaces": items})
|
|
}
|
|
|
|
func (ctrl *ClusterController) Deployments(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
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 12*time.Second)
|
|
defer cancel()
|
|
items, err := services.ListClusterDeployments(ctx, cluster, c.Param("namespace"))
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"deployments": items})
|
|
}
|
|
|
|
type ClusterController struct {
|
|
service *services.ClusterService
|
|
}
|
|
|
|
func NewClusterController() *ClusterController {
|
|
return &ClusterController{service: services.NewClusterService()}
|
|
}
|
|
|
|
func (ctrl *ClusterController) Create(c *gin.Context) {
|
|
var req dto.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)
|
|
}
|