resourcewatcher

package
v0.2.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

pkg/controller/resourcewatcher

Resource watcher fan-out for the controller.

Overview

ResourceWatcherComponent materialises the configuration's spec.watchedResources map into a set of running Kubernetes informers (one pkg/k8s/watcher.Watcher per resource type). It also auto-injects an haproxy-pods watcher built from the CRD's spec.podSelector, so the rest of the controller never has to care whether the user listed it explicitly.

For each watcher the component:

  • Resolves apiVersion/resources to a GVR
  • Merges the global watchedResourcesIgnoreFields list with any per-resource overrides
  • Indexes resources using the configured indexBy JSONPath expressions
  • Applies the per-resource debounceInterval override (or the 2s pkg/k8s/types.DefaultDebounceInterval when empty; "0" disables debouncing so every change fires immediately) to the watcher's leading-edge refractory window
  • Forwards add/update/delete events as ResourceIndexUpdatedEvent and emits ResourceSyncCompleteEvent once the informer's initial list completes

The exposed stores are read by pkg/controller/renderer and pkg/controller/dryrunvalidator to build template contexts.

Quick Start

import "gitlab.com/haproxy-haptic/haptic/pkg/controller/resourcewatcher"

watcher, err := resourcewatcher.New(cfg, k8sClient, bus, logger)
if err != nil { /* ... */ }
go watcher.Start(ctx)

// Access stores after IndexSynchronizedEvent has fired
stores := watcher.GetAllStores() // map[string]types.Store

The constructor returns *ResourceWatcherComponent (not Watcher); the function name is New, not NewResourceWatcherComponent. The CRD config and a fully constructed *pkg/k8s/client.Client must be passed in — the component does not parse the CRD itself.

Events

  • Subscribes: none directly (the underlying watchers are driven by Kubernetes)
  • Publishes: ResourceIndexUpdatedEvent (one per change), ResourceSyncCompleteEvent (one per resource type, once)

License

Apache-2.0 — see root LICENSE.

Documentation

Overview

Package resourcewatcher provides the ResourceWatcherComponent that creates and manages watchers for all Kubernetes resources defined in the controller configuration.

The component:

  • Creates a k8s.Watcher for each resource type in Config.WatchedResources
  • Merges global WatchedResourcesIgnoreFields with per-resource ignore fields
  • Publishes ResourceIndexUpdatedEvent on resource changes
  • Publishes ResourceSyncCompleteEvent when a resource type completes initial sync
  • Provides access to stores for template rendering

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ResourceWatcherComponent

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

ResourceWatcherComponent creates and manages watchers for all configured resources.

func New

func New(
	cfg *coreconfig.Config,
	k8sClient *client.Client,
	eventBus *busevents.EventBus,
	logger *slog.Logger,
) (*ResourceWatcherComponent, error)

New creates a new ResourceWatcherComponent.

For each entry in cfg.WatchedResources, this creates a k8s.Watcher that:

  • Watches the specified Kubernetes resource type
  • Indexes resources using the configured IndexBy expressions
  • Filters fields by merging global WatchedResourcesIgnoreFields with per-resource ignore fields
  • Publishes events to the EventBus on resource changes

Returns an error if:

  • Configuration validation fails
  • Watcher creation fails for any resource type

func (*ResourceWatcherComponent) GetAllStores

func (r *ResourceWatcherComponent) GetAllStores() map[string]types.Store

GetAllStores returns a map of all stores keyed by resource type name.

Returns a copy of the internal map to prevent external modification.

func (*ResourceWatcherComponent) GetStore

func (r *ResourceWatcherComponent) GetStore(resourceTypeName string) types.Store

GetStore returns the store for a specific resource type.

Returns:

  • The store if the resource type exists
  • nil if the resource type is not watched

func (*ResourceWatcherComponent) Start

Start begins watching all configured resources.

This method:

  • Starts all watchers in separate goroutines
  • Returns immediately without blocking
  • Continues running until ctx is cancelled

Use WaitForAllSync() to wait for initial synchronization to complete.

func (*ResourceWatcherComponent) WaitForAllSync

func (r *ResourceWatcherComponent) WaitForAllSync(ctx context.Context) error

WaitForAllSync blocks until all watchers have completed initial synchronization.

Returns:

  • nil if all watchers synced successfully
  • error if sync fails or context is cancelled

Jump to

Keyboard shortcuts

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