Documentation
¶
Overview ¶
Package store provides storage implementations for indexed Kubernetes resources.
This package offers two store types: - Memory store: Fast in-memory storage with complete resources - Cached store: Memory-efficient storage with API-backed retrieval and caching
Index ¶
- Constants
- type CachedStore
- func (s *CachedStore) Add(resource any, keys []string) error
- func (s *CachedStore) CacheSize() int
- func (s *CachedStore) Clear() error
- func (s *CachedStore) Delete(keys ...string) error
- func (s *CachedStore) EvictExpired() int
- func (s *CachedStore) Get(keys ...string) ([]any, error)
- func (s *CachedStore) List() ([]any, error)
- func (s *CachedStore) ModCount() (uint64, bool)
- func (s *CachedStore) Size() int
- func (s *CachedStore) Update(resource any, keys []string) error
- type CachedStoreConfig
- type MemoryStore
- func (s *MemoryStore) Add(resource any, keys []string) error
- func (s *MemoryStore) Clear() error
- func (s *MemoryStore) Delete(keys ...string) error
- func (s *MemoryStore) Get(keys ...string) ([]any, error)
- func (s *MemoryStore) List() ([]any, error)
- func (s *MemoryStore) ModCount() (uint64, bool)
- func (s *MemoryStore) Size() int
- func (s *MemoryStore) Update(resource any, keys []string) error
- type StoreError
Constants ¶
const DefaultMaxCacheSize = 256
DefaultMaxCacheSize is the default maximum number of entries in the LRU cache.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedStore ¶
type CachedStore struct {
// contains filtered or unexported fields
}
CachedStore stores only resource references in memory and fetches resources from the Kubernetes API on access. Fetched resources are cached with a TTL.
This reduces memory usage for large resources (e.g., Secrets) at the cost of API latency on cache misses.
Supports non-unique index keys by storing multiple resource references per composite key.
Thread-safe for concurrent access.
func NewCachedStore ¶
func NewCachedStore(cfg *CachedStoreConfig) (*CachedStore, error)
NewCachedStore creates a new API-backed store with caching.
func (*CachedStore) Add ¶
func (s *CachedStore) Add(resource any, keys []string) error
Add inserts a new resource into the store.
func (*CachedStore) CacheSize ¶
func (s *CachedStore) CacheSize() int
CacheSize returns the number of cached resources.
func (*CachedStore) Clear ¶
func (s *CachedStore) Clear() error
Clear removes all resources from the store.
func (*CachedStore) Delete ¶
func (s *CachedStore) Delete(keys ...string) error
Delete removes a resource from the store. NOTE: With non-unique index keys, this removes ALL resources matching the provided keys.
func (*CachedStore) EvictExpired ¶
func (s *CachedStore) EvictExpired() int
EvictExpired removes expired cache entries.
func (*CachedStore) Get ¶
func (s *CachedStore) Get(keys ...string) ([]any, error)
Get retrieves all resources matching the provided index keys.
func (*CachedStore) List ¶
func (s *CachedStore) List() ([]any, error)
List returns all resources in the store.
func (*CachedStore) ModCount ¶
func (s *CachedStore) ModCount() (uint64, bool)
ModCount returns the modification counter and whether tracking is supported. The counter is incremented on every mutation (Add, Update, Delete, Clear). This enables external caching layers to detect store changes without polling.
func (*CachedStore) Size ¶
func (s *CachedStore) Size() int
Size returns the number of tracked resources in the store.
type CachedStoreConfig ¶
type CachedStoreConfig struct {
// NumKeys is the number of index keys (must match indexer configuration)
NumKeys int
// CacheTTL is the cache entry time-to-live
CacheTTL time.Duration
// MaxCacheSize is the maximum number of entries in the LRU cache.
// When exceeded, the least recently used entry is evicted.
// Default: 256
MaxCacheSize int
// Client is the Kubernetes dynamic client for fetching resources
Client dynamic.Interface
// GVR identifies the resource type to fetch
GVR schema.GroupVersionResource
// Namespace restricts fetching to a specific namespace (empty = all namespaces)
Namespace string
// Indexer processes fetched resources (field filtering)
Indexer *indexer.Indexer
// Logger for debug and warning messages (optional, uses slog.Default if nil)
Logger *slog.Logger
}
CachedStoreConfig configures a CachedStore.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore stores complete Kubernetes resources in memory using nested maps.
This provides O(1) lookup performance at the cost of higher memory usage. Resources are stored with their full specification after field filtering.
Supports non-unique index keys by storing multiple resources per composite key.
Thread-safe for concurrent access.
Immutability Contract ¶
Resources stored in MemoryStore are pre-converted (floats to ints) at storage time and MUST NOT be mutated by callers. The slices returned by Get() are direct references to internal data structures for performance. Callers MUST NOT:
- Modify elements of returned slices
- Append to or reslice returned slices
- Modify fields within returned resources
Note: List() returns a fresh slice copy for thread safety, but the resource objects within are still references to internal data and must not be mutated.
func NewMemoryStore ¶
func NewMemoryStore(numKeys int) *MemoryStore
NewMemoryStore creates a new memory-backed store.
Parameters:
- numKeys: Number of index keys (must match indexer configuration)
func (*MemoryStore) Add ¶
func (s *MemoryStore) Add(resource any, keys []string) error
Add inserts a new resource into the store. If resources with the same index keys already exist, the new resource is appended. The slice is kept sorted by namespace/name for deterministic Get() results.
func (*MemoryStore) Clear ¶
func (s *MemoryStore) Clear() error
Clear removes all resources from the store.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(keys ...string) error
Delete removes a resource from the store. NOTE: With non-unique index keys, this method cannot identify which specific resource to delete when multiple resources have the same index keys. It removes ALL resources matching the provided keys. The watcher should call this with the resource's actual namespace+name as the index keys to delete a specific resource.
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(keys ...string) ([]any, error)
Get retrieves all resources matching the provided index keys.
Returns a direct reference to the internal slice for exact key matches. Callers MUST NOT modify the returned slice or its elements (see Immutability Contract).
For partial key matches, a new slice is constructed from matching entries and sorted for deterministic order.
func (*MemoryStore) List ¶
func (s *MemoryStore) List() ([]any, error)
List returns all resources in the store. Returns a fresh copy of all resources to avoid race conditions.
func (*MemoryStore) ModCount ¶
func (s *MemoryStore) ModCount() (uint64, bool)
ModCount returns the modification counter and whether tracking is supported. The counter is incremented on every mutation (Add, Update, Delete, Clear). This enables external caching layers to detect store changes without polling.
func (*MemoryStore) Size ¶
func (s *MemoryStore) Size() int
Size returns the number of resources in the store.
func (*MemoryStore) Update ¶
func (s *MemoryStore) Update(resource any, keys []string) error
Update modifies an existing resource or adds it if it doesn't exist. For non-unique index keys, it finds the resource by namespace+name and replaces it. The slice is kept sorted by namespace/name for deterministic Get() results.
type StoreError ¶
StoreError represents a generic store operation error.
func (*StoreError) Error ¶
func (e *StoreError) Error() string
func (*StoreError) Unwrap ¶
func (e *StoreError) Unwrap() error