<p>What's the one liner command to replace 2 commands like below to get the Kubernetes secret's token? Example usecase will be getting token from kubernetes-dashboard-admin's secret to login and view kubernetes-dashboard.</p>

<p>Command example:</p>

<pre><code>$ kubectl describe serviceaccount default
Name:                default
Namespace:           default
Labels:              &lt;none&gt;
Annotations:         &lt;none&gt;
Image pull secrets:  &lt;none&gt;
Mountable secrets:   default-token-zvxf4
Tokens:              default-token-zvxf4
Events:              &lt;none&gt;

$ kubectl describe secret default-token-zvxf4
Name:         default-token-zvxf4
Namespace:    default
Labels:       &lt;none&gt;
Annotations:  kubernetes.io/service-account.name: default
              kubernetes.io/service-account.uid: 809835e7-2564-439f-82f3-14762688ca80

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  7 bytes
token:      TOKENHERE
</code></pre>

Ryan
<p>Answer that I discovered was below. By using <a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="nofollow noreferrer">jsonpath</a> to retrieve and <a href="https://man7.org/linux/man-pages/man1/xargs.1.html" rel="nofollow noreferrer">xargs</a> to pass the secret name/output to second command. Will need to decode the encrypted token with <a href="https://linux.die.net/man/1/base64" rel="nofollow noreferrer">base64</a> at the end.</p>

<pre><code>$ kubectl get serviceaccount default -o=jsonpath='{.secrets[0].name}' | xargs kubectl get secret -ojsonpath='{.data.token}' | base64 --decode
TOKENHERE%
</code></pre>

<p><em>The tailing % is not part of the token</em></p>

<p>This should be able to work on MacOS without install additional app like <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer">jq</a> which should be able to do the same. Hope this is helpful for others.</p>
