Skip to content

refactor: extract pkg/api/gpu_utilization_worker.go to pkg/api/gpuworker [architect] - #23736

Merged
hivecommons-hive[bot] merged 1 commit into
mainfrom
arch/extract-gpuworker
Sep 25, 2026
Merged

hivecommons-hive[bot] merged 1 commit into
mainfrom
arch/extract-gpuworker

Conversation

@hivecommons-hive

Copy link
Copy Markdown
Contributor

Refactor

Extract pkg/api/gpu_utilization_worker.go (413 LOC production + 484 LOC tests) — a self-contained periodic background worker — out of pkg/api, whose surface everywhere else is HTTP routing/wiring, into a dedicated pkg/api/gpuworker/ subpackage. Rename GPUUtilizationWorker → gpuworker.Worker and NewGPUUtilizationWorker → gpuworker.New to match Go's package-qualified naming conventions.

Only two intra-package callers exist and are updated in-place:

  • pkg/api/server.go:272 — construction site inside NewServer.
  • pkg/api/server_runtime.go:39 — the background.gpuUtilWorker field type.

Why

The extraction mirrors an already-established pattern in this repo. pkg/agent's background workers (PredictionWorker, InsightWorker, DeviceTracker, MetricsHistory) were moved into pkg/agent/workers/ in #18271 for the same reason: workers with their own dependency graph do not belong in the HTTP-wiring package. The GPU utilization worker is the last remaining pkg/api-embedded worker of that class. The sibling pkg/gpu/scraper.go header comment ("extracted from pkg/agent to break the pkg/api → pkg/agent import dependency (#17131, #17640)") shows how sensitive this codebase is to directional coupling — keeping a worker with 6 cross-cutting imports inside pkg/api invites the same problem.

Hidden test-scoped init discovered during extraction

The pre-extraction pkg/api/gpu_utilization_worker_test.go imported github.com/kubestellar/console/pkg/agent, and that transitive import is what pulled pkg/agent's init() into pkg/api's test binary. pkg/agent/init.go is the only assigner of ai.SetClusterContextProviders, which NewServer (server.go, line 228 post-refactor) calls unconditionally. Moving the test file out of pkg/api therefore caused TestNewServer_ExplicitDevModeStartsWithoutOAuth (and any other NewServer-based test) to nil-deref an unassigned SetClusterContextProviders variable.

To preserve the exact test-binary side effect without behaviour drift, this PR adds a two-line pkg/api/agent_init_test.go with _ "github.com/kubestellar/console/pkg/agent", with a comment explaining the coupling and pointing at #23735. The underlying production anti-pattern — real callers depending on init() from a package they never import — is not fixed here; it is called out on the tracking issue so a follow-up can wire the var explicitly (constructor injection, or an explicit pkg/agent.Init() call from cmd/console/main.go plus a matching setup in NewServer's test seam).

Validation

  • go build ./... ✅
  • go vet ./... ✅ (no warnings)
  • go test ./pkg/api/ ./pkg/api/gpuworker/ ./pkg/api/metrics/ ✅
    • ok github.com/kubestellar/console/pkg/api 2.069s
    • ok github.com/kubestellar/console/pkg/api/gpuworker 0.117s
    • ok github.com/kubestellar/console/pkg/api/metrics 0.032s

No workflow (.github/workflows/) files touched — this PR is safely within the architect agent's push scope.

Refs #23735 (needs-human: Step B — moving pkg/api/gpuworker further to pkg/gpu with a Metrics interface — remains open; also the underlying ai.SetClusterContextProviders init-time coupling deserves its own follow-up).


Filed by architect agent (ACMM L6 — full mode)

— hive: agent=architect backend=copilot model=claude-opus-4.7 copilot=1.0.88

…g/api/gpuworker

Move the 413 LOC GPU utilization background worker (Start/Stop/collect
cycle, DCGM scrape, snapshot retention, threshold alerting) and its 484
LOC test suite out of pkg/api's HTTP-routing package into a dedicated
pkg/api/gpuworker subpackage. Rename the exported constructor and type
from NewGPUUtilizationWorker/GPUUtilizationWorker to gpuworker.New /
gpuworker.Worker to match Go naming conventions in the new namespace.
Only two intra-package callers (pkg/api/server.go and server_runtime.go)
needed updating.

This mirrors the pattern set by the pkg/agent/workers extraction
(#18271: PredictionWorker/InsightWorker/DeviceTracker/MetricsHistory)
and keeps pkg/api focused on HTTP wiring. No behaviour change: the
worker's constructor signature and public methods (Start/Stop) are
preserved verbatim.

A companion pkg/api/agent_init_test.go blank-imports pkg/agent to
preserve a hidden test-scoped side effect: the moved worker_test.go
transitively pulled pkg/agent into pkg/api's test binary, and its
init() is what assigns ai.SetClusterContextProviders, called
unconditionally by NewServer. The underlying anti-pattern (production
code depending on init() from a package it never imports) is documented
in the follow-up on #23735.

Refs #23735 (Step A of the two-step extraction; Step B — move to
pkg/gpu with a Metrics interface — remains open).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: hive-bot <bot@hive>
@kubestellar-prow kubestellar-prow Bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Sep 25, 2026
@netlify

netlify Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for kubestellarconsole canceled.

Name Link
🔨 Latest commit c7df8fb
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/6ab6457012838900080def5b

@kubestellar-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign mikespreitzer for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@github-actions github-actions Bot added the ai-generated Pull request generated by AI label Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hey @hivecommons-hive[bot] — thanks for opening this PR!

🤖 This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

@kubestellar-prow kubestellar-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 25, 2026
@hivecommons-hive
hivecommons-hive Bot merged commit 47b5c85 into main Sep 25, 2026
40 of 42 checks passed
@kubestellar-prow
kubestellar-prow Bot deleted the arch/extract-gpuworker branch September 25, 2026 10:26
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

@github-actions

Copy link
Copy Markdown
Contributor

Post-merge build verification passed ✅

Both Go and frontend builds compiled successfully against merge commit 47b5c855fa02f89b04bf4feafe59aa993b350b3e.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Post-Merge Verification: passed

Commit: 47b5c855fa02f89b04bf4feafe59aa993b350b3e
Specs run: smoke.spec.ts
Report: https://github.com/kubestellar/console/actions/runs/36123951196

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

Labels

ai-generated Pull request generated by AI dco-signoff: yes Indicates the PR's author has signed the DCO. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tier/2-standard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants