synchronizer

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

README

pkg/dataplane/synchronizer

Synchronizes configuration and auxiliary files to HAProxy instances.

Overview

Coordinates deployment of HAProxy configuration and auxiliary files to Dataplane API.

License

See main repository for license information.

Documentation

Overview

Package synchronizer provides configuration synchronization between desired state and HAProxy via the Dataplane API.

It orchestrates the comparison, transaction management, and policy enforcement for safe configuration updates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OperationError

type OperationError struct {
	Operation comparator.Operation
	Cause     error
}

OperationError represents a failed operation with its error.

type SyncOperationsResult

type SyncOperationsResult struct {
	// ReloadTriggered indicates whether a HAProxy reload was triggered.
	// true when commit status is 202, false when 200.
	ReloadTriggered bool

	// ReloadID is the reload identifier from the Reload-ID response header.
	// Only set when ReloadTriggered is true.
	ReloadID string
}

SyncOperationsResult contains information about a synchronization operation.

func SyncOperations

func SyncOperations(ctx context.Context, dpClient *client.DataplaneClient, operations []comparator.Operation, tx *client.Transaction) (*SyncOperationsResult, error)

SyncOperations executes a list of operations within the provided transaction.

This function must be called within a transaction context (e.g., via VersionAdapter.ExecuteTransaction). The transaction provides automatic retry logic on version conflicts.

Parameters:

  • ctx: Context for cancellation and timeout
  • client: The DataplaneClient
  • operations: List of operations to execute
  • tx: The transaction to execute operations within (from VersionAdapter)

Returns:

  • SyncOperationsResult with reload information
  • Error if any operation fails

Example:

adapter := client.NewVersionAdapter(client, 3)
err := adapter.ExecuteTransaction(ctx, func(ctx context.Context, tx *client.Transaction) error {
    result, err := synchronizer.SyncOperations(ctx, client, diff.Operations, tx)
    return err
})

type SyncOptions

type SyncOptions struct {
	// Policy determines how the sync is performed
	Policy SyncPolicy

	// ContinueOnError determines whether to continue applying operations
	// if one fails. If false, the first error stops execution.
	ContinueOnError bool

	// ValidateBeforeApply runs HAProxy validation before committing changes.
	// This adds an extra API call but provides safety.
	ValidateBeforeApply bool
}

SyncOptions configures the synchronization behavior.

func DefaultSyncOptions

func DefaultSyncOptions() SyncOptions

DefaultSyncOptions returns the default sync options.

func DryRunOptions

func DryRunOptions() SyncOptions

DryRunOptions returns options configured for dry-run mode.

type SyncPolicy

type SyncPolicy string

SyncPolicy defines how synchronization should be performed.

const (
	// PolicyDryRun performs comparison and validation but does not apply changes.
	// Useful for preview and testing.
	PolicyDryRun SyncPolicy = "dry-run"

	// PolicyApply applies all changes via the Dataplane API with automatic
	// retry on version conflicts. This is the standard policy for production use.
	PolicyApply SyncPolicy = "apply"

	// PolicyApplyForce applies changes without retry limits. Use with caution
	// as this may result in many retries in high-contention scenarios.
	PolicyApplyForce SyncPolicy = "apply-force"
)

func (SyncPolicy) IsDryRun

func (p SyncPolicy) IsDryRun() bool

IsDryRun returns true if this is a dry-run policy.

func (SyncPolicy) MaxRetries

func (p SyncPolicy) MaxRetries() int

MaxRetries returns the maximum number of retries for this policy. Returns -1 for unlimited retries (PolicyApplyForce).

func (SyncPolicy) ShouldApply

func (p SyncPolicy) ShouldApply() bool

ShouldApply returns true if this policy should apply changes.

func (SyncPolicy) String

func (p SyncPolicy) String() string

String returns the string representation of the policy.

type SyncResult

type SyncResult struct {
	// Success indicates whether the sync completed successfully
	Success bool

	// Policy used for this sync
	Policy SyncPolicy

	// Diff contains the configuration differences that were (or would be) applied
	Diff *comparator.ConfigDiff

	// Applied operations (may be subset if ContinueOnError is false)
	AppliedOperations []comparator.Operation

	// Failed operations with their errors
	FailedOperations []OperationError

	// Duration of the sync operation
	Duration time.Duration

	// Retries indicates how many times the operation was retried
	Retries int

	// Message provides additional context about the result
	Message string
}

SyncResult represents the outcome of a synchronization operation.

func NewFailureResult

func NewFailureResult(policy SyncPolicy, diff *comparator.ConfigDiff, applied []comparator.Operation, failed []OperationError, duration time.Duration, retries int, message string) *SyncResult

NewFailureResult creates a failed sync result.

func NewNoChangesResult

func NewNoChangesResult(policy SyncPolicy, duration time.Duration) *SyncResult

NewNoChangesResult creates a result for when there are no changes to apply.

func NewSuccessResult

func NewSuccessResult(policy SyncPolicy, diff *comparator.ConfigDiff, applied []comparator.Operation, duration time.Duration, retries int) *SyncResult

NewSuccessResult creates a successful sync result.

func (*SyncResult) HasChanges

func (r *SyncResult) HasChanges() bool

HasChanges returns true if there are configuration changes.

func (*SyncResult) HasFailures

func (r *SyncResult) HasFailures() bool

HasFailures returns true if any operations failed.

func (*SyncResult) String

func (r *SyncResult) String() string

String returns a human-readable summary of the sync result.

type Synchronizer

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

Synchronizer orchestrates configuration synchronization between desired state and HAProxy via the Dataplane API.

It uses a Comparator to generate fine-grained diffs and executes operations within transactions with automatic retry on version conflicts.

func New

New creates a new Synchronizer instance.

Example:

client, _ := client.New(client.Config{
    BaseURL:  "http://localhost:5555/v2",
    Username: "admin",
    Password: "secret",
})
sync := synchronizer.New(client)
result, err := sync.Sync(ctx, currentConfig, desiredConfig, synchronizer.DefaultSyncOptions())

func (*Synchronizer) Sync

func (s *Synchronizer) Sync(ctx context.Context, current, desired *parser.StructuredConfig, opts SyncOptions) (*SyncResult, error)

Sync synchronizes the current configuration to match the desired configuration.

The sync process: 1. Compares current and desired configurations 2. Generates ordered operations to transform current -> desired 3. Executes operations within a transaction (if policy allows) 4. Handles retries on version conflicts 5. Returns detailed results including successes/failures

Parameters:

  • ctx: Context for cancellation and timeout
  • current: The current HAProxy configuration
  • desired: The desired HAProxy configuration
  • opts: Sync options controlling behavior

Returns a SyncResult with details about what was changed and any errors.

func (*Synchronizer) SyncFromStrings

func (s *Synchronizer) SyncFromStrings(ctx context.Context, currentConfig, desiredConfig string, opts SyncOptions) (*SyncResult, error)

SyncFromStrings is a convenience method that parses configuration strings and performs synchronization.

This is useful for testing and simple use cases where you have raw HAProxy configuration strings.

func (*Synchronizer) WithLogger

func (s *Synchronizer) WithLogger(logger *slog.Logger) *Synchronizer

WithLogger sets a custom logger for the synchronizer.

Jump to

Keyboard shortcuts

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