Skip to content
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

Uninstall/cleanup procedures #1491

Closed
gageorsburn opened this issue Sep 4, 2018 · 56 comments · Fixed by #3006
Closed

Uninstall/cleanup procedures #1491

gageorsburn opened this issue Sep 4, 2018 · 56 comments · Fixed by #3006
Labels
kind/bug lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@gageorsburn
Copy link
Contributor

I don't have a lot of logs backing this because my lab cluster has been in a lot of flux lately but I was running into an issue where the kube-controller-manager pods were crashing whenever a newer version of kubevirt was installed which would cause complete unstability within the cluster.

E0813 19:17:56.372793       1 memcache.go:153] couldn't get resource list for subresources.kubevirt.io/v1alpha1: the server could not find the requested resource
E0813 19:17:56.422505       1 memcache.go:153] couldn't get resource list for subresources.kubevirt.io/v1alpha2: <nil>

I had no idea where those were coming from until today when I looked at the apiservice objects.

 gageorsburn@mbp  ~/Projects/osi/slackforward   master  kubectl get apiservices | grep kubevirt
v1alpha1.cdi.kubevirt.io                3d
v1alpha1.subresources.kubevirt.io       103d
v1alpha2.subresources.kubevirt.io       33d

I think older versions are causing issues. Kind of a weird issue/hard to word. It might be good to start some documentation keeping track of all of the resources that need to be cleaned up across version upgrades, uninstalls, etc.

@cynepco3hahue
Copy link

@gageorsburn IIRC to deploy a new version of kubevirt you need to delete all old KubeVirt components and install a new one after it.

kubectl delete -f kubevirt_old.yaml
kubectl create -f kubevirt_new.yaml

I think this flow will exist until we will write some operator.

I think older versions are causing issues. Kind of a weird issue/hard to word. It might be good to start some documentation keeping track of all of the resources that need to be cleaned up across version upgrades, uninstalls, etc.

Good point, we need to document upgrade procedure.

@fabiand
Copy link
Member

fabiand commented Sep 5, 2018

/cc @rthallisey something for the operator.

@rthallisey
Copy link
Contributor

ack. Thanks for the mention. Adding to trello for tracking

@gageorsburn
Copy link
Contributor Author

@cynepco3hahue The main thing I wanted to point out is that even after kubectl delete -f kubevirt_old.yaml was run an apiservice for the kubevirt API still existed.

@cynepco3hahue
Copy link

@gageorsburn I see, thanks for the information, I forgot that we create some bunch of k8s entities via the code.

@fabiand
Copy link
Member

fabiand commented Sep 5, 2018 via email

@rmohr
Copy link
Member

rmohr commented Sep 5, 2018

@davidvossel I actually wonder if we could add a stub api server
registration ot the release yaml, which is then "reused" by the real
registration. If it's in the release yaml, then removal with the yaml would
work.

We are reusing it if it already exists, so in theory it might work, but we are creating more objects (secrests for certificates for instance). Also moving kubevirt to a separate namespace than kube-system would not solve the whole issue, since not all things we need to create/use are namespaced.

I fear that right now, for a real uninstall doing what we do in https://github.com/kubevirt/kubevirt/blob/master/cluster/clean.sh is the only reliable way to do it for as long as we have yaml based releases and not operator based ones.

@rmohr
Copy link
Member

rmohr commented Sep 5, 2018

@fabiand but if we move kubevirt to a separate namespace, like you suggested we could at least shorten the needed commands in https://github.com/kubevirt/kubevirt/blob/master/cluster/clean.sh.

@fabiand
Copy link
Member

fabiand commented Sep 5, 2018

Yes, a dedicated namespace should at least improve.

@davidvossel
Copy link
Member

We are reusing it if it already exists, so in theory it might work, but we are creating more objects (secrests for certificates for instance).

right, that cert generation is ultimately what prevents us from doing this. If we created those objects using manifests with predictable cert values that the api-server knew needed to be replaced, then i suppose it's technically possible to add these objects to the manifests. I'm nervous about how fragile that might be though.

@mlsorensen
Copy link
Contributor

I actually ran into an interesting issue around this just a day ago. When I upgraded from kubernetes 1.11.2 to 1.11.3, suddenly namespaces wouldn't delete, they'd get stuck in 'terminating'. It looks to have been caused by a lingering 'v1alpha1.subresources.kubevirt.io' and kubernetes validating the namespace deletion against all the apiservices.

@gageorsburn
Copy link
Contributor Author

@mlsorensen Not sure if you've come across the work around for that yet but you can output the namespace to json, delete the finalizer and apply the namespace json.

@kubevirt-bot
Copy link
Contributor

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@kubevirt-bot kubevirt-bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 2, 2019
@kubevirt-bot
Copy link
Contributor

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

/lifecycle rotten

@kubevirt-bot kubevirt-bot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Feb 1, 2019
@fabiand
Copy link
Member

fabiand commented Feb 8, 2019 via email

@fabiand
Copy link
Member

fabiand commented Feb 8, 2019

@gageorsburn
Copy link
Contributor Author

@fabiand Yeah we've had good results with the operator so far. Honestly overall KubeVirt has been a lot more stable than it was last year.

@azaiter
Copy link

azaiter commented May 1, 2019

Just as an FYI to whoever looking to fully uninstall kubevirt and having namespace state as "Terminating" because of kubevirt api resources that are still dangling:
adapted from this file

Just run the following commands:

# the namespace of kubevirt installation
export namespace=kubevirt
export labels=("operator.kubevirt.io" "operator.cdi.kubevirt.io" "kubevirt.io" "cdi.kubevirt.io")
export namespaces=(default ${namespace} "<other-namespaces that has kubevirst resources>")

kubectl -n ${namespace} delete kv kubevirt
kubectl -n ${namespace} patch kv kubevirt --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
kubectl get vmis --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep foregroundDeleteVirtualMachine | while read p; do
    arr=($p)
    name="${arr[0]}"
    namespace="${arr[1]}"
    kubectl patch vmi $name -n $namespace --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done

for i in ${namespaces[@]}; do
    for label in ${labels[@]}; do
        kubectl -n ${i} delete deployment -l ${label}
        kubectl -n ${i} delete ds -l ${label}
        kubectl -n ${i} delete rs -l ${label}
        kubectl -n ${i} delete pods -l ${label}
        kubectl -n ${i} delete services -l ${label}
        kubectl -n ${i} delete pvc -l ${label}
        kubectl -n ${i} delete rolebinding -l ${label}
        kubectl -n ${i} delete roles -l ${label}
        kubectl -n ${i} delete serviceaccounts -l ${label}
        kubectl -n ${i} delete configmaps -l ${label}
        kubectl -n ${i} delete secrets -l ${label}
        kubectl -n ${i} delete jobs -l ${label}
    done
done

for label in ${labels[@]}; do
    kubectl delete validatingwebhookconfiguration -l ${label}
    kubectl delete pv -l ${label}
    kubectl delete clusterrolebinding -l ${label}
    kubectl delete clusterroles -l ${label}
    kubectl delete customresourcedefinitions -l ${label}
    kubectl delete scc -l ${label}
    kubectl delete apiservices -l ${label}

    kubectl get apiservices -l ${label} -o=custom-columns=NAME:.metadata.name,FINALIZERS:.metadata.finalizers --no-headers | grep foregroundDeletion | while read p; do
        arr=($p)
        name="${arr[0]}"
        kubectl -n ${i} patch apiservices $name --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
    done
done

@fabiand
Copy link
Member

fabiand commented May 6, 2019

@azaiter actually a good hint how to cleanup the mess.

All in all it's recommneded to delete the KubeVirt CR which will do the cleanup properly, then delete the namespace.

I'm however wondering if we could do some trick to enable the nice cleanup based on namespace somehow ...

@mayankmishra12
Copy link

Still the issues is there ,not able to uninstall kubevirt. I tried both the script which is described in above comment and and the step which are given in official docs.

@fabiand
Copy link
Member

fabiand commented Dec 30, 2019 via email

@fabiand
Copy link
Member

fabiand commented Jan 3, 2020

A few additions:

It is known that deletion/uninstall is not working correctly when a user is trying to do this procedure by removing the namespace kubevirt, this is because of many reasons.

It is also known that uninstall via the kubevirt CR is working reliably.

Our recommendation is to use the operator for deployment and undeployment, and after undeployment to remove the operator.

@candlerb
Copy link

This looks suspicious. Note that there is a kubectl issue where it says on the first get that the CRD does not exist. I am pretty sure that at this state the CR still existed. If you would have done another get it would very likely have shown you the KubeVirt CR.

Let me try again:

root@nuc1:~/k8s# kubectl apply -f kubevirt-operator.yaml
namespace/kubevirt created
customresourcedefinition.apiextensions.k8s.io/kubevirts.kubevirt.io created
clusterrole.rbac.authorization.k8s.io/kubevirt.io:operator created
serviceaccount/kubevirt-operator created
clusterrole.rbac.authorization.k8s.io/kubevirt-operator created
clusterrolebinding.rbac.authorization.k8s.io/kubevirt-operator created
deployment.apps/virt-operator created
root@nuc1:~/k8s# kubectl apply -f kubevirt-cr.yaml
kubevirt.kubevirt.io/kubevirt created
root@nuc1:~/k8s# kubectl get pod -w -n kubevirt
... wait until everything is 1/1 running ...
root@nuc1:~/k8s# kubectl delete -f kubevirt-cr.yaml
kubevirt.kubevirt.io "kubevirt" deleted
root@nuc1:~/k8s# kubectl get all -n kubevirt
NAME                                   READY   STATUS        RESTARTS   AGE
pod/virt-api-b48c9cdd4-gfr99           0/1     Terminating   0          76s
pod/virt-controller-64774c6f65-ndpbh   0/1     Terminating   0          54s
pod/virt-operator-bd7d6c76b-4nkqb      1/1     Running       0          106s
pod/virt-operator-bd7d6c76b-n5m4l      1/1     Running       0          106s

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/virt-operator   2/2     2            2           106s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/virt-operator-bd7d6c76b   2         2         2       106s
root@nuc1:~/k8s# kubectl get all -n kubevirt
NAME                                READY   STATUS    RESTARTS   AGE
pod/virt-operator-bd7d6c76b-4nkqb   1/1     Running   0          113s
pod/virt-operator-bd7d6c76b-n5m4l   1/1     Running   0          113s

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/virt-operator   2/2     2            2           113s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/virt-operator-bd7d6c76b   2         2         2       113s
<< on subsequent repeats it seems to stay like this >>

Note that this time, I didn't get the empty response from kubectl get all -n kubevirt after removing the CR.

Continuing:

root@nuc1:~/k8s# kubectl delete -f kubevirt-operator.yaml
namespace "kubevirt" deleted
customresourcedefinition.apiextensions.k8s.io "kubevirts.kubevirt.io" deleted
clusterrole.rbac.authorization.k8s.io "kubevirt.io:operator" deleted
serviceaccount "kubevirt-operator" deleted
clusterrole.rbac.authorization.k8s.io "kubevirt-operator" deleted
clusterrolebinding.rbac.authorization.k8s.io "kubevirt-operator" deleted
deployment.apps "virt-operator" deleted
<< hangs here >>

In another window:

root@nuc1:~# kubectl get ns kubevirt
NAME       STATUS        AGE
kubevirt   Terminating   3m32s
root@nuc1:~# kubectl api-resources --verbs=list --namespaced -o name >/dev/null
error: unable to retrieve the complete list of server APIs: subresources.kubevirt.io/v1alpha3: the server is currently unable to handle the request

@fabiand
Copy link
Member

fabiand commented Jan 15, 2020 via email

@candlerb
Copy link

@fabiand:

Could you instead please try the following:

kubectl delete -n kubevirt kubevirt kubevirt
kubectl delete namespace kubevirt

Should I do kubectl delete -f kubevirt-cr.yaml followed by the above commands, or are the above commands done by themselves?

@candlerb
Copy link

Check the webhook registrations in the other window.

root@nuc1:~# kubectl api-resources | grep hook
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
error: unable to retrieve the complete list of server APIs: subresources.kubevirt.io/v1alpha3: the server is currently unable to handle the request
root@nuc1:~# kubectl get mutatingwebhookconfigurations --all-namespaces
NAME               CREATED AT
virt-api-mutator   2020-01-12T16:43:33Z
root@nuc1:~# kubectl get validatingwebhookconfigurations --all-namespaces
NAME                 CREATED AT
virt-api-validator   2020-01-12T16:43:33Z

@fabiand
Copy link
Member

fabiand commented Jan 15, 2020 via email

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

So this gest confusing now. Let me clarify:

You can do

kubectl delete -f kubevirt-cr.yaml

or

kubectl delete -n kubevirt kubevirt kubevirt

Then wait until

kubectl get -n kubevirt kubevirt kubevirt

returns No resources found..

Then only virt-operator should be left in the kubevirt namespace.
In theory you can now delete the namespace.

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

In future, as Roman noted, we can leverage finalizers to erase the webhooks.

@fabiand Webhooks and APIServices are not namespaced (so should not block a namespace delete) and we also don't have a finalizer on them (so nothing should block their deletion, even if they were in the namespace).

@fabiand
Copy link
Member

fabiand commented Jan 15, 2020 via email

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

Ok, just checked the kube-controller logs. Indeed the left-over APIService is the cause:

E0115 16:47:50.145458       1 namespace_controller.go:148] unable to retrieve the complete list of server APIs: subresources.kubevirt.io/v1alpha3: the server is currently unable to handle the request

@candlerb that is probably what you meant.

Running

kubectl delete apiservices v1alpha3.subresources.kubevirt.io
kubectl delete namespace kubevirt

works. Seems like all apiservices need to be online and available, or deleting namespaces is blocked. Independent if resources are present or not.

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

especially the webhook to me is the reason for the hanging deletion.

That is wrong. It is the APIService, see above.

@fabiand
Copy link
Member

fabiand commented Jan 15, 2020 via email

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

Here the right uninstall procedure until we have a fix:

kubectl delete -n kubevirt kubevirt kubevirt --wait=true # --wait=true should anyway be default
kubectl delete apiservices v1alpha3.subresources.kubevirt.io # this is the fix
kubectl delete mutatingwebhookconfigurations virt-api-mutator # not blocking but would be left
kubectl delete validatingwebhookconfigurations virt-api-validator # not blocking but would be left
kubectl delete -f kubevirt-operator.yaml

Sorry for the inconvenience.

@candlerb
Copy link

I confirm this uninstall procedure works, thank you!

root@nuc1:~/k8s# kubectl delete -n kubevirt kubevirt kubevirt --wait=true
kubevirt.kubevirt.io "kubevirt" deleted
root@nuc1:~/k8s# kubectl delete apiservices v1alpha3.subresources.kubevirt.io
apiservice.apiregistration.k8s.io "v1alpha3.subresources.kubevirt.io" deleted
root@nuc1:~/k8s# kubectl delete mutatingwebhookconfigurations virt-api-mutator
mutatingwebhookconfiguration.admissionregistration.k8s.io "virt-api-mutator" deleted
root@nuc1:~/k8s# kubectl delete validatingwebhookconfigurations virt-api-validator
validatingwebhookconfiguration.admissionregistration.k8s.io "virt-api-validator" deleted
root@nuc1:~/k8s# kubectl delete -f kubevirt-operator.yaml
namespace "kubevirt" deleted
customresourcedefinition.apiextensions.k8s.io "kubevirts.kubevirt.io" deleted
clusterrole.rbac.authorization.k8s.io "kubevirt.io:operator" deleted
serviceaccount "kubevirt-operator" deleted
clusterrole.rbac.authorization.k8s.io "kubevirt-operator" deleted
clusterrolebinding.rbac.authorization.k8s.io "kubevirt-operator" deleted
deployment.apps "virt-operator" deleted
root@nuc1:~/k8s#

@fabiand
Copy link
Member

fabiand commented Jan 15, 2020

I'm reopening this bug because the apiserver removal still needs to be done manually.

/reopen

@kubevirt-bot kubevirt-bot reopened this Jan 15, 2020
@kubevirt-bot
Copy link
Contributor

@fabiand: Reopened this issue.

In response to this:

I'm reopening this bug because the apiserver removal still needs to be done manually.

/reopen

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.

@rmohr
Copy link
Member

rmohr commented Jan 15, 2020

👍

@rmohr
Copy link
Member

rmohr commented Jan 22, 2020

The uninstall procedure is now documented. Hope to have the code fix which makes this obsolete merged soon.

@zakkg3
Copy link

zakkg3 commented Feb 4, 2020

I am having the same problem, installed cdi and deleting a random namespace now gets stuck.
my ns have finalizer: kuberenetes
found this tread with a workaround like @candlerb did: kubernetes/kubernetes#60807

@candlerb
Copy link

candlerb commented Feb 4, 2020

The working sequence to uninstall kubevirt is now documented here:
https://kubevirt.io/user-guide/docs/latest/administration/intro.html#delete

@headwhacker
Copy link

headwhacker commented Aug 15, 2020

https://kubevirt.io/user-guide/#/installation/updating-and-deleting-installs?id=deleting-kubevirt

Following this document. Managed to remove the kubevirt namespace, validatingwebhookconfigurations and some apiservices.

However, when I delete these 4 apiservices. they just keep coming back. Anyone has any idea why?

$ kubectl delete apiservices v1alpha1.cdi.kubevirt.io v1alpha1.snapshot.kubevirt.io v1alpha3.kubevirt.io v1beta1.cdi.kubevirt.io
apiservice.apiregistration.k8s.io "v1alpha1.cdi.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.snapshot.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha3.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1beta1.cdi.kubevirt.io" deleted
$ kubectl get apiservices | grep -i kubevirt
v1alpha1.cdi.kubevirt.io Local True 13s
v1alpha1.snapshot.kubevirt.io Local True 13s
v1alpha3.kubevirt.io Local True 13s
v1beta1.cdi.kubevirt.io Local True 13s

Update:
I managed to delete these by manually deleting CRDs related to kubevirt.

@fabiand
Copy link
Member

fabiand commented Aug 16, 2020 via email

@JinjunXiong
Copy link

In the Get Started document, we always started to create the CR and Operator

kubectl create -f _out/manifests/release/kubevirt-operator.yaml
kubectl create -f _out/manifests/release/kubevirt-cr.yaml

But let's say that I've done the above in my k8s cluster, and am now ready redeploy a newly updated operator and CR. My questions are:

  • Should I follow the discussion here to delete operator and CR first, and then follow the above procedure to redeploy the new operator and CR? My concern of this approach is that it seemed the deletion itself was not fully functional yet.
  • If not, what would be the recommended procedure?

@candlerb
Copy link

I was playing with kubevirt again, and I managed to break a k3s cluster by applying the kubevirt resources, creating a couple of VMs, and then removing kubevirt with kubectl delete -f .... The namespace kubevirt was in Terminating state for 5 days.

In this state, even kubectl api-resources was failing:

# kubectl api-resources --verbs=list --namespaced -o name
configmaps
endpoints
events
limitranges
persistentvolumeclaims
pods
podtemplates
replicationcontrollers
resourcequotas
secrets
serviceaccounts
services
controllerrevisions.apps
daemonsets.apps
deployments.apps
replicasets.apps
statefulsets.apps
horizontalpodautoscalers.autoscaling
cronjobs.batch
jobs.batch
leases.coordination.k8s.io
endpointslices.discovery.k8s.io
events.events.k8s.io
ingresses.extensions
helmchartconfigs.helm.cattle.io
helmcharts.helm.cattle.io
addons.k3s.cattle.io
kubevirts.kubevirt.io
virtualmachineinstances.kubevirt.io
pods.metrics.k8s.io
ingresses.networking.k8s.io
networkpolicies.networking.k8s.io
poddisruptionbudgets.policy
rolebindings.rbac.authorization.k8s.io
roles.rbac.authorization.k8s.io
error: unable to retrieve the complete list of server APIs: subresources.kubevirt.io/v1: the server is currently unable to handle the request

Manually deleting resources where I can:

# kubectl delete -n kubevirt kubevirt kubevirt
Error from server (InternalError): Internal error occurred: failed calling webhook "kubevirt-validator.kubevirt.io": Post "https://kubevirt-operator-webhook.kubevirt.svc:443/kubevirt-validate-delete?timeout=10s": service "kubevirt-operator-webhook" not found

# kubectl delete apiservices v1alpha3.subresources.kubevirt.io
apiservice.apiregistration.k8s.io "v1alpha3.subresources.kubevirt.io" deleted

# kubectl delete mutatingwebhookconfigurations virt-api-mutator
mutatingwebhookconfiguration.admissionregistration.k8s.io "virt-api-mutator" deleted

# kubectl delete validatingwebhookconfigurations virt-api-validator
validatingwebhookconfiguration.admissionregistration.k8s.io "virt-api-validator" deleted

# kubectl api-resources --verbs=list --namespaced -o name
configmaps
endpoints
events
limitranges
persistentvolumeclaims
pods
podtemplates
replicationcontrollers
resourcequotas
secrets
serviceaccounts
services
controllerrevisions.apps
daemonsets.apps
deployments.apps
replicasets.apps
statefulsets.apps
horizontalpodautoscalers.autoscaling
cronjobs.batch
jobs.batch
leases.coordination.k8s.io
endpointslices.discovery.k8s.io
events.events.k8s.io
ingresses.extensions
helmchartconfigs.helm.cattle.io
helmcharts.helm.cattle.io
addons.k3s.cattle.io
kubevirts.kubevirt.io
virtualmachineinstances.kubevirt.io
pods.metrics.k8s.io
ingresses.networking.k8s.io
networkpolicies.networking.k8s.io
poddisruptionbudgets.policy
rolebindings.rbac.authorization.k8s.io
roles.rbac.authorization.k8s.io
error: unable to retrieve the complete list of server APIs: subresources.kubevirt.io/v1: the server is currently unable to handle the request

Still broken, and systemctl restart k3s doesn't help. But I found a partial solution here:

# kubectl get apiservice | egrep 'NAME|kube'
NAME                                   SERVICE                      AVAILABLE                 AGE
v1beta1.metrics.k8s.io                 kube-system/metrics-server   True                      28d
v1.subresources.kubevirt.io            kubevirt/virt-api            False (ServiceNotFound)   4d23h
v1.kubevirt.io                         Local                        True                      118s
v1alpha3.kubevirt.io                   Local                        True                      118s

# kubectl delete apiservice v1.subresources.kubevirt.io
apiservice.apiregistration.k8s.io "v1.subresources.kubevirt.io" deleted

Now I can list api-resources, which lets me use another clue here:

# kubectl api-resources --verbs=list --namespaced -o name \
    | xargs -n 1 kubectl get --show-kind --ignore-not-found -n kubevirt
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
NAME                            AGE   PHASE
kubevirt.kubevirt.io/kubevirt   5d    Deleting

At least I now know which resource is preventing the namespace from being deleted. However, I still get this error trying to delete it:

# kubectl delete -n kubevirt kubevirt kubevirt
Error from server (InternalError): Internal error occurred: failed calling webhook "kubevirt-validator.kubevirt.io": Post "https://kubevirt-operator-webhook.kubevirt.svc:443/kubevirt-validate-delete?timeout=10s": service "kubevirt-operator-webhook" not found

I was able to forcibly delete namespace this way:

# kubectl get ns kubevirt -o json >tmp.json
# vi tmp.json   # turn the finalizers into an empty list
# kubectl replace --raw "/api/v1/namespaces/kubevirt/finalize" -f ./tmp.json

... but actually I shouldn't have done that, as there's still that kubevirt resource hanging around:

# kubectl get all -n kubevirt
NAME                            AGE    PHASE
kubevirt.kubevirt.io/kubevirt   5d1h   Deleting

# kubectl get kubevirt kubevirt -n kubevirt -o yaml
...
status:
  conditions:
  - lastProbeTime: "2021-05-19T18:04:00Z"
    lastTransitionTime: "2021-05-19T18:04:00Z"
    message: Deletion was triggered
    reason: DeletionInProgress
    status: "False"
    type: Available
  - lastProbeTime: "2021-05-19T18:04:00Z"
    lastTransitionTime: "2021-05-19T18:04:00Z"
    message: Deletion was triggered
    reason: DeletionInProgress
    status: "False"
    type: Progressing
  - lastProbeTime: "2021-05-19T18:04:00Z"
    lastTransitionTime: "2021-05-19T18:04:00Z"
    message: Deletion was triggered
    reason: DeletionInProgress
    status: "True"
    type: Degraded
...
  phase: Deleting

(Digs further) maybe it was the test VMs I had created? The pods are long gone though.

# kubectl get crd
NAME                                  CREATED AT
addons.k3s.cattle.io                  2021-04-26T08:32:16Z
helmcharts.helm.cattle.io             2021-04-26T08:32:16Z
helmchartconfigs.helm.cattle.io       2021-04-26T08:32:16Z
kubevirts.kubevirt.io                 2021-05-19T16:45:20Z
virtualmachineinstances.kubevirt.io   2021-05-19T16:45:28Z

# kubectl get kubevirts.kubevirt.io
No resources found in default namespace.

# kubectl get virtualmachineinstances.kubevirt.io
NAME         AGE    PHASE     IP           NODENAME
example      5d1h   Running   10.42.0.88   brian-kit
vm-example   5d     Running   10.42.0.90   brian-kit

# kubectl get all -A | grep example
default     virtualmachineinstance.kubevirt.io/example      5d1h   Running   10.42.0.88   brian-kit
default     virtualmachineinstance.kubevirt.io/vm-example   5d     Running   10.42.0.90   brian-kit

# kubectl delete virtualmachineinstances.kubevirt.io example
virtualmachineinstance.kubevirt.io "example" deleted
<< hangs here >>
^C

# kubectl describe virtualmachineinstance.kubevirt.io example
...
  Finalizers:
    foregroundDeleteVirtualMachine
...
Status:
  Active Pods:
    57eeed0e-5115-4eae-8246-e9bf5b0d0a2c:  brian-kit
  Conditions:
    Last Probe Time:       <nil>
    Last Transition Time:  <nil>
    Message:               cannot migrate VMI which does not use masquerade to connect to the pod network
    Reason:                InterfaceNotLiveMigratable
    Status:                False
    Type:                  LiveMigratable
    Last Probe Time:       <nil>
    Last Transition Time:  2021-05-19T16:50:06Z
    Status:                True
    Type:                  Ready

...

There's definitely no VM pod (kubectl get pod -A). It must be possible to remove the finalizer from this resource too, but by myself I couldn't find a --raw path which works:

# kubectl replace --raw "/apis/kubevirt.io/v1/namespaces/default/virtualmachineinstances/example/finalize" -f tmp.json
Error from server (NotFound): virtualmachineinstances.kubevirt.io "example" not found

# kubectl get virtualmachineinstances.kubevirt.io example
NAME      AGE    PHASE     IP           NODENAME
example   5d1h   Running   10.42.0.88   brian-kit

However, the final clue was here:

# kubectl patch crd/virtualmachineinstances.kubevirt.io -p '{"metadata":{"finalizers":[]}}' --type=merge
customresourcedefinition.apiextensions.k8s.io/virtualmachineinstances.kubevirt.io patched
# kubectl get crd
NAME                              CREATED AT
addons.k3s.cattle.io              2021-04-26T08:32:16Z
helmcharts.helm.cattle.io         2021-04-26T08:32:16Z
helmchartconfigs.helm.cattle.io   2021-04-26T08:32:16Z
kubevirts.kubevirt.io             2021-05-19T16:45:20Z
# kubectl delete crd kubevirts.kubevirt.io
customresourcedefinition.apiextensions.k8s.io "kubevirts.kubevirt.io" deleted
<< hangs here >>
^C
# kubectl patch crd/kubevirts.kubevirt.io -p '{"metadata":{"finalizers":[]}}' --type=merge
customresourcedefinition.apiextensions.k8s.io/kubevirts.kubevirt.io patched
# kubectl get crd
NAME                              CREATED AT
addons.k3s.cattle.io              2021-04-26T08:32:16Z
helmcharts.helm.cattle.io         2021-04-26T08:32:16Z
helmchartconfigs.helm.cattle.io   2021-04-26T08:32:16Z

Then:

# kubectl get all -n kubevirt
error: the server doesn't have a resource type "kubevirts"

(a couple of mins later)
# kubectl get all -n kubevirt
No resources found in kubevirt namespace.

I think that's clean enough now. Phew :-)

@ajith29
Copy link

ajith29 commented Nov 16, 2021

Still not able to delete namespace need to run this

kubectl get apiservices | grep kubevirt
v1.subresources.kubevirt.io kubevirt/virt-api False (ServiceNotFound) 59d
kubectl delete apiservices v1.subresources.kubevirt.io
apiservice.apiregistration.k8s.io "v1.subresources.kubevirt.io" deleted
kubectl delete ns kubevirt
namespace "kubevirt" deleted
`

@willzhang
Copy link

willzhang commented Jan 12, 2023

it will reinstall again

# kubectl get apiservices | grep kubevirt |awk '{print $1}' |xargs kubectl delete apiservices
apiservice.apiregistration.k8s.io "v1.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.cdi.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.clone.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.export.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.instancetype.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.migrations.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.pool.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.snapshot.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha1.upload.cdi.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha2.instancetype.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1alpha3.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1beta1.cdi.kubevirt.io" deleted
apiservice.apiregistration.k8s.io "v1beta1.upload.cdi.kubevirt.io" deleted

# kubectl get ns
NAME                     STATUS        AGE
cdi                      Terminating   3d1h
default                  Active        3d1h
elastic-system           Active        98m
ingress-nginx            Active        3d1h
kube-node-lease          Active        3d1h
kube-prometheus-stack    Active        72m
kube-public              Active        3d1h
kube-system              Active        3d1h
kubekey-system           Active        3d1h
kubernetes-dashboard     Active        3d1h
kubevirt                 Terminating   3d1h
metallb-system           Active        3d1h
monitoring               Active        3d1h
neuvector                Active        3d1h
nginx                    Active        6h7m
openebs                  Active        3d1h
sriov-network-operator   Active        3d1h
# kubectl get apiservices | grep kubevirt 
v1.kubevirt.io                            Local     True        16s
v1alpha1.cdi.kubevirt.io                  Local     True        16s
v1alpha1.clone.kubevirt.io                Local     True        16s
v1alpha1.export.kubevirt.io               Local     True        16s
v1alpha1.instancetype.kubevirt.io         Local     True        16s
v1alpha1.migrations.kubevirt.io           Local     True        16s
v1alpha1.pool.kubevirt.io                 Local     True        16s
v1alpha1.snapshot.kubevirt.io             Local     True        16s
v1alpha2.instancetype.kubevirt.io         Local     True        16s
v1alpha3.kubevirt.io                      Local     True        16s
v1beta1.cdi.kubevirt.io                   Local     True        16s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.