I'm looking into a system where we set up a new namespace for each feature branch in our test clusters. The idea is to modify the namespace names with a suffix through the deployment pipeline, while also implementing some useful labels. The controllers would handle database creation and secret population as normal, although we would need to be careful with naming conventions and possibly use validating webhooks. Additionally, I would like to have a way to notify the team post-pipeline about the main entry points for testing.
I have a couple of questions: First, does this approach make sense for ephemeral environments? Is using namespaces the right choice? Secondly, has anyone built a controller that can automatically clean up unused namespaces? Would that be based on metrics or some kind of scheduling?
4 Answers
In my experience, we ran into scaling problems with having a namespace for every feature branch. We switched it up to use an ephemeral namespace for each pull request, which seems to manage resources better. Also, I'm curious about your idea of setting up a database per namespace because that can drastically increase costs. Perhaps it would make more sense for developers to share a non-production database across those ephemeral namespaces.
We've implemented a similar concept using ArgoCD alongside some bash scripting to handle the setup. We utilize Argo Workflows to kick things off and employ CloudNativePG to maintain an ephemeral state within the cluster. Eventually, I had to create some automation to clean up and garbage collect old namespaces once they were no longer needed.
I use this in some of my work projects with shared managed clusters. Even though our official tools don’t support it well, we still manage to make it work, and it’s been fantastic! Instead of a namespace for each branch, we create a new namespace for each automated build and for each developer. Each PR build generates a unique namespace, allowing complete freedom for developers. We deploy a CronJob to clean up the namespace two hours after it’s created, and if tests succeed, we destroy the namespace immediately. This method has dramatically reduced resource usage while helping every developer test in a realistic, production-like environment.
If you're not tied to using CRDs, I recommend checking out Project Capsule. It fits your needs perfectly, allowing you to apply labels and enforce a tenant prefix on namespace names. It simplifies management as you can associate your merge requests or pull requests as tenants and create the namespaces accordingly. Once you're finished with a feature branch, just clean up the tenant, and all related namespaces will be deleted automatically.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically