lua

package
v0.26.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const TriggerTypeCron = "cron"

TriggerTypeCron 表示定时触发

View Source
const TriggerTypeManual = "manual"

TriggerTypeManual 表示手动触发

Variables

This section is empty.

Functions

func InitClusterInspection

func InitClusterInspection()

InitClusterInspection initializes the cluster inspection system This function is now idempotent and can be called multiple times

func StopClusterInspection

func StopClusterInspection()

StopClusterInspection 停止集群巡检定时任务

Types

type CancellableJob

type CancellableJob struct {
	// contains filtered or unexported fields
}

CancellableJob 可取消的任务包装

func NewCancellableJob

func NewCancellableJob(f func(ctx context.Context)) *CancellableJob

func (*CancellableJob) Cancel

func (cj *CancellableJob) Cancel()

func (*CancellableJob) Run

func (cj *CancellableJob) Run()

type CheckEvent

type CheckEvent struct {
	Status     string         `json:"status"` // 检查状态(如“正常”、“失败”)
	Msg        string         `json:"msg"`
	Extra      map[string]any `json:"extra,omitempty"`
	ScriptName string         `json:"scriptName"` // 检测脚本名称
	Kind       string         `json:"kind"`       // 检查的资源类型
	CheckDesc  string         `json:"checkDesc"`  // 检查脚本内容描述
	Namespace  string         `json:"ns"`         // 资源命名空间
	Name       string         `json:"name"`       // 资源名称
}

CheckEvent 用于记录每次检测的详细信息,包括检测状态、消息、额外上下文、脚本名称、资源类型、描述、命名空间和资源名。

type CheckResult

type CheckResult struct {
	Name         string
	StartTime    time.Time
	EndTime      time.Time
	LuaRunOutput string
	LuaRunError  error
	Events       []CheckEvent
}

type Inspection

type Inspection struct {
	Cluster string // 集群名称

	Schedule *models.InspectionSchedule // 巡检计划ID
	// contains filtered or unexported fields
}

func NewLuaInspection

func NewLuaInspection(schedule *models.InspectionSchedule, cluster string) *Inspection

func (*Inspection) Start

func (p *Inspection) Start() []CheckResult

type Kubectl

type Kubectl struct {
	// contains filtered or unexported fields
}

type ScheduleBackground

type ScheduleBackground struct {
}

func NewScheduleBackground

func NewScheduleBackground() *ScheduleBackground

func (*ScheduleBackground) Add

func (s *ScheduleBackground) Add(scheduleID uint)

func (*ScheduleBackground) AddCronJobFromDB

func (s *ScheduleBackground) AddCronJobFromDB()

AddCronJobFromDB 后台自动执行调度

func (*ScheduleBackground) AutoGenerateSummary

func (s *ScheduleBackground) AutoGenerateSummary(recordID uint)

AutoGenerateSummary 如果启用了AI总结,则自动生成总结 该方法在巡检执行完成后被调用,生成的AI总结会保存到数据库中 调用时机:巡检完成后立即调用,在发送webhook之前 设计原则:AI总结生成与webhook发送分离,确保职责单一

func (*ScheduleBackground) GetSummaryMsg

func (s *ScheduleBackground) GetSummaryMsg(recordID uint) (*SummaryMsg, error)

GetSummaryMsg 获取巡检记录的汇总信息 该函数根据记录ID查询巡检记录详情,包括关联的调度任务信息和失败事件统计 返回的数据中,record_date字段会将原始的UTC时间转换为本地时间,以便前端正确显示 参数:

  • recordID: 巡检记录的唯一标识符

返回值:

  • *SummaryMsg: 包含巡检汇总信息的结构体,包括记录日期、调度信息、错误统计等
  • error: 如果查询失败或记录不存在则返回错误

func (*ScheduleBackground) IsEventStatusPass

func (s *ScheduleBackground) IsEventStatusPass(status string) bool

IsEventStatusPass 判断事件状态是否为通过 这里的通过状态包括:正常、pass、ok、success、通过 入库前将状态描述文字统一为正常、失败两种

func (*ScheduleBackground) PushToHooksByRecordID

func (s *ScheduleBackground) PushToHooksByRecordID(recordID uint) ([]*api.SendResult, error)

PushToHooksByRecordID 根据巡检记录ID发送webhook通知 该方法从数据库中获取已生成的AI总结,然后发送到所有关联的webhook 调用时机:在AutoGenerateSummaryIfEnabled()完成后调用 设计原则:单纯的webhook发送功能,不负责AI总结生成

func (*ScheduleBackground) Remove

func (s *ScheduleBackground) Remove(scheduleID uint)

func (*ScheduleBackground) RunByCluster

func (s *ScheduleBackground) RunByCluster(ctx context.Context, scheduleID *uint, cluster string, triggerType string) (*models.InspectionRecord, error)

RunByCluster 启动一次巡检任务,并记录执行及每个脚本的结果到数据库 scheduleID: 定时任务ID cluster: 目标集群 triggerType: 触发类型(manual/cron)

func (*ScheduleBackground) SaveSummaryBack

func (s *ScheduleBackground) SaveSummaryBack(id uint, summary string, summaryErr error, resultRaw string) error

SaveSummaryBack 保存AI总结结果到数据库 参数:id 巡检记录ID,summary AI总结内容,summaryErr AI总结错误,resultRaw 原始巡检结果JSON字符串

func (*ScheduleBackground) SummaryByAI

func (s *ScheduleBackground) SummaryByAI(ctx context.Context, msg *SummaryMsg) (string, error)

SummaryByAI 生成巡检总结 参数:msg 包含巡检数据和AI配置的消息 返回:总结内容和错误信息

func (*ScheduleBackground) Update

func (s *ScheduleBackground) Update(scheduleID uint)

type SummaryMsg

type SummaryMsg struct {
	RecordDate       string                         `json:"record_date"`        // 巡检记录日期(本地时间格式)
	RecordID         uint                           `json:"record_id"`          // 巡检记录ID
	ScheduleID       *uint                          `json:"schedule_id"`        // 巡检计划ID
	ScheduleName     string                         `json:"schedule_name"`      // 巡检计划名称
	Cluster          string                         `json:"cluster"`            // 集群名称
	TotalRules       int                            `json:"total_rules"`        // 总规则数
	FailedCount      int                            `json:"failed_count"`       // 失败数量
	FailedList       []*models.InspectionCheckEvent `json:"failed_list"`        // 失败事件列表
	AIEnabled        bool                           `json:"ai_enabled"`         // 是否启用AI汇总
	AIPromptTemplate string                         `json:"ai_prompt_template"` // AI提示模板
}

SummaryMsg 巡检记录汇总信息结构体 用于替代原来的 map[string]any 返回类型,提供类型安全和更好的性能

type TaskManager

type TaskManager struct {
	// contains filtered or unexported fields
}

TaskManager 任务管理

func NewTaskManager

func NewTaskManager() *TaskManager

func (*TaskManager) Add

func (tm *TaskManager) Add(name, spec string, jobFunc func(ctx context.Context)) error

Add 新增任务,name 用于后续管理

func (*TaskManager) Remove

func (tm *TaskManager) Remove(name string)

Remove 删除任务(不会中断正在运行的 job,但会 cancel 长任务)

func (*TaskManager) Start

func (tm *TaskManager) Start()

func (*TaskManager) Stop

func (tm *TaskManager) Stop() context.Context

func (*TaskManager) Update

func (tm *TaskManager) Update(name, spec string, jobFunc func(ctx context.Context)) error

Update = Remove + Add(立即生效),如果需要立刻触发一次可以在 Add 后主动 go jobFunc(ctx)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL