三种采集器(host-promtail / k8s-daemonset / k8s-fluentbit-sidecar),每种关联已部署 Loki: - host-promtail:SSH 安装 Promtail(复用 deployBinary) - k8s-daemonset:helm 安装 Promtail DaemonSet(复用 deployHelm) - k8s-fluentbit-sidecar:kubectl patch 注入 Fluent Bit sidecar 含 Collector 数据模型、三个部署器、配置模板、CRUD API、前端页面。 附带 UI 优化:ErrorBoundary、部署卡片浏览器访问按钮、菜单名称调整。 Co-Authored-By: Claude <noreply@anthropic.com>
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"lokideploy/models"
|
|
"lokideploy/services"
|
|
"lokideploy/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CollectorController struct {
|
|
service *services.CollectorService
|
|
}
|
|
|
|
func NewCollectorController() *CollectorController {
|
|
return &CollectorController{service: services.NewCollectorService()}
|
|
}
|
|
|
|
type CreateCollectorRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Type string `json:"type" binding:"required"`
|
|
LokiDeploymentID uint `json:"lokiDeploymentID" binding:"required"`
|
|
HostID *uint `json:"hostID"`
|
|
ClusterID *uint `json:"clusterID"`
|
|
TargetDeployment string `json:"targetDeployment"`
|
|
Namespace string `json:"namespace"`
|
|
LogPath string `json:"logPath"`
|
|
Labels string `json:"labels"`
|
|
}
|
|
|
|
func (ctrl *CollectorController) Create(c *gin.Context) {
|
|
var req CreateCollectorRequest
|
|
if !utils.BindAndValidate(c, &req) {
|
|
return
|
|
}
|
|
collector, err := ctrl.service.Create(&services.CreateCollectorRequest{
|
|
Name: req.Name, Type: req.Type, LokiDeploymentID: req.LokiDeploymentID,
|
|
HostID: req.HostID, ClusterID: req.ClusterID, TargetDeployment: req.TargetDeployment,
|
|
Namespace: req.Namespace, LogPath: req.LogPath, Labels: req.Labels,
|
|
})
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(collector))
|
|
}
|
|
|
|
func (ctrl *CollectorController) Get(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
collector, err := ctrl.service.Get(uint(id))
|
|
if err != nil {
|
|
utils.NotFound(c, "采集器不存在")
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(collector))
|
|
}
|
|
|
|
func (ctrl *CollectorController) List(c *gin.Context) {
|
|
page, pageSize := utils.Pagination(c)
|
|
collectors, 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(collectors), Total: total, Page: page, PageSize: pageSize,
|
|
})
|
|
}
|
|
|
|
func (ctrl *CollectorController) 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 *CollectorController) 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 *CollectorController) Install(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
go ctrl.service.Install(uint(id))
|
|
utils.Success(c, gin.H{"message": "已开始部署采集器"})
|
|
}
|
|
|
|
func (ctrl *CollectorController) Uninstall(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
go ctrl.service.Uninstall(uint(id))
|
|
utils.Success(c, gin.H{"message": "已开始卸载采集器"})
|
|
}
|