Obtain Kubernetes API key with Java

12/9/2019

I'm using k8s java client and need a way to get OAuth access token for some clusters. Now I can do that only with this bash script:

export KUBECONFIG=~/.kube/<config-file>

APISERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
SECRET_NAME=$(kubectl get secrets | grep ^default | cut -f1 -d ' ')
TOKEN=$(kubectl describe secret $SECRET_NAME | grep -E '^token' | cut -f2 -d':' | tr -d " ")

echo "TOKEN: ${TOKEN}"

Is there a way to do that with java code? Don't ask for the whole solution but at least for some direction to look.

-- Alexey Usharovski
java
kubernetes
oauth

2 Answers

12/9/2019

The Kubernetes config is stored as a YAML file. Use a library to read and parse it, example

In the end you'll get an object with all the keys and values from the config. Just access directly what you need.

-- Dávid Molnár
Source: StackOverflow

12/9/2019

Kubernetes is not involved in the OAuth side of things at all. That’s up to your IdP. More normally you would use a ServiceAccount token for automation though.

-- coderanger
Source: StackOverflow