store

package
v0.0.1-dev.12 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 12 Imported by: 0

README

Rune State Store

The state store package provides persistent storage for Rune's resources, supporting CRUD operations, versioning, watching for changes, and transactional updates.

Overview

The store package implements a flexible, extensible storage interface with BadgerDB as the primary implementation. The store provides:

  • CRUD operations for any resource type
  • Namespaced resources
  • Resource versioning and history
  • Atomic transactions
  • Watch mechanisms for reactive patterns

Interface

The core Store interface provides the following operations:

// Store defines the interface for state storage
type Store interface {
    Open(path string) error
    Close() error
    Create(ctx context.Context, resourceType string, namespace string, name string, resource interface{}) error
    Get(ctx context.Context, resourceType string, namespace string, name string, resource interface{}) error
    List(ctx context.Context, resourceType string, namespace string) ([]interface{}, error)
    Update(ctx context.Context, resourceType string, namespace string, name string, resource interface{}) error
    Delete(ctx context.Context, resourceType string, namespace string, name string) error
    Watch(ctx context.Context, resourceType string, namespace string) (<-chan WatchEvent, error)
    Transaction(ctx context.Context, fn func(tx Transaction) error) error
    GetHistory(ctx context.Context, resourceType string, namespace string, name string) ([]HistoricalVersion, error)
    GetVersion(ctx context.Context, resourceType string, namespace string, name string, version string) (interface{}, error)
}

Repositories (Adapters)

To provide a clean, testable API per resource without duplicating storage logic, we introduce small, typed repository adapters over the single core Store. These repos accept a standard reference (ref) string for addressing resources and delegate to the core store using consistent keys and versioning from util.go.

  • One DB (Badger), one Store implementation
  • Repos live under pkg/store/repos/
  • Secrets are encrypted at rest via a codec within the core store (transparent to callers)
Resource Reference (Ref)

Canonical URI form:

  • Name-based: rune://<type>/<namespace>/<name>
  • ID-based: rune://<type>/<namespace>/id/<id>

Shorthand FQDNs like secret:db-credentials.prod.rune are also supported by the parser.

Helpers:

  • ParseRef(string) (Ref, error)
  • FormatRef(rt, ns, name string) string
Example (SecretRepo)
core := store.NewBadgerStore(logger)
_ = core.Open("/path/to/data")
defer core.Close()

secRepo := repos.NewSecretRepo(core)

ref := repos.FormatRef(types.ResourceTypeSecret, "prod", "db-credentials")
secret := &types.Secret{Name: "db-credentials", Namespace: "prod", Type: "static", Data: map[string]string{"username":"admin","password":"s3cr3t"}}

if err := secRepo.Create(ctx, ref, secret); err != nil { /* handle */ }
got, err := secRepo.Get(ctx, ref)
Base Repo

BaseRepo[T] provides common Create/Get/Update/Delete on top of the core Store. Resource-specific repos compose it and add behavior only as needed. Secrets use transparent encryption at rest; configs use default JSON.

Usage

Creating a Store
import (
    "context"
    "log"
    "razorbill.dev/rune/pkg/store"
)

// Create a BadgerDB store
logger := log.New(os.Stdout, "", log.LstdFlags)
s := store.NewBadgerStore(logger)

// Open the store
if err := s.Open("/path/to/data"); err != nil {
    log.Fatalf("Failed to open store: %v", err)
}
defer s.Close()
Working with Resources
// Define a resource
type Service struct {
    Name  string   `json:"name"`
    Image string   `json:"image"`
    Ports []int    `json:"ports"`
}

// Create a resource
service := Service{
    Name:  "my-service",
    Image: "nginx:latest",
    Ports: []int{80, 443},
}

ctx := context.Background()
if err := s.Create(ctx, "services", "default", "my-service", service); err != nil {
    log.Fatalf("Failed to create service: %v", err)
}

// Get a resource
var retrievedService Service
if err := s.Get(ctx, "services", "default", "my-service", &retrievedService); err != nil {
    log.Fatalf("Failed to get service: %v", err)
}

// Update a resource
service.Image = "nginx:1.19"
if err := s.Update(ctx, "services", "default", "my-service", service); err != nil {
    log.Fatalf("Failed to update service: %v", err)
}

// List resources
services, err := s.List(ctx, "services", "default")
if err != nil {
    log.Fatalf("Failed to list services: %v", err)
}

// Delete a resource
if err := s.Delete(ctx, "services", "default", "my-service"); err != nil {
    log.Fatalf("Failed to delete service: %v", err)
}
Transactions
// Execute multiple operations in a transaction
err := s.Transaction(ctx, func(tx store.Transaction) error {
    // Create a service
    service := Service{Name: "service-1", Image: "image-1"}
    if err := tx.Create("services", "default", "service-1", service); err != nil {
        return err
    }
    
    // Create another service
    service2 := Service{Name: "service-2", Image: "image-2"}
    if err := tx.Create("services", "default", "service-2", service2); err != nil {
        return err
    }
    
    return nil
})
Watching for Changes
// Watch for changes to services in the default namespace
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

events, err := s.Watch(ctx, "services", "default")
if err != nil {
    log.Fatalf("Failed to set up watch: %v", err)
}

go func() {
    for event := range events {
        switch event.Type {
        case store.WatchEventCreated:
            log.Printf("Service created: %s", event.Name)
        case store.WatchEventUpdated:
            log.Printf("Service updated: %s", event.Name)
        case store.WatchEventDeleted:
            log.Printf("Service deleted: %s", event.Name)
        }
    }
}()
Version History
// Get version history
history, err := s.GetHistory(ctx, "services", "default", "my-service")
if err != nil {
    log.Fatalf("Failed to get history: %v", err)
}

// Get a specific version
version := history[1].Version
oldVersion, err := s.GetVersion(ctx, "services", "default", "my-service", version)
if err != nil {
    log.Fatalf("Failed to get version: %v", err)
}

Implementation Notes

  • Resources are stored as serialized JSON (secrets are encrypted at rest transparently)
  • Keys are organized hierarchically by resource type, namespace, and name
  • Versions are stored separately with metadata and timestamps
  • Watch events are distributed through an in-memory pub/sub system
Resource Types

The core recognizes standard types, including:

  • service, instance, namespace, scaling_operation, deletion_operation
  • secret (encrypted at rest), config

Future Enhancements

  • Addition of a metrics subsystem for monitoring store operations
  • Advanced query capabilities with filtering and sorting
  • Pluggable serialization formats
  • Integration with external storage systems (etcd, etc.)

Testing

TestStore

The package includes a TestStore implementation specifically designed for testing:

import (
    "context"
    "testing"
    "github.com/rzbill/rune/pkg/store"
    "github.com/rzbill/rune/pkg/types"
)

func TestMyFunction(t *testing.T) {
    // Create a new TestStore
    testStore := store.NewTestStore()
    
    // Use the convenience methods to add test data
    ctx := context.Background()
    
    // Create test services and instances
    service := &types.Service{
        ID:        "test-service",
        Name:      "test-service",
        Namespace: "default",
    }
    
    instance := &types.Instance{
        ID:        "test-instance",
        Name:      "test-instance",
        Namespace: "default",
        ServiceID: "test-service",
    }
    
    // Add to store with helper methods
    testStore.CreateService(ctx, service)
    testStore.CreateInstance(ctx, instance)
    
    // Function to test
    result := MyFunctionThatUsesStore(testStore)
    
    // Assertions
    // ...
}
Advantages of TestStore

The TestStore offers several advantages for testing:

  1. No external dependencies: Works in-memory without need for file system access
  2. Built-in helper methods: Simplified creation and retrieval of common types
  3. Type-aware: Handles type copying and conversion automatically
  4. Reset capability: Easily clear all data between tests
  5. Simplified setup: Directly load test data with SetupTestData method
Mock vs TestStore

While the package also includes a MockStore based on testify/mock, TestStore is often more convenient:

  • Use MockStore when you need to verify specific method calls or simulate errors
  • Use TestStore when you need a working store implementation with real data

The TestStore implementation is more natural for integration testing, while MockStore is better for strict unit testing with precise expectations.

Documentation

Overview

Package store provides a state storage interface and implementations for the Rune platform.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAlreadyExistsError

func IsAlreadyExistsError(err error) bool

IsAlreadyExistsError checks if an error is an already exists error.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError checks if an error is a not found error.

func MakeKey

func MakeKey(resourceType types.ResourceType, namespace, name string) []byte

MakeKey creates a standardized key for a resource.

func MakePrefix

func MakePrefix(resourceType types.ResourceType, namespace string) []byte

MakePrefix creates a prefix for listing resources by type and namespace.

func MakeVersionKey

func MakeVersionKey(resourceType types.ResourceType, namespace, name, version string) []byte

MakeVersionKey creates a standardized key for a resource version.

func MakeVersionPrefix

func MakeVersionPrefix(resourceType types.ResourceType, namespace, name string) []byte

MakeVersionPrefix creates a prefix for listing resource versions.

func ParseKey

func ParseKey(key []byte) (resourceType, namespace, name string, ok bool)

ParseKey parses a key into its components.

func ParseVersionKey

func ParseVersionKey(key []byte) (resourceType, namespace, name, version string, ok bool)

ParseVersionKey parses a version key into its components.

func ResourceTypes

func ResourceTypes() []string

ResourceTypes returns a slice of the standard resource types used in the system.

func UnmarshalResource

func UnmarshalResource(source interface{}, target interface{}) error

UnmarshalResource converts a resource interface to a target type using JSON marshaling/unmarshaling. This is useful for converting between different types that share the same JSON structure. The function handles the conversion by first marshaling the source to JSON and then unmarshaling into the target type.

Types

type BadgerStore

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

BadgerStore implements the Store interface using BadgerDB.

func NewBadgerStore

func NewBadgerStore(logger log.Logger) *BadgerStore

NewBadgerStore creates a new BadgerDB-backed store.

func NewBadgerStoreWithOptions

func NewBadgerStoreWithOptions(logger log.Logger, opts StoreOptions) *BadgerStore

NewBadgerStoreWithOptions creates a BadgerStore with options

func (*BadgerStore) Close

func (s *BadgerStore) Close() error

Close closes the BadgerDB database.

func (*BadgerStore) Create

func (s *BadgerStore) Create(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Create creates a new resource.

func (*BadgerStore) CreateResource

func (s *BadgerStore) CreateResource(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

func (*BadgerStore) Delete

func (s *BadgerStore) Delete(ctx context.Context, resourceType types.ResourceType, namespace string, name string) error

Delete deletes a resource.

func (*BadgerStore) Get

func (s *BadgerStore) Get(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Get retrieves a resource.

func (*BadgerStore) GetHistory

func (s *BadgerStore) GetHistory(ctx context.Context, resourceType types.ResourceType, namespace string, name string) ([]HistoricalVersion, error)

GetHistory retrieves historical versions of a resource.

func (*BadgerStore) GetInstanceByID

func (s *BadgerStore) GetInstanceByID(ctx context.Context, namespace string, instanceID string) (*types.Instance, error)

GetInstance retrieves an instance by namespace and instanceID.

func (*BadgerStore) GetLimits

func (s *BadgerStore) GetLimits() (Limits, Limits)

GetLimits returns configured limits for secrets and configs

func (*BadgerStore) GetOpts

func (s *BadgerStore) GetOpts() StoreOptions

GetOpts returns the configured store options

func (*BadgerStore) GetVersion

func (s *BadgerStore) GetVersion(ctx context.Context, resourceType types.ResourceType, namespace string, name string, version string) (interface{}, error)

GetVersion retrieves a specific version of a resource.

func (*BadgerStore) List

func (s *BadgerStore) List(ctx context.Context, resourceType types.ResourceType, namespace string, resource interface{}) error

List retrieves all resources of a given type in a namespace.

func (*BadgerStore) ListAll

func (s *BadgerStore) ListAll(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

ListAll retrieves all resources of a given type in all namespaces.

func (*BadgerStore) Open

func (s *BadgerStore) Open(path string) error

Open opens the BadgerDB database.

func (*BadgerStore) Transaction

func (s *BadgerStore) Transaction(ctx context.Context, fn func(tx Transaction) error) error

Transaction executes multiple operations in a single transaction.

func (*BadgerStore) Update

func (s *BadgerStore) Update(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}, opts ...UpdateOption) error

Update updates an existing resource.

func (*BadgerStore) Watch

func (s *BadgerStore) Watch(ctx context.Context, resourceType types.ResourceType, namespace string) (<-chan WatchEvent, error)

Watch sets up a watch for changes to resources of a given type.

type BadgerTransaction

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

BadgerTransaction implements the Transaction interface.

func (*BadgerTransaction) Create

func (t *BadgerTransaction) Create(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Create creates a resource within the transaction.

func (*BadgerTransaction) Delete

func (t *BadgerTransaction) Delete(resourceType types.ResourceType, namespace string, name string) error

Delete deletes a resource within the transaction.

func (*BadgerTransaction) Get

func (t *BadgerTransaction) Get(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Get retrieves a resource within the transaction.

func (*BadgerTransaction) Update

func (t *BadgerTransaction) Update(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Update updates a resource within the transaction.

type EventSource

type EventSource string
const (
	EventSourceOrchestrator     EventSource = "orchestrator"
	EventSourceAPI              EventSource = "api"
	EventSourceReconciler       EventSource = "reconciler"
	EventSourceHealthController EventSource = "health-controller"
)

type HistoricalVersion

type HistoricalVersion struct {
	// Version is the version identifier.
	Version string

	// Timestamp is when this version was created.
	Timestamp time.Time

	// Resource is the resource data for this version.
	Resource interface{}
}

HistoricalVersion represents a historical version of a resource.

type Limits

type Limits struct {
	MaxObjectBytes   int
	MaxKeyNameLength int
}

Limits defines per-resource limits

type MemoryStore

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

MemoryStore is a simple in-memory implementation of the Store interface for testing.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory store.

func (*MemoryStore) Close

func (m *MemoryStore) Close() error

Close closes the memory store.

func (*MemoryStore) Create

func (m *MemoryStore) Create(ctx context.Context, resourceType types.ResourceType, namespace, name string, value interface{}) error

Create creates an object in the memory store.

func (*MemoryStore) CreateResource

func (m *MemoryStore) CreateResource(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(ctx context.Context, resourceType types.ResourceType, namespace, name string) error

Delete deletes an object from the memory store.

func (*MemoryStore) EnsureNamespace

func (m *MemoryStore) EnsureNamespace(resourceType types.ResourceType, namespace string)

EnsureNamespace ensures that a namespace exists for a resource type

func (*MemoryStore) EnsureResourceType

func (m *MemoryStore) EnsureResourceType(resourceType types.ResourceType)

EnsureResourceType ensures that a resource type exists in the store

func (*MemoryStore) Get

func (m *MemoryStore) Get(ctx context.Context, resourceType types.ResourceType, namespace, name string, value interface{}) error

Get retrieves an object from the memory store.

func (*MemoryStore) GetHistory

func (m *MemoryStore) GetHistory(ctx context.Context, resourceType types.ResourceType, namespace, name string) ([]HistoricalVersion, error)

func (*MemoryStore) GetInstanceByID

func (m *MemoryStore) GetInstanceByID(ctx context.Context, namespace, instanceID string) (*types.Instance, error)

GetInstance retrieves an instance by ID.

func (*MemoryStore) GetOpts

func (m *MemoryStore) GetOpts() StoreOptions

GetOpts returns zero-value options for memory store

func (*MemoryStore) GetVersion

func (m *MemoryStore) GetVersion(ctx context.Context, resourceType types.ResourceType, namespace, name, version string) (interface{}, error)

func (*MemoryStore) List

func (m *MemoryStore) List(ctx context.Context, resourceType types.ResourceType, namespace string, value interface{}) error

List lists objects from the memory store.

func (*MemoryStore) ListAll

func (m *MemoryStore) ListAll(ctx context.Context, resourceType types.ResourceType, value interface{}) error

func (*MemoryStore) Open

func (m *MemoryStore) Open(dbPath string) error

Open initializes the memory store.

func (*MemoryStore) Transaction

func (m *MemoryStore) Transaction(ctx context.Context, fn func(ctx Transaction) error) error

func (*MemoryStore) Update

func (m *MemoryStore) Update(ctx context.Context, resourceType types.ResourceType, namespace, name string, value interface{}, opts ...UpdateOption) error

Update updates an object in the memory store.

func (*MemoryStore) Watch

func (m *MemoryStore) Watch(ctx context.Context, resourceType types.ResourceType, namespace string) (<-chan WatchEvent, error)

type MockStore

type MockStore struct {
	mock.Mock
}

func (*MockStore) Close

func (m *MockStore) Close() error

func (*MockStore) Create

func (m *MockStore) Create(ctx context.Context, resourceType, namespace, name string, value interface{}) error

func (*MockStore) Delete

func (m *MockStore) Delete(ctx context.Context, resourceType, namespace, name string) error

func (*MockStore) Get

func (m *MockStore) Get(ctx context.Context, resourceType, namespace, name string, value interface{}) error

func (*MockStore) GetHistory

func (m *MockStore) GetHistory(ctx context.Context, resourceType, namespace, name string) ([]HistoricalVersion, error)

func (*MockStore) GetVersion

func (m *MockStore) GetVersion(ctx context.Context, resourceType, namespace, name, version string) (interface{}, error)

func (*MockStore) List

func (m *MockStore) List(ctx context.Context, resourceType, namespace string) ([]interface{}, error)

func (*MockStore) Open

func (m *MockStore) Open(dbPath string) error

func (*MockStore) Transaction

func (m *MockStore) Transaction(ctx context.Context, fn func(ctx Transaction) error) error

func (*MockStore) Update

func (m *MockStore) Update(ctx context.Context, resourceType, namespace, name string, value interface{}) error

func (*MockStore) Watch

func (m *MockStore) Watch(ctx context.Context, resourceType, namespace string) (<-chan WatchEvent, error)

type Store

type Store interface {
	// Open initializes and opens the store.
	Open(path string) error

	// Close closes the store and releases resources.
	Close() error

	// Create creates a new resource.
	Create(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

	// CreateResource creates a new resource.
	CreateResource(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

	// Get retrieves a resource by type, namespace, and name.
	Get(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

	// GetInstanceByID retrieves an instance by namespace and instanceID.
	GetInstanceByID(ctx context.Context, namespace string, instanceID string) (*types.Instance, error)

	// List retrieves all resources of a given type in a namespace.
	List(ctx context.Context, resourceType types.ResourceType, namespace string, resource interface{}) error

	// ListAll retrieves all resources of a given type in all namespaces.
	ListAll(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

	// Update updates an existing resource.
	Update(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}, opts ...UpdateOption) error

	// Delete deletes a resource.
	Delete(ctx context.Context, resourceType types.ResourceType, namespace string, name string) error

	// Watch sets up a watch for changes to resources of a given type.
	Watch(ctx context.Context, resourceType types.ResourceType, namespace string) (<-chan WatchEvent, error)

	// Transaction executes multiple operations in a single transaction.
	Transaction(ctx context.Context, fn func(tx Transaction) error) error

	// GetHistory retrieves historical versions of a resource.
	GetHistory(ctx context.Context, resourceType types.ResourceType, namespace string, name string) ([]HistoricalVersion, error)

	// GetVersion retrieves a specific version of a resource.
	GetVersion(ctx context.Context, resourceType types.ResourceType, namespace string, name string, version string) (interface{}, error)

	// GetOpts returns the store options/config in use
	GetOpts() StoreOptions
}

Store defines the interface for state storage operations.

type StoreOptions

type StoreOptions struct {
	// Path is the directory path for Badger (if separate from Open arg)
	Path string

	// SecretEncryptionEnabled toggles secret at-rest encryption availability
	SecretEncryptionEnabled bool

	// KEKOptions configures loading the master key for wrapping secret DEKs
	KEKOptions *crypto.KEKOptions

	// KEKBytes is a precomputed KEK for tests
	KEKBytes []byte

	// Limits for secrets and configs
	SecretLimits Limits
	ConfigLimits Limits
}

StoreOptions configure the core store

type TestStore

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

TestStore provides a simple in-memory implementation for testing purposes. Unlike MockStore, it doesn't require setting up expectations and is more convenient for basic tests that need a functional store.

func NewTestStore

func NewTestStore() *TestStore

NewTestStore creates a new TestStore instance.

func NewTestStoreWithOptions

func NewTestStoreWithOptions(opts StoreOptions) *TestStore

NewTestStoreWithOptions creates a new TestStore instance with options

func (*TestStore) Close

func (s *TestStore) Close() error

Close implements the Store interface.

func (*TestStore) Create

func (s *TestStore) Create(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Create implements the Store interface.

func (*TestStore) CreateInstance

func (s *TestStore) CreateInstance(ctx context.Context, instance *types.Instance) error

CreateInstance adds an instance to the test store

func (*TestStore) CreateResource

func (s *TestStore) CreateResource(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

func (*TestStore) CreateService

func (s *TestStore) CreateService(ctx context.Context, service *types.Service) error

CreateService adds a service to the test store

func (*TestStore) Delete

func (s *TestStore) Delete(ctx context.Context, resourceType types.ResourceType, namespace string, name string) error

Delete implements the Store interface.

func (*TestStore) Get

func (s *TestStore) Get(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}) error

Get implements the Store interface.

func (*TestStore) GetHistory

func (s *TestStore) GetHistory(ctx context.Context, resourceType types.ResourceType, namespace string, name string) ([]HistoricalVersion, error)

GetHistory implements the Store interface.

func (*TestStore) GetInstanceByID

func (s *TestStore) GetInstanceByID(ctx context.Context, namespace, id string) (*types.Instance, error)

GetInstance retrieves an instance from the test store

func (*TestStore) GetOpts

func (s *TestStore) GetOpts() StoreOptions

GetOpts returns test store options

func (*TestStore) GetService

func (s *TestStore) GetService(ctx context.Context, namespace, name string) (*types.Service, error)

GetService retrieves a service from the test store

func (*TestStore) GetVersion

func (s *TestStore) GetVersion(ctx context.Context, resourceType types.ResourceType, namespace string, name string, version string) (interface{}, error)

GetVersion implements the Store interface.

func (*TestStore) List

func (s *TestStore) List(ctx context.Context, resourceType types.ResourceType, namespace string, resource interface{}) error

List implements the Store interface.

func (*TestStore) ListAll

func (s *TestStore) ListAll(ctx context.Context, resourceType types.ResourceType, resource interface{}) error

ListAll retrieves all resources of a given type in all namespaces.

func (*TestStore) ListInstances

func (s *TestStore) ListInstances(ctx context.Context, namespace string) ([]types.Instance, error)

ListInstances returns all instances in a namespace

func (*TestStore) ListServices

func (s *TestStore) ListServices(ctx context.Context, namespace string) ([]*types.Service, error)

ListServices returns all services in a namespace

func (*TestStore) Open

func (s *TestStore) Open(path string) error

Open implements the Store interface.

func (*TestStore) Reset

func (s *TestStore) Reset()

Reset clears all data in the store

func (*TestStore) SetupTestData

func (s *TestStore) SetupTestData(resources map[types.ResourceType]map[string]map[string]interface{}) error

SetupTestData adds predefined test data to the store

func (*TestStore) Transaction

func (s *TestStore) Transaction(ctx context.Context, fn func(tx Transaction) error) error

Transaction implements the Store interface.

func (*TestStore) Update

func (s *TestStore) Update(ctx context.Context, resourceType types.ResourceType, namespace string, name string, resource interface{}, opts ...UpdateOption) error

Update implements the Store interface.

func (*TestStore) Watch

func (s *TestStore) Watch(ctx context.Context, resourceType types.ResourceType, namespace string) (<-chan WatchEvent, error)

Watch implements the Store interface.

type Transaction

type Transaction interface {
	// Create creates a new resource within the transaction.
	Create(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

	// Get retrieves a resource within the transaction.
	Get(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

	// Update updates a resource within the transaction.
	Update(resourceType types.ResourceType, namespace string, name string, resource interface{}) error

	// Delete deletes a resource within the transaction.
	Delete(resourceType types.ResourceType, namespace string, name string) error
}

Transaction represents a store transaction.

type UpdateOption

type UpdateOption func(*UpdateOptions)

UpdateOption is a function that configures update options

func WithHealthController

func WithHealthController() UpdateOption

WithHealthController marks an update as originating from the health controller

func WithOrchestrator

func WithOrchestrator() UpdateOption

WithOrchestrator marks an update as originating from the orchestrator

func WithReconciler

func WithReconciler() UpdateOption

WithReconciler marks an update as originating from the reconciler

func WithSource

func WithSource(source EventSource) UpdateOption

WithSource adds a source identifier to an update operation

type UpdateOptions

type UpdateOptions struct {
	// Source identifies the origin of an update
	Source EventSource
}

UpdateOptions contains settings for update operations

func ParseUpdateOptions

func ParseUpdateOptions(opts ...UpdateOption) UpdateOptions

ParseUpdateOptions builds an UpdateOptions struct from a list of option functions

type WatchEvent

type WatchEvent struct {
	// Type is the type of event (created, updated, deleted).
	Type WatchEventType

	// ResourceType is the type of resource affected.
	ResourceType types.ResourceType

	// Namespace is the namespace of the resource.
	Namespace string

	// Name is the name of the resource.
	Name string

	// Resource is the resource data.
	Resource interface{}

	// Source identifies who triggered this change (empty for external changes)
	Source EventSource
}

WatchEvent represents a change to a resource.

type WatchEventType

type WatchEventType string

WatchEventType defines the type of watch event.

const (
	// WatchEventCreated indicates a resource was created.
	WatchEventCreated WatchEventType = "CREATED"

	// WatchEventUpdated indicates a resource was updated.
	WatchEventUpdated WatchEventType = "UPDATED"

	// WatchEventDeleted indicates a resource was deleted.
	WatchEventDeleted WatchEventType = "DELETED"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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