comparator

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

README

pkg/dataplane/comparator

HAProxy configuration comparator.

Overview

Compares parsed HAProxy configurations to determine if deployment is needed.

Quick Start

import "haptic/pkg/dataplane/comparator"

comp := comparator.New()
needsUpdate := comp.Compare(current, desired)

License

See main repository for license information.

Documentation

Overview

Package comparator provides comparison functions for HAProxy Enterprise Edition sections.

This file contains comparison functions for EE-only sections: - Bot Management Profiles (v3.0+ EE) - Captcha (v3.0+ EE) - WAF Profile (v3.2+ EE) - WAF Global (v3.2+ EE)

Package comparator provides fine-grained configuration comparison and operation generation for HAProxy Dataplane API synchronization.

The comparator performs attribute-level comparison between current and desired configurations, generating the minimal set of operations needed to transform one into the other.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparator

type Comparator struct {
}

Comparator performs fine-grained comparison between HAProxy configurations.

It generates the minimal set of operations needed to transform a current configuration into a desired configuration, using attribute-level granularity to minimize API calls and avoid unnecessary HAProxy reloads.

func New

func New() *Comparator

New creates a new Comparator instance.

func (*Comparator) Compare

func (c *Comparator) Compare(current, desired *parser.StructuredConfig) (*ConfigDiff, error)

Compare performs a deep comparison between current and desired configurations.

It returns a ConfigDiff containing all operations needed to transform current into desired, along with a summary of changes.

The comparison is performed at attribute-level granularity - if only a single attribute changes (e.g., server weight), only that attribute is updated rather than replacing the entire resource.

Example:

comparator := comparator.New()
diff, err := comparator.Compare(currentConfig, desiredConfig)
if err != nil {
    slog.Error("comparison failed", "error", err)
    os.Exit(1)
}

fmt.Printf("Changes: %s\n", diff.Summary.String())
for _, op := range diff.Operations {
    fmt.Printf("- %s\n", op.Describe())
}

type ConfigDiff

type ConfigDiff struct {
	// Operations is the ordered list of operations to execute
	Operations []Operation

	// Summary provides a high-level overview of changes
	Summary DiffSummary
}

ConfigDiff represents the difference between two HAProxy configurations.

It contains all operations needed to transform the current configuration into the desired configuration, along with a summary of changes.

type DiffSummary

type DiffSummary struct {
	// Total counts by operation type
	TotalCreates int
	TotalUpdates int
	TotalDeletes int

	// Global and defaults changes
	GlobalChanged   bool
	DefaultsChanged bool

	// Frontend changes
	FrontendsAdded    []string
	FrontendsModified []string
	FrontendsDeleted  []string

	// Backend changes
	BackendsAdded    []string
	BackendsModified []string
	BackendsDeleted  []string

	// Server changes (map of backend -> server names)
	ServersAdded    map[string][]string
	ServersModified map[string][]string
	ServersDeleted  map[string][]string

	// Other section changes (extensible for future sections)
	OtherChanges map[string]int // section name -> count of changes
}

DiffSummary provides a high-level overview of configuration changes.

This is useful for logging, monitoring, and decision-making about whether to proceed with a configuration update.

func NewDiffSummary

func NewDiffSummary() DiffSummary

NewDiffSummary creates an empty DiffSummary with initialized maps.

func (*DiffSummary) HasChanges

func (s *DiffSummary) HasChanges() bool

HasChanges returns true if any configuration changes are present.

func (*DiffSummary) String

func (s *DiffSummary) String() string

String returns a human-readable summary of changes.

func (*DiffSummary) TotalOperations

func (s *DiffSummary) TotalOperations() int

TotalOperations returns the total number of operations across all types.

type Operation

type Operation interface {
	// Type returns the operation type (Create, Update, Delete)
	Type() sections.OperationType

	// Section returns the configuration section this operation affects
	// (e.g., "backend", "server", "frontend", "acl")
	Section() string

	// Priority returns the execution priority for dependency ordering.
	// Lower priority operations are executed first for Creates,
	// higher priority operations are executed first for Deletes.
	Priority() int

	// Execute performs the operation using the Dataplane API client.
	// The transactionID parameter should be included in API calls for
	// atomic transaction management.
	Execute(ctx context.Context, client *client.DataplaneClient, transactionID string) error

	// Describe returns a human-readable description of the operation
	// for logging and debugging.
	Describe() string
}

Operation represents a single configuration change operation.

Operations are executed within transactions and map to specific Dataplane API endpoints for atomic configuration updates.

func OrderOperations

func OrderOperations(ops []Operation) []Operation

3. Updates (any order - resources already exist).

Directories

Path Synopsis
Package sections provides factory functions for creating HAProxy configuration operations.
Package sections provides factory functions for creating HAProxy configuration operations.
executors
Package executors provides pre-built executor functions for HAProxy configuration operations.
Package executors provides pre-built executor functions for HAProxy configuration operations.

Jump to

Keyboard shortcuts

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