Documentation
¶
Index ¶
- Constants
- Variables
- func ComparedEncryptKey(cipheredSrc, cipheredTarget string) (bool, error)
- func DecryptAgentKey(cipheredKey string) (string, error)
- func EncodeMessage(v any) (json.RawMessage, error)
- func EncryptAgentKey(text string) (string, error)
- type Action
- type Agent
- func (r *Agent) DecryptedAgentKey() error
- func (a *Agent) EncryptAgentKey() error
- func (e *Agent) LoadFromEvent(event *bus.Event) error
- func (r *Agent) RefreshCrendential()
- func (a *Agent) String() string
- func (a *Agent) TableName() string
- func (e *Agent) ToBusEvent(topic string) *bus.Event
- func (a *Agent) UpdateHeartbeatAt(t time.Time)
- func (a *Agent) ValidateKey(id, key string) error
- type AgentCrendential
- type AgentMessage
- type AgentMetricMessage
- type AgentRegistryInfo
- type AgentRegistryInfoRequest
- type AgentRegistryRequest
- type AgentStatus
- func (r *AgentStatus) Error(err error)
- func (r *AgentStatus) IsOnline() bool
- func (r *AgentStatus) Offline(t time.Time)
- func (r *AgentStatus) Online(t time.Time)
- func (r *AgentStatus) SetLatestHeartbeatAt(t time.Time)
- func (r *AgentStatus) SetOfflineAt(t time.Time)
- func (r *AgentStatus) SetStatus(status STATUS)
- func (r *AgentStatus) String() string
- func (r *AgentStatus) TableName() string
- func (r *AgentStatus) Unknown(msg string)
- type AgentVersion
- type CloseConnectionRequest
- type Connection
- func (c *Connection) Close() error
- func (c *Connection) GetErrorChan() <-chan error
- func (c *Connection) RegisterMessageHandler(msgType MessageType, handler ResponseHandler)
- func (c *Connection) RunTask(req *RunTaskRequest) error
- func (c *Connection) SetLogger(log *zerolog.Logger) *Connection
- func (c *Connection) StartMessageLoop(ctx context.Context)
- type CreateAgentRequest
- type DESCRIBE_BY
- type DescribeAgentRequest
- type GetConnectionRequest
- type HeartbeatConfig
- type HeartbeatReportRequest
- type HeartbeatReportResponse
- type MessageType
- type QueryAgentRequest
- func (r *QueryAgentRequest) SetEnabled(v bool) *QueryAgentRequest
- func (r *QueryAgentRequest) SetEnvName(v string) *QueryAgentRequest
- func (r *QueryAgentRequest) SetFeature(v string) *QueryAgentRequest
- func (r *QueryAgentRequest) SetStatus(v STATUS) *QueryAgentRequest
- func (r *QueryAgentRequest) String() string
- type Register
- type ResponseHandler
- type RunTaskRequest
- func (e *RunTaskRequest) LoadFromEvent(event *bus.Event) error
- func (r *RunTaskRequest) LogCallback() func(content string)
- func (r *RunTaskRequest) LogDebug(msg string)
- func (r *RunTaskRequest) LogError(msg string)
- func (r *RunTaskRequest) LogInfo(msg string)
- func (r *RunTaskRequest) LogSuccess(msg string)
- func (r *RunTaskRequest) LogWarn(msg string)
- func (r *RunTaskRequest) SetLogCallback(callback func(content string)) *RunTaskRequest
- func (r *RunTaskRequest) String() string
- func (e *RunTaskRequest) ToBusEvent(topic string) *bus.Event
- type RunTaskResponse
- type STATUS
- type Service
- type TaskConfirmMessage
- type TaskHandler
- type TaskLogMessage
- type UpdateAgentRequest
Constants ¶
View Source
const ( HEADER_AGENT_ID = "x-agent-id" HEADER_AGENT_KEY = "x-agent-key" HEADER_AGENT_HOSTNAME = "x-agent-hostname" HEADER_AGENT_ADDRESS = "x-agent-address" HEADER_AGENT_GIT_TAG = "x-agent-git-tag" HEADER_AGENT_GIT_COMMIT = "x-agent-git-commit" HEADER_AGENT_GIT_BRANCH = "x-agent-git-branch" HEADER_AGENT_BUILD_TIME = "x-agent-build-time" )
View Source
const (
APP_NAME = "agent"
)
Variables ¶
View Source
var ( StringToStatus = map[string]STATUS{ "UNKNOWN": STAUTS_UNKNOWN, "ONLINE": STATUS_ONLINE, "ERROR": STATUS_ERROR, "OFFLINE": STATUS_OFFLINE, } StatusToString = map[STATUS]string{ STAUTS_UNKNOWN: "UNKNOWN", STATUS_ONLINE: "ONLINE", STATUS_ERROR: "ERROR", STATUS_OFFLINE: "OFFLINE", } )
Functions ¶
func ComparedEncryptKey ¶
func DecryptAgentKey ¶
func EncryptAgentKey ¶
Types ¶
type Agent ¶
type Agent struct {
Id string `json:"id" gorm:"column:id;" optional:"true" description:"Agent Id"`
CreateTime time.Time `json:"create_time" gorm:"column:create_time;type:datetime" optional:"true" description:"创建时间"`
UpdateTime time.Time `json:"update_time" gorm:"column:update_time;type:datetime" optional:"true" description:"更新时间"`
CreateAgentRequest
AgentRegistryInfo
HeartbeatConfig
AgentStatus
AgentCrendential
}
func NewAgent ¶
func NewAgent(req CreateAgentRequest) *Agent
func (*Agent) DecryptedAgentKey ¶
func (*Agent) EncryptAgentKey ¶
func (*Agent) RefreshCrendential ¶
func (r *Agent) RefreshCrendential()
func (*Agent) UpdateHeartbeatAt ¶
func (*Agent) ValidateKey ¶
type AgentCrendential ¶
type AgentCrendential struct {
// Agent Key
Key string `json:"key" gorm:"column:key;type:varchar(200)" mask:",3,4" optional:"true" description:"Agent Key"`
// keys会否加密
IsEncrypted bool `json:"is_encrypted" gorm:"column:is_encrypted;type:bool" optional:"true" description:"是否加密"`
// 凭证刷新时间
RefreshAt time.Time `json:"refresh_at" gorm:"column:refresh_at;type:datetime" optional:"true" description:"刷新时间"`
// 旧key
OldKey string `json:"old_key" gorm:"column:old_key;type:varchar(200)" mask:",3,4" optional:"true" description:"旧key"`
}
func (AgentCrendential) TableName ¶
func (c AgentCrendential) TableName() string
type AgentMessage ¶
type AgentMessage struct {
Type MessageType `json:"type"`
RequestId string `json:"request_id"`
Data json.RawMessage `json:"data"`
}
通用响应消息
type AgentMetricMessage ¶
type AgentMetricMessage struct {
AgentId string `json:"agent_id"`
ReportTime time.Time `json:"report_time"`
// CPU 指标
CPUUsagePercent float64 `json:"cpu_usage_percent"`
CPUCores int `json:"cpu_cores"`
// 内存指标
MemoryUsedMB int64 `json:"memory_used_mb"`
MemoryTotalMB int64 `json:"memory_total_mb"`
MemoryUsagePercent float64 `json:"memory_usage_percent"`
// 磁盘指标
DiskUsedGB int64 `json:"disk_used_gb"`
DiskTotalGB int64 `json:"disk_total_gb"`
DiskUsagePercent float64 `json:"disk_usage_percent"`
// 负载指标
LoadAverage1 float64 `json:"load_average_1"`
LoadAverage5 float64 `json:"load_average_5"`
LoadAverage15 float64 `json:"load_average_15"`
// 关键资源指标
ProcessCount int `json:"process_count"` // 系统进程总数
InodeUsagePercent float64 `json:"inode_usage_percent"` // Inode 使用率 (%)
FileHandleUsagePercent float64 `json:"file_handle_usage_percent"` // 文件句柄使用率 (%)
// 任务执行指标
RunningTasks int `json:"running_tasks"`
// Runtime 指标
GoroutineCount int `json:"goroutine_count"`
}
Agent 指标消息
type AgentRegistryInfo ¶
type AgentRegistryInfo struct {
// 注册到的节点名称
NodeName string `json:"node_name" gorm:"column:node_name;type:varchar(100)" optional:"true" description:"节点名称"`
// Agent 运行的主机名称
HostName string `json:"host_name" gorm:"column:host_name;type:varchar(100)" optional:"true" description:"主机名称"`
// Address of the agent
Address string `json:"address" validate:"required" gorm:"column:address;type:varchar(100)" description:"地址"`
// 注册时间
RegistryAt *time.Time `json:"registry_at" gorm:"column:registry_at;type:datetime" optional:"true" description:"注册时间"`
// Agent 版本信息
AgentVersion
}
func (*AgentRegistryInfo) SetNodeName ¶
func (r *AgentRegistryInfo) SetNodeName(name string)
func (*AgentRegistryInfo) SetRegistryAt ¶
func (r *AgentRegistryInfo) SetRegistryAt(t time.Time)
func (*AgentRegistryInfo) String ¶
func (r *AgentRegistryInfo) String() string
func (*AgentRegistryInfo) TableName ¶
func (r *AgentRegistryInfo) TableName() string
type AgentRegistryInfoRequest ¶
type AgentRegistryInfoRequest struct {
AgentRegistryInfo
AgentStatus
}
func (*AgentRegistryInfoRequest) TableName ¶
func (r *AgentRegistryInfoRequest) TableName() string
type AgentRegistryRequest ¶
type AgentRegistryRequest struct {
// Agent Id
AgentId string `json:"agent_id" description:"Agent Id"`
// Agent Key
AgentKey string `json:"agent_key" description:"Agent Key"`
// 注册
AgentRegistryInfo
// contains filtered or unexported fields
}
func NewAgentRegistryRequest ¶
func NewAgentRegistryRequest() *AgentRegistryRequest
func (*AgentRegistryRequest) GetWebSocketConn ¶
func (a *AgentRegistryRequest) GetWebSocketConn() *websocket.Conn
func (*AgentRegistryRequest) SetWebSocketConn ¶
func (a *AgentRegistryRequest) SetWebSocketConn(conn *websocket.Conn)
type AgentStatus ¶
type AgentStatus struct {
// Status of the agent
Status STATUS `` /* 142-byte string literal not displayed */
// Message of the agent
Message string `json:"message" gorm:"column:message;type:text" optional:"true" description:"状态描述"`
// 最近一次上报心态报的时间
LatestHeartbeatAt *time.Time `` /* 141-byte string literal not displayed */
// 离线时间
OfflineAt *time.Time `json:"offline_at" gorm:"column:offline_at;type:datetime" optional:"true" description:"离线时间"`
}
func NewAgentOnlineStatus ¶
func NewAgentOnlineStatus() *AgentStatus
func (*AgentStatus) Error ¶
func (r *AgentStatus) Error(err error)
func (*AgentStatus) IsOnline ¶
func (r *AgentStatus) IsOnline() bool
func (*AgentStatus) Offline ¶
func (r *AgentStatus) Offline(t time.Time)
func (*AgentStatus) Online ¶
func (r *AgentStatus) Online(t time.Time)
func (*AgentStatus) SetLatestHeartbeatAt ¶
func (r *AgentStatus) SetLatestHeartbeatAt(t time.Time)
func (*AgentStatus) SetOfflineAt ¶
func (r *AgentStatus) SetOfflineAt(t time.Time)
func (*AgentStatus) SetStatus ¶
func (r *AgentStatus) SetStatus(status STATUS)
func (*AgentStatus) String ¶
func (r *AgentStatus) String() string
func (*AgentStatus) TableName ¶
func (r *AgentStatus) TableName() string
func (*AgentStatus) Unknown ¶
func (r *AgentStatus) Unknown(msg string)
type AgentVersion ¶
type AgentVersion struct {
// Git Tag
GitTag string `json:"git_tag" gorm:"column:git_tag;type:varchar(100)" optional:"true" description:"Git Tag"`
// Build Time
BuildTime string `json:"build_time" gorm:"column:build_time;type:varchar(100)" optional:"true" description:"Build Time"`
// Git Branch
GitBranch string `json:"git_branch" gorm:"column:git_branch;type:varchar(100)" optional:"true" description:"Git Branch"`
// Git Commit
GitCommit string `json:"git_commit" gorm:"column:git_commit;type:varchar(100)" optional:"true" description:"Git Commit"`
}
type CloseConnectionRequest ¶
type CloseConnectionRequest struct {
GetConnectionRequest
// 是否跳过计数递减(用于重连场景)
SkipDecrement bool `json:"skip_decrement"`
// 是否跳过设置 OFFLINE 状态(用于重连场景,避免状态污染)
SkipOffline bool `json:"skip_offline"`
}
func NewCloseConnectionRequest ¶
func NewCloseConnectionRequest(agentId string) *CloseConnectionRequest
func (*CloseConnectionRequest) SetSkipDecrement ¶
func (r *CloseConnectionRequest) SetSkipDecrement(skip bool) *CloseConnectionRequest
func (*CloseConnectionRequest) SetSkipOffline ¶
func (r *CloseConnectionRequest) SetSkipOffline(skip bool) *CloseConnectionRequest
type Connection ¶
type Connection struct {
Agent *Agent
// contains filtered or unexported fields
}
func NewConnection ¶
func NewConnection(ws *websocket.Conn, agent *Agent) *Connection
func (*Connection) Close ¶
func (c *Connection) Close() error
func (*Connection) GetErrorChan ¶
func (c *Connection) GetErrorChan() <-chan error
GetErrorChan 获取错误通知channel
func (*Connection) RegisterMessageHandler ¶
func (c *Connection) RegisterMessageHandler(msgType MessageType, handler ResponseHandler)
RegisterMessageHandler 注册消息处理器
func (*Connection) RunTask ¶
func (c *Connection) RunTask(req *RunTaskRequest) error
通过Agent连接 通知Agent执行任务
func (*Connection) SetLogger ¶
func (c *Connection) SetLogger(log *zerolog.Logger) *Connection
SetLogger 设置日志器
func (*Connection) StartMessageLoop ¶
func (c *Connection) StartMessageLoop(ctx context.Context)
StartMessageLoop 启动消息循环,处理来自 Agent 的响应 返回error channel用于通知连接错误
type CreateAgentRequest ¶
type CreateAgentRequest struct {
// 哪些环境下运行
Envs []string `json:"envs" validate:"required" gorm:"column:envs;type:json;serializer:json;not null;default:'[]'" description:"环境名称"`
// Name of the agent
Name string `json:"name" validate:"required" gorm:"column:name;type:varchar(100)" description:"名称"`
// Description of the agent
Description string `json:"description" validate:"required" gorm:"column:description;type:text" description:"描述"`
// Enabled status of the agent
Enabled *bool `json:"enabled" gorm:"column:enabled;type:bool" description:"是否启用"`
// 允许运行中的最大任务数
MaxRunningTasks int `json:"max_running_tasks" gorm:"column:max_running_tasks;type:int" description:"允许运行中的最大任务数"`
// Metadata of the agent
Metadata map[string]string `` /* 126-byte string literal not displayed */
// Tags of the agent
Label map[string]string `json:"label" gorm:"column:label;type:json;serializer:json;not null;default:'{}'" optional:"true" description:"标签"`
// Features of the agent, 比如是否支持 镜像扫描, 镜像上传
Features []string `` /* 196-byte string literal not displayed */
}
func NewCreateAgentRequest ¶
func NewCreateAgentRequest() *CreateAgentRequest
func (*CreateAgentRequest) AddEnv ¶
func (r *CreateAgentRequest) AddEnv(envs ...string)
func (*CreateAgentRequest) AddFeature ¶
func (r *CreateAgentRequest) AddFeature(features ...string)
func (*CreateAgentRequest) Validate ¶
func (r *CreateAgentRequest) Validate() error
type DESCRIBE_BY ¶
type DESCRIBE_BY int
const ( DESCRIBE_BY_ID DESCRIBE_BY = iota DESCRIBE_BY_NAME )
type DescribeAgentRequest ¶
type DescribeAgentRequest struct {
// 方式
DescribeBy DESCRIBE_BY `json:"describe_by"`
// Agent ID
Value string `json:"value"`
}
func NewDescribeAgentRequest ¶
func NewDescribeAgentRequest(id string) *DescribeAgentRequest
func (*DescribeAgentRequest) WithId ¶
func (r *DescribeAgentRequest) WithId(id string) *DescribeAgentRequest
func (*DescribeAgentRequest) WithName ¶
func (r *DescribeAgentRequest) WithName(name string) *DescribeAgentRequest
type GetConnectionRequest ¶
type GetConnectionRequest struct {
AgentId string `json:"agent_id" validate:"required"`
}
func NewGetConnectionRequest ¶
func NewGetConnectionRequest(agentId string) *GetConnectionRequest
type HeartbeatConfig ¶
type HeartbeatReportRequest ¶
type HeartbeatReportRequest struct {
AgentId string `json:"agent_id" validate:"required"`
}
func NewHeartbeatReportRequest ¶
func NewHeartbeatReportRequest(agentId string) *HeartbeatReportRequest
type HeartbeatReportResponse ¶
type HeartbeatReportResponse struct {
Agent *Agent `json:"agent"`
}
type MessageType ¶
type MessageType string
消息类型
const ( MESSAGE_TYPE_TASK_RESULT MessageType = "task_result" // 任务结果 MESSAGE_TYPE_TASK_LOG MessageType = "task_log" // 任务日志 MESSAGE_TYPE_TASK_CONFIRM MessageType = "task_confirm" // 任务调度确认 MESSAGE_TYPE_AGENT_METRIC MessageType = "agent_metric" // Agent 指标上报 )
type QueryAgentRequest ¶
type QueryAgentRequest struct {
request.PageRequest
// 环境名称
EnvName *string `json:"env_name" form:"env_name"`
// Agent状态
Enabled *bool `json:"enabled" form:"enabled"`
// AgentIds
AgentIds []string `json:"agent_ids" form:"agent_ids"`
// 特性, 比如sam_deploy
Feature string `json:"feature" form:"feature"`
// 资源标签过滤
Label map[string]string `json:"label" form:"label"`
// 状态
Status *STATUS `json:"status" form:"status"`
}
func NewQueryAgentRequest ¶
func NewQueryAgentRequest() *QueryAgentRequest
func (*QueryAgentRequest) SetEnabled ¶
func (r *QueryAgentRequest) SetEnabled(v bool) *QueryAgentRequest
func (*QueryAgentRequest) SetEnvName ¶
func (r *QueryAgentRequest) SetEnvName(v string) *QueryAgentRequest
func (*QueryAgentRequest) SetFeature ¶
func (r *QueryAgentRequest) SetFeature(v string) *QueryAgentRequest
func (*QueryAgentRequest) SetStatus ¶
func (r *QueryAgentRequest) SetStatus(v STATUS) *QueryAgentRequest
func (*QueryAgentRequest) String ¶
func (r *QueryAgentRequest) String() string
type Register ¶
type Register interface {
// 注册Agent
RegisterAgent(context.Context, *AgentRegistryRequest) (*Connection, error)
// 获取连接
GetConnection(context.Context, *GetConnectionRequest) (*Connection, error)
// 关闭连接
CloseConnection(context.Context, *CloseConnectionRequest) error
// 心跳上报
HeartbeatReport(context.Context, *HeartbeatReportRequest) (*HeartbeatReportResponse, error)
}
func GetRegister ¶
func GetRegister() Register
type ResponseHandler ¶
type ResponseHandler interface {
HandleResponse(ctx context.Context, msg *AgentMessage) error
}
响应处理器接口
type RunTaskRequest ¶
type RunTaskRequest struct {
RequestId string `json:"request_id"`
Action Action `json:"action"`
Task *task.Task `json:"task"`
// contains filtered or unexported fields
}
func NewRunTaskRequest ¶
func NewRunTaskRequest(action Action, task *task.Task) *RunTaskRequest
func (*RunTaskRequest) LoadFromEvent ¶
func (e *RunTaskRequest) LoadFromEvent(event *bus.Event) error
func (*RunTaskRequest) LogCallback ¶
func (r *RunTaskRequest) LogCallback() func(content string)
LogCallback 获取日志回调函数
func (*RunTaskRequest) LogDebug ¶
func (r *RunTaskRequest) LogDebug(msg string)
LogDebug 输出 DEBUG 日志(委托给Task对象)
func (*RunTaskRequest) LogError ¶
func (r *RunTaskRequest) LogError(msg string)
LogError 输出 ERROR 日志(委托给Task对象)
func (*RunTaskRequest) LogInfo ¶
func (r *RunTaskRequest) LogInfo(msg string)
LogInfo 输出 INFO 日志(委托给Task对象)
func (*RunTaskRequest) LogSuccess ¶
func (r *RunTaskRequest) LogSuccess(msg string)
LogSuccess 输出 SUCCESS 日志(委托给Task对象)
func (*RunTaskRequest) LogWarn ¶
func (r *RunTaskRequest) LogWarn(msg string)
LogWarn 输出 WARNING 日志(委托给Task对象)
func (*RunTaskRequest) SetLogCallback ¶
func (r *RunTaskRequest) SetLogCallback(callback func(content string)) *RunTaskRequest
SetLogCallback 设置日志回调函数(直接设置到Task对象)
func (*RunTaskRequest) String ¶
func (r *RunTaskRequest) String() string
func (*RunTaskRequest) ToBusEvent ¶
func (e *RunTaskRequest) ToBusEvent(topic string) *bus.Event
type RunTaskResponse ¶
type RunTaskResponse struct {
RunTaskRequest
*script.ExecutionResult
}
func NewRunTaskResponse ¶
func NewRunTaskResponse(req *RunTaskRequest, result *script.ExecutionResult) *RunTaskResponse
func (*RunTaskResponse) UpdateTaskStatus ¶
func (r *RunTaskResponse) UpdateTaskStatus()
注意 Log已经通过LogCallback回调函数发送,不需要在这里携带
type STATUS ¶
type STATUS int
func ParseSTATUSFromString ¶
ParseSTATUSFromString 从字符串解析 STATUS
func (STATUS) MarshalJSON ¶
func (*STATUS) UnmarshalJSON ¶
type Service ¶
type Service interface {
// 创建Agent
CreateAgent(context.Context, *CreateAgentRequest) (*Agent, error)
// 更新Agent
UpdateAgent(context.Context, *UpdateAgentRequest) (*Agent, error)
// 查询列表
QueryAgent(context.Context, *QueryAgentRequest) (*types.Set[*Agent], error)
// 查询详情
DescribeAgent(context.Context, *DescribeAgentRequest) (*Agent, error)
// 运行任务
RunTask(context.Context, *task.Task) error
}
func GetService ¶
func GetService() Service
type TaskConfirmMessage ¶
调度确认消息
type TaskHandler ¶
type TaskHandler interface {
// HandleTask 任务处理
HandleTask(context.Context, *RunTaskRequest) (*RunTaskResponse, error)
}
type TaskLogMessage ¶
type TaskLogMessage struct {
TaskId string `json:"task_id"`
Content string `json:"content"`
Time time.Time `json:"time"` // 精确时间(纳秒级)
Sequence int64 `json:"sequence"` // 序列号,同一秒内的顺序
}
日志消息
type UpdateAgentRequest ¶
type UpdateAgentRequest struct {
// Id
DescribeAgentRequest
// Agent 信息
CreateAgentRequest
}
func NewUpdateAgentRequest ¶
func NewUpdateAgentRequest() *UpdateAgentRequest
Source Files
¶
Click to show internal directories.
Click to hide internal directories.