0

Due to minikube issues 13841 and 13872 (which I believe to be the same), I have to use minikube 1.23.2. I try using minikube start --kubernetes-version v... with version 1.24+ but none works (not sure why). Thus, I cannot use the convenient command kubectl -n ... create token .... Seba's answer to this question shows how to generate the token with this older kubectl version:

$ export secret=$(kubectl get serviceaccount default -o jsonpath='{.secrets[0].name}')
$ kubectl get secret $secret -o jsonpath='{.data.token}' | base64 --decode

Apparently, the command is only good for service account default. How can I rewrite that script so it can be used for other users as follows?

$ export my_script=....
$ my_script another_account
CaTx
  • 1,421
  • 4
  • 21
  • 42

2 Answers2

1

If I'm guessing correctly what you are trying to ask,

#!/bin/sh
secret=$(kubectl get serviceaccount "${1-default}" -o jsonpath='{.secrets[0].name}')
kubectl get secret "$secret" -o jsonpath='{.data.token}' | base64 --decode

which defaults to serviceaccount default but uses whatever you passed in as the first-command-line argument if you do.

If you save this as ktoken, chmod +x the file, and make sure the directory it's in is on your PATH (perhaps with a symlink), you can run

ktoken

to run it for the default account, and

ktoken otheraccount

to run it for otheraccount.

Tangentially, there is no need to export a variable unless you need subprocesses to have access to it.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • How do I use this with `another_account`? Putting it right before `-o`? Can the second line be packaged so that I can call like: `get_token another_account`? – CaTx Nov 23 '22 at 16:22
  • I updated the answer with a newbie-friendly instruction. – tripleee Nov 24 '22 at 05:19
  • Thanks! Is there a way to do it with `alias` and not having to save it into a file? – CaTx Nov 24 '22 at 05:25
  • Aliases can't take arguments, but any code you put in a script could be a function instead if you prefer. Perhaps see also https://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter – tripleee Nov 24 '22 at 05:26
  • I see! Looks like this is the only way. – CaTx Nov 24 '22 at 05:36
0

After checking out this question, I can see one way to do this is creating a get_token.sh file with the following content:

#!/bin/bash
user_account=$1
function get_token() {
        secret=$(kubectl get serviceaccount "$1" -o jsonpath='{.secrets[0].name}')
        kubectl get secret "$secret" -o jsonpath='{.data.token}' | base64 --decode
}

get_token $user_account

Then I can call from terminal:

$ bash get_token.sh another_account

Another way not involving a bash script file is to define the get_token function then use it:

$ function get_token() { secret=$(kubectl get serviceaccount "$1" -o jsonpath='{.secrets[0].name}') &&  kubectl get secret "$secret" -o jsonpath='{.data.token}' | base64 --decode; }
$ get_token another_account

Not sure if there are other or better way of doing it.

CaTx
  • 1,421
  • 4
  • 21
  • 42