Skip to content

Harden namespace checks#50

Open
brucetony wants to merge 1 commit into
mainfrom
ns-patch
Open

Harden namespace checks#50
brucetony wants to merge 1 commit into
mainfrom
ns-patch

Conversation

@brucetony

@brucetony brucetony commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

There were several method calls that took namespace as an input parameters, but were not set and fell back to "default" causing the PO to crash when deployed in non-default namespaces with RBAC enabled. This PR updates all methods that use the namespace context to use the get_current_namespace() method.

Additionally, to cut down on IO, the @lru_cache() decorator was added to the get_current_namespace() method since it is called often and this value should not change once deployed.

Summary by CodeRabbit

  • Bug Fixes

    • Namespace handling now follows the current Kubernetes context when one isn’t provided, helping operations run in the correct environment.
    • Log retrieval, pod status checks, deployment actions, and subscription cleanup now use the resolved namespace consistently.
    • Cached namespace lookups are cleared in tests to prevent cross-test interference.
  • Tests

    • Updated tests to verify namespace-aware log retrieval behavior.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR replaces hardcoded 'default' Kubernetes namespace values with dynamic resolution via a new cached get_current_namespace() helper. Function and method signatures across API, kubernetes helpers, resource entities, message-broker client, and status modules now accept optional namespace parameters that fall back to runtime resolution when omitted.

Changes

Dynamic Kubernetes namespace resolution

Layer / File(s) Summary
Cached namespace resolution helper
src/k8s/utils.py, tests/test_k8s_utils.py
get_current_namespace() gains an @lru_cache(maxsize=1) decorator; delete_k8s_resource resolves namespace at runtime instead of defaulting to 'default'; a test fixture clears the cache before/after each test.
PodOrchestrationAPI namespace defaulting
src/api/api.py
Constructor's namespace parameter changes from str = 'default' to Optional[str] = None, with self.namespace computed via namespace or get_current_namespace().
Kubernetes helper functions namespace resolution
src/k8s/kubernetes.py
create_harbor_secret, create_analysis_deployment, delete_deployment, get_analysis_logs, get_pod_status, _get_logs, and _get_pods all switch to optional namespace parameters resolved via get_current_namespace(); pod listing in create_analysis_deployment now uses the resolved namespace.
Analysis entity and subscription teardown namespacing
src/resources/analysis/entity.py, src/utils/mb_client.py
Analysis.start resolves namespace dynamically when omitted; Analysis.stop now passes namespace=self.namespace to delete_subscription, which itself resolves namespace via get_current_namespace() when not provided.
Log retrieval and status namespace scoping
src/resources/utils.py, src/status/status.py, tests/test_resources_utils.py
retrieve_logs and stop_analysis pass explicit namespace to get_analysis_logs; _stream_stuck_logs passes namespace to get_pod_status; tests updated to assert calls include namespace="default".

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

  • PrivateAIM/node-pod-orchestration#47: Both PRs modify the analysis stop flow and delete_subscription in src/utils/mb_client.py, with this PR further scoping it to dynamic namespace resolution.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is related to the PR’s namespace-handling hardening, though it is broader than the specific code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ns-patch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/resources/analysis/entity.py (1)

59-77: 🗄️ Data Integrity & Integration | 🔵 Trivial | 💤 Low value

Use self.namespace in the deployment call for consistency. Pass the resolved namespace into create_analysis_deployment so the start path uses the same value as the DB row and teardown path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/resources/analysis/entity.py` around lines 59 - 77, The deployment
creation path is using the raw namespace argument instead of the resolved value
stored in self.namespace, which can diverge from the DB row and teardown flow.
Update the create_analysis_deployment call in the analysis entity start path to
pass self.namespace, matching the earlier database.create_analysis and keeping
namespace usage consistent throughout the entity lifecycle.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/resources/analysis/entity.py`:
- Around line 59-77: The deployment creation path is using the raw namespace
argument instead of the resolved value stored in self.namespace, which can
diverge from the DB row and teardown flow. Update the create_analysis_deployment
call in the analysis entity start path to pass self.namespace, matching the
earlier database.create_analysis and keeping namespace usage consistent
throughout the entity lifecycle.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6f49b875-0691-496e-9267-d194e4664444

📥 Commits

Reviewing files that changed from the base of the PR and between a8af972 and 15065f7.

📒 Files selected for processing (9)
  • src/api/api.py
  • src/k8s/kubernetes.py
  • src/k8s/utils.py
  • src/resources/analysis/entity.py
  • src/resources/utils.py
  • src/status/status.py
  • src/utils/mb_client.py
  • tests/test_k8s_utils.py
  • tests/test_resources_utils.py

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant