<p>I would like to create a cron job which selects the name kubernetes secrets from a given type (e.g kubernetes.io/tls) older than 29 days.</p>

<p>I tried these 2 ways:</p>

<p>1 - Using grep:</p>

<pre><code>kubectl get secrets --all-namespaces| grep kubernetes.io/tls
cicd           devkins.infra.mydomain.ninja.tls         kubernetes.io/tls  2 14d
cust-ao-xmas   cust-ao-xmas.infra.mydomain.ninja.tls    kubernetes.io/tls  2 1h
dev            dev.mydomain.com.tls                     kubernetes.io/tls  2 4h
logging        graylog.infra.mydomain.ninja.tls         kubernetes.io/tls  2 10d
</code></pre>

<p>2 Using jsonpath</p>

<pre><code>kubectl get secrets --all-namespaces -o jsonpath='{range .items[?(@.type=="kubernetes.io/tls")]}[{.metadata.namespace},{.metadata.name},{.metadata.creationTimestamp}]{"\n"}{end}'
[cicd,devkins.infra.mydomain.ninja.tls,2017-12-13T22:57:02Z]
[cust-ao-xmas,cust-ao-xmas.infra.mydomain.ninja.tls,2017-12-28T03:13:32Z]
[dev,dev.mydomain.com.tls,2017-12-28T00:59:10Z]
[logging,graylog.infra.mydomain.ninja.tls,2017-12-17T22:23:38Z]
</code></pre>

<p>Both of them return the answers but make it extremely hard to parse</p>

<p>Is there a better way to achieve this?</p>

<p>This is what I use... There is probably better way of doing it, but works for my case (secrets older than 24hrs).</p>

<pre><code>kubectl get secrets -o json | jq -r "[.items[] | {name: .metadata.name, startTime: .metadata.creationTimestamp | fromdate } | select(.startTime &lt; (now | . - 86400))]" | jq -r ".[].name"
</code></pre>
