resourceloader

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

README

pkg/controller/resourceloader

Thin event-loop wrapper around pkg/controller/component.Base for resource-loading components (configloader, credentialsloader).

Overview

Both Stage-1 loaders share an identical pattern: subscribe to one resource-changed event type, type-assert the payload, parse / validate it, publish a "parsed" event. Before this package they each duplicated the subscribe-on-construction + dispatch-with-recover scaffold; now they share BaseLoader.

BaseLoader is just *component.Base with one extra layer: it accepts an EventProcessor interface (a single ProcessEvent(event) method) instead of component.EventHandler. The split exists because the loaders predate the consolidated component package — keeping the older method name avoids a cross-package rename. New components should use component.New directly; the existing loaders use this wrapper for source-stability.

Quick Start

import (
    "log/slog"

    "gitlab.com/haproxy-haptic/haptic/pkg/controller/events"
    "gitlab.com/haproxy-haptic/haptic/pkg/controller/resourceloader"
    busevents "gitlab.com/haproxy-haptic/haptic/pkg/events"
)

type MyLoader struct {
    *resourceloader.BaseLoader
}

func (l *MyLoader) ProcessEvent(event busevents.Event) {
    // type-assert, parse, publish a "parsed" event
}

func New(bus *busevents.EventBus, logger *slog.Logger) *MyLoader {
    l := &MyLoader{}
    l.BaseLoader = resourceloader.NewBaseLoader(
        bus, logger, "my-loader", 100, l,
        events.EventTypeMyResourceChanged,
    )
    return l
}

The variadic eventTypes argument at the end is the typed-subscription filter — pass the specific event types the loader cares about so the bus filters at the source.

See Also

License

Apache-2.0 — see root LICENSE.

Documentation

Overview

Package resourceloader provides shared event loop infrastructure for loader components that watch a single resource type and parse/transform its data.

BaseLoader is a thin wrapper over pkg/controller/component.Base that keeps the ProcessEvent naming familiar to existing loader implementations (configloader, credentialsloader).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseLoader

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

BaseLoader is a resource-loader flavoured wrapper around component.Base that exposes the same field accessors (EventBus, Logger, Name) as before and delegates event dispatch to the processor's ProcessEvent method.

Panics inside ProcessEvent are caught by the embedded base, logged with the event type and then swallowed so the event loop keeps running.

func NewBaseLoader

func NewBaseLoader(
	eventBus *busevents.EventBus,
	logger *slog.Logger,
	name string,
	bufferSize int,
	processor EventProcessor,
	eventTypes ...string,
) *BaseLoader

NewBaseLoader creates a new base loader with the given configuration.

func (*BaseLoader) AssertUnstructured

func (b *BaseLoader) AssertUnstructured(eventTypeName string, resource any) (*unstructured.Unstructured, bool)

AssertUnstructured type-asserts a resource carried by an event to *unstructured.Unstructured. On a type mismatch it logs an error tagged with the event type name and returns (nil, false); callers should early-return on a false result. Every loader on the bus receives resources via watcher events whose payloads are unstructured, so this is the single chokepoint for the "invalid resource type" failure mode.

func (*BaseLoader) HandleEvent

func (b *BaseLoader) HandleEvent(event busevents.Event)

HandleEvent implements component.EventHandler by forwarding to the processor so loader implementations do not need to change their method name.

type EventProcessor

type EventProcessor interface {
	// ProcessEvent handles a single event from the EventBus.
	ProcessEvent(event busevents.Event)
}

EventProcessor defines the interface for loader-specific event handling logic.

Each loader (configloader, credentialsloader) implements this interface to provide its specific parsing logic while reusing the common event loop infrastructure.

Jump to

Keyboard shortcuts

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