Documentation ¶
Index ¶
- Constants
- Variables
- func ReturnRest(resp *RestResponse)
- type AgentBase
- type AgentInfo
- type AgentResource
- type AgentResourceExternal
- type AllocatedInfo
- type AllocatedResource
- type CallBack4Command
- type CallBackSelector
- type CmdType
- type Command
- type CommandInfo
- type CommandResultInfo
- type CommandStatusType
- type HandleWithUser
- type MySQL
- type MySQLConf
- type NotifyAgentData
- type ReportAgentResource
- type ResHTTPHandle
- type Resource
- type ResourceManager
- type RestResponse
Constants ¶
const ( AgentResourceCheckTime = 20 * time.Second AgentReportInterval = 15 // secs AgentReportTimeoutCounter = 3 AgentTaskTimeoutSecs = 30 // secs LogResourceStatInterval = 20 * time.Second LogP2PResourceTemplate = "\tCity: %s[total cpu:%d,user num:%d]" LeastP2PCPUPerUser = 96 P2PQueryKey = "released" P2PRecordMessageTemplate = "p2p:%s:%s" )
const vars
const ( LabelKeyGOOS = "os" LabelValueGOOSWindows = "windows" LabelValueGOOSDarwin = "darwin" )
define const strings
const ( VersionV1 = "v1" PathV1 = prefix + VersionV1 )
define vars
const (
URLExecuteCommand = "http://%s:%d/api/v1/build/executecommand"
)
define urls
Variables ¶
var ( ErrInitHTTPHandle = fmt.Errorf("failled to init http handle") ErrResourceReported = fmt.Errorf("resource reported is not valid") LetterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") )
vars for error define
var ( ErrorResourceNoExist = fmt.Errorf("resource no exist") ErrorResourceNotEnought = fmt.Errorf("resource not enought") )
var (
ErrNilObject = fmt.Errorf("object is nil")
)
define errors
Functions ¶
Types ¶
type AgentBase ¶
type AgentBase struct { IP string `json:"ip"` Port int `json:"port"` Message string `json:"message"` Cluster string `json:"cluster"` Labels map[string]string `json:"labels"` }
AgentBase : agent info
type AgentInfo ¶
type AgentInfo struct { Base AgentBase `json:"base"` Total Resource `json:"total"` Free Resource `json:"free"` Allocated []*AllocatedInfo `json:"allocated"` }
AgentInfo : agent info
func (*AgentInfo) FreeToExternal ¶
func (a *AgentInfo) FreeToExternal() *AgentResourceExternal
FreeToExternal format free resource to AgentResourceExternal
type AgentResource ¶
type AgentResource struct { Cluster string `gorm:"column:cluster;primary_key" sql:"type:varchar(64)" json:"cluster"` IP string `gorm:"column:ip;primary_key" sql:"type:varchar(32)" json:"ip"` UpdatedAt time.Time `gorm:"column:updated_at" json:"-"` TotalCPU float32 `gorm:"column:total_cpu" json:"total_cpu"` TotalMemory float32 `gorm:"column:total_memory" json:"total_memory"` TotalDisk float32 `gorm:"column:total_disk" json:"total_disk"` FreeCPU float32 `gorm:"column:free_cpu" json:"free_cpu"` FreeMemory float32 `gorm:"column:free_memory" json:"free_memory"` FreeDisk float32 `gorm:"column:free_disk" json:"free_disk"` }
AgentResource : table for agent resource
func (AgentResource) TableName ¶
func (ar AgentResource) TableName() string
TableName : get table name for AgentResource
type AgentResourceExternal ¶
AgentResourceExternal : agent info
type AllocatedInfo ¶
type AllocatedInfo struct { AllocatedResource Resource `json:"allocated_resource"` // 资源使用者id UserID string `json:"user_id"` // 资源分配id(分配资源时生成的id) ResBatchID string `json:"res_batch_id"` Commands []*CommandInfo `json:"commands"` }
AllocatedInfo : 已分配的资源信息
type AllocatedResource ¶
type AllocatedResource struct { UpdatedAt time.Time `gorm:"column:updated_at" json:"-"` UserID string `gorm:"column:user_id;primary_key" sql:"type:varchar(64)" json:"user_id"` ResourceBatchID string `gorm:"column:resource_batch_id;primary_key" sql:"type:varchar(128)" json:"resource_batch_id"` Released int32 `gorm:"column:released;index" json:"released"` AllocatedTime int64 `gorm:"column:allocated_time" json:"allocated_time"` ReleasedTime int64 `gorm:"column:released_time" json:"released_time"` AllocatedAgent string `gorm:"column:allocated_agent" sql:"type:text" json:"allocated_agent"` Message string `gorm:"column:message" sql:"type:text" json:"message"` }
AllocatedResource : table for allocated resource
func (AllocatedResource) TableName ¶
func (ar AllocatedResource) TableName() string
TableName : get table name for AllocatedResource
type CallBack4Command ¶
type CallBack4Command func(usedRes []*CommandResultInfo) error
CallBack4Command : 回调函数,用于处理agent上报的使用的资源信息
type CallBackSelector ¶
type CallBackSelector func(free []*AgentResourceExternal, condition interface{}) ([]*AgentResourceExternal, error)
CallBackSelector : 回调函数,用于用户选择需要的资源
type Command ¶
type Command struct { // 命令的工作目录 Dir string `json:"dir"` // 可执行文件路径,如果是相对路径,则相对于Dir Path string `json:"path"` Cmd string `json:"cmd"` Parameters []string `json:"parameters"` Env map[string]string `json:"env"` Additional map[string]string `json:"additional"` CmdType CmdType `json:"cmd_type"` // 用户自定义ID,具体含义由资源使用者自己解释 UserDefineID string `json:"user_define_id"` // 保存关联的命令字和id,比如执行释放命令时,需要带上启动命令和进程id,便于agent侧执行相应的释放 ReferCmd string `json:"refer_cmd"` ReferID string `json:"refer_id"` }
Command : cmd struct to execute
type CommandInfo ¶
type CommandInfo struct { // 远端关联的id,比如拉起的进程id ID string `json:"id"` Cmd string `json:"cmd"` Port int `json:"port"` // 状态 Status CommandStatusType `json:"status"` // 用户自定义ID,具体含义由资源使用者自己解释 UserDefineID string `json:"user_define_id"` }
CommandInfo : 命令信息
type CommandResultInfo ¶
type CommandResultInfo struct { IP string `json:"ip"` // 远端关联的id,比如拉起的进程id ID string `json:"id"` Cmd string `json:"cmd"` // 状态 Status CommandStatusType `json:"status"` // 用户自定义ID,具体含义由资源使用者自己解释 UserDefineID string `json:"user_define_id"` }
CommandResultInfo : 命令执行结果信息
type CommandStatusType ¶
type CommandStatusType int
CommandStatusType : type for worker status
const ( // 已分配,等待worker拉起 CommandStatusInit CommandStatusType = iota // Command成功 CommandStatusSucceed // Command失败 CommandStatusFail )
define resource status
func (CommandStatusType) String ¶
func (rst CommandStatusType) String() string
String return the string of CommandStatusType
type HandleWithUser ¶
type HandleWithUser interface { // 根据传入的查询条件 和注册的 选择资源回调函数选择资源;记录针对该批资源的通知回调函数 // 返回值: 资源列表和错误信息 GetFreeResource( resBatchID string, condition interface{}, callbackSelector CallBackSelector, callback4UsedRes CallBack4Command) ([]*AgentResourceExternal, error) // 释放resourceID标记的相关资源 ReleaseResource(resBatchID string) error // 返回资源id关联的资源列表 ListResource(resBatchID string) ([]*AgentResourceExternal, error) // 在远端agent上执行命令,错误返回值表示当前操作是否正常,不代表远端命令已经执行成功 ExecuteCommand(ip string, resBatchID string, cmd *Command) error // 返回资源id关联的command列表 ListCommands(resBatchID string) ([]*CommandResultInfo, error) // p2p资源不是独占的,逻辑不一样 GetFreeP2PResource( resBatchID string, condition interface{}, callbackSelector CallBackSelector, groupKey string, platform string) ([]*AgentResourceExternal, error) ReleaseP2PResource(resBatchID, platform, groupKey string) error }
HandleWithUser : to manage agent resource with specified user
func NewResourceHandleWithUser ¶
func NewResourceHandleWithUser(mgr *directResourceManager, userID string) (HandleWithUser, error)
NewResourceHandleWithUser : new handleWithUser
type MySQL ¶
type MySQL interface { PutAgentResource(ar *AgentResource) error ListAllocateResource(opts commonMySQL.ListOptions) ([]*AllocatedResource, int64, error) GetAllocatedResource(userID, resourceBatchID string) (*AllocatedResource, error) PutAllocatedResource(ar *AllocatedResource) error }
MySQL is used for managing mysql, all the resource will be wrap from struct into mysql row before sending. Also, the resource get from mysql row will be wrap into struct and return to caller.
type MySQLConf ¶
type MySQLConf struct { MySQLStorage string MySQLDatabase string MySQLUser string MySQLPwd string Charset string MysqlTableOption string }
MySQLConf define mysql config
type NotifyAgentData ¶
type NotifyAgentData struct { // 资源使用者的ID UserID string `json:"user_id"` ResBatchID string `json:"res_batch_id"` // 用户自定义ID,具体含义由资源使用者自己解释 UserDefineID string `json:"user_define_id"` // 命令的工作目录 Dir string `json:"dir"` // 可执行文件路径,如果是相对路径,则相对于agent运行目录 Path string `json:"path"` Cmd string `json:"cmd"` Parameters []string `json:"parameters"` Env map[string]string `json:"env"` Additional map[string]string `json:"additional"` CmdType string `json:"cmd_type"` // 保存关联的命令字和id,比如执行释放命令时,需要带上启动命令和进程id,便于agent侧执行相应的释放 ReferCmd string `json:"refer_cmd"` ReferID string `json:"refer_id"` }
NotifyAgentData : struct to notify agent
type ReportAgentResource ¶
type ReportAgentResource struct {
AgentInfo
}
ReportAgentResource : struct of report resource
type ResHTTPHandle ¶
type ResHTTPHandle struct {
// contains filtered or unexported fields
}
ResHTTPHandle : http handle for resource report
func NewResourceHTTPHandle ¶
func NewResourceHTTPHandle(conf *config.DirectResourceConfig, mgr *directResourceManager) (*ResHTTPHandle, error)
NewResourceHTTPHandle : return new http handle
func (*ResHTTPHandle) GetActions ¶
func (a *ResHTTPHandle) GetActions() []*httpserver.Action
GetActions : Get V1 actions
type Resource ¶
type Resource struct { CPU float64 `json:"cpu"` Mem float64 `json:"mem"` Disk float64 `json:"disk"` }
Resource : 资源信息
type ResourceManager ¶
type ResourceManager interface { // 注册用户信息,返回用户相关的handle RegisterUser(userID string, releaseCmd *Command) (HandleWithUser, error) Run() error }
ResourceManager : to manage agent resource
func NewResourceManager ¶
func NewResourceManager(conf *config.DirectResourceConfig, roleEvent types.RoleChangeEvent) (ResourceManager, error)
NewResourceManager : new ResourceManager
type RestResponse ¶
type RestResponse struct { Resp *restful.Response HTTPCode int Data interface{} ErrCode commonTypes.ServerErrCode //ErrCode int Message string Extra map[string]interface{} WrapFunc func([]byte) []byte }
RestResponse : data struct for http response