Add the Loki query UI, access policies, ingress insights, alert and registry settings, and their backend APIs. Include the related Kubernetes deployment and collector updates required by the integrated application. Validated with go test ./..., npm run build, and npm run lint.
173 lines
4.2 KiB
Go
173 lines
4.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"opsxloki/dto"
|
|
"opsxloki/models"
|
|
"opsxloki/services"
|
|
"opsxloki/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)
|
|
}
|