Skip to content

controller: serve several runner images from one controller, routed by label #15

Done

Description

@openbotgit

Problem

A second runner image (the pg variant) currently needs a whole second controller, with its own label, prefix and image. A GitHub App has one webhook URL, so only one controller receives webhooks; the other can only poll. Measured on a live install: the polling pool's median queued-to-started time was 74 s (p90 116 s) against 58 s (p90 81 s) for the webhook pool. Both controllers also scan the same repositories each cycle, doubling GitHub API use for those repositories.

For anyone else using this project the problem grows with every image they add: each one is another controller that polls and rescans.

Proposed change

One controller with a list of pools. Each pool has a label, an image, a service prefix, a maximum number of runners and its scopes.

  • One GitHub scan per scope per cycle. Each queued job is assigned to the pool whose label it asks for, using the existing canTake rule per pool.
  • reconcile.Make plans per pool, which is the existing logic run once per pool; MaxRunners stays per pool.
  • One webhook nudges every pool.
  • Configuration: one structured variable (for example RUNNER_POOLS as JSON) holding the pools. The current single-pool variables keep working as a one-pool shorthand until the old setup is removed.
  • Remove the second-controller setup from the docs once the pooled controller is running; do not keep both paths.

Acceptance

  • Unit test: one cycle with jobs for two labels creates services from the right image and prefix for each, and makes one set of GitHub list calls per scope.
  • Unit test: a pool at its maximum does not block the other pool.
  • A webhook queued event for either label starts a cycle.
  • Existing single-pool configuration loads unchanged (one pool).
  • Service prefixes of different pools cannot overlap (the existing prefix rule, checked across pools).
  • Deploy docs describe the pool list and no longer describe a second controller.

Size M. Revert by redeploying the previous controller image with the single-pool variables.

Activity

  1. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    Moving to Ready: the acceptance above is complete and nothing else blocks it. The private pool now has three images (base, pg, php), so one pooled controller pays off now.

  2. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    Work has started on this.

  3. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    Plan

    Approach

    • Config. New variable RUNNER_POOLS, a JSON array of pools: [{"label":"railway","image":"ghcr.io/...:vX","prefix":"rr-","max_runners":4,"scopes":["repo:acme/app"]}, ...]. Every field is required and unknown fields are refused, so a typo fails at start. RUNNER_POOLS together with any of RUNNER_IMAGE, RUNNER_LABEL, RUNNER_SERVICE_PREFIX, MAX_RUNNERS or RUNNER_SCOPES is refused (two answers for the same thing). Without RUNNER_POOLS, those five variables build one pool exactly as today, with the same defaults. RUNNER_REGION, RUNNER_GROUP_ID, MAX_CREATE_PER_CYCLE and the timings stay controller-wide; MAX_CREATE_PER_CYCLE applies per pool, since each pool's plan is the existing logic run once.
    • Checks across pools. Each prefix passes the existing prefix rule and no two pools share one (the rule already makes sure one valid prefix is never the start of another). Labels are unique without case (GitHub compares them without case), and a pool label cannot be self-hosted, linux or x64. The org-scope runner group check covers every pool's scopes.
    • GitHub (ghapp). Config.Label becomes Labels. canTake returns which pool label a job asks for: every label of the job must be self-hosted, linux, x64 or exactly one pool label (a job that asks for two pool labels fits no runner, so it is nobody's). Job gains Label. JITConfig takes the label of the runner it registers. One scan per scope serves every pool.
    • Railway. Config.Prefix becomes Prefixes; List keeps the exact-name rule for each prefix. The image moves from the client config to Create(ctx, name, image, vars). One project listing per cycle.
    • Controller. Config takes Pools (label, image, prefix, max runners, scopes) in place of Scopes, Prefix and MaxRunners. A cycle reads each distinct scope once (a scope in two pools is read once), lists Railway once, then for each pool: its services (by prefix), its queued count per scope (jobs whose label is the pool's), reconcile.Make with the pool's MaxRunners and the readable scopes of that pool, then reaps and creates with the pool's prefix, image and label. Late-job alerts count only the pool's own services and creations. Unreadable-scope, 404 and failing-cycle handling stay per scope, unchanged. reconcile does not change.
    • Webhook. Handler takes the list of labels; a queued event carrying any pool label nudges the one loop, which serves every pool.
    • Docs. README and docs/deploy.md describe RUNNER_POOLS and the move from two controllers to one (stop the second controller first, then put both pools, with their current prefixes, in RUNNER_POOLS, so the running services are adopted). The second-controller section goes. CHANGELOG [Unreleased].

    Invariants not to break

    • A service is listed, counted or deleted only when its name is exactly <prefix><scope hash>-<6 hex> for a configured prefix; a pool never reaps another pool's services.
    • A busy runner is never removed before the job timeout; a scope that cannot be read keeps its services untouched.
    • A pool at its maximum never blocks another pool; MAX_RUNNERS stays per pool.
    • Secrets never reach errors or logs.
    • Existing single-pool variables load to the same behaviour.

    Tests, one per acceptance item

    1. Controller: one cycle, two pools sharing a scope, one job per label: a service of each prefix is created from its own image with a JIT config for its own label, and QueuedJobs/Runners are called once per scope.
    2. Controller: pool A already at max_runners with a job queued, pool B with a job: B gets its service, A gets none.
    3. Webhook: a queued event for either label calls the handler; an event for neither does not. Main wiring passes every pool label.
    4. Config: the existing single-pool environment loads to one pool with today's values and defaults.
    5. Config: two pools with the same prefix are refused; also duplicate labels, RUNNER_POOLS mixed with single-pool variables, bad JSON and missing fields.
    6. Docs updated as above; checked by reading.

    Plus ghapp tests: a job is routed to the label it asks for, a job with two pool labels is not taken, JITConfig registers the given label; railway tests: listing with two prefixes, Create uses the image it is given.

    Rollback: redeploy the previous controller image with the single-pool variables (and the second controller, if it was used).

    Size M and on the risk list (it runs the production runners), so this plan gets an independent review before building.

  4. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    Plan, revised after an independent review

    The review found real gaps; these changes are now part of the plan.

    • Late-job alerts. Each pool's pass sees only its own jobs (split by Job.Label), its own services and its own creations, so another pool's new service never hides a late job. The cleanup of alert marks runs once per cycle over every pool's jobs, so a shared scope never clears another pool's mark and re-alerts it. Tests: two pools share a scope and pool A's job is late, so two cycles give exactly one alert; pool B creating a service does not hide pool A's late job.
    • Pool isolation tests. (a) Pool B's busy service in a scope only B serves is neither deleted nor counted by pool A. (b) In a shared scope, an idle pool A runner does not stop pool B from creating a service for its job. (c) An exited service is deleted once per cycle, not once per pool.
    • Region per pool. Each pool takes an optional region that falls back to RUNNER_REGION, so a Postgres controller that ran in its own region keeps it after the move. Railway's Create takes the pool (image and region).
    • Distinct scopes. Reads, the 404 and unreadable counts, the missing-scope alerts and the runner deletes go over each distinct scope once. A service whose scope is unreadable is left alone whatever its pool. Test: a shared unreadable scope gives unreadable_scopes: 1 and neither pool's services in it are touched.
    • Labels. Job.Label carries the configured spelling. The webhook now matches labels without case, like canTake. Test: a Railway job routes to pool railway, and its webhook wakes the loop.
    • Wiring. config.Config gets Labels() and Prefixes(), which are tested there; main only passes them on.
    • Docs. Drain a pool before removing it (its services are no longer listed once its prefix is gone). N pools can create up to N x MAX_CREATE_PER_CYCLE services in one cycle. A check that fails: grep -i "second controller" over README.md and docs/deploy.md finds nothing, and README documents RUNNER_POOLS.
    • Gate. The per-pool step moves out of Cycle into its own function to keep CRAP under 10. Every new config branch gets a test.
    • Nits taken. A bad scope in a pool names RUNNER_POOLS and the pool. The config string, the created log line and the late-job alert name the pool label. Status.Queued stays the sum across pools, which a test checks.

    Stages, in commit order on one PR (the job is one PR, so the review's suggestion to split it becomes ordered commits): failing tests first; then the plumbing in ghapp, railway and webhook; then the config; then the controller cycle; then docs and CHANGELOG.

  5. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    Started. The failing tests are on the branch and the draft PR is #57. Next: the pool type and the plumbing in the GitHub, Railway and webhook clients, then the config, the controller cycle and the docs. Nothing blocks it.

  6. parsilver commented on Nov 26, 2026

    @parsilver
    Contributor

    PR #57 is ready for review. One controller now serves every pool from RUNNER_POOLS, and the single-pool variables still load as one pool. Each acceptance item has its test, listed in the PR. CI is green, including the quality gate: coverage, CRAP, duplication and mutation all pass, with the waivers in the description. The next step is review and merge. Nothing has been deployed, so the first pooled deploy should be watched: the startup line should list both pools, then jobs of both labels should get runners.

    Notes: move off the separate Postgres controller by stopping it before the pooled controller takes its prefix, keeping the same prefixes so the running services are adopted. The steps are in the deploy doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions