Skip to content

Detect in-cluster namespace from service account file#113

Closed
Copilot wants to merge 4 commits intofeat/in-cluster-supportfrom
copilot/sub-pr-109-yet-again
Closed

Detect in-cluster namespace from service account file#113
Copilot wants to merge 4 commits intofeat/in-cluster-supportfrom
copilot/sub-pr-109-yet-again

Conversation

Copy link

Copilot AI commented Feb 1, 2026

Description

When running in a Kubernetes pod, the namespace was hardcoded to "default" instead of detecting the actual namespace. This made the in-cluster context inaccurate for pods running in other namespaces.

Changes

  • detectInClusterNamespace(): Reads namespace from /var/run/secrets/kubernetes.io/serviceaccount/namespace, the standard Kubernetes service account mount
  • readNamespaceFromFile(): Testable helper that handles file I/O with fallback to "default" on errors (missing file, empty content)
  • LoadInClusterConfig(): Uses detected namespace instead of hardcoded "default"
  • Logging: Added namespace to in-cluster config load message

Example

// Before: always used "default" namespace
contextInfo.Namespace = "default"

// After: reads actual namespace from service account
namespace := detectInClusterNamespace()  // Returns "production" if pod is in production namespace
contextInfo.Namespace = namespace

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 1, 2026 12:33
- Add detectInClusterNamespace() helper function to read namespace from /var/run/secrets/kubernetes.io/serviceaccount/namespace
- Update LoadInClusterConfig to use the detected namespace instead of hardcoded "default"
- Add comprehensive test cases for namespace detection logic
- Include namespace in in-cluster config loading log message

Co-authored-by: basebandit <8973567+basebandit@users.noreply.github.com>
- Extract readNamespaceFromFile function to enable proper unit testing
- Update tests to test the actual function rather than duplicating logic
- Add test for detectInClusterNamespace default behavior
- Remove unused strings import from test file

Co-authored-by: basebandit <8973567+basebandit@users.noreply.github.com>
Co-authored-by: basebandit <8973567+basebandit@users.noreply.github.com>
Copilot AI changed the title [WIP] Update implementation based on feedback for in-cluster support Detect in-cluster namespace from service account file Feb 1, 2026
Copilot AI requested a review from basebandit February 1, 2026 12:37
@basebandit basebandit closed this Feb 1, 2026
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.

2 participants