lifecycle

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 10 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 PRClosedReason = "PR closed"

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

Variables

This section is empty.

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, every catalog service of its repo is deployed there at its head SHA, unless another PR already pins one of them. 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 on a non-closed 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 and applies the first matching rule:

  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.

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

A ttl of 0 disables rule 2. A failing preview read or rule 1-2 delete is logged and counted, and the sweep goes on; Sweep then returns an error carrying the count. 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 []string,
		urls preview.URLConfig,
		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 ReconcileAPI

type ReconcileAPI interface {
	DeployAPI
	TeardownAPI
	GetPullRequest(ctx context.Context, owner, repo string, number int) (*gitops.PullRequest, error)
	GetPreview(ctx context.Context, slug string) (*preview.PreviewValues, string, 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)
}

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

type SweepAPI

type SweepAPI interface {
	TeardownAPI
	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)
}

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

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
	FetchServiceCatalog(ctx context.Context) (*gitops.ServiceCatalog, 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