<p>I am trying to delete temporary pods and other artifacts using helm delete. I am trying to run this helm delete to run on a schedule. Here is my stand alone command which works</p>

<p><code>helm delete --purge $(helm ls -a -q temppods.*)</code></p>

<p>However if i try to run this on a schedule as below i am running into issues.</p>

<p>Here is what mycron.yaml looks like:</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronbox
  namespace: mynamespace
spec:
  serviceAccount: cron-z
  successfulJobsHistoryLimit: 1
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cronbox
            image: alpine/helm:2.9.1
            args: ["delete", "--purge", "$(helm ls -a -q temppods.*)"
            env:
            - name: TILLER_NAMESPACE
              value: mynamespace-build
            - name: KUBECONFIG
              value: /kube/config
            volumeMounts:
            - mountPath: /kube
              name: kubeconfig
          restartPolicy: OnFailure
          volumes:
          - name: kubeconfig
            configMap:
              name: cronjob-kubeconfig
</code></pre>

<p>I ran</p>

<pre><code>oc create -f ./mycron.yaml
</code></pre>

<p>This created the cronjob</p>

<p>Every 5th minute a pod is getting created and the helm command that is part of the cron job runs.</p>

<p>I am expecting the artifacts/pods name beginning with temppods* to be deleted.</p>

<p>What i see in the logs of the pod is:</p>

<pre><code>Error: invalid release name, must match regex ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+$ and the length must not longer than 53
</code></pre>

lr-pal
<p>The CronJob container spec is trying to delete a release named (literally):</p>

<pre><code>$(helm ls -a -q temppods.*)
</code></pre>

<p>This release doesn't exist, and fails <code>helm</code>s expected naming conventions. </p>

<h3>Why</h3>

<p>The <code>alpine/helm:2.9.1</code> container image has an <a href="https://docs.docker.com/engine/reference/builder/#entrypoint" rel="nofollow noreferrer">entrypoint</a> of <code>helm</code>. This means any arguments are passes directly to the <code>helm</code> binary via <a href="https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example" rel="nofollow noreferrer">exec</a>. No shell expansion (<code>$()</code>) occurs as there is no shell running.  </p>

<h3>Fix</h3>

<p>To do what you are expecting you can use <code>sh</code> which is available in alpine images. </p>

<pre><code>sh -uexc 'releases=$(helm ls -a -q temppods.*); helm delete --purge $releases'
</code></pre>

<p>In a Pod spec this translates to:</p>

<pre><code>spec:
  containers:
  - name: cronbox
    command: 'sh'
    args:
    - '-uexc'
    - 'releases=$(helm ls -a -q temppods.*); helm delete --purge $releases;'
</code></pre>

<h3>Helm</h3>

<p>As a side note, helm is not the most reliable tool when clusters or releases get into vague states. Running multiple helm commands interacting with within the same release at the same time usually spells disaster and this seems on the surface like that is likely. Maybe there is a question in other ways to achieve this process your are implementing?</p>
