Skip to content

Conversation

@kacpersaw
Copy link
Contributor

Summary

This PR adds support for reading CODER_AGENT_TOKEN from Kubernetes secrets via secretKeyRef, in addition to the existing inline value support.

Fixes #139

Changes

  • Add resolveEnvValue helper function that resolves env var values from either direct values or secretKeyRef references
  • Update Pod handler to use resolveEnvValue for token resolution
  • Update ReplicaSet handler to use resolveEnvValue for token resolution
  • Add comprehensive tests for secretKeyRef functionality

Backward Compatibility

This change is fully backward compatible:

Scenario Before After
env.Value set directly ✅ Works ✅ Works (unchanged)
env.ValueFrom.SecretKeyRef ❌ Ignored ✅ Works
No RBAC for secrets N/A ⚠️ Logs warning, continues
  • Existing inline env.Value tokens continue to work unchanged
  • secretKeyRef support is additive, not a breaking change
  • Optional secrets that don't exist are handled gracefully (silent skip)
  • Errors fetching required secrets log warnings and skip the pod rather than crashing

RBAC Requirements

Users who want to use secretKeyRef will need to ensure their service account has RBAC permissions to get secrets in the watched namespaces:

- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get"]

Testing

  • Added TestPodEventsWithSecretRef - verifies pods with secretKeyRef work correctly
  • Added TestReplicaSetEventsWithSecretRef - verifies ReplicaSets with secretKeyRef work correctly
  • Added TestPodEventsWithOptionalMissingSecret - verifies optional missing secrets are handled gracefully
  • All existing tests continue to pass

@kacpersaw kacpersaw force-pushed the kacpersaw/agent-token-k8s-secret branch 2 times, most recently from 802b1e2 to 8f6d20d Compare December 16, 2025 11:13
@kacpersaw kacpersaw marked this pull request as ready for review December 16, 2025 11:58
Copy link
Member

@deansheather deansheather left a comment

Choose a reason for hiding this comment

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

The necessary RBAC changes should be updated in the helm templates

This change adds support for reading CODER_AGENT_TOKEN from Kubernetes
secrets via secretKeyRef, in addition to the existing inline value support.

Changes:
- Add resolveEnvValue helper function that resolves env var values from
  either direct values or secretKeyRef references
- Update Pod handler to use resolveEnvValue for token resolution
- Update ReplicaSet handler to use resolveEnvValue for token resolution
- Add comprehensive tests for secretKeyRef functionality

The implementation is fully backward compatible:
- Existing inline env.Value tokens continue to work unchanged
- secretKeyRef support is additive, not a breaking change
- Optional secrets that don't exist are handled gracefully
- Errors fetching required secrets log warnings and skip the pod

Users who want to use secretKeyRef will need to ensure their service
account has RBAC permissions to get secrets in the watched namespaces.

Fixes #139
Required to support reading CODER_AGENT_TOKEN from Kubernetes secrets
via secretKeyRef.
Add integration tests that verify CODER_AGENT_TOKEN can be read from
Kubernetes secrets via secretKeyRef for both Pods and ReplicaSets.
@kacpersaw kacpersaw force-pushed the kacpersaw/agent-token-k8s-secret branch from 0013ad7 to abe9899 Compare December 16, 2025 14:19
@kacpersaw kacpersaw merged commit 2e92dd4 into main Dec 16, 2025
3 checks passed
@kacpersaw kacpersaw deleted the kacpersaw/agent-token-k8s-secret branch December 16, 2025 14:25
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.

Support using CODER_AGENT_TOKEN passed through Kubernetes Secret

2 participants