configloader

package
v0.1.0-alpha.12 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

pkg/controller/configloader

ConfigLoader component - parses ConfigMap data into configuration structures.

Overview

Event-driven component that subscribes to ConfigResourceChangedEvent, extracts YAML configuration from ConfigMap resources, and publishes ConfigParsedEvent for valid configurations.

Part of: Stage 1 (Config Management) in controller lifecycle

Installation

import "haptic/pkg/controller/configloader"

Quick Start

import (
    "haptic/pkg/controller/configloader"
    "haptic/pkg/events"
)

// Create component
loader := configloader.NewConfigLoaderComponent(bus, logger)

// Start component (blocks until context cancelled)
go loader.Start(ctx)

// Component automatically processes ConfigResourceChangedEvent
// and publishes ConfigParsedEvent

API

NewConfigLoaderComponent
func NewConfigLoaderComponent(bus *busevents.EventBus, logger *slog.Logger) *ConfigLoaderComponent

Creates a new ConfigLoader component.

Parameters:

  • bus: EventBus for subscribing and publishing
  • logger: Structured logger for diagnostics

Returns: ConfigLoaderComponent ready to start

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

Starts the component's event loop. Blocks until context is cancelled. Returns nil on graceful shutdown.

Process:

  1. Subscribes to EventBus
  2. Waits for ConfigResourceChangedEvent
  3. Extracts ConfigMap data["config"]
  4. Parses YAML using config.ParseConfig
  5. Publishes ConfigParsedEvent on success
Stop
func (c *ConfigLoaderComponent) Stop()

Gracefully stops the component.

Events

Subscribes To

ConfigResourceChangedEvent:

type ConfigResourceChangedEvent struct {
    Resource interface{}  // *unstructured.Unstructured (ConfigMap)
}

Published by ConfigChange watcher when ConfigMap changes.

Publishes

ConfigParsedEvent (on success):

type ConfigParsedEvent struct {
    Config        *config.Config
    ConfigVersion string  // ConfigMap resourceVersion
    SecretVersion string  // Empty at this stage
}

Published when YAML successfully parsed into config.Config.

ConfigMap Format

Expected ConfigMap structure:

apiVersion: v1
kind: ConfigMap
metadata:
  name: haproxy-config
  namespace: default
data:
  config: |
    templates:
      main: |
        global
          maxconn 2000
        defaults
          mode http
    watched_resources:
      - name: ingresses
        group: networking.k8s.io
        version: v1
        resource: ingresses

Required:

  • data.config: YAML configuration string

Error Handling

Component logs errors but does not publish events for failures:

Missing "config" key:

Error: ConfigMap data missing 'config' key

Invalid YAML:

Error: Failed to parse configuration YAML

Invalid resource type:

Error: ConfigResourceChangedEvent contains invalid resource type

No data field:

Error: ConfigMap has no data field

Example Usage

Basic Setup
func main() {
    bus := events.NewEventBus(100)
    logger := slog.Default()

    loader := configloader.NewConfigLoaderComponent(bus, logger)

    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    go loader.Start(ctx)

    // Wait for ConfigParsedEvent
    eventChan := bus.Subscribe("consumer", 10)
    for event := range eventChan {
        if parsed, ok := event.(*events.ConfigParsedEvent); ok {
            fmt.Printf("Config loaded: version %s\n", parsed.ConfigVersion)
        }
    }
}

Integration

Controller creates ConfigLoader in Stage 1:

// Stage 1: Config Management
configLoader := configloader.NewConfigLoaderComponent(bus, logger)
go configLoader.Start(ctx)

Component runs for entire controller lifecycle, processing ConfigMap updates.

License

See main repository for license information.

Documentation

Index

Constants

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

	// EventBufferSize is the size of the event subscription buffer.
	// Size 50: Low-volume component (~1 event per config change).
	EventBufferSize = 50
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigLoaderComponent

type ConfigLoaderComponent struct {
	*resourceloader.BaseLoader
}

ConfigLoaderComponent subscribes to ConfigResourceChangedEvent and parses config data.

This component is responsible for: - Converting HAProxyTemplateConfig CRD Spec to config.Config - Publishing ConfigParsedEvent for successfully parsed configs - Logging errors for conversion failures

Architecture: This is a pure event-driven component with no knowledge of watchers or Kubernetes. It simply reacts to ConfigResourceChangedEvent and produces ConfigParsedEvent.

func NewConfigLoaderComponent

func NewConfigLoaderComponent(eventBus *busevents.EventBus, logger *slog.Logger) *ConfigLoaderComponent

NewConfigLoaderComponent creates a new ConfigLoader component.

Parameters:

  • eventBus: The EventBus to subscribe to and publish on
  • logger: Structured logger for diagnostics

Returns:

  • *ConfigLoaderComponent ready to start

func (*ConfigLoaderComponent) ProcessEvent

func (c *ConfigLoaderComponent) ProcessEvent(event busevents.Event)

ProcessEvent handles a single event from the EventBus.

Jump to

Keyboard shortcuts

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