Skip to content

fix(k8s): the catalog check times out in a 1 CPU container #8002

Done

Description

@dan-petty

Problem Description

devops k8s catalog --check renders the Argo CD applications in a pool of 8 threads (_WORKERS in src/devops_cli/k8s/catalog.py), also in a container with a limit of 1 CPU. A kustomize render that fetches a remote base then can pass the default limit of 27 s that kustomize puts on its git commands, and the command fails. In an implement Job no change of the item can fix this, so the step spends all its repair rounds.

On 2026-12-01 the build of dan-petty/homelab#377 stopped this way after 6 repair rounds, 5.5 million tokens and 65 minutes. Failing command: uv run devops k8s catalog --check --k8s-dir k8s. Error: Kustomize build failed ... k8s/monitoring/dashboards in git checkout FETCH_HEAD of the remote base devops-cli//k8s/devops/dashboards?ref=v0.265.4. The live agent container had a limit of 1 CPU.

Reproduction Steps

  1. Render a kustomization that names the remote base https://github.com/dan-petty/devops-cli//k8s/devops/dashboards?ref=v0.265.4 once on 1 CPU.
  2. Render it 8 times in parallel on 1 CPU.

Expected vs Actual Behavior

Expected: the number of renders that run at the same time follows the CPUs that the process can use, so the command passes in the runner.

Actual: one render takes 5.5 s. Each of 8 parallel renders takes 25.3 s to 26.8 s, at the limit of 27 s, on a devcontainer that is faster than the node.

os.process_cpu_count() does not read a cgroup CPU quota. Python 3.13 and later reads PYTHON_CPU_COUNT, which the pod template can set from limits.cpu with the downward API. The timeout query parameter of a kustomize remote base also raises the limit in the environment repository.

Environment Diagnostics

Service v0.266.0, runner v0.265.4, devcontainer with kubectl kustomize.

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Source

Source: https://github.com/dan-petty/homelab/issues/377

Alternatives check of 2026-12-01: none.
Tool: none found.
Reason: Searches found no maintained Python library that reads cgroup v2 cpu.max or the cgroup v1 quota files. psutil and joblib give CPU count or affinity only, so the small render_worker_count helper stays necessary.

Lacks:

  • Read of cgroup v2 cpu.max quota
  • Read of cgroup v1 cpu.cfs_quota_us and cpu.cfs_period_us
  • Quota-aware worker count for the render pools
  • Bounds of 1 through 8 on the worker count

Proposed design

Problem statement

The catalog check renders Argo CD applications in a fixed pool of 8 threads. In a 1 CPU container, eight kustomize renders run at the same time and overload the CPU. A remote base fetch can exceed the 27 s git timeout of kustomize. The command fails, and an implement job spends all repair rounds. The fix adds a shared helper that reads the cgroup CPU quota and sets the worker count.

Acceptance criteria

Requirement: Shared helper module

The project MUST provide render_worker_count(cgroup_root=...) in src/devops_cli/k8s/workers.py.

Scenario: Helper import and call

  • WHEN The test imports render_worker_count from devops_cli.k8s.workers and calls it with cgroup_root=tmp_path.
  • THEN The call returns an integer from 1 through 8.

Requirement: Cgroup v2 quota sets workers

The render_worker_count() helper MUST return the ceiling of the cgroup v2 cpu.max quota divided by its period, bounded to 1 through 8.

Scenario: A 1 CPU cgroup v2 quota

  • WHEN The test writes 100000 100000 to cpu.max under a temporary cgroup root and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 1.

Requirement: Cgroup v2 unlimited quota falls back

The render_worker_count() helper MUST use the process CPU count when the cgroup v2 cpu.max file holds max.

Scenario: A cgroup v2 quota of max

  • WHEN The test writes max 100000 to cpu.max under a temporary cgroup root, patches os.process_cpu_count to return 3, and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 3.

Requirement: Cgroup v1 quota sets workers

The render_worker_count() helper MUST return the ceiling of the cgroup v1 quota divided by its period, bounded to 1 through 8.

Scenario: A 1 CPU cgroup v1 quota

  • WHEN The test writes 100000 to cpu/cpu.cfs_quota_us and 100000 to cpu/cpu.cfs_period_us under a temporary cgroup root and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 1.

Requirement: Cgroup v1 unlimited quota falls back

The render_worker_count() helper MUST use the process CPU count when the cgroup v1 quota is -1.

Scenario: A cgroup v1 quota of -1

  • WHEN The test writes -1 to cpu/cpu.cfs_quota_us and 100000 to cpu/cpu.cfs_period_us under a temporary cgroup root, patches os.process_cpu_count to return 3, and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 3.

Requirement: Process CPU count fallback

The render_worker_count() helper MUST use os.process_cpu_count() when no cgroup CPU quota is set.

Scenario: No cgroup quota

  • WHEN The test uses a temporary cgroup root with no cpu.max file and no cpu/cpu.cfs_quota_us file, patches os.process_cpu_count to return 3, and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 3.

Requirement: Process CPU count None fallback

The render_worker_count() helper MUST return 1 when os.process_cpu_count() returns None and no cgroup CPU quota is set.

Scenario: No process CPU count

  • WHEN The test uses a temporary cgroup root with no quota file, patches os.process_cpu_count to return None, and calls render_worker_count(cgroup_root=tmp_path).
  • THEN The call returns 1.

Requirement: Worker count bounds

The render_worker_count() helper MUST keep the worker count between 1 and 8.

Scenario: High and zero CPU counts

  • WHEN The test patches os.process_cpu_count to return 64, then to return 0, and calls render_worker_count(cgroup_root=tmp_path) for each value with no quota file.
  • THEN The first call returns 8 and the second call returns 1.

Requirement: Catalog uses the worker count

The catalog build MUST use render_worker_count() as the max_workers value of its thread pool.

Scenario: Catalog pool size

  • WHEN The test patches devops_cli.k8s.catalog.render_worker_count to return 2, patches ThreadPoolExecutor in the same module, and runs build_catalog with a fake environment that has one application.
  • THEN The fake ThreadPoolExecutor records max_workers=2.

Requirement: Other render pools use the worker count

The app render and image pin pools MUST use render_worker_count() as their max_workers value.

Scenario: Shared worker count

  • WHEN The test patches devops_cli.k8s.app_render.render_worker_count and devops_cli.k8s.image_pins.render_worker_count to return 2, patches ThreadPoolExecutor in both modules, and runs the code that creates each pool.
  • THEN Each fake ThreadPoolExecutor records max_workers=2.

Requirement: Fixed worker constant removed

The catalog, app render, and image pin modules MUST NOT define a fixed _WORKERS constant.

Scenario: Fixed worker constant removed

  • WHEN The test imports devops_cli.k8s.catalog, devops_cli.k8s.app_render, and devops_cli.k8s.image_pins.
  • THEN The test asserts that no module has an attribute named _WORKERS.

Existing Tool Considered

No open, maintained tool or library covers the need. The alternatives check of 2026-12-01 in the issue body records verdict none and names no tool. Its Lacks: list holds the cgroup reads and the bounded worker count. The change builds src/devops_cli/k8s/workers.py with render_worker_count(). Cost: about 60 lines, 3,000 tokens to build, 1,000 tokens to keep and fix, and 30 minutes.

Key questions

  • Where should the shared worker count helper live?: Put render_worker_count() in src/devops_cli/k8s/workers.py. The function takes a cgroup_root argument with the default /sys/fs/cgroup. (best practice) [sources: src/devops_cli/k8s/catalog.py:58, src/devops_cli/k8s/app_render.py:43, src/devops_cli/k8s/image_pins.py:88]
    • Chosen: New module src/devops_cli/k8s/workers.py. Three modules need the same function. A separate module avoids an import cycle and gives one test target.
    • Rejected: Keep the helper in catalog.py. app_render.py and image_pins.py would import the catalog module. That creates a cycle and mixes concerns.
    • Rejected: Copy the helper into each module. Three copies drift and need three sets of tests.
  • How should the helper count the CPUs that the process can use?: Read cgroup v2 cpu.max first. Then read cgroup v1 cpu.cfs_quota_us and cpu.cfs_period_us. Use os.process_cpu_count() when no quota is set. Return the ceiling of quota divided by period, bounded to 1 through 8. Return 1 when os.process_cpu_count() returns None. (best practice) [sources: src/devops_cli/k8s/catalog.py:58, src/devops_cli/k8s/app_render.py:43, src/devops_cli/k8s/image_pins.py:88]
    • Chosen: Read cgroup quota and fall back to process count. It gives 1 worker for a 1 CPU limit and still works outside a container.
    • Rejected: Keep the fixed pool of 8. It causes the timeout in a 1 CPU container.
    • Rejected: Use only os.process_cpu_count(). The issue states that it does not read a cgroup CPU quota.
  • Should the same worker count apply to the app render and image pin pools?: Yes. Replace the fixed _WORKERS value in catalog.py, app_render.py, and image_pins.py with render_worker_count(). (best practice) [sources: src/devops_cli/k8s/catalog.py:58, src/devops_cli/k8s/app_render.py:43, src/devops_cli/k8s/image_pins.py:88]
    • Chosen: Use the shared function in all three pools. One source of truth fixes the same fan-out risk in each render command.
    • Rejected: Change only the catalog pool. The same timeout risk stays in the other render commands.
  • Should the runner pod template set PYTHON_CPU_COUNT from limits.cpu?: No. The helper reads the cgroup quota directly. The pod template needs no environment variable. This keeps the fix testable offline and avoids a manifest change. (best practice) [sources: k8s/devops/roadmap-service/kustomization.yaml, src/devops_cli/k8s/catalog.py:58]
    • Chosen: Read the cgroup quota in code. It works in any container and a unit test can prove it.
    • Rejected: Set PYTHON_CPU_COUNT in the pod template. It needs a manifest change and a live check, and it duplicates the cgroup value.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/k8s
    source/agent
    status/new
    effort/low
    on Dec 1, 2026
  2. dan-petty commented on Dec 1, 2026

    @dan-petty
    OwnerAuthor
    • Priority: The build of dan-petty/homelab#377 failed after 6 repair rounds because of this, which is a CI failure.
    • Related: fix(k8s): k8s lint of all charts nears its 300 s limit #7900, which asks for a different change. Both fix k8s subcommands that time out from parallelism, but the candidate fixes catalog --check worker count while #7900 fixes k8s lint exceeding its 300 s limit.
  3. added
    value/high
    priority/p0-critical
    on Dec 1, 2026
  4. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • pypi package cgroup cpu limit python worker count, results: 5
    • python library read cgroup cpu quota cpu.max available cpus, results: 5
    • python cgroup cpu quota os.cpu_count container aware package, results: 5
    • "cpu.max" python detect container cpu limit library github, results: 5
    • psutil cpu_count cgroup v2 quota support, results: 5
    • github cgroup cpu limit detection python script, results: 5
    • pypi cpu-count package, results: 5
    • num_cpus cgroup quota crate, results: 5
    • automaxprocs python equivalent cgroup cpu quota, results: 5
    • "cgroup" python package cpu quota parse cpu.max github readme, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Dec 1, 2026
  7. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  8. added
    owner/service
    on Dec 1, 2026
  9. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    Plan state: branch fix/8002-the-catalog-check-times-out-in, draft pull request #8017. The pull request plans the item and closes no issue. The stage job and the step fix-plan edit this comment, and a comment of each run tells what the run did.

    The task groups, with the tokens of the inputs of each step, for a budget of 64000 tokens:

    1. Add the render worker count helper: 30783 tokens
    2. Use the worker count in the catalog build: 27190 tokens
    3. Use the worker count in the other render pools: 32805 tokens
  10. 18 remaining items

  11. added
    status/in-review
    and removed
    status/blocked
    on Dec 1, 2026
  12. removed
    status/in-review
    on Dec 1, 2026
  13. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    Live since 2026-12-01 16:44 UTC

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

    effort/lowpriority/p0-criticalscope/k8ssource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions