<p>I have end-to-end tests written in pytest, running on a Kubernetes cluster in Namespace <code>foo</code>. Now I want to add simple chaos engineering to the tests to check the resilience of my services. For this, I only need to delete specific pods within <code>foo</code> -- since K8s restarts the corresponding service, this simulates the service being temporarily unreachable.</p>
<p>What is a simple way to delete a specific pod in the current namespace with Python?</p>
<hr />
<p>What I have tried so far:</p>
<p>Since I did not find a suitable example in <a href="https://github.com/kubernetes-client/python/tree/master/examples" rel="noreferrer">https://github.com/kubernetes-client/python/tree/master/examples</a>, but the ones using pods looked quite complex,
I looked into <code>kubetest</code>, which seems very simple and elegant.</p>
<p>I wanted to use the <code>kube</code> fixture and then do something like this:</p>
<pre><code>pods = kube.get_pods()
for pod in pods:
  if can_be_temporarily_unreachable(pod):
    kube.delete(pod)
</code></pre>
<p>I thought calling pytest with parameter <code>--in-cluster</code> would tell <code>kubetest</code> to use the current cluster setup and not create new K8s resources.
However, <code>kubetest</code> wants to create a new namespace for each test case that uses the <code>kube</code> fixture, which I do not want.
Is there a way to tell <code>kubetest</code> not to create new namespaces but do everything in the current namespace?</p>
<p>Though <code>kubetest</code> looks very simple and elegant otherwise, I am happy to use another solution, too.
A simple solution that requires little time and maintenance and does not complicate (reading) the tests would be awesome.</p>

DaveFar
<p>you can use <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md" rel="noreferrer"><code>delete_namespaced_pod</code></a>(from <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md" rel="noreferrer">CoreV1Api</a>) to delete specific pods within a namespace.</p>
<p>Here is an example:</p>
<pre><code>from kubernetes import client, config
from kubernetes.client.rest import ApiException
config.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(&quot;Exception when calling CoreV1Api-&gt;delete_namespaced_pod: %s\n&quot; % e)
</code></pre>
