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
CodePudding user response:
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.
CodePudding user response:
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.
