Skip to content

About

ConfigHub bot that records container repositories (ghcr.io) as fact units

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

44 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

registrybot

A ConfigHub bot that watches container repositories and records what they hold as ConfigHub units.

For every repository it is told to watch, registrybot maintains one unit — a fact unit — holding named streams that each pick out the one tag a consumer most likely wants (the built-in "stable", or streams you define such as "main"). It learns about changes by polling the GitHub Packages API and, when a webhook is configured, by receiving GitHub package events that make it look sooner.

That is its whole job. What happens when a fact unit changes — which deployments pick up the new tag, through which promotion steps, with what approvals — is ConfigHub's, expressed as links from the units that consume the fact. registrybot never touches those units. See docs/design.md for the reasoning and the fact schema.

registrybot follows the k8sbot pattern: a small Go process built into a public image, authenticated to ConfigHub as a service account, deployed from a manifest that ConfigHub itself manages. Only ghcr.io is supported in this version.

How it works

Two things make registrybot look at a repository, and both lead to the same place:

  • Polling. Every pollInterval (default 5m) the bot re-reads its configuration document and enqueues every repository in it.
  • Webhooks. A GitHub package (or legacy registry_package) event names a repository. If that repository is watched it is enqueued once the repository has settled: one image push is many package events spread over a few minutes (per-architecture manifests, buildcache tags, the manifest list, the cosign signature and attestation), so the bot waits until webhooks.settle (default 2m) has passed with no further delivery for that repository, capped at webhooks.maxDelay (default 10m) from the first, and reconciles once. A poll that falls inside the window leaves the repository to the timer. Deliveries for unwatched repositories are acknowledged and ignored. Nothing in the payload is trusted: it only decides when to look.

A single worker drains the queue. For each repository it lists the package's active versions from the GitHub Packages API, builds the fact document, and compares it with what the unit holds. If the facts changed it writes a new revision with a one-line description such as registrybot observed ghcr.io/confighub/k8sbot: stable=v0.3.1@2a9c5d3ee6ff (each stream's tag and the first 12 characters of its digest, so a moving tag still reads as a change). If nothing changed it writes nothing, so an idle repository produces no revision churn. A fact unit that does not exist yet is created (toolchain AppConfig/YAML, labeled registrybot.confighub.com/repository=<repo>).

The bot holds no state it cannot rebuild from GitHub and ConfigHub. Restart it any time.

The fact unit

# Facts about ghcr.io/confighub/k8sbot, observed by registrybot from the GitHub Packages API.
# Do not edit: the next observation overwrites this unit. Link to it instead.
configHub:
  configSchema: registrybot.confighub.com/v1alpha4
  configName: k8sbot
repository: ghcr.io/confighub/k8sbot
registry: ghcr.io
owner: confighub
source: github-packages
url: https://github.com/orgs/confighub/packages/container/package/k8sbot
streams:
  main: null                   # a configured stream nothing matched yet
  stable:                      # highest release version (prereleases excluded)
    tag: v0.3.1
    digest: sha256:41aa…

Downstream units link to streams.<name>.tag (or streams.<name>.digest), so that "which tag counts as stable" is decided once, in the stream definition, and every consumer follows it. The unit deliberately carries no list of tags: the registry already has that, nothing can link into a list by key, and the unit's revision history is the history of what each stream pointed at, and when.

The configuration document

What to watch is itself configuration: one YAML document, re-read every poll cycle, so a change reconfigures the running bot with no restart. examples/registrybot-config.yaml is a commented example, and Where the document comes from describes the two ways it reaches the bot. The shape:

pollInterval: 5m
webhooks:
  settle: 2m                        # reconcile once deliveries for a repository stop for this long
  maxDelay: 10m                     # ...but never later than this after the first delivery
defaults:
  space: registry-facts             # where fact units go; read from a unit, default: that unit's space
  exclude: ["^sha-", "^pr-"]        # tags to ignore
  streams:
    main: { pattern: "^main$" }
repositories:
  - repository: ghcr.io/confighub/k8sbot
  - repository: ghcr.io/confighub/cubbychat
    unit: cubbychat-image           # default: <owner>-<name>
    streams:
      stable: { semver: ">=1.0.0 <2.0.0" }
      next:   { semver: ">=2.0.0-0" }    # "-0" admits prereleases

A stream with semver is the highest tag that parses as a release version (v1.2.3, 1.2.3, 1.2.3-rc.1; strict MAJOR.MINOR.PATCH) and satisfies the constraint. A stream with only pattern is the most recently pushed tag matching it. Both together narrow by pattern first. The built-in stable stream, { semver: ">=0.0.0" }, is always present unless you redefine it.

Where the document comes from

The document reaches registrybot in one of two ways. It refuses to start when both are configured.

  • A file, REGISTRYBOT_CONFIG_FILE. In a cluster it is a ConfigMap mounted as a directory, shipped with the Deployment. This is what manifests/registrybot.yaml and the installer package set up by default. Uploaded to ConfigHub by the installer, the document is an AppConfig/YAML unit that ConfigHub renders into the ConfigMap, so it is edited in its own format and released with the rest of the deployment: a change takes a publish and a sync, and starts a new pod, because the upload puts a hash of the ConfigMap on the pod template. Applied as a plain manifest, a change to the ConfigMap needs no new pod: the kubelet refreshes the mounted file and the bot reads it at its next cycle. A document read from a file must name the space for fact units in defaults.space.
  • A ConfigHub unit, REGISTRYBOT_CONFIG_SPACE and REGISTRYBOT_CONFIG_UNIT. The bot reads the AppConfig/YAML unit from ConfigHub itself, every cycle, so an edit takes effect within one poll interval, with no publish and nothing gating it. The package's config-unit component sets this up.

Discovery

By default the document is an allowlist: a webhook for an unlisted repository is acknowledged and ignored. With discovery.fromWebhooks: true, a signed package event for an unlisted repository whose owner the policy admits creates a fact unit for it from defaults and polls it from then on. This is what makes an organization-level webhook useful: every image the organization publishes gets a fact unit without anyone editing the document.

discovery:
  fromWebhooks: true
  owners: [confighub, acme]          # registry namespaces allowed; empty = any
  exclude: ["^confighub/ui-preview-", "-preview$"]   # "<owner>/<name>" patterns to ignore

The fact units are the record. At every cycle the bot lists the units in the default space that carry its registrybot.confighub.com/repository label and watches those, so a restart forgets nothing and the bot never edits its own configuration. To stop watching a discovered repository, delete its fact unit or tighten the policy; an entry in repositories always takes precedence over a discovered one for the same repository.

Metrics

registrybot records its counters with OpenTelemetry and serves them in the Prometheus text format at /metrics on METRICS_ADDR (:9090 by default, apart from the webhook receiver's port), through the OpenTelemetry Prometheus exporter (the OpenTelemetry instrument name is in parentheses):

  • registrybot_reconciles_total (registrybot.reconciles) — reconciles of one repository, from a poll or a settled webhook, by result: written when the fact unit got a new revision (including when it was created), unchanged when its facts were already current and nothing was written, and failure when the reconcile failed and is left to the next poll. /status names the repository and its last error.
  • registrybot_webhook_deliveries_total (registrybot.webhook_deliveries) — deliveries to POST /webhooks/github, by result: queued when the delivery named a watched repository; discovered when it named one registrybot was not watching and the discovery policy admitted it, so it now gets a fact unit; ignored when it was acknowledged with nothing to reconcile (a ping, another event, a package that is not a container, or an unwatched repository discovery did not admit, which is every unwatched one while discovery is off); and rejected when it was answered with an error (unreadable, too large, a bad signature, or no package in the payload). It counts deliveries, not repositories: one image push is many deliveries for the same repository. Nothing is counted while the receiver is disabled.

A reconcile that fails because registrybot is shutting down is not counted as a failure. Every series is exported from the start, at zero, and the counters restart from zero with each process. /metrics holds only these counters: no Go runtime or process metrics, and no OpenTelemetry target_info, so the OTEL_* resource environment variables have no effect. Serving them is best-effort: if the address cannot be opened, registrybot warns and goes on without metrics.

The manifests name the container port metrics and set it to 9090. If you change the port in METRICS_ADDR, change containerPort to match. Set METRICS_ENABLED=false to turn metrics off. They include no Service port, ServiceMonitor or scrape annotations; add whichever your Prometheus uses to discover targets.

Configuration

Process configuration is environment only. Everything about what to watch lives in the configuration document above.

Variable Required Description
CONFIGHUB_URL yes ConfigHub base URL, e.g. https://hub.confighub.com
CONFIGHUB_AUTH_PRIVATE_KEY one credential An Ed25519 private JWK registered against a service account with cub serviceaccount key add, inline or as a file path
CONFIGHUB_TOKEN one credential A static session token. Local development only; it is never refreshed
REGISTRYBOT_CONFIG_FILE yes* Path of the configuration document, usually in a mounted ConfigMap. *Exactly one of _FILE and _SPACE is required; registrybot refuses to start with a file and a unit. See Where the document comes from
REGISTRYBOT_CONFIG_SPACE yes* Slug of the space holding the configuration unit, when registrybot reads the document from ConfigHub itself
REGISTRYBOT_CONFIG_UNIT no Slug of the configuration unit. Default registrybot. Not allowed with REGISTRYBOT_CONFIG_FILE
GITHUB_TOKEN yes Classic personal access token with read:packages, or a GitHub App installation token with packages read. Used to list versions
GITHUB_API_URL no Default https://api.github.com; set for GitHub Enterprise Server
GITHUB_WEBHOOK_SECRET no The secret GitHub signs deliveries with. Absent: webhook endpoint disabled, polling only
REGISTRYBOT_POLL_INTERVAL no Default 5m. The configuration document's pollInterval overrides it
REGISTRYBOT_LISTEN_ADDR no Default :8080
METRICS_ENABLED no true (the default) or false; false turns metrics off and registrybot opens no port. Any other value stops registrybot at startup. See Metrics
METRICS_ADDR no Address to serve Prometheus metrics on, at /metrics. Default :9090; changing the port means changing the manifests' containerPort too. See Metrics

Exactly one ConfigHub credential kind must be set; the bot refuses to guess between two.

HTTP endpoints: POST /webhooks/github (signature-checked), GET /healthz, GET /status (what is watched and whether it was discovered, last attempt/success/write per repository, current stream tags). Prometheus metrics are served apart, on METRICS_ADDR; see Metrics.

Setup

Install the cub CLI (see docs.confighub.com), then:

# 1. A space for the fact units, and for the bot's configuration unit if it reads one.
cub space create registrybot

# 2. The service account the bot runs as, and a key for it. --generate stores the
#    private JWK at ~/.confighub/keys/registrybot.jwk and registers the public half.
cub serviceaccount create registrybot
cub serviceaccount key add registrybot --generate registrybot

# 3. What the bot needs on the space: list, create and write fact units, and
#    read the configuration unit if it reads one.
cub space update registrybot \
  --permission View:serviceaccount:registrybot \
  --permission ViewChildren:serviceaccount:registrybot \
  --permission CreateChildren:serviceaccount:registrybot \
  --permission EditChildren:serviceaccount:registrybot

The service account has no organization role, so it can do only what these grants allow. If fact units go to other spaces (defaults.space or a repository's space in the configuration document), grant the same on each of them. Run the bot with the document in a file:

CONFIGHUB_URL=https://hub.confighub.com \
CONFIGHUB_AUTH_PRIVATE_KEY=~/.confighub/keys/registrybot.jwk \
REGISTRYBOT_CONFIG_FILE=examples/registrybot-config.yaml \
GITHUB_TOKEN=ghp_... \
registrybot

Within one poll cycle each repository in the document has a fact unit. Edit the document to add repositories or change streams; the bot picks it up on its next cycle.

To have the bot read the document from a ConfigHub unit instead, create the unit and name its space in place of the file:

cub unit create --space registrybot --toolchain AppConfig/YAML \
  registrybot examples/registrybot-config.yaml
REGISTRYBOT_CONFIG_SPACE=registrybot ...

For local development, CONFIGHUB_TOKEN=$(cub auth get-token) stands in for the private key.

Webhooks

Polling alone is complete; webhooks add immediacy, and with discovery enabled they are also how new repositories enter. Set GITHUB_WEBHOOK_SECRET, expose POST /webhooks/github to GitHub, and add a webhook on the organization (or repository) with content type application/json, the same secret, and only the Packages event selected. Organization webhooks see every package in the organization; repository webhooks see only packages linked to that repository. GitHub's ping is answered with 200; deliveries for repositories the bot does not watch and does not discover are answered with 202 and ignored.

Deploy

Installer package

package/ is an installer package of the deployment, at a pinned release. Each release tag publishes it as oci://ghcr.io/confighub/packages/registrybot:<version>, the release without the v. This is how registrybot is deployed from ConfigHub: cub package install registrybot --space <space> installs it as the base that each cluster's variant is cloned from. It is also where the source of the configuration document is chosen. See its README.

Plain manifest

manifests/registrybot.yaml is the same deployment as one file, for a cluster that is not managed from ConfigHub: a Namespace, a ServiceAccount (no token mounted; the bot needs no Kubernetes API access), the ConfigMap that holds the configuration document, a single-replica Deployment whose env list carries every supported variable, and a ClusterIP Service in front of the webhook receiver. Apply it from a checkout of the release tag it names. Credentials come from a Secret named registrybot-secrets that neither the manifest nor the package defines; supply it out of band with keys CONFIGHUB_AUTH_PRIVATE_KEY (the JWK itself), GITHUB_TOKEN, and optionally GITHUB_WEBHOOK_SECRET. How the Service is exposed to GitHub is a per-cluster decision.

Build and release

go test ./...
go build ./...
docker build -t ghcr.io/confighub/registrybot:dev .
  • .github/workflows/build.yml runs the tests and builds development images (main, pr-N, sha-…) on pushes to main and on PRs. No semver, no :latest.
  • .github/workflows/release.yml cuts a release on a vX.Y.Z tag: the semver image (plus :latest) and the installer package. The manifest and the package name the release they run, so set it first: scripts/set-version.sh vX.Y.Z, merge that, then push the tag. release.yml fails for a tag they don't name.

Every image carries its third-party notices at /app/THIRD_PARTY_LICENSES.txt and /app/OS_LICENSE_NOTICE.txt, and every released manifest has an SPDX SBOM attestation. After changing dependencies, run ./scripts/gen-third-party-licenses.sh and commit the result; CI fails the PR if the committed file is stale or a copyleft dependency is linked in.

Status and roadmap

This is a working prototype built to explore what a registry integration looks like as a plain ConfigHub client and what a "fact unit" should contain. Known limits and likely next steps:

  • ghcr.io only. Observation goes through the GitHub Packages API. An OCI distribution backend (/v2/<name>/tags/list plus manifest HEADs) would cover any registry and public images without a token, at the cost of timestamps.
  • Discovery is webhook-only. A repository that never fires a webhook (published before the hook existed) is not discovered; list it explicitly or push once. A discovery.fromListing that enumerates an organization's packages would close that gap.
  • Configuration reload is by polling, of the file or of the unit. Subscribing to ConfigHub's event log for changes to the configuration unit would make edits to it take effect at once.
  • Single replica. Two instances would race on the same units. Leader election is unnecessary at this scale; a per-repository sharding key would be the way to scale out.
  • Schema. registrybot.confighub.com/v1alpha4 will change as consumers tell us what they need to link to. Existing fact units keep the schema annotation they were created with; configHub.configSchema in the data is authoritative.

About

ConfigHub bot that records container repositories (ghcr.io) as fact units

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages