Documentation
¶
Overview ¶
Package preview provides slug generation and values file building.
Index ¶
- Constants
- func ArgoCDAppsURL(argocdURL, slug string) string
- func BuildExpirySummary(slug string, ttl time.Duration, commitMessage, commitURL string) string
- func BuildLabelConflictSummary(base string, labels []string) string
- func BuildNoServiceSummary(slug string) string
- func BuildPreviewLimitSummary(slug string, limit int) string
- func BuildServiceHeldSummary(slug, service, holderURL, base string) string
- func BuildSummary(slug string, values PreviewValues, urls URLConfig, argocdURL string, ...) string
- func BuildTeardownSummary(slug string, removed []string, deleted bool, ...) string
- func Equal(a, b PreviewValues) bool
- func EqualExceptUpdatedAt(a, b PreviewValues, serviceName string) bool
- func ExpandImageTag(format, commitSha string) string
- func LastDeploy(vals PreviewValues) (time.Time, bool)
- func ParseLabel(label, base string) (string, bool, bool)
- func ParsePRURL(u string) (string, string, int, bool)
- func ReservedNamespaceAnnotations() []string
- func ReservedNamespaceLabels() []string
- func Slugify(branch string) string
- func ValidImageTagFormat(format string) bool
- type Catalog
- type ChartSettings
- type DependencyChart
- type DependencyEntry
- type DeployInputs
- type Metadata
- type NamespaceMetadata
- type PreviewValues
- func ApplyHosts(vals PreviewValues, slug string, urls URLConfig) PreviewValues
- func BuildPreviewValues(serviceName, commitSha string, catalog []string, inputs DeployInputs) PreviewValues
- func UnpinPR(vals PreviewValues, owner, repo string, number int) (PreviewValues, []string, bool)
- func UnpinPRServices(vals PreviewValues, owner, repo string, number int, services []string) (PreviewValues, []string, bool)
- func UpdatePreviewValues(existing PreviewValues, serviceName, commitSha string, inputs DeployInputs) PreviewValues
- type Projection
- type Resolution
- type ServiceEntry
- type URLConfig
Constants ¶
const ( DefaultChartPath = "k8s/" DefaultImageTagParam = "app.image.tag" DefaultHostParam = "app.ingress.host" )
Chart settings a service gets when the catalog does not set them.
const APIVersion = "badil.dev/v1beta1"
APIVersion is the apiVersion of the preview values contract badil writes.
const DefaultBranch = "main"
DefaultBranch is the branch a service tracks when the catalog sets none.
const DefaultImageTag = "{sha}"
DefaultImageTag is the image tag format when badil.yaml sets none: the full commit SHA.
const MaxImageTagFormatLen = 93
MaxImageTagFormatLen is the longest image tag format: with {sha} expanded to a 40-character SHA, the tag stays within the 128 characters of an OCI tag.
const MaxSlugLen = 53
MaxSlugLen is the longest slug Slugify returns. It is Helm's release-name limit: Argo CD renders the preview-apps chart with the slug as release name (the ApplicationSet's releaseName), and Helm rejects names over 53 characters.
const OCIScheme = "oci://"
OCIScheme starts the RepoURL of a dependency chart in an OCI registry.
Variables ¶
This section is empty.
Functions ¶
func ArgoCDAppsURL ¶ added in v0.21.0
ArgoCDAppsURL returns the Argo CD UI's application list at argocdURL (no trailing slash) filtered on the label selector app.kubernetes.io/instance=<slug>, which every service and dependency Application of the preview carries.
func BuildExpirySummary ¶
BuildExpirySummary renders the comment badil leaves on a preview's open PRs once the cleanup sweeper deleted it for having no deploy within ttl.
func BuildLabelConflictSummary ¶
BuildLabelConflictSummary renders the comment badil leaves on a PR that carries several named labels of the preview label base. labels are their names, as written.
func BuildNoServiceSummary ¶ added in v0.22.0
BuildNoServiceSummary renders the comment badil leaves on a labelled PR that changes no service of the preview slug: no file it changes matches the paths of its repository's services, so nothing is deployed.
func BuildPreviewLimitSummary ¶ added in v0.17.0
BuildPreviewLimitSummary renders the comment badil leaves on a PR it refused to deploy because creating its preview slug would exceed the preview cap of limit previews.
func BuildServiceHeldSummary ¶
BuildServiceHeldSummary renders the comment badil leaves on a PR it refused to deploy because service is already pinned in the preview slug by the PR at holderURL. base is the preview label.
func BuildSummary ¶
func BuildSummary( slug string, values PreviewValues, urls URLConfig, argocdURL string, excluded []string, commitMessage, commitURL string, ) string
BuildSummary renders the markdown summary that badil posts back as a PR comment. Each service line shows the pinned commit (with a link if derivable) or a 🔄 badge naming the branch an unpinned entry tracks, followed by a link to the entry's written host, if any. One line per dependency follows, with its chart name and version and the link to its written host, if any. When excluded is not empty, a line after the lists names the catalog services and dependencies left out of the preview (Projection.Excluded). When at least one service is pinned, a note follows saying a pinned service starts once CI has pushed its image. When argocdURL (the catalog's preview.argocdURL) is set, a line links to the Argo CD application list filtered on the preview (ArgoCDAppsURL). The trailing line links to the gitops commit that recorded the change.
func BuildTeardownSummary ¶
func BuildTeardownSummary(slug string, removed []string, deleted bool, reason, commitMessage, commitURL string) string
BuildTeardownSummary renders the comment badil leaves on a PR once its services were unpinned from the preview, or the preview was deleted. reason says why (for example "PR closed").
func Equal ¶ added in v0.19.0
func Equal(a, b PreviewValues) bool
Equal reports whether a and b encode to the same YAML, so that a nil and an empty namespace, list or map are equal, and so is a number in a dependency's values whether decoded as a float or an integer.
func EqualExceptUpdatedAt ¶ added in v0.16.0
func EqualExceptUpdatedAt(a, b PreviewValues, serviceName string) bool
EqualExceptUpdatedAt reports whether a and b are Equal, ignoring only the updated-at of serviceName's entry.
func ExpandImageTag ¶ added in v0.21.0
ExpandImageTag returns the image tag of format, a ValidImageTagFormat, for an entry pinned to commitSha, or for a branch-tracking entry when commitSha is empty: its placeholder becomes commitSha or its first 7 or 8 characters (all of it when shorter), or the Argo CD variable of the deployed revision, as the image tag table of docs/CONFIGURATION.md shows.
func LastDeploy ¶
func LastDeploy(vals PreviewValues) (time.Time, bool)
LastDeploy returns the newest updated-at (RFC3339) across the pinned entries of vals. Entries without a commitSha, without metadata or whose updated-at does not parse are ignored. The bool is false when no pinned entry has a parseable updated-at.
func ParseLabel ¶
ParseLabel classifies label against the preview label base. It returns, in order, the slug, whether label is named and whether it is the plain label. A named label starts with "<base>:" (case-insensitively); its slug is the Slugify of the rest, trimmed of spaces, and is "" when that rest has no alphanumeric character. The plain label equals base (case-insensitively). Any other label returns all zero values.
func ParsePRURL ¶
ParsePRURL extracts the owner, repo and PR number from a github.com pull request URL. Trailing path segments (e.g. /files) are ignored. ok is false when u is not a recognizable PR URL.
func ReservedNamespaceAnnotations ¶ added in v0.21.0
func ReservedNamespaceAnnotations() []string
ReservedNamespaceAnnotations returns the preview Namespace annotations the preview-apps chart sets, which NamespaceMetadata.Annotations cannot hold.
func ReservedNamespaceLabels ¶ added in v0.21.0
func ReservedNamespaceLabels() []string
ReservedNamespaceLabels returns the preview Namespace labels the preview-apps chart sets, which NamespaceMetadata.Labels cannot hold.
func Slugify ¶
Slugify converts a VCS branch name into a DNS-safe slug used as the preview folder name and AppProject/Namespace identifier: lowercase, non-alphanumerics collapsed to a single '-', leading/trailing dashes trimmed. A slug longer than MaxSlugLen is cut and suffixed with a short hash of the full slug, so the result stays distinct and deterministic.
func ValidImageTagFormat ¶ added in v0.21.0
ValidImageTagFormat reports whether format is a valid image tag format: an optional prefix, one of {sha}, {sha7} or {sha8}, an optional suffix, at most MaxImageTagFormatLen characters.
Types ¶
type Catalog ¶ added in v0.21.0
type Catalog struct {
// Services lists the catalog's services, in order.
Services []string
// Repos maps each service to its git URL.
Repos map[string]string
// Branches maps a service to the branch it tracks; DefaultBranch when
// absent.
Branches map[string]string
// ImageTag is the image tag format of a service without one in
// ImageTags; DefaultImageTag when empty.
ImageTag string
// ImageTags maps a service to its own image tag format.
ImageTags map[string]string
// Charts maps a service to its chart settings; an empty field, or an
// absent service, takes its default (DefaultChartPath,
// DefaultImageTagParam, DefaultHostParam, no values files).
Charts map[string]ChartSettings
// Namespace is the extra metadata of the preview Namespace; nil for none.
Namespace *NamespaceMetadata
// Dependencies are the upstream charts of badil.yaml, in order, as
// parsed: HostParam as written, Host empty.
Dependencies []DependencyEntry
// Needs maps a service or dependency to the services and dependencies it
// needs. When empty, a preview runs every service and dependency;
// otherwise only its pinned entries and what they need (included).
Needs map[string][]string
// GitopsRepoURL is the git URL of the gitops repo.
GitopsRepoURL string
}
Catalog is what ApplyCatalog projects a preview file on: the services of badil.yaml, in file order, and their settings.
type ChartSettings ¶ added in v0.21.0
type ChartSettings struct {
Path string `yaml:"path"`
ImageTagParam string `yaml:"imageTagParam"`
HostParam string `yaml:"hostParam"`
ValueFiles []string `yaml:"valueFiles,omitempty"`
}
ChartSettings is how a service's chart is rendered: the chart path in the service repo, the Helm parameters set to the image tag and to the preview host, and extra values files applied in order. In a preview file every field but ValueFiles is set.
func (ChartSettings) JSONSchemaExtend ¶ added in v0.21.0
func (ChartSettings) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend requires path, imageTagParam and hostParam, makes every string non-empty and valueFiles, when present, a non-empty list.
type DependencyChart ¶ added in v0.21.0
type DependencyChart struct {
RepoURL string `yaml:"repoURL"`
Name string `yaml:"name,omitempty"`
Version string `yaml:"version"`
}
DependencyChart is where a dependency's chart comes from: an OCI chart reference (RepoURL starts with OCIScheme, Name is empty) or a chart Name in the Helm repository at RepoURL, at Version.
func (DependencyChart) ChartName ¶ added in v0.21.0
func (c DependencyChart) ChartName() string
ChartName returns the name of the chart: Name, or the last path segment of an OCI reference.
func (DependencyChart) JSONSchemaExtend ¶ added in v0.21.0
func (DependencyChart) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend requires repoURL and version, non-empty, with repoURL an oci:// or https:// URL. name is rejected with an OCIScheme repoURL and required otherwise.
type DependencyEntry ¶ added in v0.21.0
type DependencyEntry struct {
Name string `yaml:"name"`
Chart DependencyChart `yaml:"chart"`
Values map[string]any `yaml:"values,omitempty"`
ValueFiles []string `yaml:"valueFiles,omitempty"`
HostParam string `yaml:"hostParam,omitempty"`
Host string `yaml:"host,omitempty"`
}
DependencyEntry is one item in PreviewValues.Dependencies: an upstream chart deployed in every preview, never pinned and without an image tag. Values are passed to the chart as an object and ValueFiles are paths in the gitops repo. HostParam is the Helm parameter set to Host, the ingress host badil computed for an exposed dependency: in a preview file both are set or both are empty.
func (DependencyEntry) JSONSchemaExtend ¶ added in v0.21.0
func (DependencyEntry) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend requires name and chart, makes every string non-empty and valueFiles, when present, a non-empty list, and requires host and hostParam together. values stays a free-form object.
type DeployInputs ¶
type DeployInputs struct {
GitopsRepo string // "owner/repo"
ServiceName string
HeadRef string
CommitSha string
PRAuthor string
PRURL string
PRNumber string
Timestamp string
WorkflowRunURL string
}
DeployInputs collects the per-deploy values needed to build or update a preview environment's values.yaml.
type Metadata ¶
type Metadata struct {
PRAuthor string `yaml:"pr-author"`
PRURL string `yaml:"pr-url"`
PRNumber string `yaml:"pr-number"`
CreatedAt string `yaml:"created-at"`
UpdatedAt string `yaml:"updated-at"`
VCSRefName string `yaml:"vcs.ref.name"`
PipelineRunURL string `yaml:"cicd.pipeline.run.url"`
}
Metadata is the per-service annotation bag. yaml.v3 emits the known keys in field order and quotes any value that would not read back as a string, so the values file is byte-stable and safe for YAML 1.1 readers such as Helm. Every key is always written: the contract requires all seven, and has no other key.
func BuildServiceMetadata ¶
func BuildServiceMetadata(inputs DeployInputs) *Metadata
BuildServiceMetadata constructs the canonical Metadata for a deploy event. On a fresh create, both created-at and updated-at equal inputs.Timestamp; UpdatePreviewValues overwrites updated-at while preserving the existing created-at.
func (Metadata) JSONSchemaExtend ¶ added in v0.18.0
func (Metadata) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend requires every metadata key.
type NamespaceMetadata ¶ added in v0.21.0
type NamespaceMetadata struct {
Labels map[string]string `yaml:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty"`
}
NamespaceMetadata is the extra labels and annotations of the preview Namespace.
func (NamespaceMetadata) JSONSchemaExtend ¶ added in v0.21.0
func (NamespaceMetadata) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend makes both fields optional and rejects, in each, the keys the preview-apps chart sets on the Namespace (ReservedNamespaceLabels, ReservedNamespaceAnnotations). A reserved key maps to the false schema rather than to a propertyNames rule, whose failure Helm reports at a random location.
type PreviewValues ¶
type PreviewValues struct {
APIVersion string `yaml:"apiVersion"`
GitopsRepoURL string `yaml:"gitopsRepoURL"`
Namespace *NamespaceMetadata `yaml:"namespace,omitempty"`
Services []ServiceEntry `yaml:"services"`
Dependencies []DependencyEntry `yaml:"dependencies"`
}
PreviewValues is the root document persisted at previews/<slug>/values.yaml. GitopsRepoURL is the git URL of the gitops repo, from which a dependency reads its valueFiles. Namespace is the extra metadata of the preview Namespace, nil when badil.yaml sets none. Dependencies are the upstream charts of badil.yaml, in its order.
func ApplyHosts ¶
func ApplyHosts(vals PreviewValues, slug string, urls URLConfig) PreviewValues
ApplyHosts returns a copy of vals where every service entry exposed by urls carries its preview host (URLConfig.Host for slug) and every other service entry has an empty Host. A dependency exposed by urls with a HostParam carries its host too; every other dependency has an empty Host and an empty HostParam. When urls is not enabled, every Host is empty. The root fields are kept. vals is never mutated.
func BuildPreviewValues ¶
func BuildPreviewValues(serviceName, commitSha string, catalog []string, inputs DeployInputs) PreviewValues
BuildPreviewValues constructs a fresh PreviewValues for a slug given the service catalog. The triggering service (serviceName) is pinned to commitSha and carries metadata; every other catalog service renders as a bare entry tracking its branch. The result has apiVersion APIVersion; ApplyCatalog sets each entry's repoURL, branch, chart and imageTag.
The order of services in the result matches the order of the catalog slice.
func UnpinPR ¶
func UnpinPR(vals PreviewValues, owner, repo string, number int) (PreviewValues, []string, bool)
UnpinPR returns a copy of vals where every entry deployed by the PR owner/repo#number (matched on its metadata pr-url; owner and repo compare case-insensitively) is reset to a bare branch-tracking entry. It also returns the names of the unpinned services in file order, and whether any entry is still pinned to a commit. The root fields and every entry's other fields (repoURL, branch, imageTag, chart, host) are kept as written: ApplyCatalog then gives an unpinned entry its branch-tracking imageTag. vals is never mutated.
func UnpinPRServices ¶ added in v0.22.0
func UnpinPRServices( vals PreviewValues, owner, repo string, number int, services []string, ) (PreviewValues, []string, bool)
UnpinPRServices is UnpinPR restricted to the entries whose name is in services: the PR's entries for other services stay pinned.
func UpdatePreviewValues ¶
func UpdatePreviewValues(existing PreviewValues, serviceName, commitSha string, inputs DeployInputs) PreviewValues
UpdatePreviewValues returns a copy of existing with a deploy event merged in:
- if serviceName already has an entry, overwrite commitSha and metadata but preserve the original created-at;
- if the service is absent, append a new pinned entry.
Other services and the root fields are left untouched (branch-tracking entries keep their state). existing is never mutated.
func (PreviewValues) JSONSchemaExtend ¶ added in v0.18.0
func (PreviewValues) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend requires apiVersion, gitopsRepoURL, services and dependencies, rejects other root keys, fixes apiVersion to APIVersion and makes gitopsRepoURL non-empty: the published contract of a preview values file.
type Projection ¶ added in v0.21.0
type Projection struct {
// Values is the projected file.
Values PreviewValues
// Excluded lists the catalog services then the catalog dependencies, in
// badil.yaml order, that Values leaves out because no pinned service
// needs them, directly or transitively. It is nil when the catalog
// declares no needs or every part runs.
Excluded []string
// Shadowed lists the catalog dependencies, in order, left out because a
// kept pinned entry of a service removed from the catalog has their name.
Shadowed []string
}
Projection is a preview file projected on a Catalog (ApplyCatalog).
func ApplyCatalog ¶ added in v0.18.0
func ApplyCatalog(vals PreviewValues, cat Catalog) Projection
ApplyCatalog returns vals projected on cat, never mutating vals:
- apiVersion is APIVersion, gitopsRepoURL is cat.GitopsRepoURL and namespace is cat.Namespace;
- one entry per name of cat.Services, in that order: the existing entry of that name (keeping its commitSha, metadata and host) or a bare {name}, with repoURL from cat.Repos, branch from cat.Branches (DefaultBranch when absent), chart from cat.Charts with its defaults applied, and imageTag its format expanded for the entry's commitSha (ExpandImageTag);
- then, in file order, every entry whose service is not in cat.Services, is pinned (commitSha set) and has a repoURL, unchanged (it keeps the chart and imageTag it was last written with);
- a service listed twice keeps only its first entry;
- every other entry is dropped;
- dependencies are cat.Dependencies, in that order, except one named like a service entry of the result (a kept pinned entry of a service removed from the catalog), which is left out (Shadowed). Their host is ApplyHosts's to set;
- when cat declares needs, only the included services and dependencies are kept: the pinned entries and their transitive needs, every other one left out (Excluded).
type Resolution ¶
type Resolution struct {
// Slug is the target preview, "" when none.
Slug string
// Named holds the distinct slugs of the valid named labels, sorted.
Named []string
// NamedLabels holds the names of the valid named labels, as written,
// sorted case-insensitively.
NamedLabels []string
// Ignored holds the names of the named labels whose slug is empty, in
// input order.
Ignored []string
// Conflict is true when the named labels target two or more previews.
Conflict bool
}
Resolution is the preview a PR's labels target.
func ResolveTarget ¶
func ResolveTarget(labels []string, headRef, base string) Resolution
ResolveTarget returns the preview targeted by labels for a PR on headRef, where base is the preview label. Exactly one distinct named slug wins; two or more are a conflict with no target. Without a valid named label, the plain label targets Slugify(headRef). Otherwise there is no target.
type ServiceEntry ¶
type ServiceEntry struct {
Name string `yaml:"name"`
RepoURL string `yaml:"repoURL"`
Branch string `yaml:"branch"`
CommitSha string `yaml:"commitSha,omitempty"`
ImageTag string `yaml:"imageTag"`
Chart ChartSettings `yaml:"chart"`
Host string `yaml:"host,omitempty"`
Metadata *Metadata `yaml:"metadata,omitempty"`
}
ServiceEntry is one item in PreviewValues.Services. RepoURL and Branch are the service's git URL and the branch it tracks when not pinned. CommitSha and Metadata are set together, only on a pinned entry: the service is then deployed at CommitSha. ImageTag is the image tag of the service, its format expanded (ExpandImageTag), and Chart how its chart is rendered. Host is the ingress host badil computed for an exposed service, and is empty otherwise.
func (ServiceEntry) JSONSchemaExtend ¶ added in v0.18.0
func (ServiceEntry) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend sets the constraints of a service entry that reflection does not produce: name, repoURL, branch, imageTag and chart are required, imageTag is non-empty, and commitSha and metadata are set together (a pinned entry carries both).
type URLConfig ¶
type URLConfig struct {
// Domain is the parent of every preview host. Empty means off.
Domain string
// TLS selects https (true) or http (false) for the PR-comment links.
TLS bool
// Expose maps each exposed service name to its link path (default "/").
Expose map[string]string
}
URLConfig is the parsed `preview` block of the gitops repo's badil.yaml. The zero value writes no hosts; links follow the hosts already written in the preview file.
func (URLConfig) Host ¶
Host returns the per-preview host of service: <service>-<slug>.<domain>. A <service>-<slug> label longer than 63 characters (the DNS label limit) is cut and suffixed with a short hash of the full label.
func (URLConfig) Link ¶
Link returns the Markdown-safe link text (<host><path>) and URL (<scheme>://<host><path>, with the path percent-encoded) for the host written on service's entry. The path is service's Expose path, or "/" when service is not a key of Expose. The boolean is false only when host is empty.