Help us shape the future of Go! Take the 2026 Go Developer Survey

resourcewatcher

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 18 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 100ms 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 "github.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 Option

type Option func(*options)

Option configures New beyond its required arguments.

func WithSelfWriteFilter

func WithSelfWriteFilter(f types.SelfWriteFilter) Option

WithSelfWriteFilter hands every watcher the filter that recognises this controller's own writes, so an echoed status write refreshes the store without triggering a reconciliation.

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,
	opts ...Option,
) (*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) FreshStores

func (r *ResourceWatcherComponent) FreshStores(ctx context.Context) (map[string]types.Store, error)

FreshStores snapshots each watched collection directly from the API server.

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
  • Continues running until ctx is cancelled
  • Waits for every watcher to stop before returning

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