storage

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: 9 Imported by: 0

Documentation

Overview

Package storage provides persistent storage for labelgate.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = &StorageError{Code: "not_found", Message: "resource not found"}
	ErrConflict = &StorageError{Code: "conflict", Message: "resource already exists"}
)

Common errors

Functions

func IsConflict

func IsConflict(err error) bool

IsConflict checks if the error is a conflict error.

func IsNotFound

func IsNotFound(err error) bool

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 Migration

type Migration struct {
	Version int
	SQL     string
}

Migration represents a database migration.

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) GetAgent

func (s *SQLiteStorage) GetAgent(ctx context.Context, id string) (*Agent, error)

GetAgent retrieves an agent 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

func (s *SQLiteStorage) GetSyncState(ctx context.Context, key string) (string, error)

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.

func (*SQLiteStorage) Vacuum

func (s *SQLiteStorage) Vacuum(ctx context.Context) error

Vacuum performs database vacuum.

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

type StorageError struct {
	Code    string
	Message string
}

StorageError represents a storage error.

func (*StorageError) Error

func (e *StorageError) Error() string

Jump to

Keyboard shortcuts

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