discovery

package
v0.1.0-alpha.9 Latest Latest
Warning

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

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

README

pkg/controller/discovery

HAProxy pod discovery component.

Overview

Discovers and tracks HAProxy pods in the cluster for configuration deployment.

Quick Start

discovery := discovery.NewDiscoveryComponent(bus, k8sClient, config, logger)
go discovery.Start(ctx)

Events

  • Subscribes: ConfigValidatedEvent
  • Publishes: HAProxyPodsDiscoveredEvent, HAProxyPodAddedEvent, HAProxyPodRemovedEvent

License

See main repository for license information.

Documentation

Overview

Package discovery provides the Discovery event adapter component.

This package wraps the pure Discovery component (pkg/dataplane/discovery) with event-driven coordination. It subscribes to configuration, credentials, and pod change events, and publishes discovered HAProxy endpoints.

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.
	EventBufferSize = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component struct {
	// 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) Name

func (c *Component) Name() string

Name returns the unique identifier for this component. Implements the lifecycle.Component interface.

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 begins the Discovery component's event processing loop.

This method:

  • Maintains state from config and credential updates
  • Triggers discovery when HAProxy pods change (via ResourceSyncCompleteEvent)
  • Publishes discovered endpoints
  • Runs until context is cancelled

Returns an error if the event loop fails.

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