-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't delete namespaces from test environment #880
Comments
Whilst I appreciate this is expected behaviour, it would be handy to know what the advice is for separating tests from one another. @ekuefler have you found a way forward? I too am trying to keep my tests independent in envtest. My operator queries all resources of a given kind, so I need to ensure its queries are not populated with resources from previous tests! :) Edit: for future time travelling envtest writers, I eventually got what I wanted with the following approach (caveat emptor, I only just wrote this and could find issues, your mileage may vary 😄): var createdResources []runtime.Object
deleteResourceAfterTest := func(o runtime.Object) {
createdResources = append(createdResources, o)
}
BeforeEach(func() {
log.Info("resetting created resources list")
createdResources = nil
})
AfterEach(func() {
for i := len(createdResources) - 1; i >= 0; i-- {
r := createdResources[i]
key, err := client.ObjectKeyFromObject(r)
Expect(err).To(Succeed())
log.Info("deleting resource", "namespace", key.Namespace, "name", key.Name, "test", CurrentGinkgoTestDescription().FullTestText)
Expect(k8sClient.Delete(ctx, r)).To(Succeed())
_, isNamespace := r.(*corev1.Namespace)
if !isNamespace {
log.Info("waiting for resource to disappear", "namespace", key.Namespace, "name", key.Name, "test", CurrentGinkgoTestDescription().FullTestText)
Eventually(func() error {
return k8sClient.Get(ctx, key, r)
}, timeout, interval).Should(HaveOccurred())
log.Info("deleted resource", "namespace", key.Namespace, "name", key.Name, "test", CurrentGinkgoTestDescription().FullTestText)
}
}
}) |
/kind feature |
/milestone Next |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
/remove-lifecycle rotten |
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 I brought in
|
If this is a priority for your testing setup, I would strongly recommend using Kind or K3s as your test control plane rather than the default one launched by envtest. That will provide a more complete setup, including kube-controller-manager and the related namespace and gc controllers. |
I was having a little trouble tracking down documentation for launching kind or k3s clusters from envtest. Is there example documentation anywhere? Based on what I'm gleaning from https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest is it correct to assume that one would exec |
Yep, that. It will use your default kubeconfig so set whatever context you need before starting and then enable UseExistingCluster. |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-contributor-experience at kubernetes/community. |
I think we can close this ticket, as running kube-controller-manager from envtest is out of scope and better served by UseExistingCluster as mentioned above (in combination with tools like Kind or K3s, or other minimalist Kubernetes environments). /close |
@coderanger: Closing this issue. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
- and longer timeout - this doesn't work properly yet - there's an issue with envtest - more info: kubernetes-sigs/controller-runtime#880
Remove resource cleanup, because it is not supported: kubernetes-sigs/controller-runtime#880 Implement check for Service Account in workloads. Signed-off-by: Christian Zunker <[email protected]>
Remove resource cleanup, because it is not supported: kubernetes-sigs/controller-runtime#880 Implement check for Service Account in workloads. Signed-off-by: Christian Zunker <[email protected]>
I am re-open this one since it is not sorted out and was defined as a priority/important. |
The documentation moved:
https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation |
Out of curiosity: is there anywhere explanation of the why of this behaviour of |
Wild guess, because we don't have a kube-controller-manager in envtest (which also causes a bunch of other limitations, e.g. object garbage collection) |
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 inAfterEach
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 theTerminating
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
?The text was updated successfully, but these errors were encountered: