configchange

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

README

pkg/controller/configchange

ConfigMap change handler.

Overview

Watches specific ConfigMap and publishes change events.

Quick Start

handler := configchange.NewConfigChangeHandler(bus, k8sClient, configMapName, namespace, logger)
go handler.Start(ctx)

Events

  • Publishes: ConfigResourceChangedEvent

License

See main repository for license information.

Documentation

Index

Constants

View Source
const (
	// ComponentName is the unique identifier for this component.
	ComponentName = "configchange-handler"

	// EventBufferSize is the size of the event subscription buffer.
	// Size 50: Moderate-volume component handling config and validation events.
	EventBufferSize = 50

	// DefaultReinitDebounceInterval is the default time to wait after the last config
	// change before signaling controller reinitialization. This allows rapid CRD updates
	// to be coalesced, ensuring templates are fully rendered before reinitialization starts.
	DefaultReinitDebounceInterval = 500 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CRDWatcher

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

CRDWatcher watches HAProxyTemplateConfig CRD changes and publishes ConfigResourceChangedEvent.

This component bridges the Kubernetes informer pattern with the controller's event-driven architecture. It watches a single HAProxyTemplateConfig CRD instance and publishes events whenever the resource is added, updated, or deleted.

Architecture: - Uses generated informer from pkg/generated/informers - Publishes ConfigResourceChangedEvent (same as ConfigMap watcher for compatibility) - Converts typed CRD to unstructured format for consistent handling downstream.

func NewCRDWatcher

func NewCRDWatcher(
	client versioned.Interface,
	eventBus *busevents.EventBus,
	logger *slog.Logger,
	namespace string,
	name string,
) *CRDWatcher

NewCRDWatcher creates a new CRDWatcher.

Parameters:

  • client: Kubernetes client for HAProxyTemplateConfig CRD
  • eventBus: EventBus to publish ConfigResourceChangedEvent
  • logger: Structured logger for diagnostics
  • namespace: Namespace containing the HAProxyTemplateConfig resource
  • name: Name of the HAProxyTemplateConfig resource to watch

Returns:

  • *CRDWatcher ready to start

func (*CRDWatcher) Start

func (w *CRDWatcher) Start(ctx context.Context) error

Start begins watching the HAProxyTemplateConfig CRD.

This method blocks until Stop() is called or the context is canceled. It should typically be run in a goroutine.

Example:

watcher := NewCRDWatcher(client, bus, logger, "default", "haproxy-config")
go watcher.Start(ctx)

func (*CRDWatcher) Stop

func (w *CRDWatcher) Stop()

Stop gracefully stops the watcher.

type ConfigChangeHandler

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

ConfigChangeHandler coordinates configuration validation and detects config changes.

This component has two main responsibilities:

  1. Validation Coordination: Subscribes to ConfigParsedEvent, sends ConfigValidationRequest, collects responses from validators using scatter-gather pattern, and publishes ConfigValidatedEvent or ConfigInvalidEvent.

  2. Change Detection: Subscribes to ConfigValidatedEvent and signals the controller to reinitialize via the configChangeCh channel with debouncing to coalesce rapid changes.

Debouncing behavior: When multiple CRD config changes arrive in rapid succession, the handler debounces the reinitialization signal. This ensures all pending renders complete before reinitialization starts, preventing the race condition where reinitialization cancels in-progress renders.

Architecture: This component bridges the gap between configuration parsing and validation, and between validation and controller reinitialization. It uses the scatter-gather pattern from the event bus for coordinating validation across multiple validators.

func NewConfigChangeHandler

func NewConfigChangeHandler(
	eventBus *busevents.EventBus,
	logger *slog.Logger,
	configChangeCh chan<- *coreconfig.Config,
	validators []string,
	debounceInterval time.Duration,
) *ConfigChangeHandler

NewConfigChangeHandler creates a new ConfigChangeHandler.

Parameters:

  • eventBus: The EventBus to subscribe to and publish on
  • logger: Structured logger for diagnostics
  • configChangeCh: Channel to signal controller reinitialization with validated config
  • validators: List of expected validator names (e.g., ["basic", "template", "jsonpath"])
  • debounceInterval: Time to wait after last config change before triggering reinitialization. Use 0 for default (500ms).

Returns:

  • *ConfigChangeHandler ready to start

func (*ConfigChangeHandler) SetInitialConfigVersion

func (h *ConfigChangeHandler) SetInitialConfigVersion(version string)

SetInitialConfigVersion sets the initial config version to prevent reinitialization on bootstrap ConfigValidatedEvent.

This must be called after fetching the initial config but before CRDWatcher starts. When CRDWatcher's informer triggers onAdd for the existing CRD, it publishes a ConfigValidatedEvent with the same version. Without this tracking, that event would trigger reinitialization, creating an infinite loop.

Parameters:

  • version: The resourceVersion from the initial CRD fetch

func (*ConfigChangeHandler) Start

func (h *ConfigChangeHandler) Start(ctx context.Context) error

Start begins processing events from the EventBus.

This method blocks until Stop() is called or the context is canceled. The component is already subscribed to the EventBus (subscription happens in constructor). Returns nil on graceful shutdown.

Example:

go handler.Start(ctx)

func (*ConfigChangeHandler) Stop

func (h *ConfigChangeHandler) Stop()

Stop gracefully stops the component.

Jump to

Keyboard shortcuts

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