Documentation
¶
Overview ¶
Package sync provides coordination between GitHub labels and CRD status fields. This package implements bidirectional synchronization to ensure consistency between the external GitHub API state and internal Kubernetes CRD state.
Synchronization patterns:
- GitHub → CRD: Primary sync direction (GitHub is source of truth)
- CRD → GitHub: Used for operator-initiated state changes
- Conflict resolution: GitHub wins in case of conflicts
- Recovery: CRD controllers can recreate missing GitHub labels
The coordinator works with both PullRequestTracker and DriftMonitor controllers to maintain consistent state representation across the system.
Index ¶
- type Coordinator
- func (c *Coordinator) CreateDriftMonitorForPRT(ctx context.Context, prt *v1alpha1.PullRequestTracker, clusters []string) ([]*v1alpha1.DriftMonitor, error)
- func (c *Coordinator) EnsureConsistency(ctx context.Context, prt *v1alpha1.PullRequestTracker) (bool, error)
- func (c *Coordinator) SyncGitHubToPRT(ctx context.Context, prt *v1alpha1.PullRequestTracker) (*SyncResult, error)
- func (c *Coordinator) SyncPRTToGitHub(ctx context.Context, prt *v1alpha1.PullRequestTracker) (*SyncResult, error)
- type GitHubClient
- type LabelManager
- type Logger
- type SyncResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Coordinator ¶
type Coordinator struct {
Client client.Client
GitHubClient GitHubClient
LabelManager LabelManager
Logger Logger
}
Coordinator manages bidirectional synchronization between GitHub labels and CRD status. It ensures consistency and handles conflict resolution when state diverges.
func NewCoordinator ¶
func NewCoordinator( client client.Client, githubClient GitHubClient, labelManager LabelManager, logger Logger, ) *Coordinator
NewCoordinator creates a new sync coordinator.
Parameters:
- client: Kubernetes client for CRD operations
- githubClient: GitHub API client interface
- labelManager: Label management interface
- logger: Logging interface
Returns a configured Coordinator ready for synchronization operations.
func (*Coordinator) CreateDriftMonitorForPRT ¶
func (c *Coordinator) CreateDriftMonitorForPRT( ctx context.Context, prt *v1alpha1.PullRequestTracker, clusters []string, ) ([]*v1alpha1.DriftMonitor, error)
CreateDriftMonitorForPRT creates a DriftMonitor for a merged PullRequestTracker. This is called when a PR transitions to merged state and needs drift monitoring.
Parameters:
- ctx: Request context
- prt: The merged PullRequestTracker
- clusters: List of clusters to monitor
Returns the created DriftMonitor resources and any error.
func (*Coordinator) EnsureConsistency ¶
func (c *Coordinator) EnsureConsistency(ctx context.Context, prt *v1alpha1.PullRequestTracker) (bool, error)
EnsureConsistency performs a full consistency check between GitHub and CRD state. This is used for reconciliation and recovery scenarios.
Steps:
- Get state from both GitHub and CRD
- Detect any inconsistencies
- Apply conflict resolution (GitHub wins)
- Update CRD to match GitHub if needed
Parameters:
- ctx: Request context
- prt: The PullRequestTracker to check
Returns true if state was consistent, false if corrections were made.
func (*Coordinator) SyncGitHubToPRT ¶
func (c *Coordinator) SyncGitHubToPRT(ctx context.Context, prt *v1alpha1.PullRequestTracker) (*SyncResult, error)
SyncGitHubToPRT synchronizes GitHub label state to a PullRequestTracker CRD. This is the primary sync direction used by the PullRequestTracker controller.
Steps:
- Get current PR data from GitHub
- Extract state from GitHub labels
- Update PullRequestTracker status to match
- Update CRD conditions appropriately
Parameters:
- ctx: Request context
- prt: The PullRequestTracker to update
Returns the sync result and any error that occurred.
func (*Coordinator) SyncPRTToGitHub ¶
func (c *Coordinator) SyncPRTToGitHub(ctx context.Context, prt *v1alpha1.PullRequestTracker) (*SyncResult, error)
SyncPRTToGitHub synchronizes a PullRequestTracker's state to GitHub labels. This is used when the operator needs to update GitHub based on CRD changes.
Steps:
- Get current PR data from GitHub
- Convert CRD state to domain state
- Apply state to GitHub labels
- Handle any conflicts or errors
Parameters:
- ctx: Request context
- prt: The PullRequestTracker to sync
Returns the sync result and any error that occurred.
type GitHubClient ¶
type GitHubClient interface {
// GetPR retrieves a pull request from GitHub by number.
GetPR(ctx context.Context, repo github.Repository, number int) (*github.PullRequest, error)
// AddLabel adds a label to a pull request.
AddLabel(ctx context.Context, repo github.Repository, number int, label string) error
// RemoveLabel removes a label from a pull request.
RemoveLabel(ctx context.Context, repo github.Repository, number int, label string) error
}
GitHubClient defines the interface for GitHub operations needed for sync.
type LabelManager ¶
type LabelManager interface {
// GetCurrentState determines the current state from GitHub labels.
GetCurrentState(pr github.PullRequest) pr.State
// SyncToGitHub ensures GitHub labels match the desired state.
SyncToGitHub(ctx context.Context, pr github.PullRequest, state pr.State) (*SyncResult, error)
// ApplyState applies a state to a PR by updating its labels atomically.
ApplyState(ctx context.Context, pr github.PullRequest, state pr.State) error
}
LabelManager defines the interface for GitHub label state management.
type Logger ¶
type Logger interface {
Info(msg string, keysAndValues ...interface{})
Error(err error, msg string, keysAndValues ...interface{})
V(level int) Logger
}
Logger defines the logging interface used by the coordinator.
type SyncResult ¶
type SyncResult struct {
// InitialState is the state before synchronization
InitialState pr.State
// FinalState is the state after synchronization
FinalState pr.State
// LabelsAdded contains the labels that were added
LabelsAdded []string
// LabelsRemoved contains the labels that were removed
LabelsRemoved []string
// Errors contains any errors that occurred during sync
Errors []error
// SyncTime is when the synchronization completed
SyncTime time.Time
}
SyncResult represents the result of a synchronization operation.