lifecycle

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lifecycle drives previews: it deploys and tears down a pull request's part of its preview, and sweeps every preview of the gitops repo.

Index

Constants

View Source
const (
	TriggerPush = "push"
	TriggerAPI  = "api"
)

Config sync triggers, the trigger attribute of the config sync logs.

View Source
const PRClosedReason = "PR closed"

PRClosedReason is the teardown reason for a closed (or merged) PR.

Variables

View Source
var (
	// ErrConfigUnavailable wraps a badil.yaml that can't be read or parsed.
	ErrConfigUnavailable = errors.New("badil.yaml unavailable")
	// ErrPreviewNotFound is a slug whose preview file does not exist.
	ErrPreviewNotFound = errors.New("preview not found")
)

Errors of Syncer.Sync. Callers test them with errors.Is.

Functions

func DeployPR

func DeployPR(
	ctx context.Context,
	api DeployAPI,
	catalog *gitops.ServiceCatalog,
	slug string,
	inputs preview.DeployInputs,
	skipUnchanged bool,
) (*gitops.WriteResult, error, error)

DeployPR pins inputs.ServiceName at inputs.CommitSha in the preview slug, then posts the preview summary on every open PR of the preview. It returns the write result, the fan-out error (possibly joined, nil when every comment landed) and the write error; on a write error nothing is commented. With skipUnchanged, a write that would change nothing but the service's updated-at is skipped (gitops.Client.WritePreviewWithRetry): the result is then Unchanged and nothing is commented. It does not log.

func ReconcilePR

func ReconcilePR(ctx context.Context, api ReconcileAPI, ev Event)

ReconcilePR brings the preview in line with the PR's current state on GitHub. When the payload shows the event cannot concern a preview label, nothing is read. Otherwise the PR is read and its labels resolve its target preview (preview.ResolveTarget). The PR is first torn down from every other preview it may still be pinned in. Then, when it is open and has a target, the catalog services of its repo that it touches (touchedServices) are deployed there at its head SHA, unless another PR already pins one of them, or the preview does not exist and preview.maxPreviews previews already do; before that deploy, the PR's entries there for the other services of its repo are unpinned. A PR that touches no service is torn down from its target and told so, and the reconcile is skipped. An open PR with conflicting named labels is told so. Fork PRs and repos outside the catalog are skipped. Every outcome is logged.

A closed event is not guarded by its payload nor skipped for a repo outside the catalog, and a closed fork PR is still torn down. When the catalog or the PR can't be read, it falls back to tearing down the payload's head branch and named labels as closed.

A service already deployed as it would be written (only its updated-at would change) is not written nor commented again, unless the cleanup removed the PR from another preview. When every service is, the reconcile is skipped as already deployed.

Each call records exactly one outcome through the Recorder: failed when the catalog, the PR or the target preview can't be read, or the previews can't be listed, on a non-closed event, when the PR's files can't be listed or the unpin of its untouched services fails, on any event, or when no service write succeeded and at least one failed; otherwise skipped (with its reason) when the reconcile was skipped; otherwise deployed when a service write succeeded; otherwise torn_down.

func Sweep

func Sweep(ctx context.Context, api SweepAPI, now time.Time, ttl time.Duration) error

Sweep visits every preview of the gitops repo once, applies the first matching rule 1-3, then syncs every preview that rules 1 and 2 did not match (step 4). A preview whose rule 1-2 delete is cancelled by the re-check is not synced: the deploy that cancelled it already projected it.

  1. no pinned entry: the preview is deleted;
  2. ttl > 0 and the newest updated-at of its pinned entries is before now - ttl: the preview is deleted and its open PRs are told it expired;
  3. otherwise every distinct PR pinning an entry is looked up. A closed one goes through TeardownPR. An open one whose labels (preview.ResolveTarget) conflict, or target another preview, is unpinned without a comment (unpinOnly); one with no target, pinned outside its branch slug, goes through TeardownPR. Other open PRs are left alone.
  4. after rule 3, the preview is rewritten to match the catalog (SyncPreviewWithRetry). A rewrite is recorded and logged; when it changed the service list or a host, the open PRs' comments are refreshed.

The catalog is fetched once, when there is a preview, for the preview label and step 4. When it can't be read, a warning is logged, only closed PRs are torn down by rule 3 and step 4 is skipped.

A ttl of 0 disables rule 2. A failing preview read, rule 1-2 delete or step 4 sync is logged and counted, and the sweep goes on; Sweep then returns an error carrying the count. A failed comment refresh is only logged. A closed-PR teardown failure is only logged by TeardownPR, not counted, and retried on the next run. A ListPreviews error is returned at once.

The number of listed previews and each rule 1-2 deletion are recorded through the Recorder; teardowns are recorded with the sweep trigger.

func TeardownPR

func TeardownPR(ctx context.Context, api TeardownAPI, tt Target, reason, trigger string)

TeardownPR unpins the PR from its preview (or deletes the preview when no pin remains), then edits the PR's comment, which states reason, and, when the preview survives, refreshes the remaining PRs' summaries. Comment failures are logged and never undo the teardown. The teardown is recorded once, with trigger.

Types

type DeployAPI

type DeployAPI interface {
	WritePreviewWithRetry(
		ctx context.Context,
		slug string,
		catalog *gitops.ServiceCatalog,
		serviceName, commitSha string,
		inputs preview.DeployInputs,
		createMessage, updateMessage string,
		skipUnchanged bool,
	) (*gitops.WriteResult, error)
	FanOutPRComments(ctx context.Context, vals preview.PreviewValues, body string) error
}

DeployAPI is the subset of gitops.Client used by DeployPR.

type Event

type Event struct {
	Target

	// Action is the webhook action (opened, labeled, ...).
	Action string
	// HeadRef is the PR's head branch in the payload. Only the payload
	// teardown of a closed PR uses it.
	HeadRef string
	// Label is the added or removed label, on labeled and unlabeled.
	Label string
	// Labels holds the names of the PR's labels in the payload.
	Labels []string
}

Event is a pull_request webhook delivery to reconcile. Its Target's Slug is empty: the slug comes from the PR read from GitHub.

type PreviewSyncAPI added in v0.21.0

type PreviewSyncAPI interface {
	SyncPreviewWithRetry(ctx context.Context, slug string, catalog *gitops.ServiceCatalog) (*gitops.SyncResult, error)
	FanOutPRComments(ctx context.Context, vals preview.PreviewValues, body string) error
	// PreviewSynced records one preview rewritten to match badil.yaml.
	PreviewSynced()
}

PreviewSyncAPI is what syncPreview uses: the gitops sync and comment fan-out, and the rewrite counter.

type ReconcileAPI

type ReconcileAPI interface {
	DeployAPI
	TeardownAPI
	FetchServiceCatalog(ctx context.Context) (*gitops.ServiceCatalog, error)
	GetPullRequest(ctx context.Context, owner, repo string, number int) (*gitops.PullRequest, error)
	GetPreview(ctx context.Context, slug string) (*preview.PreviewValues, string, error)
	ListPreviews(ctx context.Context) ([]string, error)
	ListPullRequestFiles(ctx context.Context, owner, repo string, number int) ([]gitops.ChangedFile, error)
	UnpinServicesWithRetry(
		ctx context.Context,
		slug, owner, repo string,
		prNumber int,
		services []string,
	) (*gitops.TeardownResult, error)
}

ReconcileAPI is the subset of gitops.Client used by ReconcilePR.

type Recorder

type Recorder interface {
	// Reconcile records one ReconcilePR outcome and, when skipped, its reason.
	Reconcile(outcome, reason string)
	// Teardown records one teardown of a PR from a preview.
	Teardown(trigger, result string)
	// SweepListed records the number of previews a sweep listed.
	SweepListed(n int)
	// SweepDeleted records one preview deleted by a sweep rule.
	SweepDeleted(rule string)
	// PreviewSynced records one preview rewritten to match badil.yaml.
	PreviewSynced()
}

Recorder records lifecycle outcomes. *metrics.Metrics implements it; a nil one records nothing.

type SweepAPI

type SweepAPI interface {
	TeardownAPI
	FetchServiceCatalog(ctx context.Context) (*gitops.ServiceCatalog, error)
	ListPreviews(ctx context.Context) ([]string, error)
	GetPreview(ctx context.Context, slug string) (*preview.PreviewValues, string, error)
	GetPullRequest(ctx context.Context, owner, repo string, number int) (*gitops.PullRequest, error)
	DeletePreviewWithRetry(
		ctx context.Context,
		slug, message string,
		stillDelete func(preview.PreviewValues) bool,
	) (*gitops.DeleteResult, error)
	PreviewSyncAPI
}

SweepAPI is the subset of gitops.Client used by Sweep, plus the Recorder.

type SyncAPI added in v0.21.0

type SyncAPI interface {
	PreviewSyncAPI
	ListPreviews(ctx context.Context) ([]string, error)
	FetchServiceCatalogSHA(ctx context.Context) (*gitops.ServiceCatalog, string, error)
}

SyncAPI is the subset of gitops.Client used by Syncer, plus its recorder.

type SyncReport added in v0.21.0

type SyncReport struct {
	Synced    []string
	Unchanged int
	Failed    []string
}

SyncReport is the outcome of a run: the slugs rewritten, the number of previews left unchanged and the slugs that failed, in listing order.

type Syncer added in v0.21.0

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

Syncer runs the sweep's sync step (syncPreview) over every preview, or one, outside the sweep. One guarded run happens at a time in the process: a push run (Push) or an API run (Sync), each waiting for the guard in turn. Waiters acquire the guard in no particular order. Each run is capped at timeout from when it holds the guard.

func NewSyncer added in v0.21.0

func NewSyncer(api SyncAPI, timeout time.Duration) *Syncer

NewSyncer returns a Syncer over api whose runs are capped at timeout.

func (*Syncer) Push added in v0.21.0

func (s *Syncer) Push(ctx context.Context)

Push runs the sync of a push to the gitops default branch. Holding the guard, it reads badil.yaml's blob SHA and stops when it equals the SHA the last push run synced: a push queued behind the run that synced its content does nothing more. Otherwise every preview is synced with the catalog of that content and the SHA recorded, also when a preview failed (the sweep retries it) or the content can't be parsed (the same content would fail again). A badil.yaml that can't be read, or previews that can't be listed, record nothing, so the next push retries.

func (*Syncer) Sync added in v0.21.0

func (s *Syncer) Sync(ctx context.Context, slug string) (SyncReport, error)

Sync runs an API sync: every preview when slug is "", else the preview slug, which the caller has validated. It waits for the guard until ctx is done. It returns an error wrapping ErrConfigUnavailable when badil.yaml can't be read or parsed, ErrPreviewNotFound when slug has no preview file, the listing error when the previews can't be listed, or ctx's error when it was done before the guard was free. A failed preview is in the report, not an error. It never reads nor changes the SHA of the push runs.

type Target

type Target struct {
	Slug     string
	Owner    string
	Repo     string
	FullName string
	Number   int
}

Target identifies a preview and the PR to tear down or reconcile.

type TeardownAPI

type TeardownAPI interface {
	Recorder
	TeardownPreviewWithRetry(
		ctx context.Context,
		slug, owner, repo string,
		prNumber int,
	) (*gitops.TeardownResult, error)
	UpsertPRComment(ctx context.Context, owner, repo string, prNumber int, body string) error
	FanOutPRComments(ctx context.Context, vals preview.PreviewValues, body string) error
}

TeardownAPI is the subset of gitops.Client used by TeardownPR, plus the Recorder.

Jump to

Keyboard shortcuts

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