ConfigMap and Secret Management Best Practices
This document outlines best practices for managing ConfigMaps and Secrets in Kubernetes deployments to prevent pod crashes and configuration errors.Table of Contents
- Overview
- ConfigMap Management
- Secret Management
- Testing and Validation
- Common Pitfalls
- Troubleshooting
Overview
On 2025-11-12, we experienced pod crashes in the staging environment due to:- Missing ConfigMap keys: References to keys that didn’t exist
- Secret name mismatches: Kustomize
namePrefixnot applied correctly in JSON patches
ConfigMap Management
Adding New Configuration Keys
When adding a new configuration key that will be referenced by a deployment:-
Add to base or overlay ConfigMap (
deployments/base/configmap.yamlordeployments/overlays/*/configmap-patch.yaml): - Add to environment variables if using JSON 6902 patches:
- Run validation tests:
Required Keys Checklist
The following keys are required in production ConfigMaps: Session Management:session_backendsession_ttl_secondssession_cookie_securesession_cookie_samesitesession_max_age_seconds
rate_limit_enabledrate_limit_per_minuterate_limit_burst
circuit_breaker_failure_thresholdcircuit_breaker_recovery_timeoutcircuit_breaker_expected_exception_ratecircuit_breaker_half_open_max_calls
retry_max_attemptsretry_base_delay_secondsretry_max_delay_seconds
default_timeout_secondsllm_timeout_secondsdatabase_timeout_seconds
gdpr_storage_backendgdpr_retention_days
Optional vs Required Keys
- Optional keys: Use
optional: trueinconfigMapKeyRef:
- Required keys: Omit
optionalfield (default isfalse). Pod will fail if key is missing.
Secret Management
ExternalSecrets with Kustomize namePrefix
When using ExternalSecrets with KustomizenamePrefix, always use the prefixed name in patches:
❌ WRONG:
Adding New Secrets
-
Add to ExternalSecret template (
deployments/overlays/*/external-secrets.yaml): -
Add data mapping:
-
Create in GCP Secret Manager:
-
Run validation tests:
Naming Conventions
- Template keys: kebab-case (e.g.,
keycloak-client-id) - Data mapping keys: camelCase (e.g.,
keycloakClientId) - GCP Secret Manager:
{prefix}-{kebab-case}(e.g.,staging-keycloak-client-id)
Testing and Validation
Automated Tests
Three levels of validation:-
Unit tests - Run during development:
-
Pre-commit validation - Run before committing:
-
CI/CD validation - Runs automatically on PR/push:
- Validates all overlays
- Checks ConfigMap keys
- Verifies secret references
- Ensures Kustomize namePrefix consistency
Manual Validation
Before applying changes:Common Pitfalls
1. JSON 6902 Patches Don’t Auto-Update Secret Names
Problem: When using JSON 6902 patches withnamePrefix, the prefix is NOT automatically applied to secret names within the patch content.
Solution: Manually use the prefixed name in all JSON 6902 patch files.
2. ConfigMap Keys Added to Base But Not Overlays
Problem: New keys indeployments/base/configmap.yaml may be overridden by overlay patches.
Solution:
- Use strategic merge patches (not complete replacement)
- OR add the key to all relevant overlay patches
3. Forgetting to Create GCP Secrets
Problem: ExternalSecret configured but GCP Secret doesn’t exist. Solution:- Create GCP secret before applying ExternalSecret
- Use placeholder values in non-production environments
- Document required secrets in
docs/secrets/README.md
4. Case Mismatch in Secret Keys
Problem: Template usesmy-secret-key but data mapping uses mySecretKey.
Solution: Follow naming conventions (kebab-case in template, camelCase in data mapping).
Troubleshooting
Pod Stuck in CreateContainerConfigError
Symptom:kubectl describe pod shows:
-
Check ConfigMap exists:
-
Verify key exists:
-
If missing, add to
configmap-patch.yamland reapply:
Secret Name Mismatch
Symptom: Pod can’t find secretmcp-server-langgraph-secrets but only staging-mcp-server-langgraph-secrets exists.
Solution:
-
Check ExternalSecret target name:
- Update all references to use prefixed name in patch files.
-
Run validation tests to catch similar issues:
ExternalSecret Not Syncing
Symptom: ExternalSecret exists but Secret not created. Solution:-
Check ExternalSecret status:
-
Check External Secrets Operator logs:
-
Verify GCP Secret exists:
Prevention Measures
- Always run tests before committing ConfigMap/Secret changes
- Use the validation script in CI/CD pipelines
- Document all new secrets in team docs
- Review diffs carefully for JSON 6902 patches
- Test in staging before production deployment
References
Last Updated: 2025-11-12 Related Issues: Staging pod crashes (2025-11-12) Related Tests:
tests/deployment/test_configmap_secret_validation.py