Documentation
¶
Overview ¶
Package storage provides interfaces and implementations for persisting and retrieving pod data. It supports both local file-based storage with in-memory caching and file watching via fsnotify.
Index ¶
- type CachedLocalStorage
- func (f *CachedLocalStorage[T]) AddResource(_ context.Context, key types.ContainerID, resource T) error
- func (f *CachedLocalStorage[T]) GetAll(_ context.Context) ([]T, error)
- func (f *CachedLocalStorage[T]) GetResource(_ context.Context, key types.ContainerID) (T, error)
- func (f *CachedLocalStorage[T]) Initialize(ctx context.Context) error
- func (f *CachedLocalStorage[T]) RemoveResource(_ context.Context, key types.ContainerID) error
- func (f *CachedLocalStorage[T]) WaitUntilReady(ctx context.Context) error
- type MockStorage
- func (m *MockStorage[T]) AddResource(ctx context.Context, key types.ContainerID, resource T) error
- func (m *MockStorage[T]) GetAll(ctx context.Context) ([]T, error)
- func (m *MockStorage[T]) GetResource(ctx context.Context, key types.ContainerID) (T, error)
- func (m *MockStorage[T]) Initialize(_ context.Context) error
- func (m *MockStorage[T]) RemoveResource(ctx context.Context, key types.ContainerID) error
- func (m *MockStorage[T]) WaitUntilReady(_ context.Context) error
- type Storage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedLocalStorage ¶
CachedLocalStorage is a storage implementation that persists resources to the local filesystem and maintains an in-memory cache for fast access. Each resource is stored as a JSON file. The implementation is safe for concurrent access using RWMutex.
func NewCachedLocalStorage ¶
func NewCachedLocalStorage[T proto.Message](basePath string, newFunc func() T) *CachedLocalStorage[T]
NewCachedLocalStorage creates a new CachedLocalStorage instance. Resources are stored as individual JSON files in the specified basePath and cached in memory. The newFunc parameter is a factory function that creates new instances of type T for unmarshaling.
func (*CachedLocalStorage[T]) AddResource ¶
func (f *CachedLocalStorage[T]) AddResource(_ context.Context, key types.ContainerID, resource T) error
AddResource stores a resource atomically to disk and updates the in-memory cache. The resource is serialized as JSON and written atomically to ensure consistency.
func (*CachedLocalStorage[T]) GetAll ¶
func (f *CachedLocalStorage[T]) GetAll(_ context.Context) ([]T, error)
GetAll returns all cached resources as a slice. Resources must be initialized before calling this method.
func (*CachedLocalStorage[T]) GetResource ¶
func (f *CachedLocalStorage[T]) GetResource(_ context.Context, key types.ContainerID) (T, error)
GetResource retrieves a resource by key, checking the in-memory cache first. If not in cache, it loads the resource from disk and caches it for future access.
func (*CachedLocalStorage[T]) Initialize ¶
func (f *CachedLocalStorage[T]) Initialize(ctx context.Context) error
Initialize loads all resources from disk into the in-memory cache. It is safe to call multiple times; only the first call performs initialization. Subsequent calls return the result of the first initialization.
func (*CachedLocalStorage[T]) RemoveResource ¶
func (f *CachedLocalStorage[T]) RemoveResource(_ context.Context, key types.ContainerID) error
RemoveResource deletes a resource from disk and the in-memory cache. It does not return an error if the resource file does not exist.
func (*CachedLocalStorage[T]) WaitUntilReady ¶
func (f *CachedLocalStorage[T]) WaitUntilReady(ctx context.Context) error
WaitUntilReady blocks until the cache is populated from disk or the context is canceled. It should be called before using other methods to ensure all resources are loaded.
type MockStorage ¶
type MockStorage[T proto.Message] struct { // GetAllFunc is called by GetAll. If nil, GetAll iterates the resources map. GetAllFunc func(ctx context.Context) ([]T, error) // AddResourceFunc is called by AddResource. If nil, the resource is stored in the map. AddResourceFunc func(ctx context.Context, key types.ContainerID, resource T) error // GetResourceFunc is called by GetResource. If nil, the resource is looked up in the map. // Return os.ErrNotExist (or any error satisfying os.IsNotExist) to simulate a missing file. GetResourceFunc func(ctx context.Context, key types.ContainerID) (T, error) // RemoveResourceFunc is called by RemoveResource. If nil, the resource is deleted from the map. RemoveResourceFunc func(ctx context.Context, key types.ContainerID) error // contains filtered or unexported fields }
MockStorage is an in-memory Storage implementation intended for use in tests outside the storage package. Because Storage contains the unexported loadAll method, types in other packages cannot implement the interface directly; MockStorage bridges that gap by living in package storage while exposing configurable behaviour via exported function fields.
Like LocalStorage — the implementation it stands in for — MockStorage is safe for concurrent use. Production callers (the CNI server's liveness loop, ghost sweep and drain timers) read and write storage from several goroutines, so a double without that guarantee turns any test of those paths into a data race on the backing map rather than a test of the code under test. The configurable function fields are NOT guarded: set them before the storage is shared.
func NewMockStorage ¶
func NewMockStorage[T proto.Message]() *MockStorage[T]
NewMockStorage returns a MockStorage with an empty resource map.
func NewMockStorageWithGetAll ¶
func NewMockStorageWithGetAll[T proto.Message](fn func(ctx context.Context) ([]T, error)) *MockStorage[T]
NewMockStorageWithGetAll returns a MockStorage whose GetAll method is backed by the provided function. Use this when test cases need precise control over the data returned by GetAll, including error injection.
func (*MockStorage[T]) AddResource ¶
func (m *MockStorage[T]) AddResource(ctx context.Context, key types.ContainerID, resource T) error
func (*MockStorage[T]) GetResource ¶
func (m *MockStorage[T]) GetResource(ctx context.Context, key types.ContainerID) (T, error)
func (*MockStorage[T]) Initialize ¶
func (m *MockStorage[T]) Initialize(_ context.Context) error
func (*MockStorage[T]) RemoveResource ¶
func (m *MockStorage[T]) RemoveResource(ctx context.Context, key types.ContainerID) error
func (*MockStorage[T]) WaitUntilReady ¶
func (m *MockStorage[T]) WaitUntilReady(_ context.Context) error
type Storage ¶
type Storage[T proto.Message] interface { // Initialize prepares the storage for use, including creating directories // and loading initial data if necessary. Initialize(ctx context.Context) error // WaitUntilReady waits until the storage is ready to serve requests. // This may involve waiting for file watching mechanisms to be initialized. WaitUntilReady(ctx context.Context) error // AddResource stores a new resource with the given key. AddResource(ctx context.Context, key types.ContainerID, resource T) error // RemoveResource deletes a resource by key. RemoveResource(ctx context.Context, key types.ContainerID) error // GetResource retrieves a single resource by key. GetResource(ctx context.Context, key types.ContainerID) (T, error) // GetAll returns all stored resources from the in-memory cache. GetAll(_ context.Context) ([]T, error) // contains filtered or unexported methods }
Storage defines an interface for storing and retrieving protobuf messages. Implementations manage the lifecycle of resources and maintain consistency between persistent storage and in-memory caches.