controller-runtime: Can't delete namespaces from test environment
I’d like to use namespaces to isolate my tests from one another. For example, I want to create a new namespace with a random name in my BeforeEach and delete it in AfterEach so that individual tests don’t have to worry about cleaning up after themselves.
However, I’ve observed that namespace deletion doesn’t work when using envtest. When I delete a namespace, the deletion timestamp on resources within that namespace remain unset, and the namespace stays in the Terminating phase seemingly forever. The latter is true even if the namespace is empty (e.g. I delete it immediately after creating it).
Is this behavior expected? Is whatever mechanism that normally executes namespace deletion missing from envtest?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 16
- Comments: 16 (9 by maintainers)
Commits related to this issue
- Tests: Use DeletePropagationOrphan for ns - and longer timeout - this doesn't work properly yet - there's an issue with envtest - more info: https://github.com/kubernetes-sigs/controller-runtime/issue... — committed to clobaa/gonmap by deleted user 3 years ago
- 🧹 Fix unit tests for workloads Remove resource cleanup, because it is not supported: https://github.com/kubernetes-sigs/controller-runtime/issues/880 Implement check for Service Account in workload... — committed to mondoohq/mondoo-operator by czunker 2 years ago
- 🧹 Fix unit tests for workloads Remove resource cleanup, because it is not supported: https://github.com/kubernetes-sigs/controller-runtime/issues/880 Implement check for Service Account in workload... — committed to mondoohq/mondoo-operator by czunker 2 years ago
Here is the work-around I implemented in my test helper functions. Basically, I use the discovery API to optimistically delete all namespaced resources in the namespace I’m trying to delete and then I update the
/finalizesubresource of the namespace to remove thekubernetesfinalizer. The idea here is that it is a (perhaps poor) approximation of whatkube-controller-managerwould normally do to finalize a terminating namespace.I brought in
k8s.io/client-goin order to access the/finalizesubresource and to interact with the discovery API (though I suspect the latter is possible with the standard controller-runtime client)The documentation moved:
https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
Yep, that. It will use your default kubeconfig so set whatever context you need before starting and then enable UseExistingCluster.