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
- Variables
- func IsManualSync(reason string) bool
- type AutomaticSyncChecker
- type DataChangeDetector
- type DefaultAutomaticSyncChecker
- type DefaultDataChangeDetector
- type DefaultManualSyncChecker
- type DefaultSyncManager
- func (s *DefaultSyncManager) Delete(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) error
- func (s *DefaultSyncManager) PerformSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, *Result, *mcpregistrystatus.Error)
- func (s *DefaultSyncManager) ShouldSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string, *time.Time)
- func (s *DefaultSyncManager) UpdateManualSyncTriggerOnly(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, error)
- type Manager
- type ManualSyncChecker
- type Result
Constants ¶
const ( // Registry state related reasons ReasonAlreadyInProgress = "sync-already-in-progress" ReasonRegistryNotReady = "registry-not-ready" ReasonRequeueTimeNotElapsed = "requeue-time-not-elapsed" // 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
const ( ManualSyncReasonNoAnnotations = "no-annotations" ManualSyncReasonNoTrigger = "no-manual-trigger" ManualSyncReasonAlreadyProcessed = "manual-trigger-already-processed" ManualSyncReasonRequested = "manual-sync-requested" )
Manual sync annotation detection reasons
const ( // DefaultSyncRequeueAfterConstant is the constant default requeue interval for sync operations DefaultSyncRequeueAfterConstant = time.Minute * 5 )
Default timing constants for the sync manager
const (
// SyncTriggerAnnotation is the annotation key used to trigger registry synchronization
SyncTriggerAnnotation = "toolhive.stacklok.dev/sync-trigger"
)
Variables ¶
var ( // DefaultSyncRequeueAfter is the configurable default requeue interval for sync operations // This can be modified in tests to speed up requeue behavior DefaultSyncRequeueAfter = DefaultSyncRequeueAfterConstant )
Configurable timing variables for testing
Functions ¶
func IsManualSync ¶
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, *Result, *mcpregistrystatus.Error)
PerformSync performs the complete sync operation for the MCPRegistry The controller is responsible for setting sync status via the status collector
func (*DefaultSyncManager) ShouldSync ¶
func (s *DefaultSyncManager) ShouldSync( ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (bool, string, *time.Time)
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)
// PerformSync executes the complete sync operation
PerformSync(ctx context.Context, mcpRegistry *mcpv1alpha1.MCPRegistry) (ctrl.Result, *Result, *mcpregistrystatus.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