Simple way to delete existing pods from Python
you can use delete_namespaced_pod
(from CoreV1Api) to delete specific pods within a namespace.
Here is an example:
from kubernetes import client, configfrom kubernetes.client.rest import ApiExceptionconfig.load_incluster_config() # or config.load_kube_config()configuration = client.Configuration()with client.ApiClient(configuration) as api_client: api_instance = client.CoreV1Api(api_client) namespace = 'kube-system' # str | see @Max Lobur's answer on how to get this name = 'kindnet-mpkvf' # str | Pod name, e.g. via api_instance.list_namespaced_pod(namespace) try: api_response = api_instance.delete_namespaced_pod(name, namespace) print(api_response) except ApiException as e: print("Exception when calling CoreV1Api->delete_namespaced_pod: %s\n" % e)
A continuation to an existing answer by Tibebes. M
How do I find out the namespace that the code itself is running in?
There's two ways to do this:
- Use Downward API to pass pod namespace to a pod environment variable:
- Use your template engine / helm to additionally pass environment variable namespace to a pod. Example:
env: - name: CURRENT_NAMESPACE value: "{{ .Values.namespace }}"