discovery

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

README

pkg/controller/discovery

HAProxy pod discovery component.

Overview

Tracks the current set of HAProxy pods and the dataplane endpoints reachable on them. The component does not watch pods directly — it consumes the pod store filled by the resource watcher (via ResourceIndexUpdatedEvent for the configured haproxy-pods resource type), enriches each pod with credentials and a HAProxy version probe, and emits HAProxyPodsDiscoveredEvent containing the validated endpoints. Pods that disappear cause a HAProxyPodTerminatedEvent.

This component runs on all replicas — discovery is a read-only HAProxy probe and there's no reason to gate it on leadership. It caches the most recent HAProxyPodsDiscoveredEvent via leadership.StateReplayer and re-publishes it on BecameLeaderEvent, so a freshly-elected leader's (leader-only) deployer/scheduler get current pod state without waiting for the next pod-watcher tick.

Quick Start

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

component, err := discovery.New(bus, logger)
if err != nil { /* ... */ }
go component.Start(ctx)

The component's effective configuration (pod-selector labels, dataplane port, basic-auth credentials) is provided through the events it subscribes to, not via constructor arguments.

Events

  • Subscribes: ConfigValidatedEvent (dataplane port + selector), CredentialsUpdatedEvent, ResourceIndexUpdatedEvent (haproxy-pods), ResourceSyncCompleteEvent, BecameLeaderEvent
  • Publishes: HAProxyPodsDiscoveredEvent, HAProxyPodTerminatedEvent, HAProxyPodRejectedEvent (one per candidate that fails version/credentials probing — the metrics component turns these into haptic_haproxy_pods_rejected_total)

License

Apache-2.0 — see root LICENSE.

Documentation

Overview

Package discovery provides the Discovery event adapter component.

It tracks the set of HAProxy pods reported by the resource watcher (via the auto-injected haproxy-pods watcher), enriches each pod with credentials and a HAProxy version probe through pkg/dataplane, and publishes HAProxyPodsDiscoveredEvent / HAProxyPodTerminatedEvent so the deployer and other consumers know which endpoints to talk to.

Package discovery provides HAProxy pod discovery functionality.

This package implements pure business logic for discovering HAProxy pod endpoints based on pod resources from the Kubernetes API. It extracts pod IPs and constructs Dataplane API endpoints with credentials.

This is a pure component with no event bus dependency - event coordination is handled by the adapter in pkg/controller/discovery.

Index

Constants

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

	// EventBufferSize is the buffer size for event subscriptions.
	// High-volume to absorb pod churn bursts during scaling and rolling updates.
	EventBufferSize = busevents.HighVolumeSubscriberBuffer
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component struct {
	*component.Base
	// contains filtered or unexported fields
}

Component is the Discovery event adapter.

This component:

  • Subscribes to ConfigValidatedEvent, CredentialsUpdatedEvent, ResourceIndexUpdatedEvent, and BecameLeaderEvent
  • Maintains current state (dataplanePort, credentials, podStore)
  • Calls Discovery.DiscoverEndpoints() when relevant events occur
  • Publishes HAProxyPodsDiscoveredEvent with discovered endpoints
  • Publishes HAProxyPodTerminatedEvent when pods are removed

Event Flow:

  1. ConfigValidatedEvent → Update dataplanePort → Trigger discovery
  2. CredentialsUpdatedEvent → Update credentials → Trigger discovery
  3. ResourceIndexUpdatedEvent (haproxy-pods) → Trigger discovery
  4. BecameLeaderEvent → Re-trigger discovery for new leader's DeploymentScheduler
  5. Discovery completes → Compare with previous endpoints → Publish HAProxyPodTerminatedEvent for removed pods → Publish HAProxyPodsDiscoveredEvent

func New

func New(eventBus *busevents.EventBus, logger *slog.Logger) (*Component, error)

New creates a new Discovery event adapter component.

Parameters:

  • eventBus: The event bus for subscribing to and publishing events
  • logger: Structured logger for observability

Returns a configured Component ready to be started, or an error if local HAProxy version detection fails (which is fatal - the controller cannot start without knowing its local version for compatibility checking).

Note: The Discovery pure component is created lazily when the dataplane port is configured via ConfigValidatedEvent. This constructor only detects the local HAProxy version for future compatibility checking.

func (*Component) HandleEvent

func (c *Component) HandleEvent(event busevents.Event)

HandleEvent implements component.EventHandler: it processes incoming events and triggers discovery as needed.

func (*Component) SetPodStore

func (c *Component) SetPodStore(store types.Store)

SetPodStore sets the pod store reference.

This is called by the controller after creating the haproxy-pods resource watcher. It allows the Discovery component to access pod resources for endpoint discovery.

Thread-safe.

func (*Component) Start

func (c *Component) Start(ctx context.Context) error

Start runs the embedded component.Base event loop until the context is cancelled.

Note: Event subscription occurs in the constructor (New()) to ensure proper startup synchronization. ResourceSyncCompleteEvent is buffered until EventBus.Start() is called, so no events are missed.

type Discovery

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

Discovery discovers HAProxy pod endpoints from Kubernetes resources.

This is a pure component that takes a pod store and credentials and returns a list of Dataplane API endpoints. It has no knowledge of events or the event bus - that coordination is handled by the event adapter.

The Discovery also holds the local HAProxy version, detected at startup, which is used by the event adapter for version compatibility checking.

func (*Discovery) DiscoverEndpoints

func (d *Discovery) DiscoverEndpoints(
	podStore types.Store,
	credentials coreconfig.Credentials,
) ([]dataplane.Endpoint, error)

DiscoverEndpoints discovers HAProxy Dataplane API endpoints from pod resources.

This method:

  • Lists all pods from the provided store
  • Extracts pod IPs from pod.status.podIP
  • Checks that the dataplane container is ready
  • Constructs Dataplane API URLs (http://{IP}:{port})
  • Creates Endpoint structs with credentials

Parameters:

  • podStore: Store containing HAProxy pod resources
  • credentials: Dataplane API credentials to use for all endpoints

Returns:

  • A slice of discovered Endpoint structs
  • An error if discovery fails

Example:

endpoints, err := discovery.DiscoverEndpoints(podStore, credentials)
if err != nil {
    return fmt.Errorf("discovery failed: %w", err)
}
// Use endpoints for HAProxy synchronization

func (*Discovery) DiscoverEndpointsWithLogger

func (d *Discovery) DiscoverEndpointsWithLogger(
	podStore types.Store,
	credentials coreconfig.Credentials,
	logger *slog.Logger,
) ([]dataplane.Endpoint, error)

DiscoverEndpointsWithLogger is like DiscoverEndpoints but accepts an optional logger for debugging.

func (*Discovery) LocalVersion

func (d *Discovery) LocalVersion() *dataplane.Version

LocalVersion returns the detected local HAProxy version. This is used by the event adapter for version compatibility checking.

Jump to

Keyboard shortcuts

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