resourcestore

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package resourcestore provides centralized management of resource stores with memory-efficient overlay support for dry-run validation.

This package enables webhook validation to simulate resource changes without copying entire stores, using a shadow/overlay pattern that only tracks deltas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SingularizeResourceType

func SingularizeResourceType(plural string) string

SingularizeResourceType converts a plural resource type to singular kind.

This is a simple heuristic that handles common English pluralization rules. For proper kind resolution, use RESTMapper.KindFor().

Examples:

  • "ingresses" → "Ingress"
  • "services" → "Service"
  • "pods" → "Pod"
  • "configmaps" → "ConfigMap"

Types

type Manager

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

Manager provides centralized management of resource stores.

This is a pure component with no event dependencies. It acts as a registry for all resource stores in the controller, allowing components to access stores and create overlay stores for dry-run operations.

Thread-safe for concurrent access from multiple components.

func NewManager

func NewManager() *Manager

NewManager creates a new resource store manager.

func (*Manager) CreateOverlay

func (m *Manager) CreateOverlay(resourceType, namespace, name string, obj interface{}, op Operation) (Store, error)

CreateOverlay creates an overlay store that simulates a resource change.

The overlay store wraps the base store and only tracks the delta (single changed resource). This is memory-efficient: O(1) space regardless of the number of resources in the base store.

Parameters:

  • resourceType: The resource type (e.g., "ingresses")
  • namespace: Resource namespace (empty for cluster-scoped)
  • name: Resource name
  • obj: The resource object (for CREATE/UPDATE) or nil (for DELETE)
  • op: The operation type (CREATE, UPDATE, DELETE)

Returns:

  • An overlay store wrapping the base store with the simulated change
  • An error if the base store doesn't exist

Example:

// Simulate updating an ingress
overlay, err := manager.CreateOverlay("ingresses", "default", "my-ing", updatedIngress, OperationUpdate)
if err != nil {
    return err
}

// Use overlay for dry-run validation
resources, _ := overlay.List() // Includes updated ingress

func (*Manager) CreateOverlayMap

func (m *Manager) CreateOverlayMap(resourceType, namespace, name string, obj interface{}, op Operation) (map[string]Store, error)

CreateOverlayMap creates a map of stores with one replaced by an overlay.

This is useful for dry-run reconciliation where all stores are needed but one should simulate a resource change.

Parameters:

  • resourceType: The resource type to overlay
  • namespace: Resource namespace
  • name: Resource name
  • obj: The resource object
  • op: The operation type

Returns:

  • A map of all stores with the specified one replaced by an overlay
  • An error if the overlay creation fails

Example:

// Get all stores with ingress store overlaid
stores, err := manager.CreateOverlayMap("ingresses", "default", "my-ing", updatedIngress, OperationUpdate)
if err != nil {
    return err
}

// Use for dry-run reconciliation
err = executor.DryRunReconcile(ctx, stores)

func (*Manager) GetAllStores

func (m *Manager) GetAllStores() map[string]Store

GetAllStores returns a copy of all registered stores.

Returns:

  • A map of resource type to store

The returned map is a shallow copy, safe for iteration without holding locks.

func (*Manager) GetStore

func (m *Manager) GetStore(resourceType string) (Store, bool)

GetStore retrieves a registered store for a resource type.

Parameters:

  • resourceType: The resource type identifier

Returns:

  • The store for the resource type
  • A boolean indicating whether the store exists

func (*Manager) RegisterStore

func (m *Manager) RegisterStore(resourceType string, store Store)

RegisterStore registers a resource store for a given resource type.

Parameters:

  • resourceType: The resource type identifier (e.g., "ingresses", "services")
  • store: The store implementation to register

If a store for the resource type already exists, it will be replaced.

func (*Manager) ResourceCount

func (m *Manager) ResourceCount() int

ResourceCount returns the number of registered stores.

type Operation

type Operation string

Operation represents the type of resource operation in an overlay.

const (
	// OperationCreate indicates a new resource is being added.
	OperationCreate Operation = "CREATE"

	// OperationUpdate indicates an existing resource is being modified.
	OperationUpdate Operation = "UPDATE"

	// OperationDelete indicates a resource is being removed.
	OperationDelete Operation = "DELETE"
)

type OverlayStore

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

OverlayStore wraps a base store and simulates a single resource change.

This implements the shadow/overlay pattern for memory-efficient dry-run validation. Instead of copying the entire store, it only tracks the delta: - For CREATE/UPDATE: stores the single changed resource - For DELETE: marks the resource as deleted

Memory usage: O(1) - only the single changed resource (~10KB) Performance: O(1) for Get with matching key, O(n) for List where n = base store size

The overlay is immutable after creation and thread-safe for reads.

func NewOverlayStore

func NewOverlayStore(baseStore Store, namespace, name string, obj interface{}, op Operation) *OverlayStore

NewOverlayStore creates a new overlay store.

Parameters:

  • baseStore: The underlying store to wrap (not copied)
  • namespace: Namespace of the changed resource
  • name: Name of the changed resource
  • obj: The resource object (nil for DELETE operations)
  • op: The operation type

Returns:

  • An immutable overlay store

func (*OverlayStore) Add

func (o *OverlayStore) Add(resource interface{}, keys []string) error

Add is not supported on overlay stores (read-only).

func (*OverlayStore) Clear

func (o *OverlayStore) Clear() error

Clear is not supported on overlay stores (read-only).

func (*OverlayStore) Delete

func (o *OverlayStore) Delete(keys ...string) error

Delete is not supported on overlay stores (read-only).

func (*OverlayStore) Get

func (o *OverlayStore) Get(keys ...string) ([]interface{}, error)

Get retrieves resources matching the provided index keys.

It checks the overlay first and falls back to the base store if needed.

Behavior: - If keys match the changed resource: returns the overlay object (or empty for DELETE) - Otherwise: falls back to the base store.

Performance: O(1) for overlay hit, O(k) for base store where k = number of matches

func (*OverlayStore) List

func (o *OverlayStore) List() ([]interface{}, error)

List returns all resources with the overlay change applied.

It combines resources from the base store with the overlay modification.

Behavior: - For DELETE: excludes the deleted resource - For UPDATE: replaces the resource with the overlay version - FOR CREATE: adds the new resource.

Performance: O(n) where n = number of resources in base store

func (*OverlayStore) Update

func (o *OverlayStore) Update(resource interface{}, keys []string) error

Update is not supported on overlay stores (read-only).

type Store

type Store = types.Store

Store is the interface that resource stores must implement. This is an alias to the k8s.Store interface for convenience.

Jump to

Keyboard shortcuts

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