<p>I want to run a  loop on the pods in specific namespace, however the trick is to do it in a cronJob,<em>is it possible inline</em>?</p>
<p><code>kubectl get pods -n foo</code></p>
<p>The trick here is after you get the list of the pods, I need to loop on then and delete each one by one with timeout of 15 seconde, is it possible to do it in cronJob?</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: restart
  namespace: foo
spec:
  concurrencyPolicy: Forbid
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 600
      template:
        spec:
          serviceAccountName: pod-exterminator
          restartPolicy: Never
          containers:
            - name: kubectl
              image: bitnami/kubectl:1.22.3
              command:
                - 'kubectl'
                - 'get'
                - 'pods'
                - '--namespace=foo'
</code></pre>
<p>When running the above script it works, but when you want to run loop its get complicated, how can I do it inline?</p>

Jenney
<p>In your case you can use something like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: restart
  namespace: foo
spec:
  concurrencyPolicy: Forbid
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 600
      template:
        spec:
          serviceAccountName: pod-exterminator
          restartPolicy: Never
          containers:
            - name: kubectl
              image: bitnami/kubectl:1.22.3
              command:
              - /bin/sh
              - -c
              - kubectl get pods -o name |  while read -r POD; do kubectl delete &quot;$POD&quot;; sleep 15; done
</code></pre>
<p>However, do you really need to wait 15 seconds? If you want to be sure that pod is gone before deleting next one, you can use <code>--wait=true</code>, so the command will become:</p>
<pre><code>kubectl get pods -o name |  while read -r POD; do kubectl delete &quot;$POD&quot; --wait; done
</code></pre>
