agent

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package agent provides Agent communication functionality.

Package agent provides Agent communication functionality.

Index

Constants

View Source
const (
	QueryActionGetContainers = "get_containers"
	QueryActionGetIP         = "get_ip"
	QueryActionGetHealth     = "get_health"
)

QueryAction defines query actions.

View Source
const (
	CommandActionRefresh   = "refresh"
	CommandActionReconnect = "reconnect"
)

CommandAction defines command actions.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentConfigEntry

type AgentConfigEntry struct {
	Token         string
	DefaultTunnel string
	ConnectTo     string
}

AgentConfigEntry holds agent configuration entry.

type AgentConnection

type AgentConnection struct {
	ID            string
	Name          string
	Conn          *websocket.Conn
	DefaultTunnel string
	// contains filtered or unexported fields
}

AgentConnection represents a connected agent.

func (*AgentConnection) IsConnected

func (c *AgentConnection) IsConnected() bool

IsConnected reports whether the agent is currently connected.

type AgentHealth

type AgentHealth struct {
	DockerConnected bool   `json:"docker_connected"`
	UptimeSeconds   int64  `json:"uptime_seconds"`
	LastError       string `json:"last_error,omitempty"`
}

AgentHealth represents agent health status.

type AuthPayload

type AuthPayload struct {
	AgentID string `json:"agent_id"`
	Token   string `json:"token"`
	Version string `json:"version,omitempty"`
}

AuthPayload is the authentication message payload.

type Client

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

Client is the WebSocket client for agent outbound mode. Agent dials Main's WebSocket server.

func NewClient

func NewClient(cfg *config.Config, prov provider.Provider) *Client

NewClient creates a new agent client for outbound mode.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns whether the client is connected.

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run starts the agent client with reconnection loop. Retry behaviour is driven by cfg.Retry:

  • Attempts: max connection attempts (0 = infinite)
  • Delay: initial retry delay
  • MaxDelay: cap after exponential backoff
  • Backoff: delay multiplier per attempt

type CommandPayload

type CommandPayload struct {
	Action string `json:"action"`
}

CommandPayload is the command message payload.

type ContainerData

type ContainerData struct {
	ID       string            `json:"id"`
	Name     string            `json:"name"`
	Image    string            `json:"image"`
	Status   string            `json:"status"`
	Labels   map[string]string `json:"labels"`
	Networks map[string]string `json:"networks,omitempty"`
	Created  time.Time         `json:"created"`
	Started  time.Time         `json:"started,omitempty"`
}

ContainerData represents container data sent by agent.

func ContainerDataFromInfo

func ContainerDataFromInfo(info *types.ContainerInfo) *ContainerData

ContainerDataFromInfo converts ContainerInfo to ContainerData.

func (*ContainerData) ConvertToContainerInfo

func (c *ContainerData) ConvertToContainerInfo() *types.ContainerInfo

ConvertToContainerInfo converts ContainerData to ContainerInfo.

type ErrorPayload

type ErrorPayload struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorPayload is an error message payload.

type InboundListener

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

InboundListener is the WebSocket server for agent inbound mode. Agent listens and waits for Main to connect.

func NewInboundListener

func NewInboundListener(cfg *config.Config, prov provider.Provider) *InboundListener

NewInboundListener creates a new agent inbound listener.

func (*InboundListener) IsConnected

func (l *InboundListener) IsConnected() bool

IsConnected returns whether the listener has an active connection.

func (*InboundListener) Run

func (l *InboundListener) Run(ctx context.Context) error

Run starts the inbound listener with reconnection support.

type Message

type Message struct {
	Type      MessageType     `json:"type"`
	RequestID string          `json:"request_id,omitempty"`
	Payload   json.RawMessage `json:"payload,omitempty"`
}

Message is the WebSocket message envelope.

func NewMessage

func NewMessage(msgType MessageType, payload interface{}) (*Message, error)

NewMessage creates a new message with the given type and payload.

func NewMessageWithID

func NewMessageWithID(msgType MessageType, requestID string, payload interface{}) (*Message, error)

NewMessageWithID creates a new message with request ID.

func (*Message) ParsePayload

func (m *Message) ParsePayload(v interface{}) error

ParsePayload parses the payload into the given type.

type MessageType

type MessageType string

MessageType defines the type of WebSocket message.

const (
	// MessageTypeAuth is sent by agent to authenticate.
	MessageTypeAuth MessageType = "auth"
	// MessageTypeReport is sent by agent to report data.
	MessageTypeReport MessageType = "report"
	// MessageTypeQuery is sent by main to query agent.
	MessageTypeQuery MessageType = "query"
	// MessageTypeCommand is sent by main to send commands.
	MessageTypeCommand MessageType = "command"
	// MessageTypeResponse is a response to query/command.
	MessageTypeResponse MessageType = "response"
	// MessageTypeAck is an acknowledgment message.
	MessageTypeAck MessageType = "ack"
	// MessageTypeError is an error message.
	MessageTypeError MessageType = "error"
	// MessageTypeHeartbeat is a heartbeat/ping message.
	MessageTypeHeartbeat MessageType = "heartbeat"
)

type QueryPayload

type QueryPayload struct {
	Action string `json:"action"`
}

QueryPayload is the query message payload.

type ReportPayload

type ReportPayload struct {
	AgentID    string           `json:"agent_id"`
	Timestamp  time.Time        `json:"timestamp"`
	PublicIP   string           `json:"public_ip,omitempty"`
	Containers []*ContainerData `json:"containers"`
	Health     *AgentHealth     `json:"health"`
}

ReportPayload is the data report message payload.

type ResponsePayload

type ResponsePayload struct {
	Success bool            `json:"success"`
	Data    json.RawMessage `json:"data,omitempty"`
	Error   string          `json:"error,omitempty"`
}

ResponsePayload is a generic response payload.

type Server

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

Server is the WebSocket server for agent connections.

func NewServer

func NewServer(cfg *config.AgentServerConfig, agentConfigs map[string]*AgentConfigEntry, rec *reconciler.Reconciler, store storage.Storage, labelPrefix string) *Server

NewServer creates a new agent server.

func (*Server) ConnectToInboundAgents

func (s *Server) ConnectToInboundAgents(ctx context.Context)

ConnectToInboundAgents starts goroutines to connect to all agents that have ConnectTo configured (inbound mode). Each connection runs in its own goroutine with automatic reconnection.

func (*Server) GetConnectedAgents

func (s *Server) GetConnectedAgents() []string

GetConnectedAgents returns list of connected agents.

func (*Server) IsAgentConnected

func (s *Server) IsAgentConnected(agentID string) bool

IsAgentConnected checks if an agent is connected.

func (*Server) SendCommand

func (s *Server) SendCommand(agentID string, action string) error

SendCommand sends a command to an agent.

func (*Server) SendQuery

func (s *Server) SendQuery(agentID string, action string) error

SendQuery sends a query to an agent.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the WebSocket server.

Jump to

Keyboard shortcuts

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