Help us shape the future of Go! Take the 2026 Go Developer Survey

sync

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package sync provides synchronization management interfaces and implementations for MCPRegistry resources in the ToolHive operator.

This package implements a clean separation of concerns by extracting all sync-related logic from the controller into dedicated interfaces:

  • Manager: Main interface for orchestrating sync operations
  • DataChangeDetector: Detects changes in source data using hash comparison
  • ManualSyncChecker: Handles annotation-based manual sync triggers
  • AutomaticSyncChecker: Manages time-based automatic sync scheduling

The package follows Go best practices by using interfaces for testability and dependency injection, while providing concrete struct implementations for actual functionality.

Key features:

  • Hash-based change detection to avoid unnecessary syncs
  • Manual sync support via annotations
  • Automatic sync scheduling with configurable intervals
  • Comprehensive error handling and status management
  • Clean integration with Kubernetes controller patterns

Index

Constants

View Source
const (
	// Registry state related reasons
	ReasonAlreadyInProgress = "sync-already-in-progress"
	ReasonRegistryNotReady  = "registry-not-ready"

	// Data change related reasons
	ReasonSourceDataChanged    = "source-data-changed"
	ReasonErrorCheckingChanges = "error-checking-data-changes"

	// Manual sync related reasons
	ReasonManualWithChanges = "manual-sync-with-data-changes"
	ReasonManualNoChanges   = "manual-sync-no-data-changes"

	// Automatic sync related reasons
	ReasonErrorParsingInterval  = "error-parsing-sync-interval"
	ReasonErrorCheckingSyncNeed = "error-checking-sync-need"

	// Up-to-date reasons
	ReasonUpToDateWithPolicy = "up-to-date-with-policy"
	ReasonUpToDateNoPolicy   = "up-to-date-no-policy"
)

Sync reason constants

View Source
const (
	ManualSyncReasonNoAnnotations    = "no-annotations"
	ManualSyncReasonNoTrigger        = "no-manual-trigger"
	ManualSyncReasonAlreadyProcessed = "manual-trigger-already-processed"
	ManualSyncReasonRequested        = "manual-sync-requested"
)

Manual sync annotation detection reasons

View Source
const (
	// SyncTriggerAnnotation is the annotation key used to trigger registry synchronization
	SyncTriggerAnnotation = "toolhive.stacklok.dev/sync-trigger"
)

Variables

This section is empty.

Functions

func IsManualSync

func IsManualSync(reason string) bool

IsManualSync checks if the sync reason indicates a manual sync

Types

type AutomaticSyncChecker

type AutomaticSyncChecker interface {
	// IsIntervalSyncNeeded checks if sync is needed based on time interval
	// Returns (syncNeeded, nextSyncTime, error) where nextSyncTime is always in the future
	IsIntervalSyncNeeded(mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, time.Time, error)
}

AutomaticSyncChecker handles automatic sync timing logic

type DataChangeDetector

type DataChangeDetector interface {
	// IsDataChanged checks if source data has changed by comparing hashes
	IsDataChanged(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, error)
}

DataChangeDetector detects changes in source data

type DefaultAutomaticSyncChecker

type DefaultAutomaticSyncChecker struct{}

DefaultAutomaticSyncChecker implements AutomaticSyncChecker

func (*DefaultAutomaticSyncChecker) IsIntervalSyncNeeded

func (*DefaultAutomaticSyncChecker) IsIntervalSyncNeeded(mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, time.Time, error)

IsIntervalSyncNeeded checks if sync is needed based on time interval Returns: (syncNeeded, nextSyncTime, error) nextSyncTime is always a future time when the next sync should occur

type DefaultDataChangeDetector

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

DefaultDataChangeDetector implements DataChangeDetector

func (*DefaultDataChangeDetector) IsDataChanged

func (d *DefaultDataChangeDetector) IsDataChanged(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, error)

IsDataChanged checks if source data has changed by comparing hashes

type DefaultManualSyncChecker

type DefaultManualSyncChecker struct{}

DefaultManualSyncChecker implements ManualSyncChecker

func (*DefaultManualSyncChecker) IsManualSyncRequested

func (*DefaultManualSyncChecker) IsManualSyncRequested(mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string)

IsManualSyncRequested checks if a manual sync was requested via annotation

type DefaultSyncManager

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

DefaultSyncManager is the default implementation of Manager

func NewDefaultSyncManager

func NewDefaultSyncManager(k8sClient client.Client, scheme *runtime.Scheme,
	sourceHandlerFactory sources.SourceHandlerFactory, storageManager sources.StorageManager) *DefaultSyncManager

NewDefaultSyncManager creates a new DefaultSyncManager

func (*DefaultSyncManager) Delete

func (s *DefaultSyncManager) Delete(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) error

Delete cleans up storage resources for the MCPRegistry

func (*DefaultSyncManager) PerformSync

func (s *DefaultSyncManager) PerformSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, error)

PerformSync performs the complete sync operation for the MCPRegistry

func (*DefaultSyncManager) ShouldSync

func (s *DefaultSyncManager) ShouldSync(
	ctx context.Context,
	mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string, *time.Time, error)

ShouldSync determines if a sync operation is needed and when the next sync should occur

func (*DefaultSyncManager) UpdateManualSyncTriggerOnly

func (s *DefaultSyncManager) UpdateManualSyncTriggerOnly(
	ctx context.Context,
	mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, error)

UpdateManualSyncTriggerOnly updates the manual sync trigger tracking without performing actual sync

type Manager

type Manager interface {
	// ShouldSync determines if a sync operation is needed
	ShouldSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string, *time.Time, error)

	// PerformSync executes the complete sync operation
	PerformSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, error)

	// UpdateManualSyncTriggerOnly updates manual sync trigger tracking without performing actual sync
	UpdateManualSyncTriggerOnly(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, error)

	// Delete cleans up storage resources for the MCPRegistry
	Delete(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) error
}

Manager manages synchronization operations for MCPRegistry resources

type ManualSyncChecker

type ManualSyncChecker interface {
	// IsManualSyncRequested checks if a manual sync was requested via annotation
	IsManualSyncRequested(mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string)
}

ManualSyncChecker handles manual sync detection logic

Jump to

Keyboard shortcuts

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