Skip to content

refactor: replace magic literals with named constants#593

Merged
mchmarny merged 1 commit intomainfrom
fix/code-review-findings
Apr 16, 2026
Merged

refactor: replace magic literals with named constants#593
mchmarny merged 1 commit intomainfrom
fix/code-review-findings

Conversation

@mchmarny
Copy link
Copy Markdown
Member

Summary

Replace magic literals, fmt.Errorf, and hardcoded K8s resource names with named constants from pkg/defaults and structured errors from pkg/errors. Fix a context cancellation race in a test.

Motivation / Context

Full codebase review identified inconsistencies with project coding standards: magic numbers instead of named constants, fmt.Errorf instead of pkg/errors, and hardcoded K8s namespace strings duplicated across validators.

Fixes: N/A
Related: N/A

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Build/CI/tooling

Component(s) Affected

  • CLI (cmd/aicr, pkg/cli)
  • API server (cmd/aicrd, pkg/api, pkg/server)
  • Recipe engine / data (pkg/recipe)
  • Bundlers (pkg/bundler, pkg/component/*)
  • Collectors / snapshotter (pkg/collector, pkg/snapshotter)
  • Validator (pkg/validator)
  • Core libraries (pkg/errors, pkg/k8s)
  • Docs/examples (docs/, examples/)
  • Other: ____________

Implementation Notes

  • pkg/defaults/timeouts.go: Added 11 named constants — timeouts (RuntimeClassCheckTimeout, CNCFSubmissionTimeout, TrainerControllerPollInterval, TrainingRuntimePollInterval), output limits (TerminationLogMaxSize, ConfigMapStatusTruncateLen, AutoscalerMaxEvents, MetricsDisplayLimit), and K8s resource names (GPUOperatorNamespace, KubeSystemNamespace)
  • pkg/logging/cli.go: Replaced fmt.Errorf with errors.Wrap(errors.ErrCodeInternal, ...)
  • pkg/version/version.go: Wrapped bare sentinel returns (ErrEmptyVersion, ErrTooManyComponents) with pkgerrors.Wrap + error codes; sentinels preserved for errors.Is() compatibility
  • Validators: Replaced hardcoded "gpu-operator" and "kube-system" namespace strings with defaults.GPUOperatorNamespace / defaults.KubeSystemNamespace
  • sysctl_test.go: Added 10ms sleep before context cancellation to fix race where cancel() fired before Collect() could start

Testing

make qualify

All tests pass (74.7% coverage, threshold 70%). Lint clean on all changed packages. Pre-existing gosec G703 in argocdhelm.go is unrelated and exists on main.

Risk Assessment

  • Low — Isolated change, well-tested, easy to revert

Rollout notes: N/A — purely mechanical refactoring with no behavioral changes.

Checklist

  • Tests pass locally (make test with -race)
  • Linter passes (make lint)
  • I did not skip/disable tests to make CI green
  • I added/updated tests for new functionality
  • I updated docs if user-facing behavior changed
  • Changes follow existing patterns in the codebase
  • Commits are cryptographically signed (git commit -S)

…and pkg/errors

Addresses findings from a full codebase review:

- Replace fmt.Errorf in pkg/logging with errors.Wrap
- Wrap bare sentinel error returns in pkg/version with error codes
- Extract 11 named constants to pkg/defaults (timeouts, limits, K8s namespaces)
- Replace hardcoded timeout values with defaults constants across validators
- Replace hardcoded K8s resource names with defaults.GPUOperatorNamespace
- Fix context cancellation race in sysctl_test.go
@github-actions
Copy link
Copy Markdown

Coverage Report ✅

Metric Value
Coverage 74.6%
Threshold 70%
Status Pass
Coverage Badge
![Coverage](https://img.shields.io/badge/coverage-74.6%25-green)

Coverage unchanged by this PR.

@mchmarny mchmarny self-assigned this Apr 16, 2026
@mchmarny mchmarny added this to the v0.12 milestone Apr 16, 2026
@mchmarny mchmarny merged commit 9e356cc into main Apr 16, 2026
22 of 24 checks passed
@mchmarny mchmarny deleted the fix/code-review-findings branch April 16, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants