Created
April 24, 2022 09:26
-
-
Save codebykyle/1c481352c12264fb7da2011aba11c504 to your computer and use it in GitHub Desktop.
Delete kubernetes namespaces which are not deleting due to finalizers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
namespaces_response = requests.get('http://127.0.0.1:8001/api/v1/namespaces').json() | |
for namespace_item in namespaces_response['items']: | |
if namespace_item['status']['phase'] == 'Terminating': | |
url = 'http://127.0.0.1:8001/api/v1/namespaces/%s' % namespace_item['metadata']['name'] | |
namespace_get_data = requests.get(url).json() | |
updated_body = namespace_get_data | |
updated_body['spec'] = {} | |
updated_body['metadata']['finalizers'] = [] | |
print(updated_body) | |
requests.put(url + '/finalize', json=updated_body) | |
requests.delete(url) | |
response = requests.get(url) | |
print (response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need requests:
pip install requests
You also need to connect to your kubernetes cluster with:
kubectl proxy
Run this script to remove all finalizers on hung namespaces