git

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 62 Imported by: 0

Documentation

Overview

Package git provides Git repository operations and abstractions for the GitOps Reverser controller.

Index

Constants

View Source
const (

	// DefaultCommitWindow is the default rolling silence window used to coalesce
	// events into one commit per (author, gitTarget). Applied when
	// GitProvider.spec.push.commitWindow is unset or unparseable.
	DefaultCommitWindow = 5 * time.Second

	// PushCooldown is the minimum interval between successful pushes. The cooldown
	// is intentionally fixed: commit cadence is a user concern (commitWindow on
	// the CRD); push cadence is an implementation/politeness concern.
	PushCooldown = 5 * time.Second
)
View Source
const (
	// SigningKeyDataKey is the Secret data key for the PEM-encoded SSH private signing key.
	SigningKeyDataKey = "signing.key"
	// SigningPublicKeyDataKey is the Secret data key for the authorized_keys-format public key.
	SigningPublicKeyDataKey = "signing.pub"
	// SigningPassphraseDataKey is the Secret data key for an optional key passphrase.
	SigningPassphraseDataKey = "passphrase"
)
View Source
const (
	// DefaultCommitterName matches the default operator identity in Git history.
	DefaultCommitterName = "GitOps Reverser"
	// DefaultCommitterEmail matches the default operator email in Git history.
	DefaultCommitterEmail = "noreply@configbutler.ai"
	// DefaultEventCommitMessageTemplate reproduces the current per-event commit message shape.
	DefaultEventCommitMessageTemplate = "[{{.Operation}}] {{.APIVersion}}/{{.Resource}}/{{.Name}}"
	// DefaultReconcileCommitMessageTemplate is the default reconcile commit message shape.
	// It names the synced type for a per-type splice (e.g. "reconciled 6 secrets (last
	// resourceVersion: 1331)"), so the otherwise-indistinguishable per-type reconciles a single
	// GitTarget produces become self-describing — and the pinned resourceVersion shows exactly
	// how fresh the reconcile is, which is useful for demos and first-user trust. The plural
	// resource alone (no group/version) is chosen for readability; a custom template can add
	// {{.APIVersion}} when cross-group plural collisions matter. The {{if .Resource}} and
	// {{if .Revision}} guards fall back to "reconciled N resources" for a whole-target reconcile
	// (nil ScopeGVR) or the events-based atomic path, where the type/revision fields are empty —
	// so the subject never degrades to a trailing-space, identity-less "reconciled N ".
	DefaultReconcileCommitMessageTemplate = "reconciled {{.Count}} " +
		"{{if .Resource}}{{.Resource}}{{else}}resources{{end}}" +
		"{{if .Revision}} (last resourceVersion: {{.Revision}}){{end}}"
	// DefaultGroupCommitMessageTemplate is the default message shape for
	// finalized commit-window commits that contain multiple events.
	DefaultGroupCommitMessageTemplate = "{{.Author}} on {{.GitTarget}}: {{.Count}} resource(s)"
)
View Source
const DefaultBranchBufferMaxBytes int64 = 8 * 1024 * 1024

DefaultBranchBufferMaxBytes is the default cap on a worker's combined event buffer + unpushed-events memory. Operators override this via --branch-buffer-max-size (8Mi by default).

View Source
const (
	// EncryptionProviderSOPS is the only supported provider in this increment.
	EncryptionProviderSOPS = "sops"
)

Variables

View Source
var (
	ErrRemoteRefNotFound          = errors.New("remote ref not found")
	ErrRemoteRefNotFoundEmptyRepo = errors.New("remote ref not found (empty repo)")
)
View Source
var ErrFinalizeQueueFull = errors.New("branch worker event queue full; item dropped")

ErrFinalizeQueueFull is reported when a work item cannot be enqueued because the worker's event queue is saturated.

Functions

func AuthFromSecretData

func AuthFromSecretData(
	ctx context.Context,
	k8sClient client.Client,
	provider *v1alpha3.GitProvider,
	secret *corev1.Secret,
	hostKeys SSHHostKeyConfig,
) (transport.AuthMethod, error)

AuthFromSecretData resolves a go-git auth method from an already-fetched Git credentials Secret, accepting the Kubernetes-native, Flux, and Argo CD key dialects (the credentials Secret is the one portable artifact across those ecosystems). provider supplies the namespace and the optional knownHostsRef for SSH host trust; hostKeys supplies the install-level default and the dev escape hatch. Auth precedence is: SSH key (if present) → HTTP basic (username+password) → bearer token.

func ConstructSafeEmail

func ConstructSafeEmail(username string, domain string) string

ConstructSafeEmail takes a raw username and a domain and creates a valid git-compliant email address.

func GenerateSSHSigningKeyPair

func GenerateSSHSigningKeyPair(passphrase []byte) ([]byte, []byte, error)

GenerateSSHSigningKeyPair creates an ed25519 SSH signing keypair.

func GetCommitSigner

func GetCommitSigner(
	ctx context.Context,
	k8sClient client.Client,
	provider *v1alpha3.GitProvider,
) (gogit.Signer, error)

GetCommitSigner fetches commit signing material from the specified secret.

func GetCurrentBranch

func GetCurrentBranch(r *git.Repository) (plumbing.ReferenceName, plumbing.Hash, error)

GetCurrentBranch gets the branch that is active.

func GetHTTPAuthMethod

func GetHTTPAuthMethod(username, password string) (transport.AuthMethod, error)

GetHTTPAuthMethod returns an HTTP basic authentication method from username and password.

func GetHTTPTokenAuthMethod

func GetHTTPTokenAuthMethod(token string) (transport.AuthMethod, error)

GetHTTPTokenAuthMethod returns an HTTP bearer-token authentication method. Both Flux and Argo CD store token credentials (GitHub fine-grained PATs, GitLab project/group access tokens) under a "bearerToken" Secret key and authenticate without a username; go-git's TokenAuth sends the token as an Authorization: Bearer header.

func IsValidTargetPath

func IsValidTargetPath(p string) bool

IsValidTargetPath reports whether p is a path the writer can safely materialize into: the repository root (empty or "."), or a clean relative path. Paths the writer rejects as unsafe — absolute (leading "/"), Windows separators, or ".." traversal — are invalid and can own nothing. It mirrors sanitizePath, the write-path guard, so the overlap/admission check and the writer agree on what a target legitimately owns.

func LoadSSHCommitSigner

func LoadSSHCommitSigner(secret *corev1.Secret) (gogit.Signer, error)

LoadSSHCommitSigner loads a git-compatible SSH signer from the provided Secret.

func PushAtomic

func PushAtomic(
	ctx context.Context,
	repo *git.Repository,
	rootHash plumbing.Hash,
	rootBranch plumbing.ReferenceName,
	auth transport.AuthMethod,
) error

PushAtomic performs an atomic PushAtomic operation in a single network session. It checks if the remote branch is not touched before pushing to prevent creating diverged branches. An explcit error is returned if it failed: I don't plan to use these, we can always retry...

func SSHAuthorizedPublicKeyFromSecret

func SSHAuthorizedPublicKeyFromSecret(secret *corev1.Secret) (string, error)

SSHAuthorizedPublicKeyFromSecret derives the authorized_keys-form public key from a signing Secret.

func SmartFetch

SmartFetch performs a network sync and returns the best available LOCAL branch reference. It prioritizes the target branch but always fetches the default branch as a safety net.

Return values (example with target="refs/heads/feature"): - "refs/heads/feature", nil: Target found on remote, fetched, ready to checkout. - "refs/heads/main", nil: Target missing on remote, fell back to default branch. - "", nil: No valid branches found (empty repo).

func ValidateCommitConfig

func ValidateCommitConfig(config CommitConfig) error

ValidateCommitConfig checks that commit templates are syntactically valid.

Types

type AttachCommitRequest

type AttachCommitRequest struct {
	// Namespace, Name, UID identify the CommitRequest. UID may be empty (a
	// Metadata-level audit policy can omit it); identity then keys on
	// namespace/name only.
	Namespace string
	Name      string
	UID       string

	// Author is the effective user that requested the finalize, attributed from
	// the CommitRequest's own create audit event. Only a window whose author
	// matches is attached; this binds "the open window" to "the requesting
	// author's open window".
	Author string
	// GitTargetName / GitTargetNamespace scope the finalize to one GitTarget.
	GitTargetName      string
	GitTargetNamespace string

	// Message is the verbatim commit message to attach to the window. Empty keeps
	// the generated grouped-commit message.
	Message string
	// CloseDelaySeconds is the close-delay collect window: the worker closes the
	// attached window and finalizes it at receipt + CloseDelaySeconds (the delay is
	// anchored at attribution, §6.4.4).
	CloseDelaySeconds int32
}

AttachCommitRequest is the "bind this CommitRequest's message to the author's open window, then finalize that window after the grace" work item (§6.4 of docs/spec/commitrequest-design.md). It rides the same per-worker FIFO event queue as resource events, so by audit-stream ordering it is processed after every earlier write for that worker. Re-sends are idempotent: the worker keys pending requests by identity and keeps the first finalize deadline.

type BranchInfo

type BranchInfo struct {
	ShortName string // e.g., "main"
	Sha       string // commit hash, normally the tip of the default branch. But will be empty ("") for an unborn branch that is going to be orphaned branch (if the default branch does not exist)
	Unborn    bool   // Is true for branches that don't have commits yet: only HEAD is configured to it
}

BranchInfo contains information about a Git branch.

type BranchKey

type BranchKey struct {
	// RepoNamespace is the namespace containing the GitProvider.
	RepoNamespace string
	// RepoName is the name of the GitProvider.
	RepoName string
	// Branch is the Git branch name.
	Branch string
}

BranchKey uniquely identifies a (GitProvider, Branch) combination. This is the unit of worker ownership to prevent merge conflicts. Multiple GitTargets can share the same BranchKey (same provider+branch) but write to different paths within that branch.

func (BranchKey) String

func (k BranchKey) String() string

String returns a string representation for logging and debugging. Format: "namespace/provider-name/branch".

type BranchWorker

type BranchWorker struct {
	// Identity (immutable after creation)
	GitProviderRef       string
	GitProviderNamespace string
	Branch               string

	// Dependencies
	Client client.Client
	Log    logr.Logger
	// contains filtered or unexported fields
}

BranchWorker processes events for a single (GitProvider, Branch) combination. It can serve multiple GitTargets that write to different paths in the same branch. This design ensures serialized commits per branch, preventing merge conflicts.

func NewBranchWorker

func NewBranchWorker(
	client client.Client,
	log logr.Logger,
	providerName, providerNamespace string,
	branch string,
	writer *contentWriter,
	branchBufferMaxBytes int64,
) *BranchWorker

NewBranchWorker creates a worker for a (provider, branch) combination. Pass 0 (or a negative value) for branchBufferMaxBytes to use DefaultBranchBufferMaxBytes.

func (*BranchWorker) Enqueue

func (w *BranchWorker) Enqueue(event Event) bool

Enqueue adds a single live event to this worker's queue. It reports whether the event entered the FIFO; a false return means the queue was full and the event was dropped, so a caller advancing a durable watch cursor past this event must not treat the drop as success (see reconcile.GitTargetEventStream.OnWatchEvent).

func (*BranchWorker) EnqueueAttach

func (w *BranchWorker) EnqueueAttach(req *AttachCommitRequest)

EnqueueAttach adds a CommitRequest attach to this worker's queue. Riding the same queue as resource events is what makes it process in audit order, after every earlier write. The attach is fire-and-forget: the controller polls the outcome via LookupCommitRequestOutcome and re-sends idempotently, so a queue- full drop is recovered by the next poll rather than a synchronous reply.

func (*BranchWorker) EnqueueRequest

func (w *BranchWorker) EnqueueRequest(request *WriteRequest)

EnqueueRequest adds a write request to this worker's queue.

func (*BranchWorker) EnqueueResync

func (w *BranchWorker) EnqueueResync(request *ResyncRequest) bool

EnqueueResync adds a resync request to this worker's queue. Like a finalize signal it rides the same queue as resource events, so it is applied in order with live events: a resync enqueued during the snapshot window lands before the buffered live events that follow it. If the queue is full the request is dropped and its caller is notified immediately via the result channel.

It reports whether the request actually entered the FIFO. A dropped request never reached the queue, so a caller that gates downstream state on the resync's ordering (the per-type coverage watermark, signing-snapshot-tail-replay-failure-investigation.md §7.4) must not treat a drop as success — it would mark the target reconciled-through-Hc with no reconcile ever queued.

func (*BranchWorker) EnsurePathBootstrapped

func (w *BranchWorker) EnsurePathBootstrapped(path, targetName, targetNamespace string) error

EnsurePathBootstrapped prepares bootstrap templates locally for a path. Existing files are preserved, and only missing template files are added. The files are staged in the local worktree but never committed or pushed here.

func (*BranchWorker) GetBranchMetadata

func (w *BranchWorker) GetBranchMetadata() (bool, string, time.Time)

GetBranchMetadata returns current branch status without syncing. This is primarily used for quick status checks without triggering Git operations.

func (*BranchWorker) LookupCommitRequestOutcome

func (w *BranchWorker) LookupCommitRequestOutcome(namespace, name, uid string) (FinalizeResult, bool)

LookupCommitRequestOutcome returns a resolved CommitRequest outcome, or ok=false when the request is still in flight (or already GC'd). The controller polls this after sending its AttachCommitRequest.

func (*BranchWorker) Start

func (w *BranchWorker) Start(parentCtx context.Context) error

Start begins processing events.

func (*BranchWorker) Stop

func (w *BranchWorker) Stop()

Stop gracefully shuts down the worker.

func (*BranchWorker) SyncAndGetMetadata

func (w *BranchWorker) SyncAndGetMetadata(ctx context.Context) (*PullReport, error)

SyncAndGetMetadata fetches latest metadata from remote Git repository. Uses caching to avoid redundant fetches within 30 seconds (optimization for multiple GitTargets sharing the same branch). Returns PullReport containing branch existence, HEAD SHA, and other metadata.

type CommitConfig

type CommitConfig struct {
	Committer CommitterConfig
	Message   CommitMessageConfig
}

CommitConfig is the resolved commit behavior used by the git writer.

func ResolveCommitConfig

func ResolveCommitConfig(spec *v1alpha3.CommitSpec) CommitConfig

ResolveCommitConfig resolves API commit settings into runtime defaults.

type CommitFile

type CommitFile struct {
	Path    string
	Content []byte
}

CommitFile represents a single file to be committed.

type CommitMessageConfig

type CommitMessageConfig struct {
	EventTemplate     string
	ReconcileTemplate string
	GroupTemplate     string
}

CommitMessageConfig contains the resolved per-event, reconcile, and grouped templates.

type CommitMessageData

type CommitMessageData struct {
	Operation  string
	Group      string
	Version    string
	Resource   string
	Namespace  string
	Name       string
	APIVersion string
	Username   string
	GitTarget  string
}

CommitMessageData is the template context for per-event commit messages.

type CommitMessageKind

type CommitMessageKind string

CommitMessageKind determines which message/authorship path the executor uses.

const (
	CommitMessagePerEvent  CommitMessageKind = "event"
	CommitMessageReconcile CommitMessageKind = "reconcile"
	CommitMessageGrouped   CommitMessageKind = "group"
)

type CommitMode

type CommitMode string

CommitMode defines how a write request should be committed.

const (
	// CommitModePerEvent streams request events through the live commit window.
	// With commitWindow=0 each event finalizes immediately; otherwise events
	// coalesce by author, target, and quiet-window boundaries.
	CommitModePerEvent CommitMode = "per_event"
	// CommitModeAtomic creates one commit for all events in the request.
	CommitModeAtomic CommitMode = "atomic"
)

type CommitterConfig

type CommitterConfig struct {
	Name  string
	Email string
}

CommitterConfig defines the operator identity used as the git committer.

type Encryptor

type Encryptor interface {
	Encrypt(ctx context.Context, plain []byte, meta ResourceMeta) ([]byte, error)
}

Encryptor transforms plaintext bytes into encrypted bytes.

type Event

type Event struct {
	// Object is the sanitized Kubernetes object. Exactly one of Object or
	// FieldPatch is set for a resource mutation; a control or DELETE event may
	// carry neither.
	Object *unstructured.Unstructured

	// FieldPatch, when set, replaces Object with a bounded in-place edit of an
	// existing parent manifest (subresource audit resolution). It is mutually
	// exclusive with Object.
	FieldPatch *FieldPatch

	// Identifier contains resource identification information.
	Identifier types.ResourceIdentifier

	// Operation is the admission operation (CREATE, UPDATE, DELETE).
	Operation string

	// AuditStreamID is the FULL Redis stream position "<rv>-<seq>" this change was recorded at
	// on the per-type audit stream. It is set ONLY on the audit-tail path (ReadTypeAuditChanges)
	// and read by the per-(GitTarget, GVR) coverage-watermark gate in applyAuditChangesForType to
	// decide whether the entry is historical for a target (id <= Hc, suppress) or live (id > Hc,
	// route). The sub-sequence is load-bearing: distinct entries can share an rv (an rv-less
	// DELETE/Status rides the high-water, duplicate/same-rv writes get fresh seqs), so the gate
	// compares full positions, not bare rvs. Empty on the live admission path; not used by the
	// writer. See docs/finished/signing-snapshot-tail-replay-failure-investigation.md §7.
	AuditStreamID string

	// UserInfo contains user information for commit messages.
	UserInfo UserInfo

	// Path is the POSIX-like relative path prefix for this event's files.
	// This comes from the GitTarget that triggered this event.
	// Empty string means write to repository root.
	Path string

	// GitTargetName is the target owning this event.
	GitTargetName string

	// GitTargetNamespace is the namespace of the target owning this event.
	GitTargetNamespace string

	// BootstrapOptions controls path-scoped bootstrap file staging for this event.
	BootstrapOptions pathBootstrapOptions
}

Event represents a resource change event to be processed by a branch worker. Branch comes from the worker context (not stored in event). Path comes from the GitTarget that created this event.

func (Event) IsFieldPatch

func (e Event) IsFieldPatch() bool

IsFieldPatch reports whether the event carries a bounded field patch instead of a full object. It is the single predicate the pipeline branches on to route a patch to the in-place writer rather than the object writer.

type FieldPatch

type FieldPatch struct {
	// Assignments are the (path, value) pairs to set on the parent manifest. Paths
	// are disjoint; each owns only its own subtree, so the patch is additive and
	// leaves every unmentioned field in Git untouched.
	Assignments []manifestedit.FieldAssignment
	// Source is a bounded origin label for commit messages and metrics, e.g.
	// "deployments/scale". Never the request URI.
	//
	// The parent Kind is intentionally NOT carried here. The audit objectRef gives
	// only the GVR (plural resource), and the subresource body's own Kind (e.g.
	// "Scale") is not the parent's. The writer resolves the parent document from the
	// objectRef GVR through the same resource-identity inventory the GVR-only delete
	// uses — it already has the live-catalog mapper — so the consumer never needs
	// GVR->GVK resolution.
	Source string
}

FieldPatch is a bounded set of field assignments to an existing parent manifest, carried in place of a full Object. It is how an author-preserving subresource mutation (e.g. deployments/scale) reaches Git: set exactly the audited field paths on the already committed parent, never reconstructing the whole object. See docs/spec/scale-subresource-audit-rehydration.md.

type FinalizeOutcome

type FinalizeOutcome string

FinalizeOutcome is the terminal result of resolving a CommitRequest.

const (
	// FinalizeCommitted means an open commit window was finalized into a commit.
	FinalizeCommitted FinalizeOutcome = "Committed"
	// FinalizeNoOpenWindow means no matching same-author window was collected
	// within the grace, so nothing was committed for the request.
	FinalizeNoOpenWindow FinalizeOutcome = "NoOpenWindow"
	// FinalizeWindowMismatch means the open window belonged to a different author
	// or GitTarget than the request, so it was left untouched.
	FinalizeWindowMismatch FinalizeOutcome = "WindowMismatch"
	// FinalizeAlreadyPresent means a matching window was finalized but its events
	// produced no diff — the change already matches the remote, so no commit was
	// made (loop prevention). Resolved at finalize, never waiting on a push.
	FinalizeAlreadyPresent FinalizeOutcome = "AlreadyPresent"
)

type FinalizeResult

type FinalizeResult struct {
	// Outcome is set when Err is nil.
	Outcome FinalizeOutcome
	// SHA is the resulting commit SHA when Outcome is FinalizeCommitted.
	SHA string
	// Branch is the branch the worker operates on.
	Branch string
	// Err is set when the request could not be completed.
	Err error
}

FinalizeResult carries the resolved outcome of a CommitRequest back to the controller, polled via LookupCommitRequestOutcome.

type GroupedCommitMessageData

type GroupedCommitMessageData struct {
	// Author is the verbatim event.UserInfo.Username for the group.
	Author string
	// GitTarget is the single target this commit is bound to.
	GitTarget string
	// Count is the number of distinct resources committed.
	Count int
	// Operations counts events by operation kind (CREATE/UPDATE/DELETE).
	Operations map[string]int
	// Resources is the per-resource list, deduplicated by file path so the
	// final state is what's being committed.
	Resources []ResourceRef
}

GroupedCommitMessageData is the template context for grouped commit messages. Each grouped commit covers exactly one (author, gitTarget) tuple (see docs/spec/commit-window-refactor.md).

type PathRefusalReporter

type PathRefusalReporter func(target itypes.ResourceReference, refused *manifestanalyzer.AcceptanceRefusedError)

PathRefusalReporter surfaces a refused write plan to the layer that owns GitTarget status. A refusal is not a transient write fault: the acceptance gate or a write-boundary precondition aborted the flush before any byte was written, nothing was committed, and only a human editing the Git path can clear it — so it must reach the user as GitPathAccepted=False / Stalled=True rather than being logged and dropped.

The resync path already carries its refusal back on ResyncResult.Err, where the watch layer classifies it. The live-event paths have no result channel — a window is finalized on a timer, and its failure used to be logged and dropped — so they report through this hook instead. The watch Manager supplies it (WorkerManager.SetPathRefusalReporter), which is why the reason mapping lives there and not here.

type PendingWrite

type PendingWrite struct {
	Kind               PendingWriteKind
	Events             []Event
	CommitMessage      string
	CommitConfig       CommitConfig
	Signer             gogit.Signer
	GitTargetName      string
	GitTargetNamespace string
	Targets            map[pendingTargetKey]ResolvedTargetMetadata
	ByteSize           int64

	// Desired is the complete desired resource snapshot, set only for a
	// PendingWriteResync. The worker folds it over the worktree's content-derived
	// store to produce the resync plan (upserts + mark-and-sweep drops).
	Desired []manifestanalyzer.DesiredResource
	// ScopeGVR, when set, restricts the resync's mark-and-sweep to one type's
	// (group, resource): the M12 per-type reconcile/sweep. Desired then carries only
	// that type's objects (empty for a pure sweep), and no sibling type's document is
	// ever dropped. Nil is the whole-GitTarget resync.
	ScopeGVR *schema.GroupVersionResource
	// Revision is the cluster snapshot resourceVersion the desired set is pinned to
	// (the joined streaming-watch bookmark). Carried for diagnostics and logging.
	Revision string
	// ResyncStats, when non-nil, is populated during apply with the plan's
	// create/update/delete/skip counts so a synchronous caller can report them.
	ResyncStats *ResyncStats
	// Committed, when non-nil, is set true during apply iff the resync produced a
	// commit. A no-op resync (e.g. an empty initial snapshot) must not be retained or
	// pushed: doing so would advance the push cooldown and delay the next real
	// snapshot's push past its window.
	Committed *bool

	// CommitRequest, when set, is the CommitRequest claiming this write: it is
	// resolved Committed (with CommitSHA) once this write is pushed (§6.5 of
	// docs/spec/commitrequest-design.md). It rides the write through the
	// push cooldown and the conflict rebase-replay, so the result follows the data.
	CommitRequest *commitRequestID
	// CommitSHA is the hash of the commit this write created, captured in
	// executePendingWrite and refreshed when the write is re-executed on a
	// rebase-replay (so it is never a stale pre-rebase hash). Zero when the write
	// produced no commit (no diff).
	CommitSHA plumbing.Hash
}

PendingWrite is the unit retained until a push succeeds.

func (PendingWrite) Author

func (p PendingWrite) Author() string

Author returns the grouped commit author username for commit-shaped pending writes. It is the stable identity used for window coalescing and the grouped commit message; see AuthorUserInfo for the full signing identity.

func (PendingWrite) AuthorUserInfo

func (p PendingWrite) AuthorUserInfo() UserInfo

AuthorUserInfo returns the full author identity for commit-shaped pending writes, including any OIDC display name and email. Atomic and empty writes have no per-user author and return the zero value.

func (PendingWrite) MessageKind

func (p PendingWrite) MessageKind() CommitMessageKind

MessageKind is derived from the pending write's shape.

func (PendingWrite) Target

Target returns the single resolved target metadata for this pending write.

type PendingWriteKind

type PendingWriteKind string

PendingWriteKind distinguishes the durable write shapes retained until push.

const (
	// PendingWriteCommit is one finalized commit-shaped live-event window.
	PendingWriteCommit PendingWriteKind = "grouped_window"
	// PendingWriteAtomic is a caller-defined atomic request, typically from
	// reconciliation.
	PendingWriteAtomic PendingWriteKind = "atomic"
	// PendingWriteResync is a streaming-snapshot resync (M8): it carries the COMPLETE
	// desired resource set for one GitTarget, and the worker materialises it with a
	// content-derived mark-and-sweep against the worktree (upsert every desired
	// resource, drop every watched managed document the snapshot did not contain).
	PendingWriteResync PendingWriteKind = "resync"
)

type PullReport

type PullReport struct {
	ExistsOnRemote  bool // Branch exists on remote
	HEAD            BranchInfo
	IncomingChanges bool // SHA changed, requiring resource-level reconcile
}

PullReport provides detailed pull operation results.

func PrepareBranch

func PrepareBranch(
	ctx context.Context,
	repoURL, repoPath, targetBranchName string,
	auth transport.AuthMethod,
) (*PullReport, error)

PrepareBranch clones repository immediately when GitDestination is created, optimized for single branch usage. It tries to fetch the useful branch: either target or default.

type ReconcileCommitMessageData

type ReconcileCommitMessageData struct {
	Count      int
	GitTarget  string
	Group      string
	Version    string
	Resource   string
	APIVersion string
	Revision   string
}

ReconcileCommitMessageData is the template context for reconcile commit messages.

Group, Version, Resource, and APIVersion name the synced type, mirroring the per-event CommitMessageData fields so a reconcile template can identify its type exactly as a per-event template does. They are populated for a per-type splice (M12/R2 per-type reconcile, whose ResyncRequest carries a non-nil ScopeGVR) and left empty for a whole-target reconcile or the events-based atomic path. Revision is the cluster resourceVersion the desired set was pinned to (empty for a pure sweep or the events-based path). Any template that references these fields must render cleanly when they are absent — the default guards both with {{if}}.

type RepoInfo

type RepoInfo struct {
	DefaultBranch     *BranchInfo
	RemoteBranchCount int
}

RepoInfo represents high-level repository information.

func CheckRepo

func CheckRepo(ctx context.Context, repoURL string, auth transport.AuthMethod) (*RepoInfo, error)

CheckRepo performs lightweight connectivity checks and gathers repository metadata.

type ResolvedEncryptionConfig

type ResolvedEncryptionConfig struct {
	Provider      string
	AgeRecipients []string
}

ResolvedEncryptionConfig contains runtime encryption settings resolved from GitTarget spec.

It carries public age recipients only. The write path encrypts, it never decrypts, so no private age identity is retained, written to disk, or passed to the sops process. See docs/rbac.md.

func ResolveTargetEncryption

func ResolveTargetEncryption(
	ctx context.Context,
	k8sClient client.Client,
	target *v1alpha3.GitTarget,
) (*ResolvedEncryptionConfig, error)

ResolveTargetEncryption resolves and validates GitTarget encryption configuration.

type ResolvedTargetMetadata

type ResolvedTargetMetadata struct {
	Name             string
	Namespace        string
	Path             string
	BootstrapOptions pathBootstrapOptions
	EncryptionConfig *ResolvedEncryptionConfig
	// Placement is the GitTarget's declared new-file placement policy, resolved
	// from spec.placement. Nil when the GitTarget declares none, in which case new
	// resources are placed by sibling inference and then the canonical path.
	Placement *manifestanalyzer.PlacementPolicy
}

ResolvedTargetMetadata is the target-scoped planning data retained with a pending write so replay does not re-fetch mutable GitTarget state.

type ResourceMeta

type ResourceMeta struct {
	Identifier      itypes.ResourceIdentifier
	UID             string
	ResourceVersion string
	Generation      int64
}

ResourceMeta is passed to encryptors for context and diagnostics.

type ResourceRef

type ResourceRef struct {
	Group     string
	Version   string
	Resource  string
	Namespace string
	Name      string
}

ResourceRef is the lightweight resource identifier emitted to grouped commit templates via GroupedCommitMessageData.Resources.

func (ResourceRef) String

func (r ResourceRef) String() string

String renders the ref as group/version/resource[/namespace]/name. The format mirrors ResourceIdentifier.String for templates that want to {{range}} over Resources and just print each entry.

type ResyncRequest

type ResyncRequest struct {
	Desired            []manifestanalyzer.DesiredResource
	Revision           string
	GitTargetName      string
	GitTargetNamespace string
	// ScopeGVR, when set, makes this a per-type (M12) reconcile/sweep: the mark-and-sweep
	// is restricted to the named type's (group, resource) and Desired carries only that
	// type's objects (empty = pure sweep of a removed type). Nil is a whole-GitTarget resync.
	ScopeGVR *schema.GroupVersionResource
	// Heal marks a non-urgent drift-correcting resync (a periodic checkpoint re-anchor or a
	// removed-type sweep) that the worker DEFERS while a commit window is open, instead of
	// force-finalizing it. Because one worker serves N GitTargets and the commit window is a
	// worker singleton, a force-finalizing heal can steal a DIFFERENT GitTarget's held
	// CommitRequest window — the 8f2ad84 regression. A heal therefore waits for the worker to be
	// idle (no open window), a boundary that recurs on every silence timeout and identity switch,
	// so it never starves and, when it runs, has no window to steal. A first-sync backfill is NOT
	// a heal: it must establish initial state promptly and is ordered before the audit tail.
	Heal bool
	// Result receives exactly one reply. It is buffered (cap 1) by the emitter so
	// the worker never blocks delivering it.
	Result chan ResyncResult
}

ResyncRequest is a synchronous resync of one GitTarget against a complete, revision-pinned desired snapshot (M8). It rides the worker queue so the single git-mutating goroutine applies it in order with live events, and replies on Result once the local commit is created. The desired set is the whole watched resource state at Revision; the worker's content-derived mark-and-sweep drops any managed document the snapshot did not contain.

type ResyncResult

type ResyncResult struct {
	Stats ResyncStats
	Err   error
}

ResyncResult is the reply to a ResyncRequest: the plan's change counts, or an error if the resync could not be applied (in which case nothing was committed).

type ResyncStats

type ResyncStats struct {
	Created          int
	Updated          int
	Deleted          int
	Skipped          int
	PlacementSkipped int
}

ResyncStats summarises what a resync changed, for GitTarget status. Created, Updated, and Deleted are the materialised create / patch+replace / managed-drop counts; Skipped is documents present but not safely editable (e.g. encrypted or disallowed constructs). PlacementSkipped is new resources the writer refused to place fail-safe — placement could not be resolved safely, or the write would co-mingle sensitive and plaintext documents (placement Option B2). It is counted (not silently swallowed) and logged per-resource so a not-mirrored resource is visible in the resync summary; it is not (yet) surfaced as a dedicated GitTarget status condition.

type SOPSEncryptor

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

SOPSEncryptor encrypts YAML by invoking the external sops binary.

func NewSOPSEncryptor

func NewSOPSEncryptor(binaryPath, configPath string) *SOPSEncryptor

NewSOPSEncryptor creates an Encryptor that shells out to sops.

func NewSOPSEncryptorWithEnv

func NewSOPSEncryptorWithEnv(binaryPath, configPath, workDir string, env map[string]string) *SOPSEncryptor

NewSOPSEncryptorWithEnv creates an Encryptor that shells out to sops with additional environment variables.

func (*SOPSEncryptor) Encrypt

func (e *SOPSEncryptor) Encrypt(ctx context.Context, plain []byte, meta ResourceMeta) ([]byte, error)

Encrypt streams plaintext YAML to sops over stdin and returns encrypted YAML bytes.

type SSHHostKeyConfig

type SSHHostKeyConfig struct {
	// ControllerNamespace is the namespace the controller runs in; it scopes the install-level
	// default known-hosts ConfigMap.
	ControllerNamespace string

	// DefaultKnownHostsConfigMap names an optional install-level ConfigMap in ControllerNamespace
	// that supplies known_hosts when neither the credentials Secret nor the GitProvider supplies
	// it. Empty disables this layer.
	DefaultKnownHostsConfigMap string

	// AllowMissingKnownHosts permits SSH only when NO host-key source produced any known_hosts at
	// all (the controller's --insecure-allow-missing-known-hosts flag). A known_hosts that is
	// present but unparseable is always a hard error.
	AllowMissingKnownHosts bool
}

SSHHostKeyConfig configures where SSH known_hosts (host-trust material) are sourced and the dev-only escape hatch for a host with no pinned key. It is set once at startup and threaded to every credentials read. Its zero value fails closed: no install-level default and no missing-key opt-out.

type UserInfo

type UserInfo struct {
	Username string
	UID      string
	// DisplayName is the human-readable name from the OIDC "name" claim, when
	// the audit event carries it. Empty means "fall back to Username".
	DisplayName string
	// Email is the address from the OIDC "email" claim, when the audit event
	// carries it. Empty means "fall back to ConstructSafeEmail(Username)".
	Email string
}

UserInfo contains relevant user information for commit messages.

type WorkItem

type WorkItem struct {
	// Request is a resource-write request.
	Request *WriteRequest
	// Attach is a CommitRequest attach: bind a message to the author's window and
	// finalize it after the grace.
	Attach *AttachCommitRequest
	// Resync is a streaming-snapshot resync request (M8): a synchronous
	// request/reply that materialises a GitTarget's complete desired set.
	Resync *ResyncRequest
}

WorkItem is the unit of work in the BranchWorker queue. Exactly one of Request, Attach, or Resync is set.

type WorkerManager

type WorkerManager struct {
	Client client.Client
	Log    logr.Logger
	// contains filtered or unexported fields
}

WorkerManager manages BranchWorkers. Creates workers per (repo, branch), shared by multiple GitDestinations. Implements controller-runtime's Runnable interface for lifecycle management.

func NewWorkerManager

func NewWorkerManager(
	client client.Client,
	log logr.Logger,
	branchBufferMaxBytes int64,
	sensitiveResources types.SensitiveResourcePolicy,
) *WorkerManager

NewWorkerManager creates a new worker manager. branchBufferMaxBytes bounds each worker's combined buffer + unpushed-events memory. Pass 0 (or a negative value) to use DefaultBranchBufferMaxBytes.

func (*WorkerManager) EnsureWorker

func (m *WorkerManager) EnsureWorker(
	_ context.Context,
	providerName, providerNamespace string,
	branch string,
) error

EnsureWorker ensures a worker exists for the given (provider, branch). Worker creation/start is protected by the manager lock.

func (*WorkerManager) GetWorkerForTarget

func (m *WorkerManager) GetWorkerForTarget(
	providerName, providerNamespace string,
	branch string,
) (*BranchWorker, bool)

GetWorkerForTarget finds the worker for a target's (provider, branch). Returns the worker and true if found, nil and false otherwise. This is used by EventRouter to dispatch events to the correct worker.

func (*WorkerManager) NeedLeaderElection

func (m *WorkerManager) NeedLeaderElection() bool

NeedLeaderElection ensures only the elected leader manages workers. This prevents multiple pods from managing the same workers.

func (*WorkerManager) ReconcileWorkers

func (m *WorkerManager) ReconcileWorkers(ctx context.Context) error

ReconcileWorkers checks active GitTargets and cleans up orphaned workers. This ensures workers are removed when their GitTargets are deleted.

func (*WorkerManager) RegisterTarget

func (m *WorkerManager) RegisterTarget(
	ctx context.Context,
	targetName, targetNamespace string,
	providerName, providerNamespace string,
	branch, path string,
) error

RegisterTarget ensures a worker exists for the target's (provider, branch) and registers the target with that worker. This is called by GitTarget controller when a target becomes Ready.

func (*WorkerManager) SetMapper

func (m *WorkerManager) SetMapper(mapper typeset.Lookup)

SetMapper injects the GVK->GVR resolver used by every worker's store scan. It is called once at startup, before any GitTarget registers a worker, so each worker created by EnsureWorker carries it.

func (*WorkerManager) SetPathRefusalReporter

func (m *WorkerManager) SetPathRefusalReporter(reporter PathRefusalReporter)

SetPathRefusalReporter injects the hook every worker calls when a live write plan is refused, so the refusal reaches GitTarget status instead of being logged and dropped. Like SetMapper, it is called once at startup before any worker is created.

func (*WorkerManager) SetSSHHostKeyConfig

func (m *WorkerManager) SetSSHHostKeyConfig(cfg SSHHostKeyConfig)

SetSSHHostKeyConfig injects the SSH host-key resolution config used by every worker's credential reads. Like SetMapper, it is called once at startup before any worker is created.

func (*WorkerManager) Start

func (m *WorkerManager) Start(ctx context.Context) error

Start implements manager.Runnable interface. This is called by controller-runtime when the manager starts.

func (*WorkerManager) UnregisterTarget

func (m *WorkerManager) UnregisterTarget(
	_, _ string,
	providerName, providerNamespace string,
	branch string,
) error

UnregisterTarget removes a GitTarget from its worker. Destroys the worker if it was the last target using it. This is called by GitTarget controller when a target is deleted.

type WriteRequest

type WriteRequest struct {
	Events             []Event
	CommitMessage      string
	CommitConfig       *CommitConfig
	Signer             gogit.Signer
	GitTargetName      string
	GitTargetNamespace string
	BootstrapOptions   pathBootstrapOptions
	CommitMode         CommitMode
}

WriteRequest is the unit of work queued and written by the BranchWorker.

Directories

Path Synopsis
Package manifestedit is an isolated proof of concept for the manifest-inventory "file-agnostic placement" feature.
Package manifestedit is an isolated proof of concept for the manifest-inventory "file-agnostic placement" feature.

Jump to

Keyboard shortcuts

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