Documentation
¶
Overview ¶
Package storage provides persistent storage for labelgate.
Index ¶
- Variables
- func IsConflict(err error) bool
- func IsNotFound(err error) bool
- type Agent
- type AgentStatus
- type ManagedResource
- type Migration
- type ResourceFilter
- type ResourceStatus
- type ResourceType
- type SQLiteStorage
- func (s *SQLiteStorage) CleanupDeletedResources(ctx context.Context, before time.Time) (int64, error)
- func (s *SQLiteStorage) Close() error
- func (s *SQLiteStorage) DeleteAgent(ctx context.Context, id string) error
- func (s *SQLiteStorage) DeleteResource(ctx context.Context, id string) error
- func (s *SQLiteStorage) GetAgent(ctx context.Context, id string) (*Agent, error)
- func (s *SQLiteStorage) GetResource(ctx context.Context, id string) (*ManagedResource, error)
- func (s *SQLiteStorage) GetResourceByContainerService(ctx context.Context, containerID, serviceName string) (*ManagedResource, error)
- func (s *SQLiteStorage) GetResourceByHostname(ctx context.Context, hostname string, resourceType ResourceType) (*ManagedResource, error)
- func (s *SQLiteStorage) GetSyncState(ctx context.Context, key string) (string, error)
- func (s *SQLiteStorage) Initialize(ctx context.Context) error
- func (s *SQLiteStorage) ListAgents(ctx context.Context) ([]*Agent, error)
- func (s *SQLiteStorage) ListExpiredOrphans(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
- func (s *SQLiteStorage) ListOrphanedForCleanup(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
- func (s *SQLiteStorage) ListResources(ctx context.Context, filter ResourceFilter) ([]*ManagedResource, error)
- func (s *SQLiteStorage) SaveAgent(ctx context.Context, agent *Agent) error
- func (s *SQLiteStorage) SaveResource(ctx context.Context, resource *ManagedResource) error
- func (s *SQLiteStorage) SetSyncState(ctx context.Context, key, value string) error
- func (s *SQLiteStorage) UpdateAgentStatus(ctx context.Context, id string, connected bool, status AgentStatus) error
- func (s *SQLiteStorage) UpdateResourceError(ctx context.Context, id string, status ResourceStatus, lastError string) error
- func (s *SQLiteStorage) UpdateResourceStatus(ctx context.Context, id string, status ResourceStatus) error
- func (s *SQLiteStorage) Vacuum(ctx context.Context) error
- type Storage
- type StorageError
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = &StorageError{Code: "not_found", Message: "resource not found"} ErrConflict = &StorageError{Code: "conflict", Message: "resource already exists"} )
Common errors
Functions ¶
func IsConflict ¶
IsConflict checks if the error is a conflict error.
func IsNotFound ¶
IsNotFound checks if the error is a not found error.
Types ¶
type Agent ¶
type Agent struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
// Connection info
Connected bool `json:"connected"`
LastSeen *time.Time `json:"last_seen,omitempty"`
PublicIP string `json:"public_ip,omitempty"`
// Configuration
DefaultTunnel string `json:"default_tunnel,omitempty"`
// Status
Status AgentStatus `json:"status"`
// Timestamps
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Agent represents an agent connection.
type AgentStatus ¶
type AgentStatus string
AgentStatus represents the status of an agent.
const ( // AgentStatusActive means the agent is active. AgentStatusActive AgentStatus = "active" // AgentStatusDisconnected means the agent is disconnected. AgentStatusDisconnected AgentStatus = "disconnected" // AgentStatusRemoved means the agent has been removed. AgentStatusRemoved AgentStatus = "removed" )
type ManagedResource ¶
type ManagedResource struct {
ID string `json:"id"`
// Resource identification
ResourceType ResourceType `json:"resource_type"`
CFID string `json:"cf_id,omitempty"`
// DNS record fields
ZoneID string `json:"zone_id,omitempty"`
Hostname string `json:"hostname"`
RecordType string `json:"record_type,omitempty"`
Content string `json:"content,omitempty"`
Proxied bool `json:"proxied,omitempty"`
TTL int `json:"ttl,omitempty"`
// Tunnel fields
TunnelID string `json:"tunnel_id,omitempty"`
Service string `json:"service,omitempty"`
Path string `json:"path,omitempty"`
// Access App fields
AccessAppID string `json:"access_app_id,omitempty"` // Cloudflare Access Application ID
AccountID string `json:"account_id,omitempty"` // Cloudflare Account ID
AccessAppName string `json:"access_app_name,omitempty"` // CF Access Application display name
AccessPolicyName string `json:"access_policy_name,omitempty"` // Labelgate policy template name
AccessDecision string `json:"access_decision,omitempty"` // Policy decision (allow, block, bypass, service_auth)
// Source information
ContainerID string `json:"container_id,omitempty"`
ContainerName string `json:"container_name,omitempty"`
ServiceName string `json:"service_name"`
AgentID string `json:"agent_id,omitempty"`
// Status
Status ResourceStatus `json:"status"`
CleanupEnabled bool `json:"cleanup_enabled"`
LastError string `json:"last_error,omitempty"`
// Timestamps
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
}
ManagedResource represents a resource managed by labelgate.
type ResourceFilter ¶
type ResourceFilter struct {
ResourceType ResourceType
Hostname string
ContainerID string
ServiceName string
AgentID string
Status ResourceStatus // single status filter (for backwards compatibility)
Statuses []ResourceStatus // multi-status filter (takes precedence over Status)
Limit int
Offset int
}
ResourceFilter represents filter options for querying resources.
type ResourceStatus ¶
type ResourceStatus string
ResourceStatus represents the status of a managed resource.
const ( // StatusActive means the resource is actively managed. StatusActive ResourceStatus = "active" // StatusOrphaned means the container stopped but cleanup is disabled. StatusOrphaned ResourceStatus = "orphaned" // StatusPendingCleanup means the resource is scheduled for cleanup. StatusPendingCleanup ResourceStatus = "pending_cleanup" // StatusDeleted means the resource has been deleted from Cloudflare. StatusDeleted ResourceStatus = "deleted" // StatusError means the resource failed to be created or updated on Cloudflare. StatusError ResourceStatus = "error" )
type ResourceType ¶
type ResourceType string
ResourceType represents the type of managed resource.
const ( // ResourceTypeDNS represents a DNS record. ResourceTypeDNS ResourceType = "dns" // ResourceTypeTunnelIngress represents a Tunnel ingress rule. ResourceTypeTunnelIngress ResourceType = "tunnel_ingress" // ResourceTypeAccessApp represents a Zero Trust Access Application. ResourceTypeAccessApp ResourceType = "access_app" )
type SQLiteStorage ¶
type SQLiteStorage struct {
// contains filtered or unexported fields
}
SQLiteStorage implements Storage interface using SQLite.
func NewSQLiteStorage ¶
func NewSQLiteStorage(path string) (*SQLiteStorage, error)
NewSQLiteStorage creates a new SQLite storage instance.
func (*SQLiteStorage) CleanupDeletedResources ¶
func (s *SQLiteStorage) CleanupDeletedResources(ctx context.Context, before time.Time) (int64, error)
CleanupDeletedResources removes deleted resources older than the given time.
func (*SQLiteStorage) Close ¶
func (s *SQLiteStorage) Close() error
Close closes the database connection.
func (*SQLiteStorage) DeleteAgent ¶
func (s *SQLiteStorage) DeleteAgent(ctx context.Context, id string) error
DeleteAgent deletes an agent by ID.
func (*SQLiteStorage) DeleteResource ¶
func (s *SQLiteStorage) DeleteResource(ctx context.Context, id string) error
DeleteResource deletes a resource by ID.
func (*SQLiteStorage) GetResource ¶
func (s *SQLiteStorage) GetResource(ctx context.Context, id string) (*ManagedResource, error)
GetResource retrieves a resource by ID.
func (*SQLiteStorage) GetResourceByContainerService ¶
func (s *SQLiteStorage) GetResourceByContainerService(ctx context.Context, containerID, serviceName string) (*ManagedResource, error)
GetResourceByContainerService retrieves a resource by container and service name.
func (*SQLiteStorage) GetResourceByHostname ¶
func (s *SQLiteStorage) GetResourceByHostname(ctx context.Context, hostname string, resourceType ResourceType) (*ManagedResource, error)
GetResourceByHostname retrieves a resource by hostname and type.
func (*SQLiteStorage) GetSyncState ¶
GetSyncState retrieves a sync state value.
func (*SQLiteStorage) Initialize ¶
func (s *SQLiteStorage) Initialize(ctx context.Context) error
Initialize creates tables and runs migrations.
func (*SQLiteStorage) ListAgents ¶
func (s *SQLiteStorage) ListAgents(ctx context.Context) ([]*Agent, error)
ListAgents lists all agents.
func (*SQLiteStorage) ListExpiredOrphans ¶
func (s *SQLiteStorage) ListExpiredOrphans(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
ListExpiredOrphans returns orphaned resources with cleanup_enabled=false whose updated_at is older than the given time (for orphan_ttl DB-only cleanup).
func (*SQLiteStorage) ListOrphanedForCleanup ¶
func (s *SQLiteStorage) ListOrphanedForCleanup(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
ListOrphanedForCleanup returns orphaned resources with cleanup_enabled=true whose updated_at is older than the given time (for remove_delay CF cleanup).
func (*SQLiteStorage) ListResources ¶
func (s *SQLiteStorage) ListResources(ctx context.Context, filter ResourceFilter) ([]*ManagedResource, error)
ListResources lists resources with optional filtering.
func (*SQLiteStorage) SaveAgent ¶
func (s *SQLiteStorage) SaveAgent(ctx context.Context, agent *Agent) error
SaveAgent creates or updates an agent.
func (*SQLiteStorage) SaveResource ¶
func (s *SQLiteStorage) SaveResource(ctx context.Context, resource *ManagedResource) error
SaveResource creates or updates a resource. Uses upsert based on (resource_type, hostname, record_type) unique constraint.
func (*SQLiteStorage) SetSyncState ¶
func (s *SQLiteStorage) SetSyncState(ctx context.Context, key, value string) error
SetSyncState sets a sync state value.
func (*SQLiteStorage) UpdateAgentStatus ¶
func (s *SQLiteStorage) UpdateAgentStatus(ctx context.Context, id string, connected bool, status AgentStatus) error
UpdateAgentStatus updates agent connection status.
func (*SQLiteStorage) UpdateResourceError ¶
func (s *SQLiteStorage) UpdateResourceError(ctx context.Context, id string, status ResourceStatus, lastError string) error
UpdateResourceError updates the status and last_error of a resource. Pass an empty lastError string to clear the error.
func (*SQLiteStorage) UpdateResourceStatus ¶
func (s *SQLiteStorage) UpdateResourceStatus(ctx context.Context, id string, status ResourceStatus) error
UpdateResourceStatus updates the status of a resource.
type Storage ¶
type Storage interface {
// Initialize initializes the storage (create tables, run migrations).
Initialize(ctx context.Context) error
// Close closes the storage connection.
Close() error
// Resource operations
GetResource(ctx context.Context, id string) (*ManagedResource, error)
GetResourceByHostname(ctx context.Context, hostname string, resourceType ResourceType) (*ManagedResource, error)
GetResourceByContainerService(ctx context.Context, containerID, serviceName string) (*ManagedResource, error)
ListResources(ctx context.Context, filter ResourceFilter) ([]*ManagedResource, error)
SaveResource(ctx context.Context, resource *ManagedResource) error
UpdateResourceStatus(ctx context.Context, id string, status ResourceStatus) error
UpdateResourceError(ctx context.Context, id string, status ResourceStatus, lastError string) error
DeleteResource(ctx context.Context, id string) error
// Agent operations
GetAgent(ctx context.Context, id string) (*Agent, error)
ListAgents(ctx context.Context) ([]*Agent, error)
SaveAgent(ctx context.Context, agent *Agent) error
UpdateAgentStatus(ctx context.Context, id string, connected bool, status AgentStatus) error
DeleteAgent(ctx context.Context, id string) error
// Sync state operations
GetSyncState(ctx context.Context, key string) (string, error)
SetSyncState(ctx context.Context, key, value string) error
// Maintenance operations
CleanupDeletedResources(ctx context.Context, before time.Time) (int64, error)
ListExpiredOrphans(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
ListOrphanedForCleanup(ctx context.Context, olderThan time.Time) ([]*ManagedResource, error)
Vacuum(ctx context.Context) error
}
Storage defines the interface for persistent storage.
type StorageError ¶
StorageError represents a storage error.
func (*StorageError) Error ¶
func (e *StorageError) Error() string